示例#1
0
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
        {
            if (!zipFileEntries.TryGetValue(url, out var zipFileEntry))
            {
                throw new FileNotFoundException("File not found inside ZIP archive.");
            }

            if (mode != VirtualFileMode.Open || access != VirtualFileAccess.Read)
            {
                throw new UnauthorizedAccessException("ZIP archive are read-only.");
            }

            lock (zipFile)
            {
                if (zipFileEntry.Method == Compression.Store)
                {
                    // Open a VirtualFileStream on top of Zip FileStream
                    return(new VirtualFileStream(new FileStream(zipFileEntry.ZipFileName, FileMode.Open, FileAccess.Read), zipFileEntry.FileOffset, zipFileEntry.FileOffset + zipFileEntry.FileSize));
                }

                // Decompress it into a MemoryStream
                var buffer = new byte[zipFileEntry.FileSize];
                zipFile.ExtractFile(zipFileEntry, buffer);
                return(new MemoryStream(buffer));
            }
        }
示例#2
0
        /// <summary>
        /// Opens the stream from a given path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="mode">The stream opening mode (append, open, create, etc...).</param>
        /// <param name="access">The stream access.</param>
        /// <param name="share">The stream share mode.</param>
        /// <param name="provider">The provider used to load the stream.</param>
        /// <returns>The stream.</returns>
        public static Stream OpenStream(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share, out IVirtualFileProvider provider)
        {
            var resolveProviderResult = ResolveProvider(path, false);

            provider = resolveProviderResult.Provider;
            return(provider.OpenStream(resolveProviderResult.Path, mode, access, share));
        }
 /// <inheritdoc/>
 public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
 {
     if (localBasePath != null && url.Split(VirtualFileSystem.DirectorySeparatorChar, VirtualFileSystem.AltDirectorySeparatorChar).Contains(".."))
     {
         throw new InvalidOperationException("Relative path is not allowed in FileSystemProvider.");
     }
     return(new FileStream(ConvertUrlToFullPath(url), (FileMode)mode, (FileAccess)access, (FileShare)share));
 }
示例#4
0
            public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
            {
                if (inMemory.TryGetValue(url, out var bytes))
                {
                    return(new MemoryStream(bytes));
                }

                return(virtualFileProvider.OpenStream(url, mode, access, share, streamFlags));
            }
示例#5
0
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            if (share != VirtualFileShare.Read)
            {
                throw new NotImplementedException();
            }

            lock (files)
            {
                FileInfo fileInfo;
                bool     exists = files.TryGetValue(url, out fileInfo);
                bool     write  = access != VirtualFileAccess.Read;

                switch (mode)
                {
                case VirtualFileMode.CreateNew:
                    if (exists)
                    {
                        throw new IOException("File already exists.");
                    }
                    files.Add(url, fileInfo = new FileInfo());
                    return(new MemoryFileStream(this, fileInfo, write));

                case VirtualFileMode.Create:
                    files.Remove(url);
                    files.Add(url, fileInfo = new FileInfo());
                    return(new MemoryFileStream(this, fileInfo, write));

                case VirtualFileMode.Truncate:
                    if (!exists)
                    {
                        throw new IOException("File doesn't exists.");
                    }
                    files.Remove(url);
                    return(new MemoryStream());

                case VirtualFileMode.Open:
                    if (!exists)
                    {
                        throw new FileNotFoundException();
                    }
                    if (write)
                    {
                        throw new NotImplementedException();
                    }
                    return(new MemoryFileStream(this, fileInfo, false, fileInfo.Data));

                case VirtualFileMode.OpenOrCreate:
                    throw new NotImplementedException();
                }
            }

            return(null);
        }
示例#6
0
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            // Open or create the file through the underlying (IContentIndexMap, IOdbBackend) couple.
            // Also read/write a ObjectHeader.
            if (mode == VirtualFileMode.Open)
            {
                ObjectId objectId;
                if (url.StartsWith(ObjectIdUrl))
                {
                    ObjectId.TryParse(url.Substring(ObjectIdUrl.Length), out objectId);
                }
                else if (!contentIndexMap.TryGetValue(url, out objectId))
                {
                    throw new FileNotFoundException(string.Format("Unable to find the file [{0}]", url));
                }

                var result = objectDatabase.OpenStream(objectId, mode, access, share);

                // copy the stream into a memory stream in order to make it seek-able
                if (streamFlags == StreamFlags.Seekable && !result.CanSeek)
                {
                    var buffer = new byte[result.Length - result.Position];
                    result.Read(buffer, 0, buffer.Length);
                    return(new DatabaseReadFileStream(objectId, new MemoryStream(buffer), 0));
                }

                return(new DatabaseReadFileStream(objectId, result, result.Position));
            }

            if (mode == VirtualFileMode.Create)
            {
                if (url.StartsWith(ObjectIdUrl))
                {
                    throw new NotSupportedException();
                }

                var stream = objectDatabase.CreateStream();

                // Header will be written by DatabaseWriteFileStream
                var result = new DatabaseWriteFileStream(stream, stream.Position);

                stream.Disposed += x =>
                {
                    // Commit index changes
                    contentIndexMap[url] = x.CurrentHash;
                };

                return(result);
            }

            throw new ArgumentException("mode");
        }
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            if (localBasePath != null && url.Split(VirtualFileSystem.DirectorySeparatorChar, VirtualFileSystem.AltDirectorySeparatorChar).Contains(".."))
                throw new InvalidOperationException("Relative path is not allowed in FileSystemProvider.");

            var rawAccess = (NativeFileAccess) 0;
            if ((access & VirtualFileAccess.Read) != 0)
                rawAccess |= NativeFileAccess.Read;
            if ((access & VirtualFileAccess.Write) != 0)
                rawAccess |= NativeFileAccess.Write;

            return new NativeFileStream(ConvertUrlToFullPath(url), (NativeFileMode)mode, rawAccess);
        }
 public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
 {
     switch (access)
     {
         case VirtualFileAccess.Write:
             return new NetworkWriteStream(socketMessageLayer, RemoteUrl + url);
         case VirtualFileAccess.Read:
             var downloadFileAnswer = (DownloadFileAnswer)socketMessageLayer.SendReceiveAsync(new DownloadFileQuery { Url = RemoteUrl + url }).Result;
             return new MemoryStream(downloadFileAnswer.Data);
         default:
             throw new NotSupportedException();
     }
 }
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
        {
            if (localBasePath != null && url.Split(VirtualFileSystem.DirectorySeparatorChar, VirtualFileSystem.AltDirectorySeparatorChar).Contains(".."))
                throw new InvalidOperationException("Relative path is not allowed in FileSystemProvider.");
            var filename = ConvertUrlToFullPath(url);
            var result = new FileStream(filename, (FileMode)mode, (FileAccess)access, (FileShare)share);

#if SILICONSTUDIO_PLATFORM_IOS
            if (AutoSetSkipBackupAttribute && (mode == VirtualFileMode.CreateNew || mode == VirtualFileMode.Create || mode == VirtualFileMode.OpenOrCreate))
            {
                MonoTouch.Foundation.NSFileManager.SetSkipBackupAttribute(filename, true);
            }
#endif

            return result;
        }
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            // Open or create the file through the underlying (IContentIndexMap, IOdbBackend) couple.
            // Also read/write a ObjectHeader.
            if (mode == VirtualFileMode.Open)
            {
                ObjectId objectId;
                if (url.StartsWith(ObjectIdUrl))
                    ObjectId.TryParse(url.Substring(ObjectIdUrl.Length), out objectId);
                else if (!contentIndexMap.TryGetValue(url, out objectId))
                    throw new FileNotFoundException(string.Format("Unable to find the file [{0}]", url));

                var result = objectDatabase.OpenStream(objectId, mode, access, share);

                // copy the stream into a memory stream in order to make it seek-able
                if (streamFlags == StreamFlags.Seekable && !result.CanSeek)
                {
                    var buffer = new byte[result.Length - result.Position];
                    result.Read(buffer, 0, buffer.Length);
                    return new DatabaseReadFileStream(objectId, new MemoryStream(buffer), 0);
                }

                return new DatabaseReadFileStream(objectId, result, result.Position);
            }

            if (mode == VirtualFileMode.Create)
            {
                if (url.StartsWith(ObjectIdUrl))
                    throw new NotSupportedException();

                var stream = objectDatabase.CreateStream();

                // Header will be written by DatabaseWriteFileStream
                var result = new DatabaseWriteFileStream(stream, stream.Position);

                stream.Disposed += x =>
                    {
                        // Commit index changes
                        contentIndexMap[url] = x.CurrentHash;
                    };

                return result;
            }

            throw new ArgumentException("mode");
        }
示例#11
0
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            switch (access)
            {
            case VirtualFileAccess.Write:
                return(new NetworkWriteStream(socketMessageLayer, RemoteUrl + url));

            case VirtualFileAccess.Read:
                var downloadFileAnswer = (DownloadFileAnswer)socketMessageLayer.SendReceiveAsync(new DownloadFileQuery {
                    Url = RemoteUrl + url
                }).Result;
                return(new MemoryStream(downloadFileAnswer.Data));

            default:
                throw new NotSupportedException();
            }
        }
示例#12
0
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
        {
            if (localBasePath != null && url.Split(VirtualFileSystem.DirectorySeparatorChar, VirtualFileSystem.AltDirectorySeparatorChar).Contains(".."))
            {
                throw new InvalidOperationException("Relative path is not allowed in FileSystemProvider.");
            }
            var filename = ConvertUrlToFullPath(url);
            var result   = new FileStream(filename, (FileMode)mode, (FileAccess)access, (FileShare)share);

#if SILICONSTUDIO_PLATFORM_IOS
            if (AutoSetSkipBackupAttribute && (mode == VirtualFileMode.CreateNew || mode == VirtualFileMode.Create || mode == VirtualFileMode.OpenOrCreate))
            {
                Foundation.NSFileManager.SetSkipBackupAttribute(filename, true);
            }
#endif

            return(result);
        }
示例#13
0
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
        {
            // For now, block multithreading (not sure it can work or not)
            lock (fileSystemProvider)
            {
                if (!fileSystemProvider.FileExists(url))
                {
                    // Ensure top directory exists
                    fileSystemProvider.CreateDirectory(VirtualFileSystem.GetParentFolder(url));

                    using (var asset = PlatformAndroid.Context.Assets.Open(assetRoot + url))
                        using (var output = fileSystemProvider.OpenStream(url, VirtualFileMode.CreateNew, VirtualFileAccess.Write, share, streamType))
                            asset.CopyTo(output);
                }
            }

            return(fileSystemProvider.OpenStream(url, mode, access));
        }
示例#14
0
        /// <inheritdoc/>
        public virtual Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            var url = BuildUrl(vfsRootUrl, objectId);

            // Try to early exit if file does not exists while opening, so that it doesn't
            // throw a file not found exception for default logic.
            if (!virtualFileProvider.FileExists(url))
            {
                if (mode == VirtualFileMode.Open || mode == VirtualFileMode.Truncate)
                    throw new FileNotFoundException();

                // Otherwise, file creation is allowed, so make sure directory exists
                virtualFileProvider.CreateDirectory(ExtractPath(url));

            }

            return virtualFileProvider.OpenStream(url, mode, access, share);
        }
示例#15
0
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
        {
            // For now, block multithreading (not sure it can work or not)
            lock (fileSystemProvider)
            {
                if (!fileSystemProvider.FileExists(url))
                {
                    // Ensure top directory exists
                    fileSystemProvider.CreateDirectory(VirtualFileSystem.GetParentFolder(url));

                    using (var asset = PlatformAndroid.Context.Assets.Open(assetRoot + url))
                    using (var output = fileSystemProvider.OpenStream(url, VirtualFileMode.CreateNew, VirtualFileAccess.Write, share, streamType))
                        asset.CopyTo(output);
                }
            }

            return fileSystemProvider.OpenStream(url, mode, access);
        }
示例#16
0
        private Stream OpenStreamForRead(ObjectId objectId, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share)
        {
            if (BundleBackend != null && BundleBackend.Exists(objectId))
            {
                return(BundleBackend.OpenStream(objectId, mode, access, share));
            }

            if (backendRead1.Exists(objectId))
            {
                return(backendRead1.OpenStream(objectId, mode, access, share));
            }

            if (backendRead2 != null && backendRead2.Exists(objectId))
            {
                return(backendRead2.OpenStream(objectId, mode, access, share));
            }

            throw new FileNotFoundException();
        }
示例#17
0
        private Stream OpenStreamForRead(ObjectId objectId, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share)
        {
            if (bundleBackend != null && bundleBackend.Exists(objectId))
            {
                return(bundleBackend.OpenStream(objectId, mode, access, share));
            }

            if (backendRead1.Exists(objectId))
            {
                return(backendRead1.OpenStream(objectId, mode, access, share));
            }

            if (backendRead2 != null && backendRead2.Exists(objectId))
            {
                return(backendRead2.OpenStream(objectId, mode, access, share));
            }

            return(null);
        }
示例#18
0
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            if (share != VirtualFileShare.Read)
                throw new NotImplementedException();

            lock (files)
            {
                FileInfo fileInfo;
                bool exists = files.TryGetValue(url, out fileInfo);
                bool write = access != VirtualFileAccess.Read;

                switch (mode)
                {
                    case VirtualFileMode.CreateNew:
                        if (exists)
                            throw new IOException("File already exists.");
                        files.Add(url, fileInfo = new FileInfo());
                        return new MemoryFileStream(this, fileInfo, write);
                    case VirtualFileMode.Create:
                        files.Remove(url);
                        files.Add(url, fileInfo = new FileInfo());
                        return new MemoryFileStream(this, fileInfo, write);
                    case VirtualFileMode.Truncate:
                        if (!exists)
                            throw new IOException("File doesn't exists.");
                        files.Remove(url);
                        return new MemoryStream();
                    case VirtualFileMode.Open:
                        if (!exists)
                            throw new FileNotFoundException();
                        if (write)
                            throw new NotImplementedException();
                        return new MemoryFileStream(this, fileInfo, false, fileInfo.Data);
                    case VirtualFileMode.OpenOrCreate:
                        throw new NotImplementedException();
                }
            }

            return null;
        }
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
        {
            ZipFileEntry zipFileEntry;
            if (!zipFileEntries.TryGetValue(url, out zipFileEntry))
                throw new FileNotFoundException("File not found inside ZIP archive.");

            if (mode != VirtualFileMode.Open || access != VirtualFileAccess.Read)
                throw new UnauthorizedAccessException("ZIP archive are read-only.");

            lock (zipFile)
            {
                if (zipFileEntry.Method == Compression.Store)
                {
                    // Open a VirtualFileStream on top of Zip FileStream
                    return new VirtualFileStream(new FileStream(zipFileEntry.ZipFileName, FileMode.Open, FileAccess.Read), zipFileEntry.FileOffset, zipFileEntry.FileOffset + zipFileEntry.FileSize);
                }

                // Decompress it into a MemoryStream
                var buffer = new byte[zipFileEntry.FileSize];
                zipFile.ExtractFile(zipFileEntry, buffer);
                return new MemoryStream(buffer);
            }
        }
示例#20
0
        /// <summary>
        /// Opens a stream for the specified <see cref="ObjectId"/>.
        /// </summary>
        /// <param name="objectId">The object identifier.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="access">The access.</param>
        /// <param name="share">The share.</param>
        /// <returns>A Stream.</returns>
        /// <exception cref="System.InvalidOperationException">Read-only object database.</exception>
        public Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            if (access == VirtualFileAccess.Read)
            {
                return(OpenStreamForRead(objectId, mode, access, share));
            }

            if (backendWrite == null)
            {
                throw new InvalidOperationException("Read-only object database.");
            }

            if (backendRead1 == backendWrite)
            {
                return(backendWrite.OpenStream(objectId, mode, access, share));
            }
            else
            {
                using (var streamRead = OpenStreamForRead(objectId, VirtualFileMode.Open, VirtualFileAccess.ReadWrite, VirtualFileShare.ReadWrite))
                {
                    var stream = backendWrite.OpenStream(objectId, mode, access, share);

                    streamRead?.CopyTo(stream);
                    stream.Position = 0;
                    return(stream);
                }
            }
        }
 public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
 {
     return ThreadLocal.Value.OpenStream(url, mode, access, share, streamFlags);
 }
示例#22
0
 /// <summary>
 /// Opens the stream from a given path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="mode">The stream opening mode (append, open, create, etc...).</param>
 /// <param name="access">The stream access.</param>
 /// <param name="share">The stream share mode.</param>
 /// <returns></returns>
 public static Stream OpenStream(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read)
 {
     var resolveProviderResult = ResolveProvider(path, false);
     return resolveProviderResult.Provider.OpenStream(resolveProviderResult.Path, mode, access, share);
 }
 /// <inheritdoc/>
 public abstract Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None);
示例#24
0
 /// <inheritdoc/>
 public abstract Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None);
示例#25
0
 /// <summary>
 /// Opens the stream from a given path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="mode">The stream opening mode (append, open, create, etc...).</param>
 /// <param name="access">The stream access.</param>
 /// <param name="share">The stream share mode.</param>
 /// <param name="provider">The provider used to load the stream.</param>
 /// <returns>The stream.</returns>
 public static Stream OpenStream(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share, out IVirtualFileProvider provider)
 {
     var resolveProviderResult = ResolveProvider(path, false);
     provider = resolveProviderResult.Provider;
     return provider.OpenStream(resolveProviderResult.Path, mode, access, share);
 }
示例#26
0
        public Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            ObjectLocation objectLocation;

            lock (objects)
            {
                if (!objects.TryGetValue(objectId, out objectLocation))
                {
                    throw new FileNotFoundException();
                }
            }

            var    loadedBundle = objectLocation.LoadedBundle;
            var    streams      = objectLocation.LoadedBundle.Streams;
            Stream stream;

            // Try to reuse same streams
            lock (streams)
            {
                // Available stream?
                if ((stream = streams[objectLocation.Info.IncrementalBundleIndex]) != null)
                {
                    // Remove from available streams
                    streams[objectLocation.Info.IncrementalBundleIndex] = null;
                }
                else
                {
                    stream = VirtualFileSystem.OpenStream(loadedBundle.Files[objectLocation.Info.IncrementalBundleIndex], VirtualFileMode.Open, VirtualFileAccess.Read);
                }
            }

            if (objectLocation.Info.IsCompressed)
            {
                stream.Position = objectLocation.Info.StartOffset;
                return(new PackageFileStreamLZ4(this, objectLocation, stream, CompressionMode.Decompress, objectLocation.Info.SizeNotCompressed, objectLocation.Info.EndOffset - objectLocation.Info.StartOffset));
            }

            return(new PackageFileStream(this, objectLocation, stream, objectLocation.Info.StartOffset, objectLocation.Info.EndOffset, false));
        }
示例#27
0
 public static Task<Stream> OpenStreamAsync(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read)
 {
     return Task<Stream>.Factory.StartNew(() => OpenStream(path, mode, access, share));
 }
示例#28
0
        /// <summary>
        /// Opens a stream for the specified <see cref="ObjectId"/>.
        /// </summary>
        /// <param name="objectId">The object identifier.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="access">The access.</param>
        /// <param name="share">The share.</param>
        /// <returns>A Stream.</returns>
        /// <exception cref="System.InvalidOperationException">Read-only object database.</exception>
        public Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            if (access == VirtualFileAccess.Read)
            {
                return OpenStreamForRead(objectId, mode, access, share);
            }

            if (backendWrite == null)
                throw new InvalidOperationException("Read-only object database.");

            if (backendRead1 == backendWrite)
            {
                return backendWrite.OpenStream(objectId, mode, access, share);
            }
            else
            {
                using (var streamRead = OpenStreamForRead(objectId, VirtualFileMode.Open, VirtualFileAccess.ReadWrite, VirtualFileShare.ReadWrite))
                {
                    var stream = backendWrite.OpenStream(objectId, mode, access, share);

                    streamRead?.CopyTo(stream);
                    stream.Position = 0;
                    return stream;
                }
            }
        }
示例#29
0
        public Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            ObjectLocation objectLocation;

            lock (objects)
            {
                if (!objects.TryGetValue(objectId, out objectLocation))
                {
                    throw new FileNotFoundException();
                }
            }

            Stream stream;

            // Try to reuse same streams
            lock (bundleStreams)
            {
                // Available stream?
                if (bundleStreams.TryGetValue(objectLocation.BundleUrl, out stream))
                {
                    // Remove from available streams
                    bundleStreams.Remove(objectLocation.BundleUrl);
                }
                else
                {
                    stream = VirtualFileSystem.OpenStream(objectLocation.BundleUrl, VirtualFileMode.Open, VirtualFileAccess.Read);
                }
            }

            if (objectLocation.Info.IsCompressed)
            {
                stream.Position = objectLocation.Info.StartOffset;
                return(new PackageFileStreamLZ4(this, objectLocation.BundleUrl, stream, CompressionMode.Decompress, objectLocation.Info.SizeNotCompressed, objectLocation.Info.EndOffset - objectLocation.Info.StartOffset));
            }

            return(new PackageFileStream(this, objectLocation.BundleUrl, stream, objectLocation.Info.StartOffset, objectLocation.Info.EndOffset, false));
        }
示例#30
0
 public static Task <Stream> OpenStreamAsync(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read)
 {
     return(Task <Stream> .Factory.StartNew(() => OpenStream(path, mode, access, share)));
 }
示例#31
0
        private Stream OpenStreamForRead(ObjectId objectId, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share)
        {
            if (BundleBackend != null && BundleBackend.Exists(objectId))
                return BundleBackend.OpenStream(objectId, mode, access, share);

            if (backendRead1.Exists(objectId))
                return backendRead1.OpenStream(objectId, mode, access, share);

            if (backendRead2 != null && backendRead2.Exists(objectId))
                return backendRead2.OpenStream(objectId, mode, access, share);

            throw new FileNotFoundException();
        }
示例#32
0
        /// <summary>
        /// Opens the stream from a given path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="mode">The stream opening mode (append, open, create, etc...).</param>
        /// <param name="access">The stream access.</param>
        /// <param name="share">The stream share mode.</param>
        /// <returns></returns>
        public static Stream OpenStream(string path, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read)
        {
            var resolveProviderResult = ResolveProvider(path, false);

            return(resolveProviderResult.Provider.OpenStream(resolveProviderResult.Path, mode, access, share));
        }
示例#33
0
        /// <inheritdoc/>
        public virtual Stream OpenStream(ObjectId objectId, VirtualFileMode mode = VirtualFileMode.Open, VirtualFileAccess access = VirtualFileAccess.Read, VirtualFileShare share = VirtualFileShare.Read)
        {
            var url = BuildUrl(vfsRootUrl, objectId);

            // Try to early exit if file does not exists while opening, so that it doesn't
            // throw a file not found exception for default logic.
            if (!virtualFileProvider.FileExists(url))
            {
                if (mode == VirtualFileMode.Open || mode == VirtualFileMode.Truncate)
                {
                    throw new FileNotFoundException();
                }

                // Otherwise, file creation is allowed, so make sure directory exists
                virtualFileProvider.CreateDirectory(ExtractPath(url));
            }

            return(virtualFileProvider.OpenStream(url, mode, access, share));
        }
示例#34
0
 public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
 {
     return(ThreadLocal.Value.OpenStream(url, mode, access, share, streamFlags));
 }