/// <summary> /// Enumerate the directory and apply a given action to its members /// </summary> public PathExistence EnumerateDirectory(PathTable pathTable, AbsolutePath path, Action <AbsolutePath, string> handleChildPath) { // We can bail out early if the path is known as either a file or as being nonexistent var id = GetIndexByPath(path); bool atLeastOneChild = false; if (id >= m_bitset.Length) { // It means that the path is not in the table when we construct the pip file system. // We can safely ignore it and return Nonexistent. return(PathExistence.Nonexistent); } foreach (var childPathValue in pathTable.EnumerateImmediateChildren(path.Value)) { var index = HierarchicalNameTable.GetIndexFromValue(childPathValue.Value); if (index >= m_bitset.Length) { continue; } if (m_bitset.Contains(index)) { atLeastOneChild = true; var filePath = GetPathByIndex(pathTable, index); handleChildPath(filePath, filePath.GetName(pathTable).ToString(pathTable.StringTable)); } } return(atLeastOneChild ? PathExistence.ExistsAsDirectory : PathExistence.Nonexistent); }
/// <summary> /// Queries the existence and members of a directory in the specified file system mode /// </summary> public Possible <PathExistence> TryEnumerateDirectory(AbsolutePath path, FileSystemViewMode mode, Action <string, AbsolutePath, PathExistence> handleEntry, bool cachePathExistence = true) { FileSystemEntry entry; PathExistence existence; if (PathExistenceCache.TryGetValue(path, out entry) && entry.TryGetExistence(mode, out existence)) { if (existence == PathExistence.Nonexistent) { return(existence); } if (existence == PathExistence.ExistsAsFile) { bool isDirectorySymlinkOrJunction = false; if (entry.HasFlag(FileSystemEntryFlags.CheckedIsDirectorySymlink)) { isDirectorySymlinkOrJunction = entry.HasFlag(FileSystemEntryFlags.IsDirectorySymlink); } else { isDirectorySymlinkOrJunction = FileUtilities.IsDirectorySymlinkOrJunction(path.ToString(PathTable)); PathExistenceCache.AddOrUpdate(path, false, (key, data) => { throw Contract.AssertFailure("Entry should already be added for path"); }, (key, data, oldValue) => oldValue.SetFlag( FileSystemEntryFlags.CheckedIsDirectorySymlink | (isDirectorySymlinkOrJunction ? FileSystemEntryFlags.IsDirectorySymlink : FileSystemEntryFlags.None))); } if (!isDirectorySymlinkOrJunction) { return(existence); } } // For graph file systems, directory members can be determined by overlaying path table with existence state in-memory // For real file system, this same is true if the directory has already been enumerated if (mode != FileSystemViewMode.Real || ((entry.Flags & FileSystemEntryFlags.IsRealFileSystemEnumerated) != 0)) { foreach (var childPathValue in PathTable.EnumerateImmediateChildren(path.Value)) { var childPath = new AbsolutePath(childPathValue); PathExistence childExistence; if (TryGetKnownPathExistence(childPath, mode, out childExistence) && childExistence != PathExistence.Nonexistent) { var entryName = childPath.GetName(PathTable).ToString(PathTable.StringTable); handleEntry(entryName, childPath, childExistence); } } return(existence); } } if (mode == FileSystemViewMode.Real) { var handleDirectoryEntry = new Action <string, FileAttributes>((entryName, entryAttributes) => { // Reparse points are always treated as files. Otherwise, honor the directory attribute to determine // existence var childExistence = (entryAttributes & FileAttributes.ReparsePoint) != 0 ? PathExistence.ExistsAsFile : (entryAttributes & FileAttributes.Directory) != 0 ? PathExistence.ExistsAsDirectory : PathExistence.ExistsAsFile; var childPath = path.Combine(PathTable, entryName); childExistence = GetOrAddExistence(childPath, mode, childExistence, updateParents: false); // NOTE: Because we are caching file system state in memory, it is possible that the existence state of // files does not match the state from the file system. if (childExistence != PathExistence.Nonexistent) { handleEntry(entryName, childPath, childExistence); } }); Counters.IncrementCounter(FileSystemViewCounters.RealFileSystemEnumerations); if (cachePathExistence) { Possible <PathExistence> possibleExistence; using (Counters.StartStopwatch(FileSystemViewCounters.RealFileSystemEnumerationsDuration)) { possibleExistence = LocalDiskFileSystem.TryEnumerateDirectoryAndTrackMembership( path, handleDirectoryEntry, // This method is called during observed input processing. Currently, we simply include all entries. // TODO: In the future, we may want to restrict it based on pip's untracked scopes/paths. shouldIncludeEntry: null /* include all entries */); } if (possibleExistence.Succeeded) { existence = GetOrAddExistence(path, mode, possibleExistence.Result); PathExistenceCache.AddOrUpdate(path, false, (key, data) => { throw Contract.AssertFailure("Entry should already be added for path"); }, (key, data, oldValue) => oldValue.SetFlag(FileSystemEntryFlags.IsRealFileSystemEnumerated)); return(existence); } return(possibleExistence); } using (Counters.StartStopwatch(FileSystemViewCounters.RealFileSystemEnumerationsDuration)) { var possibleFingerprintResult = DirectoryMembershipTrackingFingerprinter.ComputeFingerprint( path.Expand(PathTable).ExpandedPath, handleEntry: handleDirectoryEntry); return(possibleFingerprintResult.Succeeded ? new Possible <PathExistence>(possibleFingerprintResult.Result.PathExistence) : new Possible <PathExistence>(possibleFingerprintResult.Failure)); } } else if (ExistsInGraphFileSystem(PipGraph.TryGetLatestFileArtifactForPath(path), mode)) { return(PathExistence.ExistsAsFile); } return(PathExistence.Nonexistent); }
/// <summary> /// Queries the existence and members of a directory in the specified file system mode /// </summary> public Possible <PathExistence> TryEnumerateDirectory(AbsolutePath path, FileSystemViewMode mode, Action <string, AbsolutePath, PathExistence> handleEntry, bool cachePathExistence = true) { FileSystemEntry entry; PathExistence existence; if (PathExistenceCache.TryGetValue(path, out entry) && entry.TryGetExistence(mode, out existence)) { if (existence != PathExistence.ExistsAsDirectory) { return(existence); } // For graph file systems, directory members can be determined by overlaying path table with existence state in-memory // For real file system, this same is true if the directory has already been enumerated if (mode != FileSystemViewMode.Real || ((entry.Flags & FileSystemEntryFlags.IsRealFileSystemEnumerated) != 0)) { foreach (var childPathValue in PathTable.EnumerateImmediateChildren(path.Value)) { var childPath = new AbsolutePath(childPathValue); PathExistence childExistence; if (TryGetKnownPathExistence(childPath, mode, out childExistence) && childExistence != PathExistence.Nonexistent) { var entryName = childPath.GetName(PathTable).ToString(PathTable.StringTable); handleEntry(entryName, childPath, childExistence); } } return(existence); } } if (mode == FileSystemViewMode.Real) { var handleDirectoryEntry = new Action <string, FileAttributes>((entryName, entryAttributes) => { var childExistence = (entryAttributes & FileAttributes.Directory) != 0 ? PathExistence.ExistsAsDirectory : PathExistence.ExistsAsFile; var childPath = path.Combine(PathTable, entryName); childExistence = GetOrAddExistence(childPath, mode, childExistence, updateParents: false); // NOTE: Because we are caching file system state in memory, it is possible that the existence state of // files does not match the state from the file system. if (childExistence != PathExistence.Nonexistent) { handleEntry(entryName, childPath, childExistence); } }); Counters.IncrementCounter(FileSystemViewCounters.RealFileSystemEnumerations); if (cachePathExistence) { Possible <PathExistence> possibleExistence; using (Counters.StartStopwatch(FileSystemViewCounters.RealFileSystemEnumerationsDuration)) { possibleExistence = LocalDiskFileSystem.TryEnumerateDirectoryAndTrackMembership(path, handleDirectoryEntry); } if (possibleExistence.Succeeded) { existence = GetOrAddExistence(path, mode, possibleExistence.Result); PathExistenceCache.AddOrUpdate(path, false, (key, data) => { throw Contract.AssertFailure("Entry should already be added for path"); }, (key, data, oldValue) => oldValue.SetFlag(FileSystemEntryFlags.IsRealFileSystemEnumerated)); return(existence); } return(possibleExistence); } using (Counters.StartStopwatch(FileSystemViewCounters.RealFileSystemEnumerationsDuration)) { var possibleFingerprintResult = DirectoryMembershipTrackingFingerprinter.ComputeFingerprint( path.Expand(PathTable).ExpandedPath, handleEntry: handleDirectoryEntry); return(possibleFingerprintResult.Succeeded ? new Possible <PathExistence>(possibleFingerprintResult.Result.PathExistence) : new Possible <PathExistence>(possibleFingerprintResult.Failure)); } } else if (ExistsInGraphFileSystem(PipGraph.TryGetLatestFileArtifactForPath(path), mode)) { return(PathExistence.ExistsAsFile); } return(PathExistence.Nonexistent); }