Beispiel #1
0
        public static CommandDelete Parse(string line, bool handleCommentsAsNormalCommands, CustomTraceLog log)
        {
            CommandDelete r = null;

            try
            {
                r = new CommandDelete();

                line = line.Trim();

                if (handleCommentsAsNormalCommands)
                {
                    line = line.TrimStart('-').Trim();
                }

                if (line.ToLower().Split(' ')[0] != "delete")
                {
                    throw new Exception("This is not delete command.");
                }

                r.line        = line;
                r.CommandName = "delete";
                var parameters = line.GetParameters(false, false, '"');
                if (parameters.Count < 1)
                {
                    throw new Exception("Invalid number of parameters.");
                }

                r.DestinationLocation = parameters[0];

                if (line.GetParameterPresence("/destKey", false, false, '/', ':'))
                {
                    r.DestinationKey = line.GetParameterValue <string>("/destKey", true, null, true, null, false, null, '/', ':');
                }

                r.DeleteOnlyContent = line.GetParameterPresence("/DeleteOnlyContent", false, false, '/', null);
            }
            catch (Exception e)
            {
                HandlerForLoging.LogException(e, log);
                r = null;
            }
            return(r);
        }
Beispiel #2
0
        private void RefreshControls(bool temporarlyDisableControls, bool useInfoFromTask, bool swapSourceAndDestination, bool repopulateBackupDates, bool regenerateDeleteCommand, bool regenerateAzCopyCommand)
        {
            DisableEvents();
            if (temporarlyDisableControls)
            {
                DisableControls();
            }
            this.lblPleaseWait.Visible = true;
            Application.DoEvents();

            string currentlySelectedTask = this.cbTasks.SelectedIndex < 0?string.Empty:(this.cbTasks.SelectedItem as CommandAzCopy).ToString();

            PopulateTasks();
            if (!string.IsNullOrEmpty(currentlySelectedTask))
            {
                try
                {
                    int i = 0;
                    foreach (var item in this.cbTasks.Items)
                    {
                        if ((item as CommandAzCopy).ToString() == currentlySelectedTask)
                        {
                            this.cbTasks.SelectedIndex = i;
                            break;
                        }
                        i++;
                    }
                }
                catch (Exception)
                {
                }
                Application.DoEvents();
            }


            CommandAzCopy task = null;

            if (useInfoFromTask && this.cbTasks.SelectedIndex >= 0)
            {
                task = this.cbTasks.SelectedItem as CommandAzCopy;

                this.tbSourceLocation.Text = task.DestinationLocation;
                this.tbSourceKey.Text      = task.DestinationKey;

                this.tbDestinationLocation.Text = task.SourceLocation;
                this.tbDestinationKey.Text      = task.SourceKey;

                Application.DoEvents();
            }

            if (swapSourceAndDestination)
            {
                string t = this.tbSourceLocation.Text;
                this.tbSourceLocation.Text      = this.tbDestinationLocation.Text;
                this.tbDestinationLocation.Text = t;

                t = this.tbSourceKey.Text;
                this.tbSourceKey.Text      = this.tbDestinationKey.Text;
                this.tbDestinationKey.Text = t;

                Application.DoEvents();
            }

            if (repopulateBackupDates)
            {
                DateTime?currentSourceDate = this.cbSourceDate.SelectedIndex < 0 ? (DateTime?)null : ((Backup)this.cbSourceDate.SelectedItem).Date;
                this.cbSourceDate.Items.Clear();
                List <KeyValuePair <object, DateTime> > pathsAndDates = new List <KeyValuePair <object, DateTime> >();
                if (!string.IsNullOrWhiteSpace(this.tbSourceLocation.Text) && this.tbSourceLocation.Text.Contains("[T]"))
                {
                    try
                    {
                        pathsAndDates = AzCopyBatch.HandlerForPaths.GetDestinationsFromDestinationWithWildcard(this.tbSourceLocation.Text, string.IsNullOrWhiteSpace(this.tbSourceKey.Text) ? null : this.tbSourceKey.Text);
                    }
                    catch (Exception e)
                    {
                        HandlerForLoging.LogException(e, this._appLog);
                    }
                }
                this.cbSourceDate.DisplayMember = "DateAsString";
                foreach (KeyValuePair <object, DateTime> pathAndDate in pathsAndDates)
                {
                    this.cbSourceDate.Items.Add(new Backup()
                    {
                        Date = pathAndDate.Value, PathOrUrl = pathAndDate.Key, Key = this.tbSourceKey.Text
                    });
                }
                if (currentSourceDate != null)
                {
                    try
                    {
                        int i = 0;
                        foreach (Backup item in this.cbSourceDate.Items)
                        {
                            if (item.Date == currentSourceDate)
                            {
                                this.cbSourceDate.SelectedIndex = i;
                                break;
                            }
                            i++;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                Application.DoEvents();
            }

            this.tbDeleteDestinationCommand.Enabled = this.cbDeleteAllItemsAtDestination.Checked;
            //if (this.cbDeleteAllItemsAtDestination.Checked)
            //{
            //	CommandDelete deleteCommand = null;
            //	try
            //	{
            //		deleteCommand = new CommandDelete(this.tbDestinationLocation.Text, string.IsNullOrWhiteSpace(this.tbDestinationKey.Text) ? null : this.tbDestinationKey.Text, true);
            //	}
            //	catch (Exception e)
            //	{
            //		deleteCommand = null;
            //	}

            //	if (deleteCommand == null)
            //	{
            //		this.btnDeleteAllItemsAtDestination.Enabled = false;
            //	}
            //	else
            //	{
            //		this.tbDeleteDestinationCommand.Text = deleteCommand.ToString();
            //	}

            //	Application.DoEvents();
            //}
            if (regenerateDeleteCommand)
            {
                CommandDelete deleteCommand = null;
                try
                {
                    deleteCommand = new CommandDelete(this.tbDestinationLocation.Text,
                                                      string.IsNullOrWhiteSpace(this.tbDestinationKey.Text) ? null : this.tbDestinationKey.Text, true);
                }
                catch (Exception e)
                {
                    deleteCommand = null;
                }

                string destLocation = this.tbDestinationLocation.Text.ToNonNullNonEmptyString("{local-path-or-azure-storage-item-url}").Trim();
                if (deleteCommand == null)
                {
                    this.tbDeleteDestinationCommand.Text = "delete " + destLocation + " /DeleteOnlyContent";
                }
                else
                {
                    this.tbDeleteDestinationCommand.Text = deleteCommand.ToString();
                }

                bool destKeyPresent = this.tbDeleteDestinationCommand.Text.GetParameterPresence("/destKey", true, false, '/', ':');
                if (!destKeyPresent && (destLocation.ToLower().StartsWith("http://") || destLocation.ToLower().StartsWith("https://")))
                {
                    string destKey = this.tbDestinationKey.Text.ToNonNullNonEmptyString("{azure-storage-key}");
                    this.tbDeleteDestinationCommand.Text = this.tbDeleteDestinationCommand.Text.Trim() + " /destKey:" + destKey;
                }
            }

            if (regenerateAzCopyCommand)
            {
                //if (task != null)
                //{
                //	this.tbAzCopyCommand.Text = task.ToString();
                //}
                //else
                //{
                string sourceLocation = this.tbSourceLocation.Text.ToNonNullNonEmptyString("{local-path-or-azure-storage-item-url}").Trim();
                string destLocation   = this.tbDestinationLocation.Text.ToNonNullNonEmptyString("{local-path-or-azure-storage-item-url}").Trim();
                string sourceKey      = this.tbSourceKey.Text.ToNonNullNonEmptyString("{azure-storage-key}");
                string destKey        = this.tbDestinationKey.Text.ToNonNullNonEmptyString("{azure-storage-key}");
                this.tbAzCopyCommand.Text = string.Format("AzCopy {0} {1}", sourceLocation, destLocation);
                if (sourceLocation.ToLower().StartsWith("http://") || sourceLocation.ToLower().StartsWith("https://"))
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /sourceKey:" + sourceKey;
                }
                if (destLocation.ToLower().StartsWith("http://") || destLocation.ToLower().StartsWith("https://"))
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /destKey:" + destKey;
                }
                else
                {
                    if (destLocation != "{local-path-or-azure-storage-item-url}" && !Directory.Exists(destLocation))
                    {
                        string newQuestion = "Folder '" + destLocation + "' does not exist. Create?";
                        if (newQuestion != lastQuestion)
                        {
                            lastQuestion = newQuestion;
                            if (MessageBox.Show(lastQuestion, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                                DialogResult.Yes)
                            {
                                try
                                {
                                    DirectoryInfo di = Directory.CreateDirectory(destLocation);
                                    this._appLog.AddLine(string.Format("Folder '{0}' created.", di.FullName));
                                }
                                catch (Exception de)
                                {
                                    HandlerForLoging.LogException(de, this._appLog);
                                    this._appLog.AddLine("Failed to create folder.");
                                }
                            }
                        }
                    }
                }
                //}

                if (this.tbAzCopyCommand.Text.Contains("[T]") && !string.IsNullOrWhiteSpace(this.cbSourceDate.Text))
                {
                    this.tbAzCopyCommand.Text = tbAzCopyCommand.Text.Replace("[T]", this.cbSourceDate.Text);
                }

                if (!string.IsNullOrWhiteSpace(this.tbAzCopyCommand.Text) &&
                    !this.tbAzCopyCommand.Text.ToUpper().Contains("/S ") && !this.tbAzCopyCommand.Text.ToUpper().Trim().EndsWith("/S"))
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /S";
                }

                bool fixEmptyFoldersParam = !this.tbAzCopyCommand.Text.GetParameterPresence("/skipFixingEmptyFolders", true, false, '/', null);
                if (!fixEmptyFoldersParam && this.cbFixEmptyFolders.Checked)
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.RemoveParameter("/skipFixingEmptyFolders", true, this.tbAzCopyCommand.Text, '/', '"', null);
                }
                else if (fixEmptyFoldersParam && !this.cbFixEmptyFolders.Checked)
                {
                    this.tbAzCopyCommand.Text = this.tbAzCopyCommand.Text.Trim() + " /skipFixingEmptyFolders";
                }
            }

            this.btnDeleteAllItemsAtDestination.Enabled = this.cbDeleteAllItemsAtDestination.Checked && !string.IsNullOrEmpty(this.tbDeleteDestinationCommand.Text);
            this.btnOverwriteDestination.Enabled        = !string.IsNullOrWhiteSpace(this.tbAzCopyCommand.Text);


            this._appLog.AddLine("Successfully refreshed controls.");
            this.lblPleaseWait.Visible = false;
            EnableEvents();
            if (temporarlyDisableControls)
            {
                EnableControls();
            }

            RefreshLog();
            Application.DoEvents();

            if (this._scheduledButton != null)
            {
                this._scheduledButton = null;
                this._scheduledButton.PerformClick();
            }
        }
Beispiel #3
0
        private void btnDeleteAllItemsAtDestination_Click(object sender, EventArgs e)
        {
            CommandDelete deleteCommand = null;

            try
            {
                deleteCommand = new CommandDelete(this.tbDestinationLocation.Text, string.IsNullOrWhiteSpace(this.tbDestinationKey.Text) ? null : this.tbDestinationKey.Text, true);
            }
            catch (Exception ex)
            {
                deleteCommand = new CommandDelete("{local-path-or-azure-storage-item-url}", null, true);
            }

            if (deleteCommand.DestinationLocation.Contains("[T]"))
            {
                MessageBox.Show("Destination location can not contain wildcard [T].", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbDestinationLocation.Focus();
            }
            else if ((!deleteCommand.DestinationLocation.ToLower().StartsWith("http://") &&
                      !deleteCommand.DestinationLocation.ToLower().StartsWith("https://")) &&
                     !Directory.Exists(deleteCommand.DestinationLocation))
            {
                MessageBox.Show("Destination location does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbDestinationLocation.Focus();
            }
            else if (deleteCommand.DestinationLocation == "{local-path-or-azure-storage-item-url}")
            {
                MessageBox.Show("Destination location not specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbDestinationLocation.Focus();
            }
            else if ((deleteCommand.DestinationLocation.ToLower().StartsWith("http://") ||
                      deleteCommand.DestinationLocation.ToLower().StartsWith("https://")) &&
                     (deleteCommand.DestinationKey == null || deleteCommand.DestinationKey == "{azure-storage-key}"))
            {
                MessageBox.Show("Destination key not specified.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbDestinationKey.Focus();
            }
            else if ((!deleteCommand.DestinationLocation.ToLower().StartsWith("http://") &&
                      !deleteCommand.DestinationLocation.ToLower().StartsWith("https://")) &&
                     (deleteCommand.DestinationKey != null && deleteCommand.DestinationKey.Length > 0))
            {
                MessageBox.Show("Destination key should not be specified for local folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbDestinationKey.Focus();
            }
            else
            {
                if (MessageBox.Show("This will empty '" + deleteCommand.DestinationLocation + "'. Proceed?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    try
                    {
                        bool r = AzCopyBatch.HandlerForTask_Delete.Execute(0, this.tbDeleteDestinationCommand.Text, null, this._appLog);
                        this._appLog.AddLine(r ? "Operation succeeded!" : "Operation failed!");
                    }
                    catch (Exception exx)
                    {
                        HandlerForLoging.LogException(exx, this._appLog);
                        this._appLog.AddLine("Operation failed.");
                    }
                }
            }
        }