Skip to content

Commit

Permalink
begin work on issue openSUSE#759
Browse files Browse the repository at this point in the history
The '-a'/'--all' flag has been added to the Snapper command line's snapshot deletion command.
  • Loading branch information
Chlorophytus committed Oct 7, 2023
1 parent d734d5b commit 62f9a54
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
68 changes: 45 additions & 23 deletions client/cmd-delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace snapper
<< _("\tsnapper delete <number>") << '\n'
<< '\n'
<< _(" Options for 'delete' command:") << '\n'
<< _("\t--all, -a\t\t\tDelete all snapshots.") << '\n'
<< _("\t--sync, -s\t\t\tSync after deletion.") << '\n'
<< endl;
}
Expand Down Expand Up @@ -83,58 +84,79 @@ namespace snapper
command_delete(GlobalOptions& global_options, GetOpts& get_opts, ProxySnappers*, ProxySnapper* snapper)
{
const vector<Option> options = {
Option("all", no_argument, 'a'),
Option("sync", no_argument, 's')
};

ParsedOpts opts = get_opts.parse("delete", options);

bool sync = false;
bool all_snapshots = false;

ParsedOpts::const_iterator opt;

if ((opt = opts.find("sync")) != opts.end())
sync = true;

if (!get_opts.has_args())
if ((opt = opts.find("all")) != opts.end())
all_snapshots = true;

if ((!get_opts.has_args()) && !all_snapshots)
{
SN_THROW(OptionsException(_("Command 'delete' needs at least one argument.")));
SN_THROW(OptionsException(_("Command 'delete' needs at least one argument when not deleting all snapshots.")));
} else if (get_opts.has_args() && all_snapshots) {
SN_THROW(OptionsException(_("Command 'delete' takes no arguments when deleting all snapshots.")));
}

ProxySnapshots& snapshots = snapper->getSnapshots();

vector<ProxySnapshots::iterator> nums;

while (get_opts.has_args())
{
string arg = get_opts.pop_arg();

if (arg.find_first_of("-") == string::npos)
{
ProxySnapshots::iterator tmp = snapshots.findNum(arg);
nums.push_back(tmp);
if(all_snapshots) {
ProxySnapshots::const_iterator active = snapshots.getActive();
ProxySnapshots::const_iterator current = snapshots.getCurrent();
for(ProxySnapshots::iterator iter = snapshots.find(1); iter != snapshots.end(); iter++) {
if((iter != active) && (iter != current)) {
nums.push_back(iter);
}
}
if(nums.empty()) {
SN_THROW(OptionsException(_("There are no snapshots that can be deleted.")));
}
else
} else {
while (get_opts.has_args())
{
pair<ProxySnapshots::iterator, ProxySnapshots::iterator> range =
snapshots.findNums(arg, "-");
string arg = get_opts.pop_arg();

if (range.first->getNum() > range.second->getNum())
swap(range.first, range.second);

for (unsigned int i = range.first->getNum(); i <= range.second->getNum(); ++i)
if (arg.find_first_of("-") == string::npos)
{
ProxySnapshots::iterator tmp = snapshots.findNum(arg);
nums.push_back(tmp);
}
else
{
ProxySnapshots::iterator x = snapshots.find(i);
if (x != snapshots.end())
pair<ProxySnapshots::iterator, ProxySnapshots::iterator> range =
snapshots.findNums(arg, "-");

if (range.first->getNum() > range.second->getNum())
swap(range.first, range.second);

for (unsigned int i = range.first->getNum(); i <= range.second->getNum(); ++i)
{
if (find_if(nums.begin(), nums.end(), [i](ProxySnapshots::iterator it)
{ return it->getNum() == i; }) == nums.end())
nums.push_back(x);
ProxySnapshots::iterator x = snapshots.find(i);
if (x != snapshots.end())
{
if (find_if(nums.begin(), nums.end(), [i](ProxySnapshots::iterator it)
{ return it->getNum() == i; }) == nums.end())
nums.push_back(x);
}
}
}
}

filter_undeletables(snapshots, nums);
}

filter_undeletables(snapshots, nums);

snapper->deleteSnapshots(nums, global_options.verbose());

Expand Down
2 changes: 2 additions & 0 deletions client/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class ProxySnapshots

void erase(iterator it) { proxy_snapshots.erase(it); }



protected:

list<ProxySnapshot> proxy_snapshots;
Expand Down
5 changes: 3 additions & 2 deletions doc/snapper.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,13 @@
</varlistentry>

<varlistentry>
<term><option>delete (remove|rm) <replaceable>number</replaceable> |
<term><option>delete (remove|rm) <replaceable>-a</replaceable> | <replaceable>number</replaceable> |
<replaceable>number1-number2</replaceable></option></term>
<listitem>
<para>Delete a snapshot or a range of snapshots.</para>
<para>Delete a snapshot, all snapshots, or a range of snapshots.</para>
<variablelist>
<varlistentry>
<term><option>-a, --all</option></term>
<term><option>-s, --sync</option></term>
<listitem>
<para>Sync the filesystem after deleting the snapshots. The
Expand Down

0 comments on commit 62f9a54

Please sign in to comment.