public IDocumentId MakeDocument(IContentManagerArenaId arena, string filePath, FileUsingMode mode = FileUsingMode.AutoDetect, bool deleteSourceFile = false, string fileName = null) { if (arena is null) { throw new ArgumentNullException(nameof(arena)); } if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentException(nameof(filePath)); } if (!File.Exists(filePath)) { throw new ArgumentException($"{nameof(filePath)} = '{filePath}' does not exists."); } fileName = fileName ?? Path.GetFileName(filePath); if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException(nameof(fileName) + " is empty."); } if (mode == FileUsingMode.AutoDetect) { mode = PathIsNetworkPath(filePath) ? FileUsingMode.CopySourceFile : FileUsingMode.UseSourceFile; } IDocumentId documentId = null; IContentProvider contentProvider = null; Action <IDocumentId> onCloseArena = OnCloseArenaDeleteDocument; switch (mode) { case FileUsingMode.UseSourceFile: { if (!deleteSourceFile) { onCloseArena = null; } documentId = new DocumentIdImpl(arena, filePath); } break; case FileUsingMode.CopySourceFile: { var arenaFilePath = Path.GetTempFileName(); if (deleteSourceFile) { File.Move(filePath, arenaFilePath, true); documentId = new DocumentIdImpl(arena, arenaFilePath); } else { contentProvider = new FileContentProvider(contentDestinationPath => File.Copy(filePath, contentDestinationPath, true), () => File.Length(filePath)); documentId = new DocumentIdImpl(arena, arenaFilePath); } } break; default: throw new NotImplementedException($"{nameof(mode)}={mode}"); } AddDocumentInfo(documentId, new DocumentInfo(fileName, onCloseArena, contentProvider)); return(documentId); }
private void OnCloseArenaDeleteDocument(IDocumentId document) { var documentPath = document.DocumentId; ExceptionHelper.ExceptionCatcher(() => File.Delete(documentPath), where : $"Error while disposing arena {document.Arena}. Probably documentId '{document.DocumentId}' still opened because some stream not disposed."); }
public static BufferedStream Open(string path, FileAccess access = FileAccess.Read) { Stream file; switch (access) { case FileAccess.Read: file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); break; case FileAccess.Write: case FileAccess.ReadWrite: file = File.Open(path, FileMode.Open, access, FileShare.None); break; default: throw new NotImplementedException($"{nameof(access)} mode is not implemented for {access}"); } try { return(new BufferedStream(file, BufferSize)); } catch { file?.Dispose(); throw; } }
private void PushPackage(PackageProject metadata, string packageFormat, byte[] data) { var directory = Path.Combine(configuration.DataPath, metadata.Name, metadata.Version.Name); Directory.CreateDirectory(directory); var file = Path.Combine(directory, GetPackageName(packageFormat, metadata.Name, metadata.Version.Name)); if (File.Exists(file)) { File.Delete(file); } File.WriteAllBytes(file, data); }
private Stream OpenDocumentStream(IDocumentId documentId, FileAccess access, FileShare share) { GetDocumentInfo(documentId).ContentProvider?.ContentPreparation(documentId.DocumentId); var file = File.Open(documentId.DocumentId, FileMode.Open, access, share); try { return(SmartLowMemoryStream.Open(file)); } catch { file?.Dispose(); throw; } }
public IDocumentId MakeDocument(IContentManagerArenaId arena, INamedStream document) { if (document is null) { throw new ArgumentNullException(nameof(document)); } if (document.Stream is null) { throw new ArgumentNullException(nameof(document) + "." + nameof(document.Stream)); } var documentId = new DocumentIdImpl(arena); using (var content = File.Open(documentId.DocumentId, FileMode.Open, FileAccess.Write, FileShare.None)) { //document.Stream.Flush(); document.Stream.CopyTo(content); //content.Flush(); }; AddDocumentInfo(documentId, new DocumentInfo(document.Name, OnCloseArenaDeleteDocument, null)); return(documentId); }
public static Stream Open() { var innerStream = File.Open(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileOptions.DeleteOnClose); try { var outerStream = new BufferedStream(innerStream, BufferSize); try { return(new NotFlushingBufferOnDisposeStream(outerStream, innerStream)); } catch { outerStream.Dispose(); throw; } } catch { innerStream?.Dispose(); throw; } }
private void CreateJob(PackageProject metadata, byte[] data) { string directory = Path.Combine(metadata.Name, metadata.Version.Name); Directory.CreateDirectory(Path.Combine(configuration.DataPath, directory)); string file = Path.Combine(configuration.DataPath, directory, metadata.Name + ".symbols.zip"); if (File.Exists(file)) { File.Delete(file); } File.WriteAllBytes(file, data); using (var zipMemoryStream = new MemoryStream(data)) using (var zipfile = ZipFile.Read(zipMemoryStream)) { var zipInfo = new TransformingWrapperPackageFile(new ZipPackageFile(zipfile), new UrlTransformation()); var addInfo = addInfoBuilder.Build(zipInfo); string binariesDirectory = Path.Combine(directory, "Binaries"); Directory.CreateDirectory(Path.Combine(configuration.DataPath, binariesDirectory)); string sourcesDirectory = Path.Combine(directory, "Sources"); Directory.CreateDirectory(Path.Combine(configuration.DataPath, sourcesDirectory)); foreach (var binaryInfo in addInfo.Binaries) { if (binaryInfo.SymbolInfo == null) { continue; } if (binaryInfo.SymbolHash != binaryInfo.SymbolInfo.Hash) { throw new InvalidOperationException(string.Format("Incorrect hash code for '{0}' binaryHash '{1}' symbolHash '{2}'.", binaryInfo.Name, binaryInfo.SymbolHash, binaryInfo.SymbolInfo.Hash)); } string binaryDirectory = Path.Combine(binariesDirectory, binaryInfo.Name, binaryInfo.SymbolHash); Directory.CreateDirectory(Path.Combine(configuration.DataPath, binaryDirectory)); using (var binaryInfoStream = binaryInfo.File.Stream) { using (var binaryStream = File.OpenWrite(Path.Combine(configuration.DataPath, binaryDirectory, binaryInfo.Name + "." + binaryInfo.Type))) binaryInfoStream.CopyTo(binaryStream); } using (var symbolInfoStream = binaryInfo.SymbolInfo.File.Stream) { using (var symbolStream = File.OpenWrite(Path.Combine(configuration.DataPath, binaryDirectory, binaryInfo.Name + "." + binaryInfo.SymbolInfo.Type))) symbolInfoStream.CopyTo(symbolStream); } string indexDirectory = Path.Combine(configuration.IndexPath, binaryInfo.Name); Directory.CreateDirectory(indexDirectory); File.AppendAllText(Path.Combine(indexDirectory, binaryInfo.SymbolHash + ".txt"), binaryDirectory + Environment.NewLine); var sourceIndex = new List <string>(); foreach (var sourceInfo in binaryInfo.SymbolInfo.SourceInfos.Where(info => info.ActualPath != null)) { string sourcePath = Path.Combine(sourcesDirectory, sourceInfo.KeyPath.Replace(":/", "")); Directory.CreateDirectory(Path.Combine(configuration.DataPath, Path.GetDirectoryName(sourcePath))); sourceIndex.Add(sourceInfo.OriginalPath + "|" + sourceInfo.KeyPath.Replace(":/", "")); using (var sourceInfoStream = sourceInfo.ActualPath.Stream) { using (var convertedStream = SourceConverter.Convert(sourceInfoStream)) { using (var sourceStream = File.OpenWrite(Path.Combine(configuration.DataPath, sourcePath))) convertedStream.CopyTo(sourceStream); } } } var txtFile = Path.Combine(configuration.DataPath, binaryDirectory, binaryInfo.Name + ".txt"); if (File.Exists(txtFile)) { File.Delete(txtFile); } File.WriteAllLines(txtFile, sourceIndex.ToArray()); } } File.Delete(file); }
public override void SetAccessControl(string path, FileSecurity fileSecurity) { var file = new Delimon.Win32.IO.File(); file.SetAccessControl(path, fileSecurity); }