示例#1
0
        public void TestDirectoryExceptions()
        {
            var fs = new PhysicalFileSystem();

            // Try to create a folder on an unauthorized location
            fs.CreateDirectory("/");
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt2"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt/yoyo"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt/c"));

            var drives = fs.EnumerateDirectories("/mnt").ToList();

            Assert.True(drives.Count > 0);

            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory("/", drives[0] / "ShouldNotHappen"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory("/mnt", drives[0] / "ShouldNotHappen"));
            Assert.Throws <DirectoryNotFoundException>(() => fs.MoveDirectory("/mnt2", drives[0] / "ShouldNotHappen"));

            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory(drives[0] / "ShouldNotHappen", "/"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory(drives[0] / "ShouldNotHappen", "/mnt"));
            Assert.Throws <DirectoryNotFoundException>(() => fs.MoveDirectory(drives[0] / "ShouldNotHappen", "/mnt2"));

            Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteDirectory("/", false));
            Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteDirectory("/mnt", false));
            Assert.Throws <DirectoryNotFoundException>(() => fs.DeleteDirectory("/mnt2", false));
            Assert.Throws <DirectoryNotFoundException>(() => fs.DeleteDirectory("/mnt/yoyo", false));
        }
        public void 建立資料夾()
        {
            using var fileSystem = new PhysicalFileSystem();
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var rootUPath         = fileSystem.ConvertPathFromInternal(rootPath);

            var subName      = "TestFolder";
            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
            fileSystem.DeleteDirectory(subPath, true);
        }
        public void 列舉根路徑內的子資料夾()
        {
            using var fileSystem = new PhysicalFileSystem();
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var rootUPath         = fileSystem.ConvertPathFromInternal(rootPath);
            var rootUPath1        = fileSystem.ConvertPathToInternal(rootUPath);
            var subName           = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var directoryEntries = fileSystem.EnumerateDirectoryEntries(subPath);

            foreach (var entry in directoryEntries)
            {
                Console.WriteLine(entry.Path);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }
        public void 修改檔案日期()
        {
            using var fileSystem = new PhysicalFileSystem();
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var rootUPath         = fileSystem.ConvertPathFromInternal(rootPath);
            var subName           = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subFile2     = $"{subPath}/1/2.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var fileEntry = fileSystem.GetFileEntry(subFile1);

            fileEntry.CreationTime   = new DateTime(1900, 1, 1);
            fileEntry.LastWriteTime  = new DateTime(1900, 1, 2);
            fileEntry.LastAccessTime = new DateTime(1900, 1, 3);

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }
示例#5
0
        static void Bundle(bool run = true)
        {
            List <string> files = Directory.GetFiles("../../../Core", "*.dll").ToList();

            files.AddRange(Directory.GetFiles("./", "*.txt"));


            PhysicalFileSystem fileSystem = new PhysicalFileSystem("../../../dev/");

            fileSystem.CreateDirectory(FileSystemPath.Parse("/system/"));

            foreach (string file in files)
            {
                if (!file.Contains("MyOS."))
                {
                    using (Stream fileStream = fileSystem.CreateFile(FileSystemPath.Parse("/system/" + new FileInfo(file).Name.Replace(".dll", ".mye").Replace(".txt", ".ini"))))
                    {
                        byte[] fileBytes = File.ReadAllBytes(file);
                        fileStream.Write(fileBytes, 0, fileBytes.Length);
                    }

                    Console.WriteLine("Written {0}", new FileInfo(file).Name.Replace(".dll", ".mye").Replace(".txt", ".ini"));
                }
            }

            Console.WriteLine("Done");

            if (run)
            {
                //process.Kill();
                //process.Start();
            }
        }
示例#6
0
        public static string GetNewLogFileName(
            string logsRoot,
            string prefix,
            string logId = null,
            PhysicalFileSystem fileSystem = null)
        {
            fileSystem = fileSystem ?? new PhysicalFileSystem();

            // TODO: Remove Directory.CreateDirectory() code from here
            // Don't change the state from an accessor.
            if (!fileSystem.DirectoryExists(logsRoot))
            {
                fileSystem.CreateDirectory(logsRoot);
            }

            logId = logId ?? DateTime.Now.ToString("yyyyMMdd_HHmmss");

            string name     = prefix + "_" + logId;
            string fullPath = Path.Combine(
                logsRoot,
                name + ".log");

            if (fileSystem.FileExists(fullPath))
            {
                fullPath = Path.Combine(
                    logsRoot,
                    name + "_" + Guid.NewGuid().ToString("N") + ".log");
            }

            return(fullPath);
        }
示例#7
0
        public static void WriteFiles(Configuration configuration, IEnumerable <CompilationUnit> compilationUnits)
        {
            var physicalFileSystem = new PhysicalFileSystem();
            var outputPath         = Configuration.OsPathToUPath(configuration.OutputPath);

            if (!physicalFileSystem.DirectoryExists(outputPath))
            {
                physicalFileSystem.CreateDirectory(outputPath);
            }

            var fileSystem = new SubFileSystem(physicalFileSystem, outputPath);

            foreach (var unit in compilationUnits)
            {
                var path = (UPath)unit.Path;
                var dir  = path.GetDirectory();

                if (!fileSystem.DirectoryExists(dir))
                {
                    fileSystem.CreateDirectory(dir);
                }

                if (!string.IsNullOrWhiteSpace(unit.Source))
                {
                    fileSystem.WriteAllText(dir / unit.Name + ".cpp", unit.Source);
                }
            }
        }
示例#8
0
        public void TestDirectory()
        {
            var fs                   = new PhysicalFileSystem();
            var pathInfo             = fs.ConvertPathFromInternal(SystemPath);
            var pathToCreate         = pathInfo / "TestCreateDirectory";
            var systemPathToCreate   = fs.ConvertPathToInternal(pathToCreate);
            var movedDirectory       = pathInfo / "TestCreateDirectoryMoved";
            var systemMovedDirectory = fs.ConvertPathToInternal(movedDirectory);

            try
            {
                // CreateDirectory
                Assert.False(Directory.Exists(systemPathToCreate));
                fs.CreateDirectory(pathToCreate);
                Assert.True(Directory.Exists(systemPathToCreate));

                // DirectoryExists
                Assert.True(fs.DirectoryExists(pathToCreate));
                Assert.False(fs.DirectoryExists(pathToCreate / "not_found"));

                // MoveDirectory
                fs.MoveDirectory(pathToCreate, movedDirectory);
                Assert.False(Directory.Exists(systemPathToCreate));
                Assert.True(fs.DirectoryExists(movedDirectory));

                // Delete the directory
                fs.DeleteDirectory(movedDirectory, false);
                Assert.False(Directory.Exists(systemMovedDirectory));
            }
            finally
            {
                SafeDeleteDirectory(systemPathToCreate);
                SafeDeleteDirectory(systemMovedDirectory);
            }
        }
        /// <summary>
        /// Determine what kind of filesystem to use.
        /// After this point we *should theoretically* not need to use System.IO.Path.
        /// </summary>
        public static (IFileSystem, DirectoryEntry) DetermineFileSystem(string path, bool readOnly = false)
        {
            var emptyPath = string.IsNullOrWhiteSpace(path);
            var extension = Path.GetExtension(path);

            Log.Debug($"Determining file system for {path}");

            IFileSystem    fileSystem;
            DirectoryEntry baseEntry;

            if (emptyPath)
            {
                fileSystem = new PhysicalFileSystem();
                baseEntry  = fileSystem.GetDirectoryEntry(
                    fileSystem.ConvertPathFromInternal(Directory.GetCurrentDirectory()));
            }
            else
            {
                // resolve path (relative to absolute)
                path = Path.GetFullPath(path);

                if (Directory.Exists(path) || extension == string.Empty)
                {
                    var physicalFileSystem = new PhysicalFileSystem();
                    var internalPath       = physicalFileSystem.ConvertPathFromInternal(path);
                    physicalFileSystem.CreateDirectory(internalPath);
                    fileSystem = new SubFileSystem(physicalFileSystem, internalPath);
                    baseEntry  = fileSystem.GetDirectoryEntry(UPath.Root);
                }
                else
                {
                    // ensure parent directory exists on disk
                    Directory.CreateDirectory(Path.GetDirectoryName(path));

                    switch (extension)
                    {
                    case "." + SqlitePattern:
                        throw new PlatformNotSupportedException("See https://github.com/QutEcoacoustics/audio-analysis/issues/289");

                        /*
                         * fileSystem = new SqliteFileSystem(
                         *  path,
                         *  readOnly ? OpenMode.ReadOnly : OpenMode.ReadWriteCreate);
                         */
                        break;

                    default:
                        throw new NotSupportedException(
                                  $"Cannot determine file system for given extension {extension}");
                    }

                    baseEntry = new DirectoryEntry(fileSystem, UPath.Root);
                }
            }

            Log.Debug($"Filesystem for {path} is {fileSystem.GetType().Name}");

            return(fileSystem, baseEntry);
        }
示例#10
0
        private static IStorage BuildFileSystemStorage(CrispinConfiguration config)
        {
            var fs = new PhysicalFileSystem();

            fs.CreateDirectory(config.ConnectionString).Wait();

            return(new FileSystemStorage(fs, config.ConnectionString));
        }
        public static Zio.IFileSystem Getfs(string path)
        {
#pragma warning disable CA2000 // Dispose objects before losing scope
            var fs = new PhysicalFileSystem();
#pragma warning restore CA2000 // Dispose objects before losing scope
            fs.CreateDirectory(fs.ConvertPathFromInternal(path));
            return(new SubFileSystem(fs, fs.ConvertPathFromInternal(path), true));
        }
        /// <summary>
        /// Creates a rooted sub-filesystem for the local content folder.
        /// </summary>
        /// <returns>The rooted filesystem.</returns>
        public static IFileSystem CreateContentFileSystem()
        {
            var realFileSystem = new PhysicalFileSystem();

            var executingAssemblyLocation  = Assembly.GetExecutingAssembly().Location;
            var executingAssemblyDirectory = Directory.GetParent(executingAssemblyLocation).FullName;

            var realContentPath = Path.GetFullPath(Path.Combine(executingAssemblyDirectory, "Content"));
            var zioContentPath  = realFileSystem.ConvertPathFromInternal(realContentPath);

            if (!realFileSystem.DirectoryExists(zioContentPath))
            {
                realFileSystem.CreateDirectory(zioContentPath);
            }

            return(new SubFileSystem(realFileSystem, zioContentPath));
        }
示例#13
0
        public GVFSDatabase(PhysicalFileSystem fileSystem, string enlistmentRoot, IDbConnectionFactory connectionFactory, int initialPooledConnections = InitialPooledConnections)
        {
            this.connectionPool    = new BlockingCollection <IDbConnection>();
            this.databasePath      = Path.Combine(enlistmentRoot, GVFSPlatform.Instance.Constants.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.VFSForGit);
            this.connectionFactory = connectionFactory;

            string folderPath = Path.GetDirectoryName(this.databasePath);

            fileSystem.CreateDirectory(folderPath);

            try
            {
                for (int i = 0; i < initialPooledConnections; i++)
                {
                    this.connectionPool.Add(this.connectionFactory.OpenNewConnection(this.databasePath));
                }

                this.Initialize();
            }
            catch (Exception ex)
            {
                throw new GVFSDatabaseException($"{nameof(GVFSDatabase)} constructor threw exception setting up connection pool and initializing", ex);
            }
        }
示例#14
0
        public static bool TryGetMaxGoodPrefetchTimestamp(
            ITracer tracer,
            GVFSEnlistment enlistment,
            PhysicalFileSystem fileSystem,
            GitObjects gitObjects,
            out long maxGoodTimestamp,
            out string error)
        {
            fileSystem.CreateDirectory(enlistment.GitPackRoot);

            string[] packs = gitObjects.ReadPackFileNames(enlistment.GitPackRoot, GVFSConstants.PrefetchPackPrefix);
            List <PrefetchPackInfo> orderedPacks = packs
                                                   .Where(pack => GetTimestamp(pack).HasValue)
                                                   .Select(pack => new PrefetchPackInfo(GetTimestamp(pack).Value, pack))
                                                   .OrderBy(packInfo => packInfo.Timestamp)
                                                   .ToList();

            maxGoodTimestamp = -1;

            int firstBadPack = -1;

            for (int i = 0; i < orderedPacks.Count; ++i)
            {
                long   timestamp = orderedPacks[i].Timestamp;
                string packPath  = orderedPacks[i].Path;
                string idxPath   = Path.ChangeExtension(packPath, ".idx");
                if (!fileSystem.FileExists(idxPath))
                {
                    EventMetadata metadata = new EventMetadata();
                    metadata.Add("pack", packPath);
                    metadata.Add("idxPath", idxPath);
                    metadata.Add("timestamp", timestamp);
                    GitProcess.Result indexResult = gitObjects.IndexPackFile(packPath);
                    if (indexResult.HasErrors)
                    {
                        firstBadPack = i;

                        metadata.Add("Errors", indexResult.Errors);
                        tracer.RelatedWarning(metadata, $"{nameof(TryGetMaxGoodPrefetchTimestamp)}: Found pack file that's missing idx file, and failed to regenerate idx");
                        break;
                    }
                    else
                    {
                        maxGoodTimestamp = timestamp;

                        metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(TryGetMaxGoodPrefetchTimestamp)}: Found pack file that's missing idx file, and regenerated idx");
                        tracer.RelatedEvent(EventLevel.Informational, $"{nameof(TryGetMaxGoodPrefetchTimestamp)}_RebuildIdx", metadata);
                    }
                }
                else
                {
                    maxGoodTimestamp = timestamp;
                }
            }

            if (firstBadPack != -1)
            {
                const int MaxDeleteRetries      = 200; // 200 * IoFailureRetryDelayMS (50ms) = 10 seconds
                const int RetryLoggingThreshold = 40;  // 40 * IoFailureRetryDelayMS (50ms) = 2 seconds

                // Delete packs and indexes in reverse order so that if prefetch is killed, subseqeuent prefetch commands will
                // find the right starting spot.
                for (int i = orderedPacks.Count - 1; i >= firstBadPack; --i)
                {
                    string packPath = orderedPacks[i].Path;
                    string idxPath  = Path.ChangeExtension(packPath, ".idx");

                    EventMetadata metadata = new EventMetadata();
                    metadata.Add("path", idxPath);
                    metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(TryGetMaxGoodPrefetchTimestamp)} deleting bad idx file");
                    tracer.RelatedEvent(EventLevel.Informational, $"{nameof(TryGetMaxGoodPrefetchTimestamp)}_DeleteBadIdx", metadata);
                    if (!fileSystem.TryWaitForDelete(tracer, idxPath, IoFailureRetryDelayMS, MaxDeleteRetries, RetryLoggingThreshold))
                    {
                        error = $"Unable to delete {idxPath}";
                        return(false);
                    }

                    metadata = new EventMetadata();
                    metadata.Add("path", packPath);
                    metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(TryGetMaxGoodPrefetchTimestamp)} deleting bad pack file");
                    tracer.RelatedEvent(EventLevel.Informational, $"{nameof(TryGetMaxGoodPrefetchTimestamp)}_DeleteBadPack", metadata);
                    if (!fileSystem.TryWaitForDelete(tracer, packPath, IoFailureRetryDelayMS, MaxDeleteRetries, RetryLoggingThreshold))
                    {
                        error = $"Unable to delete {packPath}";
                        return(false);
                    }
                }
            }

            error = null;
            return(true);
        }