Beispiel #1
0
        public async Task AddUpload(FSItem parent, string file)
        {
            Directory.CreateDirectory(cachePath);
            var fileinfo = new FileInfo(file);
            var infoId = Guid.NewGuid().ToString();

            // To copy a source file to file in cache folder
            var tempFile = Path.Combine(cachePath, infoId) + ".temp";
            var tempFileInfo = new FileInfo(tempFile);
            File.Copy(file, tempFile, true);
            BoschHelper.Encrypt(file, tempFile);

            var info = new UploadInfo
            {
                Id = infoId,
                Length = tempFileInfo.Length,
                Path = Path.Combine(parent.Path, Path.GetFileName(file)),
                ParentId = parent.Id,
                SourcePath = tempFile
            };

            var path = Path.Combine(cachePath, info.Id);
            await WriteInfo(path + ".info", info);
            leftUploads.Add(info);
            allUploads.TryAdd(info.Id, info);
            OnUploadAdded?.Invoke(info);
        }
Beispiel #2
0
        public async Task AddOverwrite(FSItem item)
        {
            try
            {
                Directory.CreateDirectory(cachePath);

                // To copy a source file to file in cache folder
                var sourceFile = Path.Combine(cachePath, item.Id);
                var sourceFileInfoPath = sourceFile + ".info";
                var tempFile = sourceFile + ".temp";
                File.Copy(sourceFile, tempFile, true);
                BoschHelper.Encrypt(sourceFile, tempFile);
                var tempFileInfo = new FileInfo(tempFile);

                File.Delete(sourceFileInfoPath);
                var info = new UploadInfo(item)
                {
                    Length = tempFileInfo.Length,
                    Path = tempFile,
                    SourcePath = tempFile,
                    Overwrite = true
                };

                await WriteInfo(sourceFileInfoPath, info);
                leftUploads.Add(info);
                allUploads.TryAdd(info.Id, info);
                OnUploadAdded?.Invoke(info);
            }
            catch(Exception ex)
            {

            }
        }
Beispiel #3
0
        public NtStatus CreateFile(string fileName, FileAccess access, FileShare share, FileMode mode, FileOptions options, FileAttributes attributes, DokanFileInfo info)
        {
            try
            {
                ////Avoid pasting file by normal way
                if (BoschHelper.IsPastingFile(fileName, mode))
                {
                    return(DokanResult.AccessDenied);
                }

                var res = Wait(MainCreateFile(fileName, access, share, mode, options, info));
#if TRACE
                var readWriteAttributes = (access & DataAccess) == 0;
                if (!(readWriteAttributes || info.IsDirectory) || (res != DokanResult.Success && !(lastFilePath == fileName && res == DokanResult.FileNotFound)))
                {
                    if (!(info.Context is IBlockStream))
                    {
                        Log.Trace($"{fileName}\r\n  Access:[{access}]\r\n  Share:[{share}]\r\n  Mode:[{mode}]\r\n  Options:[{options}]\r\n  Attr:[{attributes}]\r\nStatus:{res}");
                    }

                    lastFilePath = fileName;
                }
#endif
                return(res);
            }
            catch (Exception e) when(e.InnerException is FileNotFoundException)
            {
                Log.Error($"File not found: {fileName}\r\n  Access:[{access}]\r\n  Share:[{share}]\r\n  Mode:[{mode}]\r\n  Options:[{options}]\r\n  Attr:[{attributes}]", e);
                return(DokanResult.FileNotFound);
            }
            catch (Exception e) when(e.InnerException is TimeoutException)
            {
                Log.Error($"Timeout: {fileName}\r\n  Access:[{access}]\r\n  Share:[{share}]\r\n  Mode:[{mode}]\r\n  Options:[{options}]\r\n  Attr:[{attributes}]", e);
                return(NtStatus.Timeout);
            }
            catch (Exception e)
            {
                Log.Error($"Unexpected exception: {fileName}\r\n  Access:[{access}]\r\n  Share:[{share}]\r\n  Mode:[{mode}]\r\n  Options:[{options}]\r\n  Attr:[{attributes}]", e);
                return(DokanResult.Error);
            }
        }
Beispiel #4
0
        private async Task Download(FSItem item, Stream result, Downloader downloader)
        {
            try
            {
                Log.Trace($"Started download: {item.Name} - {item.Id}");
                var       start          = Stopwatch.StartNew();
                var       buf            = new byte[64 << 10];
                var       uncommitedSize = 0;
                const int commitSize     = 512 << 10;

                using (result)
                    using (var writer = new BufferedStream(result))
                    {
                        OnDownloadStarted?.Invoke(item);
                        while (writer.Length < item.Length)
                        {
                            var stream = await cloud.Files.Download(item.Id);

                            stream.Position = writer.Length;
                            int red;
                            do
                            {
                                red = await stream.ReadAsync(buf, 0, buf.Length);

                                if (writer.Length == 0)
                                {
                                    Log.Trace($"Got first part: {item.Name} - {item.Id} in {start.ElapsedMilliseconds}");
                                }

                                await writer.WriteAsync(buf, 0, red);

                                uncommitedSize += red;
                                if (uncommitedSize <= commitSize)
                                {
                                    continue;
                                }

                                uncommitedSize = 0;
                                await writer.FlushAsync();

                                downloader.Downloaded = writer.Length;
                            }while (red > 0);
                        }

                        await writer.FlushAsync();

                        downloader.Downloaded = writer.Length;
                    }

                Log.Trace($"Finished download: {item.Name} - {item.Id}");
                OnDownloaded?.Invoke(item);
                BoschHelper.Decrypt(downloader.Path);

                if (access.TryAdd(
                        item.Id,
                        new CacheEntry {
                    Id = item.Id, AccessTime = DateTime.UtcNow, Length = item.Length
                }))
                {
                    TotalSizeIncrease(item.Length);
                }

                if (TotalSize > CacheSize)
                {
                    var task = cleanSizeWorker.Run(TotalSize - CacheSize);
                }

                if (start.ElapsedMilliseconds > 29000)
                {
                    Log.Warn($"Downloading {item.Path} took: {start.ElapsedMilliseconds}");
                }
            }
            catch (Exception ex)
            {
                Log.ErrorTrace($"Download failed: {item.Name} - {item.Id}\r\n{ex}");
                await downloader.Failed();

                OnDownloadFailed?.Invoke(item);
            }
            finally
            {
                lock (DownloadersLock)
                {
                    Downloaders.Remove(item.Path);
                }
            }
        }