Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
Expand Up @@ -114,7 +114,7 @@ private static bool LastWriteTimesHaveChanged()

if (s_rootStoreFile != null)
{
_ = TryStatFile(s_rootStoreFile, out DateTime lastModified);
_ = TryStatFile(s_rootStoreFile, out DateTime lastModified, out _);
if (lastModified != s_fileLastWrite)
{
return true;
Expand Down Expand Up @@ -149,6 +149,7 @@ private static Tuple<SafeX509StackHandle, SafeX509StackHandle> LoadMachineStores

var uniqueRootCerts = new HashSet<X509Certificate2>();
var uniqueIntermediateCerts = new HashSet<X509Certificate2>();
var processedFiles = new HashSet<(long Ino, long Dev)>();
bool firstLoad = (s_nativeCollections == null);

if (firstLoad)
Expand Down Expand Up @@ -197,23 +198,23 @@ bool ProcessDir(string dir, out DateTime lastModified)

foreach (string file in Directory.EnumerateFiles(dir))
{
hasStoreData |= ProcessFile(file, out _, skipStat: true);
hasStoreData |= ProcessFile(file, out _);
}

return hasStoreData;
}

bool ProcessFile(string file, out DateTime lastModified, bool skipStat = false)
bool ProcessFile(string file, out DateTime lastModified)
{
bool readData = false;

if (skipStat)
if (!TryStatFile(file, out lastModified, out (long, long) fileId))
{
lastModified = default;
return false;
}
else if (!TryStatFile(file, out lastModified))

if (processedFiles.Contains(fileId))
{
return false;
return true;
}

using (SafeBioHandle fileBio = Interop.Crypto.BioNewFile(file, "rb"))
Expand Down Expand Up @@ -281,6 +282,11 @@ bool ProcessFile(string file, out DateTime lastModified, bool skipStat = false)
}
}

if (readData)
{
processedFiles.Add(fileId);
}

return readData;
}

Expand All @@ -307,7 +313,6 @@ bool ProcessFile(string file, out DateTime lastModified, bool skipStat = false)
// In order to maintain "finalization-free" the GetNativeCollections method would need to
// DangerousAddRef, and the callers would need to DangerousRelease, adding more interlocked operations
// on every call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blank line shouldn't have been removed, since the comment isn't about the following code, it's about code that's not present; making it a separate logical code paragraph. But it's not worth spinning on.

Volatile.Write(ref s_nativeCollections, newCollections);
s_recheckStopwatch.Restart();
return newCollections;
Expand Down Expand Up @@ -361,24 +366,24 @@ private static string[] GetRootStoreDirectories(out bool isDefault)
return directories;
}

private static bool TryStatFile(string path, out DateTime lastModified)
=> TryStat(path, Interop.Sys.FileTypes.S_IFREG, out lastModified);

private static bool TryStatDirectory(string path, out DateTime lastModified)
=> TryStat(path, Interop.Sys.FileTypes.S_IFDIR, out lastModified);
=> TryStat(path, Interop.Sys.FileTypes.S_IFDIR, out lastModified, out _);

private static bool TryStat(string path, int fileType, out DateTime lastModified)
private static bool TryStatFile(string path, out DateTime lastModified, out (long, long) fileId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: only keep 1 TryStatFile and update the caller that ignores fileId by passing it out _ as the last argument.

=> TryStat(path, Interop.Sys.FileTypes.S_IFREG, out lastModified, out fileId);

private static bool TryStat(string path, int fileType, out DateTime lastModified, out (long, long) fileId)
{
Comment on lines 368 to 376
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the TryStatFile/TryStatDirectory order would have been maintained so the diff showed that it was really just adding the out. But, again, it's not worth spinning on.

lastModified = default;

Interop.Sys.FileStatus status;
fileId = default;
// Use Stat to follow links.
if (Interop.Sys.Stat(path, out status) < 0 ||
if (Interop.Sys.Stat(path, out Interop.Sys.FileStatus status) < 0 ||
(status.Mode & Interop.Sys.FileTypes.S_IFMT) != fileType)
{
return false;
}

fileId = (status.Ino, status.Dev);
lastModified = DateTime.UnixEpoch + TimeSpan.FromTicks(status.MTime * TimeSpan.TicksPerSecond + status.MTimeNsec / TimeSpan.NanosecondsPerTick);
return true;
}
Expand Down