1 | // ================================================================ // |
---|
2 | // // |
---|
3 | // File : NT_userland_fixes.cxx // |
---|
4 | // Purpose : Repair situations caused by ARB bugs in userland // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in November 2013 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // ================================================================ // |
---|
11 | |
---|
12 | #include <aw_msg.hxx> |
---|
13 | |
---|
14 | #include <arbdb.h> |
---|
15 | #include <arb_strarray.h> |
---|
16 | #include <arb_file.h> |
---|
17 | |
---|
18 | #include <time.h> |
---|
19 | #include <vector> |
---|
20 | #include <string> |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | typedef void (*fixfun)(); |
---|
25 | |
---|
26 | const long DAYS = 24*60*60L; |
---|
27 | const long WEEKS = 7 * DAYS; |
---|
28 | const long MONTHS = 30 * DAYS; |
---|
29 | const long YEARS = 365 * DAYS; |
---|
30 | |
---|
31 | class UserlandCheck { |
---|
32 | vector<fixfun> fixes; |
---|
33 | time_t now; |
---|
34 | public: |
---|
35 | |
---|
36 | UserlandCheck() { time(&now); } |
---|
37 | void Register(fixfun fix, long IF_DEBUG(addedAt), long IF_DEBUG(expire)) { |
---|
38 | fixes.push_back(fix); |
---|
39 | #if defined(DEBUG) |
---|
40 | if (addedAt+expire < now) { |
---|
41 | aw_message(GBS_global_string("Warning: UserlandCheck #%zu has expired\n", fixes.size())); |
---|
42 | } |
---|
43 | #endif |
---|
44 | } |
---|
45 | void Run() { |
---|
46 | for (vector<fixfun>::iterator f = fixes.begin(); f != fixes.end(); ++f) { |
---|
47 | (*f)(); |
---|
48 | } |
---|
49 | } |
---|
50 | }; |
---|
51 | |
---|
52 | // ------------------------------------------------------------ |
---|
53 | |
---|
54 | static const char *changeDir(const char *file, const char *newDir) { |
---|
55 | char *name; |
---|
56 | GB_split_full_path(file, NULL, &name, NULL, NULL); |
---|
57 | |
---|
58 | const char *changed = GB_concat_path(newDir, name); |
---|
59 | free(name); |
---|
60 | return changed; |
---|
61 | } |
---|
62 | |
---|
63 | static void correct_wrong_placed_files(const char *filekind, const char *wrongDir, string rightDir, const char *ext) { |
---|
64 | // move files with extension 'ext' from 'wrongDir' into 'rightDir' |
---|
65 | StrArray wrong_placed_files; |
---|
66 | GBS_read_dir(wrong_placed_files, wrongDir, ext); |
---|
67 | |
---|
68 | size_t wrong_found = wrong_placed_files.size(); |
---|
69 | if (wrong_found>0) { |
---|
70 | string failed = ""; |
---|
71 | for (size_t i = 0; i<wrong_found; ++i) { |
---|
72 | const char *src = wrong_placed_files[i]; |
---|
73 | string dst = changeDir(src, rightDir.c_str()); |
---|
74 | GB_ERROR error = GB_is_regularfile(dst.c_str()) ? "target exists" : GB_rename_file(src, dst.c_str()); |
---|
75 | |
---|
76 | if (error) { |
---|
77 | if (!failed.empty()) failed += "\n"; |
---|
78 | failed = failed+"* "+wrong_placed_files[i]+" (move failed: "+error+")"; |
---|
79 | } |
---|
80 | } |
---|
81 | if (!failed.empty()) { |
---|
82 | string msg = |
---|
83 | string("Due to a bug, ARB stored ")+filekind+"s in '"+wrongDir+"'\n"+ |
---|
84 | "The attempt to move these wrong placed macros into the\n"+ |
---|
85 | "correct directory '"+rightDir+"'\n"+ |
---|
86 | "has failed. Please move the following files manually:\n"+ |
---|
87 | failed; |
---|
88 | |
---|
89 | aw_message(msg.c_str()); |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | static void fix_config_and_macros_in_props() { |
---|
95 | // macros and configs were save in wrong directory |
---|
96 | // caused by: http://bugs.arb-home.de/ticket/425 |
---|
97 | // active for: 3 weeks |
---|
98 | char *props = strdup(GB_getenvARB_PROP()); |
---|
99 | correct_wrong_placed_files("macro", props, GB_getenvARBMACROHOME(), "*.amc"); |
---|
100 | correct_wrong_placed_files("config", props, GB_getenvARBCONFIG(), "*.arbcfg"); |
---|
101 | free(props); |
---|
102 | } |
---|
103 | |
---|
104 | void NT_repair_userland_problems() { |
---|
105 | // place to repair problems caused by earlier bugs |
---|
106 | |
---|
107 | UserlandCheck checks; |
---|
108 | // generate timestamp: date "--date=2013/09/20" "+%s" |
---|
109 | // date +%s |
---|
110 | checks.Register(fix_config_and_macros_in_props, 1385141688 /* = 2013/11/22 */, 1 * YEARS); |
---|
111 | checks.Run(); |
---|
112 | } |
---|
113 | |
---|