1 //
2 //      aegis - project change supervisor
3 //      Copyright (C) 2007, 2008, 2012 Peter Miller
4 //
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 3 of the License, or
8 //      (at your option) any later version.
9 //
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program. If not, see
17 //      <http://www.gnu.org/licenses/>.
18 //
19 
20 #include <common/ac/assert.h>
21 
22 #include <common/trace.h>
23 #include <libaegis/commit.h>
24 #include <libaegis/gonzo.h>
25 #include <libaegis/os.h>
26 #include <libaegis/undo.h>
27 #include <libaegis/user.h>
28 
29 
30 void
user_ustate_write(user_ty::pointer up)31 user_ustate_write(user_ty::pointer up)
32 {
33     up->ustate_write();
34 }
35 
36 
37 void
ustate_write()38 user_ty::ustate_write()
39 {
40     trace(("user_ty::ustate_write(this = %p)\n{\n", this));
41     assert(ustate_data);
42     assert(ustate_path);
43     if (!ustate_modified)
44     {
45         trace(("}\n"));
46         return;
47     }
48 
49     //
50     // write it out
51     //
52     static int count;
53     nstring filename_new =
54         nstring::format("%s,%d", ustate_path.c_str(), ++count);
55     nstring filename_old =
56         nstring::format("%s,%d", ustate_path.c_str(), ++count);
57     gonzo_become();
58     if (ustate_is_new)
59     {
60         undo_unlink_errok(filename_new);
61         ustate_write_file(filename_new, ustate_data, 0);
62         commit_rename(filename_new, ustate_path);
63     }
64     else
65     {
66         undo_unlink_errok(filename_new);
67         ustate_write_file(filename_new, ustate_data, 0);
68         commit_rename(ustate_path, filename_old);
69         commit_rename(filename_new, ustate_path);
70         commit_unlink_errok(filename_old);
71     }
72     os_chmod(filename_new, 0644);
73     gonzo_become_undo();
74     ustate_modified = false;
75     ustate_is_new = false;
76     trace(("}\n"));
77 }
78 
79 
80 // vim: set ts=8 sw=4 et :
81