예제 #1
0
        public static void EnsureDirectoryExists(this FileSystemManager fs, string path)
        {
            path = PathTools.Normalize(path);
            if (fs.DirectoryExists(path))
            {
                return;
            }

            PathTools.GetMountNameLength(path, out int mountNameLength).ThrowIfFailure();

            // Find the first subdirectory in the path that doesn't exist
            int i;

            for (i = path.Length - 1; i > mountNameLength + 2; i--)
            {
                if (path[i] == '/')
                {
                    string subPath = path.Substring(0, i);

                    if (fs.DirectoryExists(subPath))
                    {
                        break;
                    }
                }
            }

            // path[i] will be a '/', so skip that character
            i++;

            for (; i < path.Length; i++)
            {
                if (path[i] == '/')
                {
                    string subPath = path.Substring(0, i);

                    fs.CreateDirectory(subPath);
                }
            }

            fs.CreateDirectory(path);
        }
예제 #2
0
        public static void CopyFile(this FileSystemManager fs, string sourcePath, string destPath, IProgressReport logger = null)
        {
            using (FileHandle sourceHandle = fs.OpenFile(sourcePath, OpenMode.Read))
                using (FileHandle destHandle = fs.OpenFile(destPath, OpenMode.Write | OpenMode.Append))
                {
                    const int maxBufferSize = 0x10000;

                    long fileSize   = fs.GetFileSize(sourceHandle);
                    int  bufferSize = (int)Math.Min(maxBufferSize, fileSize);

                    logger?.SetTotal(fileSize);

                    byte[] buffer = ArrayPool <byte> .Shared.Rent(bufferSize);

                    try
                    {
                        for (long offset = 0; offset < fileSize; offset += bufferSize)
                        {
                            int         toRead = (int)Math.Min(fileSize - offset, bufferSize);
                            Span <byte> buf    = buffer.AsSpan(0, toRead);

                            fs.ReadFile(sourceHandle, buf, offset);
                            fs.WriteFile(destHandle, buf, offset);

                            logger?.ReportAdd(toRead);
                        }
                    }
                    finally
                    {
                        ArrayPool <byte> .Shared.Return(buffer);

                        logger?.SetTotal(0);
                    }

                    fs.FlushFile(destHandle);
                }
        }
예제 #3
0
 public static IEnumerable <DirectoryEntry> EnumerateEntries(this FileSystemManager fs, string path, string searchPattern)
 {
     return(fs.EnumerateEntries(path, searchPattern, SearchOptions.RecurseSubdirectories));
 }
예제 #4
0
 public static IEnumerable <DirectoryEntry> EnumerateEntries(this FileSystemManager fs, string path)
 {
     return(fs.EnumerateEntries(path, "*"));
 }
예제 #5
0
 public static void CreateOrOverwriteFile(this FileSystemManager fs, string path, long size)
 {
     fs.CreateOrOverwriteFile(path, size, CreateFileOptions.None);
 }
예제 #6
0
 public static bool FileExists(this FileSystemManager fs, string path)
 {
     return(fs.GetEntryType(path) == DirectoryEntryType.File);
 }