File: common.cc

package info (click to toggle)
snapper 0.10.6-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,072 kB
  • sloc: cpp: 24,846; ansic: 1,466; sh: 1,410; makefile: 514; python: 127; ruby: 90
file content (178 lines) | stat: -rw-r--r-- 3,874 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

#include "config.h"
#include "common.h"

#include <snapper/Snapper.h>
#include <snapper/Snapshot.h>
#include <snapper/Comparison.h>
#include <snapper/File.h>
#include <snapper/SnapperDefines.h>
#include <snapper/Log.h>


extern char* program_invocation_short_name;


using namespace snapper;

Snapper* sh = NULL;

Snapshots::iterator first;
Snapshots::iterator second;

unsigned int numCreateErrors, numModifyErrors, numDeleteErrors;
#ifdef ENABLE_XATTRS
unsigned int xaCreate, xaReplace, xaDelete;
#endif


void
setup()
{
    system("/usr/bin/find " SUBVOLUME " -mindepth 1 -maxdepth 1 -not -path " SUBVOLUME "/.snapshots "
	   "-exec rm -r {} \\;");

    initDefaultLogger();

    sh = new Snapper(CONFIG, "/");
}


void
cleanup()
{
    sh->deleteSnapshot(second);
    sh->deleteSnapshot(first);
    delete sh;
}


void
first_snapshot()
{
    SCD scd;
    scd.description = CONFIG;
    scd.cleanup = "number";

    first = sh->createPreSnapshot(scd);
}


void
second_snapshot()
{
    SCD scd;
    scd.description = CONFIG;
    scd.cleanup = "number";

    second = sh->createPostSnapshot(first, scd);
}


void
check_undo_statistics(unsigned int numCreate, unsigned int numModify, unsigned int numDelete)
{
    Comparison comparison(sh, first, second, false);

    Files& files = comparison.getFiles();
    for (Files::iterator it = files.begin(); it != files.end(); ++it)
	it->setUndo(true);

    UndoStatistic rs = comparison.getUndoStatistic();

    check_equal(rs.numCreate, numCreate);
    check_equal(rs.numModify, numModify);
    check_equal(rs.numDelete, numDelete);
}


#ifdef ENABLE_XATTRS
void
check_xa_undo_statistics(unsigned int xaNumCreate, unsigned xaNumReplace, unsigned int xaNumDelete)
{
    check_equal(xaCreate, xaNumCreate);
    check_equal(xaDelete, xaNumDelete);
    check_equal(xaReplace, xaNumReplace);
}
#endif

void
undo()
{
    numCreateErrors = numModifyErrors = numDeleteErrors = 0;

    cout << "comparing snapshots..." << flush;

    Comparison comparison(sh, first, second, false);

    cout << " done" << endl;

    Files& files = comparison.getFiles();
    for (Files::iterator it = files.begin(); it != files.end(); ++it)
	it->setUndo(true);

    cout << "undoing..." << endl;

    vector<UndoStep> undo_steps = comparison.getUndoSteps();
    for (vector<UndoStep>::const_iterator it = undo_steps.begin(); it != undo_steps.end(); ++it)
    {
	switch (it->action)
	{
	    case CREATE: cout << "creating " << it->name << endl; break;
	    case MODIFY: cout << "modifying " << it->name << endl; break;
	    case DELETE: cout << "deleting " << it->name << endl; break;
	}

	if (!comparison.doUndoStep(*it))
	{
	    switch (it->action)
	    {
		case CREATE: cout << "failed to create " << it->name << endl; numCreateErrors++; break;
		case MODIFY: cout << "failed to modify " << it->name << endl; numModifyErrors++; break;
		case DELETE: cout << "failed to delete " << it->name << endl; numDeleteErrors++; break;
	    }
	}
    }

    cout << "undoing done" << endl;

#ifdef ENABLE_XATTRS
    XAUndoStatistic xs = files.getXAUndoStatistic();
    xaCreate = xs.numCreate;
    xaReplace = xs.numReplace;
    xaDelete = xs.numDelete;
#endif

}


void
check_undo_errors(unsigned int numCreate, unsigned int numModify, unsigned int numDelete)
{
    check_equal(numCreateErrors, numCreate);
    check_equal(numModifyErrors, numModify);
    check_equal(numDeleteErrors, numDelete);
}


void
check_first()
{
    Snapshots::const_iterator current = sh->getSnapshotCurrent();

    Comparison comparison(sh, first, current, false);

    const Files& files = comparison.getFiles();
    for (Files::const_iterator it = files.begin(); it != files.end(); ++it)
	cout << *it << endl;

    check_true(files.empty());
}


void
run_command(const char* command)
{
    string tmp = string("cd " SUBVOLUME " ; ") + command;
    check_zero(system(tmp.c_str()));
}