Esempio n. 1
0
        private static FileEntryItem CreateFileEntryForUpload(VolumeWriterBase volume, Options options)
        {
            var fileEntry = new FileEntryItem(BackendActionType.Put, volume.RemoteFilename);

            fileEntry.SetLocalfilename(volume.LocalFilename);
            fileEntry.Encrypt(options);
            fileEntry.UpdateHashAndSize(options);
            return(fileEntry);
        }
Esempio n. 2
0
        private async Task UploadVolumeWriter(VolumeWriterBase volumeWriter, Worker worker, CancellationToken cancelToken)
        {
            var fileEntry = new FileEntryItem(BackendActionType.Put, volumeWriter.RemoteFilename);

            fileEntry.SetLocalfilename(volumeWriter.LocalFilename);
            fileEntry.Encrypt(m_options);
            fileEntry.UpdateHashAndSize(m_options);

            await UploadFileAsync(fileEntry, worker, cancelToken).ConfigureAwait(false);
        }
Esempio n. 3
0
        private async Task RenameFileAfterErrorAsync(FileEntryItem item)
        {
            var p       = VolumeBase.ParseFilename(item.RemoteFilename);
            var guid    = VolumeWriterBase.GenerateGuid();
            var time    = p.Time.Ticks == 0 ? p.Time : p.Time.AddSeconds(1);
            var newname = VolumeBase.GenerateFilename(p.FileType, p.Prefix, guid, time, p.CompressionModule, p.EncryptionModule);
            var oldname = item.RemoteFilename;

            await m_stats.SendEventAsync(item.Operation, BackendEventType.Rename, oldname, item.Size);

            await m_stats.SendEventAsync(item.Operation, BackendEventType.Rename, newname, item.Size);

            Logging.Log.WriteInformationMessage(LOGTAG, "RenameRemoteTargetFile", "Renaming \"{0}\" to \"{1}\"", oldname, newname);
            await m_database.RenameRemoteFileAsync(oldname, newname);

            item.RemoteFilename = newname;
        }
Esempio n. 4
0
        public async Task UploadFileAsync(VolumeWriterBase item, Func <string, Task <IndexVolumeWriter> > createIndexFile = null)
        {
            var fe = new FileEntryItem(BackendActionType.Put, item.RemoteFilename);

            fe.SetLocalfilename(item.LocalFilename);

            var tcs = new TaskCompletionSource <bool>();

            var backgroundhashAndEncrypt = Task.Run(() =>
            {
                fe.Encrypt(m_options);
                return(fe.UpdateHashAndSize(m_options));
            });

            await RunOnMain(async() =>
            {
                try
                {
                    await DoWithRetry(fe, async() => {
                        if (fe.IsRetry)
                        {
                            await RenameFileAfterErrorAsync(fe).ConfigureAwait(false);
                        }

                        // Make sure the encryption and hashing has completed
                        await backgroundhashAndEncrypt.ConfigureAwait(false);

                        return(await DoPut(fe).ConfigureAwait(false));
                    }).ConfigureAwait(false);

                    if (createIndexFile != null)
                    {
                        var ix        = await createIndexFile(fe.RemoteFilename).ConfigureAwait(false);
                        var indexFile = new FileEntryItem(BackendActionType.Put, ix.RemoteFilename);
                        indexFile.SetLocalfilename(ix.LocalFilename);

                        await m_database.UpdateRemoteVolumeAsync(indexFile.RemoteFilename, RemoteVolumeState.Uploading, -1, null);

                        await DoWithRetry(indexFile, async() => {
                            if (indexFile.IsRetry)
                            {
                                await RenameFileAfterErrorAsync(indexFile).ConfigureAwait(false);
                            }

                            var res = await DoPut(indexFile).ConfigureAwait(false);

                            // Register that the index file is tracking the block file
                            await m_database.AddIndexBlockLinkAsync(
                                ix.VolumeID,
                                await m_database.GetRemoteVolumeIDAsync(fe.RemoteFilename)
                                ).ConfigureAwait(false);


                            return(res);
                        });
                    }

                    tcs.TrySetResult(true);
                }
                catch (Exception ex)
                {
                    if (ex is System.Threading.ThreadAbortException)
                    {
                        tcs.TrySetCanceled();
                    }
                    else
                    {
                        tcs.TrySetException(ex);
                    }
                }
            });

            await tcs.Task.ConfigureAwait(false);
        }