示例#1
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);
            }
        }
示例#2
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);
        }
示例#3
0
        public void ShadowDeleteDirectory()
        {
            var path = IOHelper.MapPath("FileSysTests");

            Directory.CreateDirectory(path);
            Directory.CreateDirectory(path + "/ShadowTests");
            Directory.CreateDirectory(path + "/ShadowSystem");

            var fs  = new PhysicalFileSystem(path + "/ShadowTests/", "ignore");
            var sfs = new PhysicalFileSystem(path + "/ShadowSystem/", "ignore");
            var ss  = new ShadowFileSystem(fs, sfs);

            Directory.CreateDirectory(path + "/ShadowTests/d1");
            Directory.CreateDirectory(path + "/ShadowTests/d2");

            var files = fs.GetFiles("");

            Assert.AreEqual(0, files.Count());

            var dirs = fs.GetDirectories("");

            Assert.AreEqual(2, dirs.Count());
            Assert.IsTrue(dirs.Contains("d1"));
            Assert.IsTrue(dirs.Contains("d2"));

            ss.DeleteDirectory("d1");

            Assert.IsTrue(Directory.Exists(path + "/ShadowTests/d1"));
            Assert.IsTrue(fs.DirectoryExists("d1"));
            Assert.IsFalse(ss.DirectoryExists("d1"));

            dirs = ss.GetDirectories("");
            Assert.AreEqual(1, dirs.Count());
            Assert.IsTrue(dirs.Contains("d2"));
        }
示例#4
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);
                }
            }
        }
    public void ShadowDeleteDirectory()
    {
        var path = HostingEnvironment.MapPathContentRoot("FileSysTests");

        Directory.CreateDirectory(path);
        Directory.CreateDirectory(path + "/ShadowTests");
        Directory.CreateDirectory(path + "/ShadowSystem");

        var fs  = new PhysicalFileSystem(IOHelper, HostingEnvironment, Logger, path + "/ShadowTests/", "ignore");
        var sfs = new PhysicalFileSystem(IOHelper, HostingEnvironment, Logger, path + "/ShadowSystem/", "ignore");
        var ss  = new ShadowFileSystem(fs, sfs);

        Directory.CreateDirectory(path + "/ShadowTests/d1");
        Directory.CreateDirectory(path + "/ShadowTests/d2");

        var files = fs.GetFiles(string.Empty);

        Assert.AreEqual(0, files.Count());

        var dirs = fs.GetDirectories(string.Empty);

        Assert.AreEqual(2, dirs.Count());
        Assert.IsTrue(dirs.Contains("d1"));
        Assert.IsTrue(dirs.Contains("d2"));

        ss.DeleteDirectory("d1");

        Assert.IsTrue(Directory.Exists(path + "/ShadowTests/d1"));
        Assert.IsTrue(fs.DirectoryExists("d1"));
        Assert.IsFalse(ss.DirectoryExists("d1"));

        dirs = ss.GetDirectories(string.Empty);
        Assert.AreEqual(1, dirs.Count());
        Assert.IsTrue(dirs.Contains("d2"));
    }
示例#6
0
        /// <summary>
        /// Get the config value give a setting name
        /// </summary>
        /// <param name="settingName">The name of the config setting</param>
        /// <param name="forceOutsideEnlistment">
        /// If false, will run the call from inside the enlistment if the working dir found,
        /// otherwise it will run it from outside the enlistment.
        /// </param>
        /// <returns>The value found for the setting.</returns>
        public virtual ConfigResult GetFromConfig(string settingName, bool forceOutsideEnlistment = false, PhysicalFileSystem fileSystem = null)
        {
            string command = string.Format("config {0}", settingName);
            fileSystem = fileSystem ?? new PhysicalFileSystem();

            // This method is called at clone time, so the physical repo may not exist yet.
            return
                fileSystem.DirectoryExists(this.workingDirectoryRoot) && !forceOutsideEnlistment
                    ? new ConfigResult(this.InvokeGitAgainstDotGitFolder(command), settingName)
                    : new ConfigResult(this.InvokeGitOutsideEnlistment(command), settingName);
        }
        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);
        }
示例#8
0
        public void Pull()
        {
            var filesystem = new PhysicalFileSystem();
            var workDir    = filesystem.CreateTempWorkingDir();

            var target = new GitCommandLineWrapper(workDir);

            target.Init();

            Assert.IsTrue(filesystem.DirectoryExists(Path.Combine(workDir, ".git")));

            filesystem.DeleteTempWorkingDirs();
        }
示例#9
0
        public void TestDirectorySpecial()
        {
            var fs = new PhysicalFileSystem();

            // CreateDirectory
            Assert.True(fs.DirectoryExists("/"));
            if (IsWindows)
            {
                var directories = fs.EnumerateDirectories("/").ToList();
                Assert.Equal(new List <UPath>()
                {
                    "/mnt"
                }, directories);

                var drives = fs.EnumerateDirectories("/mnt").ToList();
                Assert.True(drives.Count > 0);

                var allDrives  = DriveInfo.GetDrives().Select(d => d.Name[0].ToString().ToLowerInvariant()).ToList();
                var driveNames = drives.Select(d => d.GetName()).ToList();
                Assert.Equal(allDrives, driveNames);

                Assert.True(fs.DirectoryExists("/"));
                Assert.True(fs.DirectoryExists("/mnt"));
                Assert.True(fs.DirectoryExists(drives[0]));

                var files = fs.EnumerateFiles("/").ToList();
                Assert.True(files.Count == 0);

                files = fs.EnumerateFiles("/mnt").ToList();
                Assert.True(files.Count == 0);

                var paths = fs.EnumeratePaths("/").ToList();
                Assert.Equal(new List <UPath>()
                {
                    "/mnt"
                }, paths);
            }
        }
        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);
        }
        private void MigrateBlobSizes(ITracer tracer, string enlistmentRoot, string newBlobSizesRoot)
        {
            string             esentBlobSizeFolder = Path.Combine(enlistmentRoot, GVFSPlatform.Instance.Constants.DotGVFSRoot, BlobSizesName);
            PhysicalFileSystem fileSystem          = new PhysicalFileSystem();

            if (!fileSystem.DirectoryExists(esentBlobSizeFolder))
            {
                tracer.RelatedInfo("Copied no ESENT blob size entries. {0} does not exist", esentBlobSizeFolder);
                return;
            }

            try
            {
                using (PersistentDictionary <string, long> oldBlobSizes = new PersistentDictionary <string, long>(esentBlobSizeFolder))
                    using (BlobSizes newBlobSizes = new BlobSizes(newBlobSizesRoot, fileSystem, tracer))
                    {
                        newBlobSizes.Initialize();

                        int copiedCount = 0;
                        int totalCount  = oldBlobSizes.Count;
                        foreach (KeyValuePair <string, long> kvp in oldBlobSizes)
                        {
                            Sha1Id sha1;
                            string error;
                            if (Sha1Id.TryParse(kvp.Key, out sha1, out error))
                            {
                                newBlobSizes.AddSize(sha1, kvp.Value);

                                if (copiedCount++ % 5000 == 0)
                                {
                                    tracer.RelatedInfo("Copied {0}/{1} ESENT blob size entries", copiedCount, totalCount);
                                }
                            }
                            else
                            {
                                tracer.RelatedWarning($"Corrupt entry ({kvp.Key}) found in BlobSizes, skipping.  Error: {error}");
                            }
                        }

                        newBlobSizes.Flush();
                        newBlobSizes.Shutdown();
                        tracer.RelatedInfo("Upgrade complete: Copied {0}/{1} ESENT blob size entries", copiedCount, totalCount);
                    }
            }
            catch (EsentException ex)
            {
                tracer.RelatedWarning("BlobSizes appears to be from an older version of GVFS and corrupted, skipping upgrade of blob sizes: " + ex.Message);
            }
        }
        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);
        }
        /// <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));
        }
    public void ShadowCreateFileInDir()
    {
        var path = HostingEnvironment.MapPathContentRoot("FileSysTests");

        Directory.CreateDirectory(path);
        Directory.CreateDirectory(path + "/ShadowTests");
        Directory.CreateDirectory(path + "/ShadowSystem");

        var fs  = new PhysicalFileSystem(IOHelper, HostingEnvironment, Logger, path + "/ShadowTests/", "ignore");
        var sfs = new PhysicalFileSystem(IOHelper, HostingEnvironment, Logger, path + "/ShadowSystem/", "ignore");
        var ss  = new ShadowFileSystem(fs, sfs);

        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
        {
            ss.AddFile("sub/f1.txt", ms);
        }

        Assert.IsFalse(File.Exists(path + "/ShadowTests/sub/f1.txt"));
        Assert.IsTrue(File.Exists(path + "/ShadowSystem/sub/f1.txt"));
        Assert.IsFalse(fs.FileExists("sub/f1.txt"));
        Assert.IsTrue(ss.FileExists("sub/f1.txt"));

        Assert.IsFalse(fs.DirectoryExists("sub"));
        Assert.IsTrue(ss.DirectoryExists("sub"));

        var dirs = fs.GetDirectories(string.Empty);

        Assert.AreEqual(0, dirs.Count());

        dirs = ss.GetDirectories(string.Empty);
        Assert.AreEqual(1, dirs.Count());
        Assert.IsTrue(dirs.Contains("sub"));

        var files = ss.GetFiles("sub");

        Assert.AreEqual(1, files.Count());

        string content;

        using (var stream = ss.OpenFile("sub/f1.txt"))
        {
            content = new StreamReader(stream).ReadToEnd();
        }

        Assert.AreEqual("foo", content);
    }
示例#15
0
        /// <summary>
        /// Saves the contents of an uploaded image file.
        /// </summary>
        /// <param name="targetFolder">Location where to save the image file.</param>
        /// <param name="file">The uploaded image file.</param>
        /// <exception cref="InvalidOperationException">Invalid MIME content type.</exception>
        /// <exception cref="InvalidOperationException">Invalid file extension.</exception>
        /// <exception cref="InvalidOperationException">File size limit exceeded.</exception>
        /// <returns>The relative path where the file is stored.</returns>
        private static string SaveFile(string targetFolder, HttpPostedFile file)
        {
            const int megabyte = 1024 * 1024;

            string[] extensions = { ".gif", ".jpg", ".png", ".svg", ".webp" };
            var      fs         = new PhysicalFileSystem("~" + targetFolder);

            if (!fs.DirectoryExists(fs.GetFullPath("")))
            {
                Directory.CreateDirectory(fs.GetFullPath(""));
            }

            if (!file.ContentType.StartsWith("image/"))
            {
                throw new InvalidOperationException("Invalid MIME content type.");
            }

            var extension = Path.GetExtension(file.FileName.ToLowerInvariant());

            if (!extensions.Contains(extension))
            {
                throw new InvalidOperationException("Invalid file extension.");
            }

            if (file.ContentLength > (8 * megabyte))
            {
                throw new InvalidOperationException("File size limit exceeded.");
            }

            var fileName = Guid.NewGuid() + extension;
            var path     = Path.Combine(fs.GetFullPath(""), fileName);

            file.SaveAs(path);

            return(Combine(targetFolder, fileName));
        }