Пример #1
0
        /// <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));
        }