Esempio n. 1
0
        /// <summary>
        /// Check if a transfer needs to be completed by moving its data from the target temp dir
        /// to the final (user) destination and by locally moving the transferred folders to the
        /// grace location for deferred deletion.
        /// </summary>
        private void FinalizeTransfers()
        {
            // NOTE: this is intentionally triggered by the timer only to make sure the cleanup
            // only happens while all system parameters are within their valid ranges

            // make sure the service is in an expected state before cleaning up:
            if (_transferState != TxState.Stopped || _status.TransferInProgress)
            {
                return;
            }

            if (_status.CurrentTargetTmp.Length > 0)
            {
                Log.Debug("Finalizing transfer, cleaning up target storage location...");
                var finalDst = DestinationPath(_status.CurrentTargetTmp);
                if (!string.IsNullOrWhiteSpace(finalDst))
                {
                    if (FsUtils.MoveAllSubDirs(new DirectoryInfo(_status.CurrentTargetTmpFull()),
                                               finalDst, _config.EnforceInheritedACLs))
                    {
                        _status.CurrentTargetTmp = "";
                    }
                }
            }

            if (_status.CurrentTransferSrc.Length > 0)
            {
                if (_transferredFiles.Count == 0)
                {
                    var msg = "FinalizeTransfers: CurrentTransferSrc is set to " +
                              $"[{_status.CurrentTransferSrc}], but the list of transferred " +
                              "files is empty!\nThis indicates something went wrong during the " +
                              "transfer, maybe a local permission problem?";
                    Log.Warn(msg);
                    SendAdminEmail(msg, "Error Finalizing Transfer!");
                    try {
                        var preserve = _status.CurrentTransferSrc
                                       .Replace(_config.ManagedPath, "")
                                       .Replace(@"\", "___");
                        preserve = Path.Combine(_config.ErrorPath, preserve);
                        var stale = new DirectoryInfo(_status.CurrentTransferSrc);
                        stale.MoveTo(preserve);
                        Log.Warn("Moved stale transfer source to [{0}]!", preserve);
                    }
                    catch (Exception ex) {
                        Log.Error("Preserving the stale transfer source [{0}] in [{1}] failed: {2}",
                                  _status.CurrentTransferSrc, _config.ErrorPath, ex.Message);
                    }

                    // reset current transfer variables:
                    _status.CurrentTransferSrc  = "";
                    _status.CurrentTransferSize = 0;

                    return;
                }
                Log.Debug("Finalizing transfer, moving local data to grace location...");
                MoveToGraceLocation();
                SendTransferCompletedMail();
                _status.CurrentTransferSrc  = ""; // cleanup completed, so reset CurrentTransferSrc
                _status.CurrentTransferSize = 0;
                _transferredFiles.Clear();        // empty the list of transferred files
            }
        }