DirectoryMapFile DoGetFile(string name) { if (_data.Files.Contains(name)) { return((DirectoryMapFile)_data.Files[name]); } DirectoryMapFile dmf = new DirectoryMapFile(name); _data.Files.Add(dmf); _data.Dirty = true; return(dmf); }
/// <summary> /// Creates the file. /// </summary> /// <param name="path">The path.</param> /// <param name="fileMode">The file mode.</param> /// <param name="hash">The hash.</param> /// <param name="size">The size.</param> /// <param name="allowExternal">if set to <c>true</c> [allow external].</param> /// <returns></returns> public Stream OpenFile(string path, FileMode fileMode, string hash, long size, bool allowExternal) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } string fullPath = QQnPath.CombineFullPath(_directory, path); path = QQnPath.MakeRelativePath(_directory, fullPath); switch (fileMode) { case FileMode.CreateNew: case FileMode.Create: if (!allowExternal && File.Exists(fullPath)) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "Unmanaged file {0} exists", path)); } break; case FileMode.Open: case FileMode.Truncate: if (!_data.Files.Contains(path) && (!allowExternal || !File.Exists(path))) { throw new IOException("File does not exist"); } break; default: throw new ArgumentException("The specified mode is not supported"); } if (size < 0) { size = -1; } CreateDirectory(Path.GetDirectoryName(fullPath)); DirectoryMapFile file = DoGetFile(path); if (file.ToBeDeleted && (fileMode == FileMode.Create || fileMode == FileMode.CreateNew)) { file.ToBeDeleted = false; } return(new DirectoryMapStream(File.Open(fullPath, fileMode), file, fileMode, hash, string.IsNullOrEmpty(hash) ? -1 : size, _data.HashType)); }
/// <summary> /// Unschedules the delete. /// </summary> /// <param name="path">The path.</param> public void UnscheduleDelete(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } path = QQnPath.EnsureRelativePath(_directory, path); if (!_data.Files.Contains(path)) { return; // Not in DirectoryMap } DirectoryMapFile file = DoGetFile(path); file.ToBeDeleted = false; }
/// <summary> /// Initializes a new instance of the <see cref="DirectoryMapStream"/> class. /// </summary> /// <param name="parentStream">The parent stream.</param> /// <param name="file">The file.</param> /// <param name="mode">The mode.</param> /// <param name="hash">The hash.</param> /// <param name="size">The size.</param> /// <param name="hashType">Type of the hash.</param> internal DirectoryMapStream(Stream parentStream, DirectoryMapFile file, FileMode mode, string hash, long size, HashType hashType) : base(parentStream, true, UseAsync(mode, hash)) { if (mode == FileMode.Create || mode == FileMode.CreateNew) { _newHash = hash; _updateOnClose = true; if (string.IsNullOrEmpty(hash)) { _hashOnClose = true; _hashWhileWriting = true; } } _hashType = hashType; _file = file; _size = size; }
/// <summary> /// Deletes the specified file if it is within the DirectoryMap /// </summary> /// <param name="path"></param> public void DeleteFile(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } path = QQnPath.EnsureRelativePath(_directory, path); if (!_data.Files.Contains(path)) { return; // Not in DirectoryMap } DirectoryMapFile file = DoGetFile(path); if (file.Exists) { File.Delete(file.FullName); } _data.Files.Remove(path); }
/// <summary> /// Adds the file. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> public DirectoryMapFile AddFile(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } string fullPath = QQnPath.Combine(_directory, path); path = QQnPath.MakeRelativePath(_directory, fullPath); if (_data.Files.Contains(path) || File.Exists(fullPath)) { DirectoryMapFile dmf = DoGetFile(path); dmf.ToBeDeleted = false; return(dmf); } else { throw new FileNotFoundException("File not found", fullPath); } }