/// <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> /// 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); } }
/// <summary> /// Gets the file. /// </summary> /// <param name="path">The path.</param> /// <param name="addExistingFile">if set to <c>true</c> [add existing file].</param> /// <returns></returns> public DirectoryMapFile GetFile(string path, bool addExistingFile) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } string fullPath = QQnPath.Combine(_directory, path); path = QQnPath.MakeRelativePath(_directory, fullPath); if (_data.Files.Contains(path)) { return(DoGetFile(path)); } else if (addExistingFile && File.Exists(fullPath)) { return(AddFile(path)); } else { return(null); } }