Skip to content

Commit 8d8ef6d

Browse files
authored
FileSystemEntry.Unix: don't make a syscall to check hidden on Linux. (#63878)
1 parent 24bdc38 commit 8d8ef6d

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

src/libraries/Common/src/Interop/Unix/System.Native/Interop.LChflags.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@ internal enum UserFlags : uint
2222
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LChflagsCanSetHiddenFlag")]
2323
[SuppressGCTransition]
2424
private static partial int LChflagsCanSetHiddenFlag();
25+
26+
internal static readonly bool SupportsHiddenFlag = (CanGetHiddenFlag() != 0);
27+
28+
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_CanGetHiddenFlag")]
29+
[SuppressGCTransition]
30+
private static partial int CanGetHiddenFlag();
2531
}
2632
}

src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEntry.Unix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public FileAttributes Attributes
145145
public DateTimeOffset CreationTimeUtc => _status.GetCreationTime(FullPath, continueOnError: true);
146146
public DateTimeOffset LastAccessTimeUtc => _status.GetLastAccessTime(FullPath, continueOnError: true);
147147
public DateTimeOffset LastWriteTimeUtc => _status.GetLastWriteTime(FullPath, continueOnError: true);
148-
public bool IsHidden => _status.IsHidden(FullPath, FileName, continueOnError: true);
148+
public bool IsHidden => _status.IsFileSystemEntryHidden(FullPath, FileName);
149149
internal bool IsReadOnly => _status.IsReadOnly(FullPath, continueOnError: true);
150150

151151
public bool IsDirectory => _status.InitiallyDirectory;

src/libraries/System.Private.CoreLib/src/System/IO/FileStatus.Unix.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,20 @@ internal bool IsReadOnly(ReadOnlySpan<char> path, bool continueOnError = false)
145145
return HasReadOnlyFlag;
146146
}
147147

148-
internal bool IsHidden(ReadOnlySpan<char> path, ReadOnlySpan<char> fileName, bool continueOnError = false)
148+
internal bool IsFileSystemEntryHidden(ReadOnlySpan<char> path, ReadOnlySpan<char> fileName)
149149
{
150-
// Avoid disk hit first
150+
// Because this is called for FileSystemEntry we can assume the entry exists and
151+
// avoid initialization in some cases.
151152
if (IsNameHidden(fileName))
152153
{
153154
return true;
154155
}
155-
EnsureCachesInitialized(path, continueOnError);
156+
if (!Interop.Sys.SupportsHiddenFlag)
157+
{
158+
return false;
159+
}
160+
161+
EnsureCachesInitialized(path, continueOnError: true);
156162
return HasHiddenFlag;
157163
}
158164

src/native/libs/System.Native/entrypoints.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static const Entry s_sysNative[] =
109109
DllImportEntry(SystemNative_LockFileRegion)
110110
DllImportEntry(SystemNative_LChflags)
111111
DllImportEntry(SystemNative_LChflagsCanSetHiddenFlag)
112+
DllImportEntry(SystemNative_CanGetHiddenFlag)
112113
DllImportEntry(SystemNative_ReadProcessStatusInfo)
113114
DllImportEntry(SystemNative_Log)
114115
DllImportEntry(SystemNative_LogError)

src/native/libs/System.Native/pal_io.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,16 @@ int32_t SystemNative_LChflags(const char* path, uint32_t flags)
15531553

15541554
int32_t SystemNative_LChflagsCanSetHiddenFlag(void)
15551555
{
1556-
#if defined(UF_HIDDEN) && defined(HAVE_STAT_FLAGS) && defined(HAVE_LCHFLAGS)
1556+
#if defined(HAVE_LCHFLAGS)
1557+
return SystemNative_CanGetHiddenFlag();
1558+
#else
1559+
return false;
1560+
#endif
1561+
}
1562+
1563+
int32_t SystemNative_CanGetHiddenFlag(void)
1564+
{
1565+
#if defined(UF_HIDDEN) && defined(HAVE_STAT_FLAGS)
15571566
return true;
15581567
#else
15591568
return false;

src/native/libs/System.Native/pal_io.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,13 @@ PALEXPORT int32_t SystemNative_LChflags(const char* path, uint32_t flags);
744744
*/
745745
PALEXPORT int32_t SystemNative_LChflagsCanSetHiddenFlag(void);
746746

747+
/**
748+
* Determines if the current platform supports getting UF_HIDDEN (0x8000) flag
749+
*
750+
* Returns true (non-zero) if supported, false (zero) if not.
751+
*/
752+
PALEXPORT int32_t SystemNative_CanGetHiddenFlag(void);
753+
747754
/**
748755
* Reads the psinfo_t struct and converts into ProcessStatus.
749756
*

0 commit comments

Comments
 (0)