public void Move(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath)
        {
            bool isFile;
            if ((isFile = sourcePath.IsFile) != destinationPath.IsFile)
                throw new ArgumentException("The specified destination-path is of a different type than the source-path.");

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                {
                    using (var destinationStream = destination.CreateFile(destinationPath))
                    {
                        byte[] buffer = new byte[BufferSize];
                        int readBytes;
                        while ((readBytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                            destinationStream.Write(buffer, 0, readBytes);
                    }
                }
                source.Delete(sourcePath);
            }
            else
            {
                destination.CreateDirectory(destinationPath);
                foreach (var ep in source.GetEntities(sourcePath).ToArray())
                {
                    var destinationEntityPath = ep.IsFile
                                                    ? destinationPath.AppendFile(ep.EntityName)
                                                    : destinationPath.AppendDirectory(ep.EntityName);
                    Move(source, ep, destination, destinationEntityPath);
                }
                if (!sourcePath.IsRoot)
                    source.Delete(sourcePath);
            }
        }
Example #2
0
        public StreamFile(IFileSystem fileSystem, string path, bool readOnly)
        {
            file = fileSystem.FileExists(path) ? fileSystem.OpenFile(path, readOnly) : fileSystem.CreateFile(path);

            endPointer = file.Length;
            fileStream = new StreamFileStream(this);

            ownsFile = true;
        }
Example #3
0
 public ArgParser(string[] args, IFileSystem FileSystem)
 {
     if (FileSystem == null) { throw new ArgumentNullException("FileSystem can not be null"); };
     string configFileContents = FileSystem.ReadFile("default.json");
     if(String.IsNullOrEmpty(configFileContents)) {
         FileSystem.CreateFile("default.json",_defaultConfig);
         configFileContents = _defaultConfig;
     }
     ProjectConfig = Config.LoadConfig(configFileContents);
     if(args != null && !String.IsNullOrEmpty(args[0])) {
         Command = new NewProject(FileSystem) { Config=ProjectConfig, Name=args[0] };
     }
 }
Example #4
0
        public Logger(IFileSystem fileSystem)
        {
            //Create the StreamWriter which the logger will use for outputting to file
            _fileSystem = fileSystem;

            if (!fileSystem.FileExists(_LOG_FILE_PATH))
            {
                _logFileStream = fileSystem.CreateFile(_LOG_FILE_PATH);
            }
            else
            {
                _logFileStream = fileSystem.OpenFile(_LOG_FILE_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            }

            _logFileStreamWriter = new StreamWriter(_logFileStream);
            //Since we are logging, set autoflush to true for immediate writes
            _logFileStreamWriter.AutoFlush = true;
            _logFileStreamWriter.WriteLine("------------ New Buttercup Session (" + DateTime.Now + ") ------------" + Environment.NewLine);
        }
Example #5
0
        public static void ExtractTickets(PartitionFileSystem pfs, string outDirPath, Keyset keyset, Output Out)
        {
            var         OutDirFs   = new LocalFileSystem(outDirPath);
            IDirectory  sourceRoot = pfs.OpenDirectory("/", OpenDirectoryMode.All);
            IDirectory  destRoot   = OutDirFs.OpenDirectory("/", OpenDirectoryMode.All);
            IFileSystem sourceFs   = sourceRoot.ParentFileSystem;
            IFileSystem destFs     = destRoot.ParentFileSystem;

            foreach (var entry in FileIterator(sourceRoot))
            {
                if (entry.Name.EndsWith(".tik") || entry.Name.EndsWith(".cert"))
                {
                    destFs.CreateFile(entry.Name, entry.Size, CreateFileOptions.None);
                    using (IFile srcFile = sourceFs.OpenFile(entry.Name, OpenMode.Read))
                        using (IFile dstFile = destFs.OpenFile(entry.Name, OpenMode.Write))
                        {
                            srcFile.CopyTo(dstFile);
                        }
                }
            }
        }
Example #6
0
        public Result CreateSaveDataMetaFile(ulong saveDataId, SaveDataSpaceId spaceId, SaveMetaType type, long size)
        {
            string metaDirPath = $"/saveMeta/{saveDataId:x16}";

            Result rc = OpenSaveDataDirectoryImpl(out IFileSystem tmpMetaDirFs, spaceId, metaDirPath, true);

            using IFileSystem metaDirFs = tmpMetaDirFs;
            if (rc.IsFailure())
            {
                return(rc);
            }

            string metaFilePath = $"/{(int)type:x8}.meta";

            if (size < 0)
            {
                return(ResultFs.ValueOutOfRange.Log());
            }

            return(metaDirFs.CreateFile(metaFilePath, size, CreateFileOptions.None));
        }
Example #7
0
        public static void ExtractTickets(Xci xci, string outDirPath, Keyset keyset, Output Out)
        {
            var         OutDirFs = new LocalFileSystem(outDirPath);
            IDirectory  destRoot = OutDirFs.OpenDirectory("/", OpenDirectoryMode.All);
            IFileSystem destFs   = destRoot.ParentFileSystem;

            foreach (var entry in FileIterator(xci, keyset, Out))
            {
                var fileName = entry.subPfsFile.Name;
                Out.Log($"{fileName}\r\n");
                if (fileName.EndsWith(".tik") || fileName.EndsWith(".cert"))
                {
                    destFs.CreateFile(fileName, entry.subPfsFile.Size, CreateFileOptions.None);
                    using (IFile srcFile = entry.subPfs.OpenFile(fileName, OpenMode.Read))
                        using (IFile dstFile = destFs.OpenFile(fileName, OpenMode.Write))
                        {
                            srcFile.CopyTo(dstFile);
                        }
                }
            }
        }
        public static void Save(this System.Drawing.Bitmap bitmap, IFileSystem fileSystem, UPath path)
        {
            var extension = path.GetExtensionWithDot();

            ImageFormat format;

            switch (extension)
            {
            case ".png":
                format = ImageFormat.Png;
                break;

            default:
                throw new NotSupportedException();
            }

            using (var fileStream = fileSystem.CreateFile(path))
            {
                bitmap.Save(fileStream, format);
            }
        }
Example #9
0
        public static void WriteContents(IFileSystem fileSystem)
        {
            var rootPath            = FileSystemPath.Parse("/_doc.kml");
            var buildingB1Path      = FileSystemPath.Parse("/files/B1_Building.kml");
            var buildingB1F1Path    = FileSystemPath.Parse("/_BuildingFiles/B1_F1.kml");
            var buildingB1F2Path    = FileSystemPath.Parse("/_BuildingFiles/B1_F2.kml");
            var buildingOutdoorPath = FileSystemPath.Parse("/files/OutdoorLayout_Building.kml");
            var b1   = rootPath.ParentPath.AppendPath(buildingB1Path);
            var f1   = b1.ParentPath.AppendPath(buildingB1F1Path);
            var f2   = b1.ParentPath.AppendPath(buildingB1F2Path);
            var b2   = rootPath.ParentPath.AppendPath(buildingOutdoorPath);
            var list = new List <FileSystemPath> {
                rootPath, b1, f1, f2, b2
            };

            foreach (var path in list)
            {
                using (var stream = fileSystem.CreateFile(path)) {
                    stream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
                }
            }
        }
Example #10
0
        public ExportResult Export(string[] data, string fileName)
        {
            try
            {
                settings.Load();
                string path = settings.ExportPath;

                string fullPath = Path.Combine(path, fileName);

                if (fileSystem.FileExist(fullPath))
                {
                    fileSystem.DeleteFile(fullPath);
                }

                using (var file = fileSystem.CreateFile(fullPath))
                {
                    foreach (var s in data)
                    {
                        file.WriteLine(s);
                    }
                    file.Close();
                }

                return(new ExportResult
                {
                    Result = true,
                    FullPath = fullPath
                });
            }
            catch (Exception ex)
            {
                return(new ExportResult
                {
                    Result = false,
                    FullPath = null,
                    Error = ex.Message
                });
            }
        }
Example #11
0
        public void Copy(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath)
        {
            bool isFile;

            if ((isFile = sourcePath.IsFile) != destinationPath.IsFile)
            {
                throw new ArgumentException("The specified destination-path is of a different type than the source-path.");
            }

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                {
                    using (var destinationStream = destination.CreateFile(destinationPath))
                    {
                        var buffer = new byte[BufferSize];
                        int readBytes;
                        while ((readBytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            destinationStream.Write(buffer, 0, readBytes);
                        }
                    }
                }
            }
            else
            {
                if (!destinationPath.IsRoot)
                {
                    destination.CreateDirectory(destinationPath);
                }
                foreach (var ep in source.GetEntities(sourcePath))
                {
                    var destinationEntityPath = ep.IsFile
                                                ? destinationPath.AppendFile(ep.EntityName)
                                                : destinationPath.AppendDirectory(ep.EntityName);
                    Copy(source, ep, destination, destinationEntityPath);
                }
            }
        }
        public void CleanDirectoryRecursively_DeletesChildren()
        {
            IFileSystem fs = CreateFileSystem();

            fs.CreateDirectory("/dir".ToU8Span());
            fs.CreateDirectory("/dir/dir2".ToU8Span());
            fs.CreateFile("/dir/file1".ToU8Span(), 0, CreateFileOptions.None);

            Result rcDelete = fs.CleanDirectoryRecursively("/dir".ToU8Span());

            Result rcDir1Type = fs.GetEntryType(out DirectoryEntryType dir1Type, "/dir".ToU8Span());
            Result rcDir2Type = fs.GetEntryType(out _, "/dir/dir2".ToU8Span());
            Result rcFileType = fs.GetEntryType(out _, "/dir/file1".ToU8Span());

            Assert.True(rcDelete.IsSuccess());

            Assert.True(rcDir1Type.IsSuccess());
            Assert.Equal(DirectoryEntryType.Directory, dir1Type);

            Assert.Equal(ResultFs.PathNotFound.Value, rcDir2Type);
            Assert.Equal(ResultFs.PathNotFound.Value, rcFileType);
        }
Example #13
0
        public override void Generate(string appName, string filePrefix, uint keySize, uint days, string organization)
        {
            var openSSLConfigBuilder =
                new StringBuilder(_fileSystem.LoadTemplateFile(Resources.Resources.OpenSSLConfigTemplateFileName));

            openSSLConfigBuilder.Replace("$KEY_SIZE", keySize.ToString());
            openSSLConfigBuilder.Replace("$ORG_NAME", organization);
            openSSLConfigBuilder.Replace("$COMMON_NAME", appName);

            var certificateFolder     = _fileSystem.CombinePaths(appName, Constants.DirectoryName.Certificates);
            var certificateConfigPath = _fileSystem.CombinePaths(certificateFolder, Constants.FileName.CertificateConfig);

            try
            {
                _fileSystem.CreateDirectory(certificateFolder);
                _fileSystem.CreateFile(certificateConfigPath, openSSLConfigBuilder.ToString());
            }
            catch (Exception)
            {
                AppioLogger.Warn(string.Format(LoggingText.CertificateGeneratorFailureNonexistentDirectory, appName));
                return;
            }

            var separator = filePrefix == string.Empty ? "" : "_";

            try
            {
                _fileSystem.CallExecutable(Constants.ExecutableName.OpenSSL, certificateFolder, string.Format(Constants.ExternalExecutableArguments.OpenSSL, filePrefix + separator, days));
                _fileSystem.CallExecutable(Constants.ExecutableName.OpenSSL, certificateFolder, string.Format(Constants.ExternalExecutableArguments.OpenSSLConvertKeyFromPEM, filePrefix + separator + Constants.FileName.PrivateKeyPEM, filePrefix + separator + Constants.FileName.PrivateKeyDER));
            }
            catch (Exception)
            {
                AppioLogger.Warn(LoggingText.CertificateGeneratorFailureOpenSSLError);
            }

            _fileSystem.DeleteFile(_fileSystem.CombinePaths(certificateFolder, filePrefix + separator + Constants.FileName.PrivateKeyPEM));
            AppioLogger.Info(string.Format(LoggingText.CertificateGeneratorSuccess, filePrefix == string.Empty ? appName : appName + "/" + filePrefix));
        }
Example #14
0
        public ArgParser(string[] args, IFileSystem FileSystem)
        {
            if (FileSystem == null)
            {
                throw new ArgumentNullException("FileSystem can not be null");
            }
            ;
            string configFileContents = FileSystem.ReadFile("default.json");

            if (String.IsNullOrEmpty(configFileContents))
            {
                FileSystem.CreateFile("default.json", _defaultConfig);
                configFileContents = _defaultConfig;
            }
            ProjectConfig = Config.LoadConfig(configFileContents);
            if (args != null && !String.IsNullOrEmpty(args[0]))
            {
                Command = new NewProject(FileSystem)
                {
                    Config = ProjectConfig, Name = args[0]
                };
            }
        }
Example #15
0
        public void IFileWrite_CanReadBackWrittenData()
        {
            var data = new byte[] { 7, 4, 1, 0, 8, 5, 2, 9, 6, 3 };

            IFileSystem fs = CreateFileSystem();

            fs.CreateFile("/file".ToU8Span(), data.Length, CreateFileOptions.None);

            fs.OpenFile(out IFile file, "/file".ToU8Span(), OpenMode.Write);
            file.Write(0, data, WriteOption.None);
            file.Dispose();

            var readData = new byte[data.Length];

            fs.OpenFile(out file, "/file".ToU8Span(), OpenMode.Read);
            using (file)
            {
                Assert.True(file.Read(out long bytesRead, 0, readData, ReadOption.None).IsSuccess());
                Assert.Equal(data.Length, bytesRead);
            }

            Assert.Equal(data, readData);
        }
Example #16
0
        protected void AssertCommonReadOnly(IFileSystem fs)
        {
            Assert.True(fs.DirectoryExists("/"));

            Assert.Throws <IOException>(() => fs.CreateDirectory("/test"));
            Assert.Throws <IOException>(() => fs.DeleteDirectory("/test", true));
            Assert.Throws <IOException>(() => fs.MoveDirectory("/drive", "/drive2"));

            Assert.Throws <IOException>(() => fs.CreateFile("/toto.txt"));
            Assert.Throws <IOException>(() => fs.CopyFile("/toto.txt", "/dest.txt", true));
            Assert.Throws <IOException>(() => fs.MoveFile("/drive", "/drive2"));
            Assert.Throws <IOException>(() => fs.DeleteFile("/toto.txt"));
            Assert.Throws <IOException>(() => fs.OpenFile("/toto.txt", FileMode.Create, FileAccess.ReadWrite));
            Assert.Throws <IOException>(() => fs.OpenFile("/toto.txt", FileMode.Open, FileAccess.Write));
            Assert.Throws <IOException>(() => fs.ReplaceFile("/a/a/a.txt", "/A.txt", "/titi.txt", true));

            Assert.Throws <IOException>(() => fs.SetAttributes("/toto.txt", FileAttributes.ReadOnly));
            Assert.Throws <IOException>(() => fs.SetCreationTime("/toto.txt", DateTime.Now));
            Assert.Throws <IOException>(() => fs.SetLastAccessTime("/toto.txt", DateTime.Now));
            Assert.Throws <IOException>(() => fs.SetLastWriteTime("/toto.txt", DateTime.Now));

            AssertCommonRead(fs, true);
        }
Example #17
0
        public static string InstallExecutablePackage(IPackage package, IFileSystem fileSystem)
        {
            var targetId                = Path.GetFileName(fileSystem.Root);
            var targetEntryPoint        = targetId + Constants.BonsaiExtension;
            var targetEntryPointLayout  = targetEntryPoint + Constants.LayoutExtension;
            var packageEntryPoint       = package.Id + Constants.BonsaiExtension;
            var packageEntryPointLayout = packageEntryPoint + Constants.LayoutExtension;

            foreach (var file in package.GetContentFiles())
            {
                var effectivePath = file.EffectivePath;
                if (effectivePath == packageEntryPoint)
                {
                    effectivePath = targetEntryPoint;
                }
                else if (effectivePath == packageEntryPointLayout)
                {
                    effectivePath = targetEntryPointLayout;
                }

                using (var stream = file.GetStream())
                {
                    fileSystem.AddFile(effectivePath, stream);
                }
            }

            var manifest     = Manifest.Create(package);
            var metadata     = Manifest.Create(manifest.Metadata);
            var metadataPath = targetId + global::NuGet.Constants.ManifestExtension;

            using (var stream = fileSystem.CreateFile(metadataPath))
            {
                metadata.Save(stream);
            }

            return(fileSystem.GetFullPath(targetEntryPoint));
        }
Example #18
0
        private void SaveWithAdapter(KoreSaveInfo ksi, IFileSystem fs)
        {
            var kfi = ksi.Kfi;

            if (kfi.Adapter is IMultipleFiles multFileAdapter)
            {
                multFileAdapter.FileSystem = fs;
            }
            kfi.Adapter.LeaveOpen = false;

            var newFilename = string.IsNullOrEmpty(ksi.NewSaveFile) ? Path.GetFileName(kfi.StreamFileInfo.FileName) : Path.GetFileName(ksi.NewSaveFile);
            var streaminfo  = new StreamInfo
            {
                FileData = fs.CreateFile(newFilename),
                FileName = newFilename
            };

            (kfi.Adapter as ISaveFiles).Save(streaminfo, ksi.Version);

            if (streaminfo.FileData.CanRead)
            {
                streaminfo.FileData.Dispose();
            }
        }
        public static async Task <DocumentReference> New(string directory, string baseName, string ext, IFileSystem fs, DocumentConstructor dctor, string contents = null)
        {
            if (ext [0] != '.')
            {
                ext = '.' + ext;
            }

            //
            // Get a name
            //
            var n     = baseName + ext;
            var i     = 1;
            var p     = Path.Combine(directory, n);
            var files = await fs.ListFiles(directory);

            while (files.Exists(x => x.Path == p))
            {
                i++;
                n = baseName + " " + i + ext;
                p = Path.Combine(directory, n);
            }

            return(new DocumentReference(await fs.CreateFile(p, contents), dctor, isNew: true));
        }
Example #20
0
        public Result CreateFile(U8Span path, long size, CreateFileOptions option)
        {
            Result rc = CheckPath(new U8Span(_mountName.Name), path);

            if (rc.IsFailure())
            {
                return(rc);
            }

            if (_isPathCacheAttached)
            {
                throw new NotImplementedException();
            }
            else
            {
                rc = _fileSystem.CreateFile(path, size, option);
                if (rc.IsFailure())
                {
                    return(rc);
                }
            }

            return(Result.Success);
        }
Example #21
0
 private static void ExtractRoot(IFile inputFileBase, IFileSystem destFs, Keyset keyset, Output Out)
 {
     using (var inputFile = new FilePositionStorage(inputFileBase))
     {
         var xci = new Xci(keyset, inputFile);
         ProcessXci.GetTitleKeys(xci, keyset, Out);
         var root = xci.OpenPartition(XciPartitionType.Root);
         if (root == null)
         {
             throw new InvalidDataException("Could not find root partition");
         }
         foreach (var sub in root.Files)
         {
             using (IFile srcFile = root.OpenFile(sub.Name, OpenMode.Read))
             {
                 destFs.CreateFile(sub.Name, srcFile.GetSize(), CreateFileOptions.None);
                 using (IFile dstFile = destFs.OpenFile(sub.Name, OpenMode.Write))
                 {
                     srcFile.CopyTo(dstFile);
                 }
             }
         }
     }
 }
Example #22
0
        public void CreateFile(string fullPath)
        {
            string path = volumeProvider.GetFullPath(fullPath);

            fsDelegate.CreateFile(path);
        }
Example #23
0
		public static async Task<bool> Copy (this IFile src, IFileSystem destFileSystem, string destPath)
		{
#if PORTABLE
			return false;
#else
			IFile dest = null;
			var r = false;
			LocalFileAccess srcLocal = null;
			LocalFileAccess destLocal = null;

			try {

				dest = await destFileSystem.CreateFile (destPath, "");

				srcLocal = await src.BeginLocalAccess ();
				destLocal = await dest.BeginLocalAccess ();

				var srcLocalPath = srcLocal.LocalPath;
				var destLocalPath = destLocal.LocalPath;

				System.IO.File.Copy (srcLocalPath, destLocalPath, overwrite: true);

				r = true;

								
			} catch (Exception ex) {
				Debug.WriteLine (ex);
				r = false;
			}

			if (srcLocal != null) await srcLocal.End ();
			if (destLocal != null) await destLocal.End ();

			return r;

//			await Task.Factory.StartNew (() => {
//
//				var fc = new NSFileCoordinator (filePresenterOrNil: null);
//				NSError coordErr;
//
//				fc.CoordinateReadWrite (
//					srcPath, NSFileCoordinatorReadingOptions.WithoutChanges,
//					destPath, NSFileCoordinatorWritingOptions.ForReplacing,
//					out coordErr, (readUrl, writeUrl) => {
//
//					var r = false;
//					try {
//						File.Copy (readUrl.Path, writeUrl.Path, overwrite: true);
//						r = true;
//					} catch (Exception) {
//						r = false;
//					}
//					tcs.SetResult (r);
//				});
//			});
#endif
		}
Example #24
0
		static async Task<IFile> CopyFileAsync (this IFileSystem src, IFile file, IFileSystem dest, string destDir)
		{
			var contents = await file.ReadAllBytesAsync ();
			var newPath = await dest.GetAvailableNameAsync (Path.Combine (destDir, Path.GetFileName (file.Path)));
			return await dest.CreateFile (newPath, contents);
		}
Example #25
0
        public static async Task <bool> Copy(this IFile src, IFileSystem destFileSystem, string destPath)
        {
#if PORTABLE
            return(false);
#else
            IFile           dest      = null;
            var             r         = false;
            LocalFileAccess srcLocal  = null;
            LocalFileAccess destLocal = null;

            try {
                dest = await destFileSystem.CreateFile(destPath, "");

                srcLocal = await src.BeginLocalAccess();

                destLocal = await dest.BeginLocalAccess();

                var srcLocalPath  = srcLocal.LocalPath;
                var destLocalPath = destLocal.LocalPath;

                System.IO.File.Copy(srcLocalPath, destLocalPath, overwrite: true);

                r = true;
            } catch (Exception ex) {
                Debug.WriteLine(ex);
                r = false;
            }

            if (srcLocal != null)
            {
                await srcLocal.End();
            }
            if (destLocal != null)
            {
                await destLocal.End();
            }

            return(r);

//			await Task.Factory.StartNew (() => {
//
//				var fc = new NSFileCoordinator (filePresenterOrNil: null);
//				NSError coordErr;
//
//				fc.CoordinateReadWrite (
//					srcPath, NSFileCoordinatorReadingOptions.WithoutChanges,
//					destPath, NSFileCoordinatorWritingOptions.ForReplacing,
//					out coordErr, (readUrl, writeUrl) => {
//
//					var r = false;
//					try {
//						File.Copy (readUrl.Path, writeUrl.Path, overwrite: true);
//						r = true;
//					} catch (Exception) {
//						r = false;
//					}
//					tcs.SetResult (r);
//				});
//			});
#endif
        }
Example #26
0
        public static Task <IFile> CreateFile(this IFileSystem fs, string path, string contents)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(contents);

            return(fs.CreateFile(path, bytes));
        }
Example #27
0
        public static Result CopyFile(IFileSystem destFs, IFileSystem sourceFs, ReadOnlySpan <byte> destParentPath,
                                      ReadOnlySpan <byte> sourcePath, ref DirectoryEntry dirEntry, Span <byte> copyBuffer)
        {
            IFile srcFile = null;
            IFile dstFile = null;

            try
            {
                Result rc = sourceFs.OpenFile(out srcFile, new U8Span(sourcePath), OpenMode.Read);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                FsPath dstPath    = default;
                int    dstPathLen = StringUtils.Concat(dstPath.Str, destParentPath);
                dstPathLen = StringUtils.Concat(dstPath.Str, dirEntry.Name, dstPathLen);

                if (dstPathLen > FsPath.MaxLength)
                {
                    throw new ArgumentException();
                }

                rc = destFs.CreateFile(dstPath, dirEntry.Size, CreateFileOptions.None);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                rc = destFs.OpenFile(out dstFile, dstPath, OpenMode.Write);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                long fileSize = dirEntry.Size;
                long offset   = 0;

                while (offset < fileSize)
                {
                    rc = srcFile.Read(out long bytesRead, offset, copyBuffer, ReadOption.None);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = dstFile.Write(offset, copyBuffer.Slice(0, (int)bytesRead), WriteOption.None);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    offset += bytesRead;
                }

                return(Result.Success);
            }
            finally
            {
                srcFile?.Dispose();
                dstFile?.Dispose();
            }
        }
Example #28
0
            public Result Initialize(long commitCount, int fileSystemCount)
            {
                IFile contextFile = null;

                try
                {
                    // Open context file and create if it doesn't exist
                    Result rc = _fileSystem.OpenFile(out contextFile, ContextFileName, OpenMode.Read);

                    if (rc.IsFailure())
                    {
                        if (!ResultFs.PathNotFound.Includes(rc))
                        {
                            return(rc);
                        }

                        rc = _fileSystem.CreateFile(ContextFileName, ContextFileSize, CreateFileOptions.None);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        rc = _fileSystem.OpenFile(out contextFile, ContextFileName, OpenMode.Read);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }
                    }
                }
                finally
                {
                    contextFile?.Dispose();
                }

                try
                {
                    Result rc = _fileSystem.OpenFile(out contextFile, ContextFileName, OpenMode.ReadWrite);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    _context.Version         = CurrentContextVersion;
                    _context.State           = CommitState.NotCommitted;
                    _context.FileSystemCount = fileSystemCount;
                    _context.CommitCount     = commitCount;

                    // Write the initial context to the file
                    rc = contextFile.Write(0, SpanHelpers.AsByteSpan(ref _context), WriteOption.None);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = contextFile.Flush();
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }
                }
                finally
                {
                    contextFile?.Dispose();
                }

                return(_fileSystem.Commit());
            }
Example #29
0
 private void CreateBlankFile(string fileName)
 {
     using (Stream stream = _currentDrive.CreateFile(FileSystemPath.Parse(fileName)))
     {
     }
 }
Example #30
0
        private static void Process(string inputFilePath, string outDirPath, XciTaskType taskType, Keyset keyset, Output Out, bool verifyBeforeDecrypting = true)
        {
            using (var inputFile = File.Open(inputFilePath, FileMode.Open, FileAccess.Read).AsStorage())
                using (var outputFile = File.Open($"{outDirPath}/xciMeta.dat", FileMode.Create))
                {
                    var         OutDirFs = new LocalFileSystem(outDirPath);
                    IDirectory  destRoot = OutDirFs.OpenDirectory("/", OpenDirectoryMode.All);
                    IFileSystem destFs   = destRoot.ParentFileSystem;

                    var header = new byte[] { 0x6e, 0x73, 0x5a, 0x69, 0x70, 0x4d, 0x65, 0x74, 0x61, 0x58, 0x43, 0x49, 0x00 };
                    outputFile.Write(header, 0, header.Length);

                    var xci           = new Xci(keyset, inputFile);
                    var xciHeaderData = new byte[0x200];
                    var xciCertData   = new byte[0x200];
                    inputFile.Read(xciHeaderData, 0);
                    inputFile.Read(xciCertData, 0x7000);
                    outputFile.Write(xciHeaderData, 0, 0x200);
                    outputFile.Write(xciCertData, 0, 0x200);

                    Out.Log(Print.PrintXci(xci));

                    var root = xci.OpenPartition(XciPartitionType.Root);
                    if (root == null)
                    {
                        throw new InvalidDataException("Could not find root partition");
                    }

                    ProcessXci.GetTitleKeys(xci, keyset, Out);
                    foreach (var sub in root.Files)
                    {
                        outputFile.WriteByte(0x0A);
                        outputFile.WriteByte(0x0A);
                        var subDirNameChar = Encoding.ASCII.GetBytes(sub.Name);
                        outputFile.Write(subDirNameChar, 0, subDirNameChar.Length);
                        var subPfs = new PartitionFileSystem(new FileStorage(root.OpenFile(sub, OpenMode.Read)));
                        foreach (var subPfsFile in subPfs.Files)
                        {
                            outputFile.WriteByte(0x0A);
                            var subPfsFileNameChar = Encoding.ASCII.GetBytes(subPfsFile.Name);
                            outputFile.Write(subPfsFileNameChar, 0, subPfsFileNameChar.Length);
                            using (IFile srcFile = subPfs.OpenFile(subPfsFile.Name, OpenMode.Read))
                            {
                                if (taskType == XciTaskType.extractRomFS && subPfsFile.Name.EndsWith(".nca"))
                                {
                                    var fullOutDirPath = $"{outDirPath}/{sub.Name}/{subPfsFile.Name}";
                                    Out.Log($"Extracting {subPfsFile.Name}...\r\n");
                                    ProcessNca.Extract(srcFile.AsStream(), fullOutDirPath, verifyBeforeDecrypting, keyset, Out);
                                }
                                else
                                {
                                    var destFileName = Path.Combine(sub.Name, subPfsFile.Name);
                                    if (!destFs.DirectoryExists(sub.Name))
                                    {
                                        destFs.CreateDirectory(sub.Name);
                                    }
                                    destFs.CreateFile(destFileName, subPfsFile.Size, CreateFileOptions.None);
                                    using (IFile dstFile = destFs.OpenFile(destFileName, OpenMode.Write))
                                    {
                                        if (taskType == XciTaskType.decrypt && subPfsFile.Name.EndsWith(".nca"))
                                        {
                                            ProcessNca.Process(srcFile, dstFile, verifyBeforeDecrypting, keyset, Out);
                                        }
                                        else
                                        {
                                            srcFile.CopyTo(dstFile);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    outputFile.WriteByte(0x0A);
                    outputFile.Dispose();
                }
        }
Example #31
0
        public static Result CopyFile(IFileSystem destFileSystem, IFileSystem sourceFileSystem, U8Span destParentPath,
                                      U8Span sourcePath, ref DirectoryEntry entry, Span <byte> workBuffer)
        {
            // Open source file.
            Result rc = sourceFileSystem.OpenFile(out IFile sourceFile, sourcePath, OpenMode.Read);

            if (rc.IsFailure())
            {
                return(rc);
            }

            using (sourceFile)
            {
                // Open dest file.
                FsPath destPath;
                unsafe { _ = &destPath; } // workaround for CS0165

                var sb = new U8StringBuilder(destPath.Str);
                sb.Append(destParentPath).Append(entry.Name);

                Abort.DoAbortUnless(sb.Length < Unsafe.SizeOf <FsPath>());

                rc = destFileSystem.CreateFile(new U8Span(destPath.Str), entry.Size, CreateFileOptions.None);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                rc = destFileSystem.OpenFile(out IFile destFile, new U8Span(destPath.Str), OpenMode.Write);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                using (destFile)
                {
                    // Read/Write file in work buffer sized chunks.
                    long remaining = entry.Size;
                    long offset    = 0;

                    while (remaining > 0)
                    {
                        rc = sourceFile.Read(out long bytesRead, offset, workBuffer, ReadOption.None);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        rc = destFile.Write(offset, workBuffer.Slice(0, (int)bytesRead), WriteOption.None);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        remaining -= bytesRead;
                        offset    += bytesRead;
                    }
                }
            }

            return(Result.Success);
        }
Example #32
0
        public NTStatus CreateFile(out object handle, out FileStatus fileStatus, string path, AccessMask desiredAccess, FileAttributes fileAttributes, ShareAccess shareAccess, CreateDisposition createDisposition, CreateOptions createOptions, SecurityContext securityContext)
        {
            handle     = null;
            fileStatus = FileStatus.FILE_DOES_NOT_EXIST;
            FileAccess createAccess         = NTFileStoreHelper.ToCreateFileAccess(desiredAccess, createDisposition);
            bool       requestedWriteAccess = (createAccess & FileAccess.Write) > 0;

            bool forceDirectory = (createOptions & CreateOptions.FILE_DIRECTORY_FILE) > 0;
            bool forceFile      = (createOptions & CreateOptions.FILE_NON_DIRECTORY_FILE) > 0;

            if (forceDirectory & (createDisposition != CreateDisposition.FILE_CREATE &&
                                  createDisposition != CreateDisposition.FILE_OPEN &&
                                  createDisposition != CreateDisposition.FILE_OPEN_IF &&
                                  createDisposition != CreateDisposition.FILE_SUPERSEDE))
            {
                return(NTStatus.STATUS_INVALID_PARAMETER);
            }

            // Windows will try to access named streams (alternate data streams) regardless of the FILE_NAMED_STREAMS flag, we need to prevent this behaviour.
            if (!m_fileSystem.SupportsNamedStreams && path.Contains(":"))
            {
                // Windows Server 2003 will return STATUS_OBJECT_NAME_NOT_FOUND
                return(NTStatus.STATUS_NO_SUCH_FILE);
            }

            FileSystemEntry entry = null;

            try
            {
                entry = m_fileSystem.GetEntry(path);
            }
            catch (FileNotFoundException)
            {
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (Exception ex)
            {
                if (ex is IOException || ex is UnauthorizedAccessException)
                {
                    NTStatus status = ToNTStatus(ex);
                    Log(Severity.Verbose, "CreateFile: Error retrieving '{0}'. {1}.", path, status);
                    return(status);
                }
                else
                {
                    throw;
                }
            }

            if (createDisposition == CreateDisposition.FILE_OPEN)
            {
                if (entry == null)
                {
                    return(NTStatus.STATUS_NO_SUCH_FILE);
                }

                fileStatus = FileStatus.FILE_EXISTS;
                if (entry.IsDirectory && forceFile)
                {
                    return(NTStatus.STATUS_FILE_IS_A_DIRECTORY);
                }

                if (!entry.IsDirectory && forceDirectory)
                {
                    return(NTStatus.STATUS_OBJECT_PATH_INVALID);
                }
            }
            else if (createDisposition == CreateDisposition.FILE_CREATE)
            {
                if (entry != null)
                {
                    // File already exists, fail the request
                    Log(Severity.Verbose, "CreateFile: File '{0}' already exists.", path);
                    fileStatus = FileStatus.FILE_EXISTS;
                    return(NTStatus.STATUS_OBJECT_NAME_COLLISION);
                }

                if (!requestedWriteAccess)
                {
                    return(NTStatus.STATUS_ACCESS_DENIED);
                }

                try
                {
                    if (forceDirectory)
                    {
                        Log(Severity.Information, "CreateFile: Creating directory '{0}'", path);
                        entry = m_fileSystem.CreateDirectory(path);
                    }
                    else
                    {
                        Log(Severity.Information, "CreateFile: Creating file '{0}'", path);
                        entry = m_fileSystem.CreateFile(path);
                    }
                }
                catch (Exception ex)
                {
                    if (ex is IOException || ex is UnauthorizedAccessException)
                    {
                        NTStatus status = ToNTStatus(ex);
                        Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
                        return(status);
                    }
                    else
                    {
                        throw;
                    }
                }
                fileStatus = FileStatus.FILE_CREATED;
            }
            else if (createDisposition == CreateDisposition.FILE_OPEN_IF ||
                     createDisposition == CreateDisposition.FILE_OVERWRITE ||
                     createDisposition == CreateDisposition.FILE_OVERWRITE_IF ||
                     createDisposition == CreateDisposition.FILE_SUPERSEDE)
            {
                if (entry == null)
                {
                    if (createDisposition == CreateDisposition.FILE_OVERWRITE)
                    {
                        return(NTStatus.STATUS_OBJECT_PATH_NOT_FOUND);
                    }

                    if (!requestedWriteAccess)
                    {
                        return(NTStatus.STATUS_ACCESS_DENIED);
                    }

                    try
                    {
                        if (forceDirectory)
                        {
                            Log(Severity.Information, "CreateFile: Creating directory '{0}'", path);
                            entry = m_fileSystem.CreateDirectory(path);
                        }
                        else
                        {
                            Log(Severity.Information, "CreateFile: Creating file '{0}'", path);
                            entry = m_fileSystem.CreateFile(path);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is IOException || ex is UnauthorizedAccessException)
                        {
                            NTStatus status = ToNTStatus(ex);
                            Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
                            return(status);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    fileStatus = FileStatus.FILE_CREATED;
                }
                else
                {
                    fileStatus = FileStatus.FILE_EXISTS;
                    if (createDisposition == CreateDisposition.FILE_OPEN_IF)
                    {
                        if (entry.IsDirectory && forceFile)
                        {
                            return(NTStatus.STATUS_FILE_IS_A_DIRECTORY);
                        }

                        if (!entry.IsDirectory && forceDirectory)
                        {
                            return(NTStatus.STATUS_OBJECT_PATH_INVALID);
                        }
                    }
                    else
                    {
                        if (!requestedWriteAccess)
                        {
                            return(NTStatus.STATUS_ACCESS_DENIED);
                        }

                        if (createDisposition == CreateDisposition.FILE_OVERWRITE ||
                            createDisposition == CreateDisposition.FILE_OVERWRITE_IF)
                        {
                            // Truncate the file
                            try
                            {
                                Stream temp = m_fileSystem.OpenFile(path, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite, FileOptions.None);
                                temp.Close();
                            }
                            catch (Exception ex)
                            {
                                if (ex is IOException || ex is UnauthorizedAccessException)
                                {
                                    NTStatus status = ToNTStatus(ex);
                                    Log(Severity.Verbose, "CreateFile: Error truncating '{0}'. {1}.", path, status);
                                    return(status);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            fileStatus = FileStatus.FILE_OVERWRITTEN;
                        }
                        else if (createDisposition == CreateDisposition.FILE_SUPERSEDE)
                        {
                            // Delete the old file
                            try
                            {
                                m_fileSystem.Delete(path);
                            }
                            catch (Exception ex)
                            {
                                if (ex is IOException || ex is UnauthorizedAccessException)
                                {
                                    NTStatus status = ToNTStatus(ex);
                                    Log(Severity.Verbose, "CreateFile: Error deleting '{0}'. {1}.", path, status);
                                    return(status);
                                }
                                else
                                {
                                    throw;
                                }
                            }

                            try
                            {
                                if (forceDirectory)
                                {
                                    Log(Severity.Information, "CreateFile: Creating directory '{0}'", path);
                                    entry = m_fileSystem.CreateDirectory(path);
                                }
                                else
                                {
                                    Log(Severity.Information, "CreateFile: Creating file '{0}'", path);
                                    entry = m_fileSystem.CreateFile(path);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (ex is IOException || ex is UnauthorizedAccessException)
                                {
                                    NTStatus status = ToNTStatus(ex);
                                    Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
                                    return(status);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            fileStatus = FileStatus.FILE_SUPERSEDED;
                        }
                    }
                }
            }
            else
            {
                return(NTStatus.STATUS_INVALID_PARAMETER);
            }

            FileAccess fileAccess = NTFileStoreHelper.ToFileAccess(desiredAccess);
            Stream     stream;

            if (fileAccess == (FileAccess)0 || entry.IsDirectory)
            {
                stream = null;
            }
            else
            {
                // Note that SetFileInformationByHandle/FILE_DISPOSITION_INFO has no effect if the handle was opened with FILE_DELETE_ON_CLOSE.
                NTStatus openStatus = OpenFileStream(out stream, path, fileAccess, shareAccess, createOptions);
                if (openStatus != NTStatus.STATUS_SUCCESS)
                {
                    return(openStatus);
                }
            }

            bool deleteOnClose = (createOptions & CreateOptions.FILE_DELETE_ON_CLOSE) > 0;

            handle = new FileHandle(path, entry.IsDirectory, stream, deleteOnClose);
            if (fileStatus != FileStatus.FILE_CREATED &&
                fileStatus != FileStatus.FILE_OVERWRITTEN &&
                fileStatus != FileStatus.FILE_SUPERSEDED)
            {
                fileStatus = FileStatus.FILE_OPENED;
            }
            return(NTStatus.STATUS_SUCCESS);
        }
Example #33
0
        public void TestFile()
        {
            // Test CreateFile/OpenFile
            var stream          = fs.CreateFile("/toto.txt");
            var writer          = new StreamWriter(stream);
            var originalContent = "This is the content";

            writer.Write(originalContent);
            writer.Flush();
            stream.Dispose();

            // Test FileExists
            Assert.False(fs.FileExists(null));
            Assert.False(fs.FileExists("/titi.txt"));
            Assert.True(fs.FileExists("/toto.txt"));

            // ReadAllText
            var content = fs.ReadAllText("/toto.txt");

            Assert.Equal(originalContent, content);

            // sleep for creation time comparison
            Thread.Sleep(16);

            // Test CopyFile
            fs.CopyFile("/toto.txt", "/titi.txt", true);
            Assert.True(fs.FileExists("/toto.txt"));
            Assert.True(fs.FileExists("/titi.txt"));
            content = fs.ReadAllText("/titi.txt");
            Assert.Equal(originalContent, content);

            // Test Attributes/Times
            Assert.True(fs.GetFileLength("/toto.txt") > 0);
            Assert.Equal(fs.GetFileLength("/toto.txt"), fs.GetFileLength("/titi.txt"));
            Assert.Equal(fs.GetAttributes("/toto.txt"), fs.GetAttributes("/titi.txt"));
            Assert.NotEqual(fs.GetCreationTime("/toto.txt"), fs.GetCreationTime("/titi.txt"));
            // Because we read titi.txt just before, access time must be different
            Assert.NotEqual(fs.GetLastAccessTime("/toto.txt"), fs.GetLastAccessTime("/titi.txt"));
            Assert.Equal(fs.GetLastWriteTime("/toto.txt"), fs.GetLastWriteTime("/titi.txt"));

            var now  = DateTime.Now + TimeSpan.FromSeconds(10);
            var now1 = DateTime.Now + TimeSpan.FromSeconds(11);
            var now2 = DateTime.Now + TimeSpan.FromSeconds(12);

            fs.SetCreationTime("/toto.txt", now);
            fs.SetLastAccessTime("/toto.txt", now1);
            fs.SetLastWriteTime("/toto.txt", now2);
            Assert.Equal(now, fs.GetCreationTime("/toto.txt"));
            Assert.Equal(now1, fs.GetLastAccessTime("/toto.txt"));
            Assert.Equal(now2, fs.GetLastWriteTime("/toto.txt"));

            Assert.NotEqual(fs.GetCreationTime("/toto.txt"), fs.GetCreationTime("/titi.txt"));
            Assert.NotEqual(fs.GetLastAccessTime("/toto.txt"), fs.GetLastAccessTime("/titi.txt"));
            Assert.NotEqual(fs.GetLastWriteTime("/toto.txt"), fs.GetLastWriteTime("/titi.txt"));

            // Test MoveFile
            fs.MoveFile("/toto.txt", "/tata.txt");
            Assert.False(fs.FileExists("/toto.txt"));
            Assert.True(fs.FileExists("/tata.txt"));
            Assert.True(fs.FileExists("/titi.txt"));
            content = fs.ReadAllText("/tata.txt");
            Assert.Equal(originalContent, content);

            // Test Enumerate file
            var files = fs.EnumerateFiles("/").Select(p => p.FullName).ToList();

            files.Sort();
            Assert.Equal(new List <string>()
            {
                "/tata.txt", "/titi.txt"
            }, files);

            var dirs = fs.EnumerateDirectories("/").Select(p => p.FullName).ToList();

            Assert.Empty(dirs);

            // Check ReplaceFile
            var originalContent2 = "this is a content2";

            fs.WriteAllText("/tata.txt", originalContent2);
            fs.ReplaceFile("/tata.txt", "/titi.txt", "/titi.bak.txt", true);
            Assert.False(fs.FileExists("/tata.txt"));
            Assert.True(fs.FileExists("/titi.txt"));
            Assert.True(fs.FileExists("/titi.bak.txt"));
            content = fs.ReadAllText("/titi.txt");
            Assert.Equal(originalContent2, content);
            content = fs.ReadAllText("/titi.bak.txt");
            Assert.Equal(originalContent, content);

            // Check File ReadOnly
            fs.SetAttributes("/titi.txt", FileAttributes.ReadOnly);
            Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteFile("/titi.txt"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CopyFile("/titi.bak.txt", "/titi.txt", true));
            Assert.Throws <UnauthorizedAccessException>(() => fs.OpenFile("/titi.txt", FileMode.Open, FileAccess.ReadWrite));
            fs.SetAttributes("/titi.txt", FileAttributes.Normal);

            // Delete File
            fs.DeleteFile("/titi.txt");
            Assert.False(fs.FileExists("/titi.txt"));
            fs.DeleteFile("/titi.bak.txt");
            Assert.False(fs.FileExists("/titi.bak.txt"));
        }
		public static async Task<DocumentReference> New (string directory, string baseName, string ext, IFileSystem fs, DocumentConstructor dctor, string contents = null)
		{
			if (ext [0] != '.') {
				ext = '.' + ext;
			}

			//
			// Get a name
			//
			var n = baseName + ext;
			var i = 1;
			var p = Path.Combine (directory, n);
			var files = await fs.ListFiles (directory);
			while (files.Exists (x => x.Path == p)) {
				i++;
				n = baseName + " " + i + ext;
				p = Path.Combine (directory, n);
			}

			return new DocumentReference (await fs.CreateFile (p, contents), dctor, isNew: true);
		}
        public void BeginPageData(Vector3I offset, BlockVoxel[,,] data,
                                  Action onSuccess, Action <Exception> onFailure)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (onSuccess == null)
            {
                throw new ArgumentNullException(nameof(onSuccess));
            }
            if (onFailure == null)
            {
                throw new ArgumentNullException(nameof(onFailure));
            }
            if (data.Rank != 3)
            {
                throw new ArgumentException("The rank of the specified data " +
                                            "array was invalid.");
            }

            int width  = data.GetLength(0);
            int height = data.GetLength(1);
            int depth  = data.GetLength(2);

            if (width != height || height != depth)
            {
                throw new ArgumentException("The specified data array is " +
                                            "not cubic.");
            }

            void commitDataAsync()
            {
                try
                {
                    ProbeSaveDirectory();
                    FileSystemPath chunkFilePath = GetChunkFilePath(offset);

                    //True if at least one voxel had a non-default block key.
                    bool containedNonDefaultBlocks = false;

                    using (Stream stream = userDataFileSystem.CreateFile(
                               chunkFilePath, true))
                    {
                        stream.WriteSignedInteger(data.GetLength(0));
                        for (int x = 0; x < width; x++)
                        {
                            for (int y = 0; y < height; y++)
                            {
                                for (int z = 0; z < depth; z++)
                                {
                                    BlockVoxel voxel = data[x, y, z];
                                    stream.Write(voxel);

                                    if (voxel.BlockKey != default)
                                    {
                                        containedNonDefaultBlocks = true;
                                    }
                                }
                            }
                        }
                        stream.Flush();
                    }

                    //Removes empty chunk files to keep the registry clean.
                    if (!containedNonDefaultBlocks)
                    {
                        userDataFileSystem.Delete(chunkFilePath, false);
                    }

                    onSuccess();
                }
                catch (Exception exc) { onFailure(exc); }
            }

            void commitDataAsyncLocked()
            {
                lock (fileSystemLock) commitDataAsync();
            }

            if (userDataFileSystem.IsWritable)
            {
                Task.Run(commitDataAsyncLocked);
            }
            else
            {
                onSuccess();
            }
        }
Example #36
0
        private void EnsurePackageFiles()
        {
            if (_files != null &&
                _expandedFolderPath != null &&
                _expandedFileSystem.DirectoryExists(_expandedFolderPath))
            {
                return;
            }

            _files = new Dictionary <string, PhysicalPackageFile>();
            _supportedFrameworks = null;

            var packageName = new PackageName(Id, Version);

            // Only use the cache for expanded folders under %temp%, or set from unit tests
            if (_expandedFileSystem == _tempFileSystem || _forceUseCache)
            {
                Tuple <string, DateTimeOffset> cacheValue;
                DateTimeOffset lastModifiedTime = _fileSystem.GetLastModified(_packagePath);

                // if the cache doesn't exist, or it exists but points to a stale package,
                // then we invalidate the cache and store the new entry.
                if (!_cachedExpandedFolder.TryGetValue(packageName, out cacheValue) ||
                    cacheValue.Item2 < lastModifiedTime)
                {
                    cacheValue = Tuple.Create(GetExpandedFolderPath(), lastModifiedTime);
                    _cachedExpandedFolder[packageName] = cacheValue;
                }

                _expandedFolderPath = cacheValue.Item1;
            }
            else
            {
                _expandedFolderPath = GetExpandedFolderPath();
            }

            using (Stream stream = GetStream())
            {
                var package = new ZipArchive(stream);
                // unzip files inside package
                var files = from part in package.Entries
                            where ZipPackage.IsPackageFile(part)
                            select part;

                // now copy all package's files to disk
                foreach (ZipArchiveEntry file in files)
                {
                    string path     = file.FullName.Replace('/', '\\').Replace("%2B", "+");
                    string filePath = Path.Combine(_expandedFolderPath, path);

                    bool copyFile = true;

                    if (copyFile)
                    {
                        using (Stream partStream = file.Open())
                        {
                            try
                            {
                                using (Stream targetStream = _expandedFileSystem.CreateFile(filePath))
                                {
                                    partStream.CopyTo(targetStream);
                                }
                            }
                            catch (Exception)
                            {
                                // if the file is read-only or has an access denied issue, we just ignore it
                            }
                        }
                    }

                    var packageFile = new PhysicalPackageFile
                    {
                        SourcePath = _expandedFileSystem.GetFullPath(filePath),
                        TargetPath = path
                    };

                    _files[path] = packageFile;
                }
            }
        }
Example #37
0
        public void IDirectoryRead_AllEntriesAreReturned()
        {
            IFileSystem fs = CreateFileSystem();
            fs.CreateDirectory("/dir".ToU8Span());
            fs.CreateDirectory("/dir/dir1".ToU8Span());
            fs.CreateFile("/dir/dir1/file1".ToU8Span(), 0, CreateFileOptions.None);
            fs.CreateFile("/dir/file1".ToU8Span(), 0, CreateFileOptions.None);
            fs.CreateFile("/dir/file2".ToU8Span(), 0, CreateFileOptions.None);

            Assert.Success(fs.OpenDirectory(out IDirectory dir, "/dir".ToU8Span(), OpenDirectoryMode.All));

            var entry1 = new DirectoryEntry();
            var entry2 = new DirectoryEntry();
            var entry3 = new DirectoryEntry();
            var entry4 = new DirectoryEntry();

            Assert.Success(dir.Read(out long entriesRead1, SpanHelpers.AsSpan(ref entry1)));
            Assert.Success(dir.Read(out long entriesRead2, SpanHelpers.AsSpan(ref entry2)));
            Assert.Success(dir.Read(out long entriesRead3, SpanHelpers.AsSpan(ref entry3)));
            Assert.Success(dir.Read(out long entriesRead4, SpanHelpers.AsSpan(ref entry4)));

            Assert.Equal(1, entriesRead1);
            Assert.Equal(1, entriesRead2);
            Assert.Equal(1, entriesRead3);
            Assert.Equal(0, entriesRead4);

            bool dir1Read = false;
            bool file1Read = false;
            bool file2Read = false;

            // Entries are not guaranteed to be in any particular order
            CheckEntry(ref entry1);
            CheckEntry(ref entry2);
            CheckEntry(ref entry3);

            Assert.True(dir1Read);
            Assert.True(file1Read);
            Assert.True(file2Read);

            void CheckEntry(ref DirectoryEntry entry)
            {
                switch (StringUtils.Utf8ZToString(entry.Name))
                {
                    case "dir1":
                        Assert.False(dir1Read);
                        Assert.Equal(DirectoryEntryType.Directory, entry.Type);

                        dir1Read = true;
                        break;

                    case "file1":
                        Assert.False(file1Read);
                        Assert.Equal(DirectoryEntryType.File, entry.Type);

                        file1Read = true;
                        break;

                    case "file2":
                        Assert.False(file2Read);
                        Assert.Equal(DirectoryEntryType.File, entry.Type);

                        file2Read = true;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }