Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.IO.Tests
{
public partial class FileInfo_GetSetAttributes
{
[Fact]
[PlatformSpecific(TestPlatforms.OSX)]
public void TogglingHiddenAttribute_PreservesOtherUserFlags()
{
// UF_NODUMP (0x01) is a harmless BSD user flag that any file owner can set/clear.
const uint UF_NODUMP = 0x01;
const uint UF_HIDDEN = (uint)Interop.Sys.UserFlags.UF_HIDDEN;

string path = GetTestFilePath();
File.Create(path).Dispose();

// Set UF_NODUMP on the file directly via lchflags.
Assert.Equal(0, Interop.Sys.LChflags(path, UF_NODUMP));
Assert.Equal(0, Interop.Sys.Stat(path, out Interop.Sys.FileStatus before));
Assert.NotEqual(0u, before.UserFlags & UF_NODUMP);

// Toggle Hidden ON via the public API — this must preserve UF_NODUMP.
var fi = new FileInfo(path);
fi.Attributes |= FileAttributes.Hidden;

Assert.Equal(0, Interop.Sys.Stat(path, out Interop.Sys.FileStatus afterSet));
Assert.NotEqual(0u, afterSet.UserFlags & UF_HIDDEN);
Assert.NotEqual(0u, afterSet.UserFlags & UF_NODUMP);

// Toggle Hidden OFF — UF_NODUMP must still survive.
fi.Refresh();
fi.Attributes &= ~FileAttributes.Hidden;

Assert.Equal(0, Interop.Sys.Stat(path, out Interop.Sys.FileStatus afterClear));
Assert.Equal(0u, afterClear.UserFlags & UF_HIDDEN);
Assert.NotEqual(0u, afterClear.UserFlags & UF_NODUMP);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace System.IO.Tests
{
public class FileInfo_GetSetAttributes : InfoGetSetAttributes<FileInfo>
public partial class FileInfo_GetSetAttributes : InfoGetSetAttributes<FileInfo>
{
protected override bool CanBeReadOnly => true;
protected override FileAttributes GetAttributes(string path) => new FileInfo(path).Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
Link="Common\Interop\Unix\Interop.OpenFlags.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.MkFifo.cs"
Link="Common\Interop\Unix\Interop.MkFifo.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Stat.cs"
Link="Common\Interop\Unix\Interop.Stat.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.LChflags.cs"
Link="Common\Interop\Unix\Interop.LChflags.cs" />
<Compile Include="FileInfo\GetSetAttributes.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<Compile Include="Directory\Delete.Windows.cs" />
Expand Down
8 changes: 7 additions & 1 deletion src/native/libs/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ c_static_assert(PAL_IN_EXCL_UNLINK == IN_EXCL_UNLINK);
c_static_assert(PAL_IN_ISDIR == IN_ISDIR);
#endif // HAVE_INOTIFY

// Validate that our UserFlags enum values match the platform, since
// SystemNative_LChflags and SystemNative_FChflags pass them directly to the OS.
#if HAVE_STAT_FLAGS && defined(UF_HIDDEN)
c_static_assert(PAL_UF_HIDDEN == UF_HIDDEN);
#endif

static void ConvertFileStatus(const struct stat_* src, FileStatus* dst)
{
dst->Dev = (int64_t)src->st_dev;
Expand Down Expand Up @@ -231,7 +237,7 @@ static void ConvertFileStatus(const struct stat_* src, FileStatus* dst)
#endif

#if HAVE_STAT_FLAGS && defined(UF_HIDDEN)
dst->UserFlags = ((src->st_flags & UF_HIDDEN) == UF_HIDDEN) ? PAL_UF_HIDDEN : 0;
dst->UserFlags = (uint32_t)src->st_flags;
#else
dst->UserFlags = 0;
#endif
Expand Down
Loading