File: commit.cc

package info (click to toggle)
aegis 4.24.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 34,056 kB
  • ctags: 12,500
  • sloc: cpp: 178,528; sh: 79,948; makefile: 34,813; yacc: 4,610; perl: 1,499; ansic: 492; awk: 325
file content (350 lines) | stat: -rw-r--r-- 7,359 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
//	aegis - project change supervisor
//	Copyright (C) 1991-1995, 2002-2006, 2008 Peter Miller
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, see
//	<http://www.gnu.org/licenses/>.
//

#include <common/mem.h>
#include <common/trace.h>
#include <libaegis/commit.h>
#include <libaegis/dir.h>
#include <libaegis/dir/functor/rm_dir_tree.h>
#include <libaegis/interrupt.h>
#include <libaegis/os.h>
#include <libaegis/undo.h>


//
// This file contains "commit" functions.
//
// The idea is that aegis can be interrupted, or fail from errors, and
// behave as if "nothing" had happened.  That is, no user discernable
// difference to their environment, and certainly no changes to aegis'
// data base.
//
// To do this, many database functions write updates to temporary files
// "near" where they are to go, eventually.  A commit and an abort
// function are both issued, one to put the new file where it really
// goes, and one to remove it.  Exactly one option will be exercised.
//
// Commit actions will be performed with the user-id set the same as was
// set at the time the commit call was issued.
//

enum what_ty
{
    what_rename,
    what_unlink_errok,
    what_rmdir_errok,
    what_rmdir_tree_bg,
    what_rmdir_tree_errok,
    what_hard_link,
    what_symlink
};

struct action_ty
{
    what_ty         what;
    nstring         path1;
    nstring         path2;
    action_ty       *next;
    int             uid;
    int             gid;
    int             umask;

    action_ty() :
	what((what_ty)-1),
	next(0),
	uid(0),
	gid(0),
	umask(0)
    {
	os_become_query(&uid, &gid, &umask);
    }
};

static action_ty *head1;
static action_ty *tail1;
static action_ty *head2;


/**
  * The link1 function is used to add an new item to the head of chain1.
  *
  * @param action
  *     what to do
  * @param path1
  *     mandatory argument
  * @param path2
  *     optional argument
  */

static void
link1(what_ty what, const nstring &path1, const nstring &path2)
{
    trace(("commit::link1(what = %d, path1 = %08lX, path2 = %08lX)\n{\n",
	what, (long)path1, (long)path2));
    action_ty *new_thing = new action_ty;
    new_thing->what = what;
    new_thing->path1 = path1;
    new_thing->path2 = path2;
    if (head1)
    {
	tail1->next = new_thing;
	tail1 = new_thing;
    }
    else
	head1 = tail1 = new_thing;
    trace(("}\n"));
}


/**
  * The link2 function is used to add an new item to the head of chain2
  *
  * @param action
  *     what to do
  * @param path1
  *     mandatory argument
  * @param path2
  *     optional argument
  */

static void
link2(what_ty what, const nstring &path1, const nstring &path2)
{
    trace(("commit::link2(what = %d, path1 = %08lX, path2 = %08lX)\n{\n",
	what, (long)path1, (long)path2));
    action_ty *new_thing = new action_ty;
    new_thing->what = what;
    new_thing->path1 = path1;
    new_thing->path2 = path2;
    new_thing->next = head2;
    head2 = new_thing;
    trace(("}\n"));
}


void
commit_rename(string_ty *from, string_ty *to)
{
    commit_rename(nstring(from), nstring(to));
}


void
commit_rename(const nstring &from, const nstring &to)
{
    trace(("commit_rename(from = \"%s\", to = \"%s\")\n{\n", from.c_str(),
	to.c_str()));
    link1(what_rename, from, to);
    trace(("}\n"));
}


void
commit_symlink(string_ty *from, string_ty *to)
{
    commit_symlink(nstring(from), nstring(to));
}


void
commit_symlink(const nstring &from, const nstring &to)
{
    trace(("commit_symlink(from = \"%s\", to = \"%s\")\n{\n", from.c_str(),
	to.c_str()));
    link1(what_symlink, from, to);
    trace(("}\n"));
}


void
commit_hard_link(string_ty *from, string_ty *to)
{
    commit_hard_link(nstring(from), nstring(to));
}


void
commit_hard_link(const nstring &from, const nstring &to)
{
    trace(("commit_hard_link(from = \"%s\", to = \"%s\")\n{\n", from.c_str(),
	to.c_str()));
    link1(what_hard_link, from, to);
    trace(("}\n"));
}


void
commit_unlink_errok(string_ty *path)
{
    commit_unlink_errok(nstring(path));
}


void
commit_unlink_errok(const nstring &path)
{
    trace(("commit_unlink_errok(path = \"%s\")\n{\n", path.c_str()));
    link2(what_unlink_errok, path, "");
    trace(("}\n"));
}


void
commit_rmdir_errok(string_ty *path)
{
    commit_rmdir_errok(nstring(path));
}


void
commit_rmdir_errok(const nstring &path)
{
    trace(("commit_rmdir_errok(path = \"%s\")\n{\n", path.c_str()));
    link2(what_rmdir_errok, path, "");
    trace(("}\n"));
}


void
commit_rmdir_tree_bg(string_ty *path)
{
    commit_rmdir_tree_bg(nstring(path));
}


void
commit_rmdir_tree_bg(const nstring &path)
{
    trace(("commit_rmdir_tree_bg(path = \"%s\")\n{\n", path.c_str()));
    link2(what_rmdir_tree_bg, path, "");
    trace(("}\n"));
}


void
commit_rmdir_tree_errok(string_ty *path)
{
    commit_rmdir_tree_errok(nstring(path));
}


void
commit_rmdir_tree_errok(const nstring &path)
{
    trace(("commit_rmdir_tree_errok(path = \"%s\")\n{\n", path.c_str()));
    link2(what_rmdir_tree_errok, path, "");
    trace(("}\n"));
}


void
commit(void)
{
    //
    // Disable interrupts (such as ^C) for the duration.  Note that
    // commit consists solely of file renames and removes.  No long
    // writes are performed at this time.  Sometimes there is a lot
    // to do.
    //
    trace(("commit()\n{\n"));
    interrupt_disable();

    //
    // Perform the queued actions.
    //
    while (head1 || head2)
    {
	//
	// Take the first item off the list.
	// Note that actions may append more items to the list.
	//
	action_ty *action = 0;
	if (head1)
	{
	    action = head1;
	    head1 = action->next;
	    if (!head1)
		tail1 = 0;
	}
	else
	{
	    action = head2;
	    head2 = action->next;
	}

	//
	// Do the action.
	//
	os_become(action->uid, action->gid, action->umask);
	switch (action->what)
	{
	case what_rename:
	    os_rename(action->path1, action->path2);
	    undo_rename(action->path2, action->path1);
	    break;

	case what_symlink:
	    os_symlink(action->path1, action->path2);
	    undo_unlink_errok(action->path2);
	    break;

	case what_hard_link:
	    os_link(action->path1, action->path2);
	    undo_unlink_errok(action->path2);
	    break;

	case what_unlink_errok:
	    os_unlink_errok(action->path1);
	    break;

	case what_rmdir_errok:
	    os_rmdir_errok(action->path1);
	    break;

	case what_rmdir_tree_bg:
	    os_rmdir_bg(action->path1);
	    break;

	case what_rmdir_tree_errok:
	    if (os_exists(action->path1))
	    {
		dir_functor_rm_dir_tree eraser;
		dir_walk(action->path1, eraser);
	    }
	    break;
	}
	os_become_undo();

	//
	// Delete the list element.
	//
	delete action;
    }

    //
    // it's all committed, nothing left to undo.
    //
    undo_cancel();

    //
    // Enable interrupts once more.
    //
    interrupt_enable();
    trace(("}\n"));
}