-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.cpp
executable file
·182 lines (160 loc) · 5.63 KB
/
tree.cpp
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
/*
PureBackup - human-readable backup output
Copyright (C) 2005 Ben Wilhelm
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; version 2 of the license only.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
*/
#include "tree.h"
#include "debug.h"
#include <vector>
#include <set>
using namespace std;
bool MountTree::checkSanity() const {
CHECK(type >= 0 && type <= MTT_END);
bool checked = false;
if(type == MTT_VIRTUAL || type == MTT_FILE || type == MTT_IMPLIED) {
checked = true;
for(map<string, MountTree>::const_iterator itr = links.begin(); itr != links.end(); itr++) {
if(itr->first == "." || itr->first == "..")
return false;
if(!itr->second.checkSanity())
return false;
printf("%s\n", itr->first.c_str());
if(type == MTT_VIRTUAL) {
CHECK(itr->second.type == MTT_VIRTUAL || itr->second.type == MTT_FILE || itr->second.type == MTT_SSH || itr->second.type == MTT_NULL);
} else if(type == MTT_FILE) {
CHECK(itr->second.type == MTT_FILE || itr->second.type == MTT_ITEM || itr->second.type == MTT_MASKED || itr->second.type == MTT_IMPLIED || itr->second.type == MTT_NULL);
} else if(type == MTT_IMPLIED) {
CHECK(itr->second.type == MTT_MASKED || itr->second.type == MTT_IMPLIED);
} else {
CHECK(0);
}
}
} else {
CHECK(links.size() == 0);
}
if(type == MTT_MASKED) {
checked = true;
} else {
}
if(type == MTT_ITEM) {
checked = true;
CHECK(item.exists());
} else {
CHECK(!item.exists());
}
if(type == MTT_SSH) {
checked = true;
CHECK(ssh_user.size());
CHECK(ssh_pass.size());
CHECK(ssh_host.size());
CHECK(ssh_source.size());
} else {
CHECK(!ssh_user.size());
CHECK(!ssh_pass.size());
CHECK(!ssh_host.size());
CHECK(!ssh_source.size());
}
if(type == MTT_NULL) {
checked = true;
} else {
}
CHECK(checked);
return true;
}
void MountTree::print(int indent) const {
string spacing(indent, ' ');
if(type == MTT_VIRTUAL || type == MTT_FILE || type == MTT_SSH) {
for(map<string, MountTree>::const_iterator itr = links.begin(); itr != links.end(); itr++) {
printf("%s%s\n", spacing.c_str(), itr->first.c_str());
itr->second.print(indent + 2);
}
} else if(type == MTT_ITEM) {
} else if(type == MTT_NULL) {
printf("%sNULL\n", spacing.c_str());
} else if(type == MTT_IMPLIED) {
for(map<string, MountTree>::const_iterator itr = links.begin(); itr != links.end(); itr++) {
printf("%s%s\n", spacing.c_str(), itr->first.c_str());
itr->second.print(indent + 2);
}
} else if(type == MTT_MASKED) {
printf("%sCULLED\n", spacing.c_str());
} else {
CHECK(0);
}
}
int scanned = 0;
void MountTree::scan() {
if(type == MTT_VIRTUAL) {
for(map<string, MountTree>::iterator itr = links.begin(); itr != links.end(); itr++) {
itr->second.scan();
}
} else if(type == MTT_FILE) {
CHECK(!file_scanned);
file_scanned = true;
pair<bool, vector<DirListOut> > tfils = getDirList(file_source);
if(tfils.first) {
*this = MountTree();
type = MTT_NULL;
return;
}
vector<DirListOut> fils = tfils.second;
for(int i = 0; i < fils.size(); i++) {
if(links[fils[i].itemname].type == MTT_MASKED)
continue;
scanned++;
if(scanned % 1000 == 0) {
printf("%d scanned, %s \r", scanned, fils[i].full_path.c_str());
fflush(stdout);
}
if(links[fils[i].itemname].type != MTT_UNINITTED && links[fils[i].itemname].type != MTT_IMPLIED && links[fils[i].itemname].type != MTT_MASKED) {
printf("Type is %d at %s which is WRONG\n", links[fils[i].itemname].type, fils[i].full_path.c_str());
CHECK(0);
}
if(links[fils[i].itemname].type == MTT_MASKED) {
// Do nothing!
} else if(fils[i].null) {
links[fils[i].itemname] = MountTree(); // we need to obliterate this entirely
links[fils[i].itemname].type = MTT_NULL; // TODO: Maybe we don't want to, if there's masked stuff beneath it?
} else if(fils[i].directory) {
links[fils[i].itemname].type = MTT_FILE;
links[fils[i].itemname].file_source = fils[i].full_path;
links[fils[i].itemname].file_scanned = false;
links[fils[i].itemname].scan();
} else {
links[fils[i].itemname].type = MTT_ITEM;
links[fils[i].itemname].item = Item::MakeLocal(fils[i].full_path, fils[i].size, Metadata(fils[i].timestamp));
}
}
} else if(type == MTT_SSH) {
CHECK(0);
} else {
CHECK(0);
}
}
void MountTree::dumpItems(map<string, Item> *items, string cpath) const {
if(type == MTT_VIRTUAL || type == MTT_FILE) {
for(map<string, MountTree>::const_iterator itr = links.begin(); itr != links.end(); itr++) {
itr->second.dumpItems(items, cpath + "/" + itr->first);
}
} else if(type == MTT_ITEM) {
CHECK(!items->count(cpath));
(*items)[cpath] = item;
} else if(type == MTT_MASKED || type == MTT_NULL) {
} else {
printf("Type is %d at %s\n", type, cpath.c_str());
CHECK(0);
}
}
static MountTree mt_root;
MountTree *getRoot() {
return &mt_root;
}