Beispiel #1
0
        /// <summary>
        /// Computes the existence of a path when not cached. For graph file systems the existence state of all paths
        /// is statically known, so non-existent is returned here as not finding a cached path means it does not appear
        /// in graph file system.
        /// </summary>
        private Possible <PathExistence> ComputeAndAddCacheRealFileSystemExistence(AbsolutePath path, FileSystemViewMode mode, bool?isReadOnly = default)
        {
            Contract.Requires(mode == FileSystemViewMode.Real);

            // Optimization. Check if the path can be determined to not exist based on a parent path without
            // checking file system
            if (m_inferNonExistenceBasedOnParentPathInRealFileSystem &&
                TryInferNonExistenceBasedOnParentPathInRealFileSystem(path, out var trackedParentPath, out var intermediateParentPath) &&
                TrackRealFileSystemAbsentChildPath(trackedParentPath, descendantPath: path))
            {
                return(PathExistence.Nonexistent);
            }

            // TODO: Some kind of strategy to trigger enumerating directories with commonly probed members
            // TODO: Perhaps probabilistically enumerate based on hash of path and some counter
            var possibleExistence = LocalDiskFileSystem.TryProbeAndTrackPathForExistence(path.Expand(PathTable), isReadOnly);

            Counters.IncrementCounter(FileSystemViewCounters.RealFileSystemDiskProbes);
            if (possibleExistence.Succeeded)
            {
                GetOrAddExistence(path, mode, possibleExistence.Result);
            }

            return(possibleExistence);
        }
Beispiel #2
0
        private bool TrackRealFileSystemAbsentChildPath(AbsolutePath trackedParentPath, AbsolutePath descendantPath)
        {
            var path = descendantPath;

            while (path.IsValid && path != trackedParentPath)
            {
                GetOrAddExistence(path, FileSystemViewMode.Real, PathExistence.Nonexistent);
                path = path.GetParent(PathTable);
            }

            return(LocalDiskFileSystem.TrackAbsentPath(trackedParentPath, descendantPath));
        }
Beispiel #3
0
        /// <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);
        }
Beispiel #4
0
        /// <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);
        }