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
|
#include "pch.hpp"
#include "fsRows.h"
#include "logger.h"
#include "scriptLoader.h"
#include "rowProcessor.h"
#include "exceptions.h"
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <boost/date_time/posix_time/posix_time.hpp>
typedef boost::filesystem::directory_iterator DirEnt;
DECLARE_LOADER("fsrows", FsRows);
const Glib::ustring field_absPath("absPath");
const Glib::ustring field_relPath("relPath");
const Glib::ustring field_size("size");
const Glib::ustring field_modDate("modifiedDate");
const Glib::ustring field_user("owningUser");
const Glib::ustring field_group("owningGroup");
const Glib::ustring field_mode("mode");
const Glib::ustring field_perms("perms");
const Glib::ustring field_type("type");
static
Columns defCols() {
Columns rtn;
rtn.insert(new Column(0, "absPath"));
return rtn;
}
const Columns FsRows::SearchState::col(defCols());
bool FsRows::SpecBase::recurse(const SearchState *) const { return true; }
bool FsRows::SpecBase::matches(const SearchState *) const { return true; }
const boost::filesystem::path & FsRows::SpecBase::curPath(const SearchState * fs) const { return fs->curPath; }
unsigned int FsRows::SpecBase::depth(const SearchState * fs) const { return fs->depth; }
const struct stat & FsRows::SpecBase::curStat(const SearchState * fs) const { return fs->curStat; }
FsRows::FsRows(ScriptNodePtr p) :
RowSet(p),
root(p, "root"),
spec(p, "spec", ""),
ignoreErrors(p, "ignoreerrors", true)
{
p->script->loader.addLoadTarget(p, Storer::into<SpecBaseLoader>(&specs));
}
FsRows::~FsRows()
{
}
FsRows::Path
normalisePath(const std::string & p)
{
// Ensure there is a trailing /
if (*p.rbegin() != '/') {
return p + "/";
}
return p;
}
void
FsRows::execute(const Glib::ustring &, const RowProcessor * rp) const
{
SearchState ss(normalisePath(root()));
ss.specs = this->specs;
Glib::ustring sss = spec();
if (!sss.empty()) {
SpecSpec s;
typedef SpecSpec & (*splitter)(SpecSpec &, const Glib::ustring &, bool (*)(gunichar), boost::algorithm::token_compress_mode_type);
splitter split = &boost::algorithm::split;
split(s, sss, Glib::Unicode::isspace, boost::algorithm::token_compress_on);
for (SpecSpec::const_iterator sf = s.begin(); sf != s.end(); ) {
const Glib::ustring & name = (*sf++);
if (name[0] == '-') {
ss.specs.insert(LoaderBase::getLoader<SpecBaseLoader, NotSupported>(name.substr(1))->createWith(*sf++));
}
else {
throw NotSupported(name);
}
}
}
execute(ss, ss.fsRoot, rp);
}
void
FsRows::execute(SearchState & ss, const Path & dir, const RowProcessor * rp) const
{
ss.depth += 1;
try {
DirEnt end;
for (DirEnt itr(dir); itr != end; ++itr) {
ss.curPathStr = itr->path().string();
ss.fields[0] = ss.curPathStr;
ss.curPath = itr->path();
lstat(ss.curPathStr.c_str(), &ss.curStat);
if (boost::algorithm::all(ss.specs, boost::bind(&SpecBase::matches, _1, &ss))) {
ss.process(rp);
}
if (S_ISDIR(ss.curStat.st_mode) && boost::algorithm::all(ss.specs, boost::bind(&SpecBase::recurse, _1, &ss))) {
execute(ss, *itr, rp);
}
}
}
catch (const boost::filesystem::filesystem_error & e) {
if (!ignoreErrors()) {
throw;
}
Logger()->messagebf(LOG_WARNING, "%s when processing '%s'", e.what(), dir);
}
ss.depth -= 1;
}
FsRows::SearchState::SearchState(const Path & dir) :
fsRoot(dir)
{
fields.resize(1);
}
const Columns &
FsRows::SearchState::getColumns() const
{
return col;
}
RowState::RowAttribute
FsRows::SearchState::resolveAttr(const Glib::ustring & a) const
{
if (a == field_relPath) {
return boost::bind(&FsRows::SearchState::fileRelPath, this);
}
if (a == field_size) {
return boost::bind(&FsRows::SearchState::fileSize, this);
}
if (a == field_modDate) {
return boost::bind(&FsRows::SearchState::fileModDate, this);
}
if (a == field_user) {
return boost::bind(&FsRows::SearchState::fileUser, this);
}
if (a == field_group) {
return boost::bind(&FsRows::SearchState::fileGroup, this);
}
if (a == field_mode) {
return boost::bind(&FsRows::SearchState::fileMode, this);
}
if (a == field_perms) {
return boost::bind(&FsRows::SearchState::filePerms, this);
}
if (a == field_type) {
return boost::bind(&FsRows::SearchState::fileType, this);
}
return RowState::resolveAttr(a);
}
void
FsRows::SearchState::foreachAttr(const AttrAction & action) const
{
action(field_relPath, fileRelPath());
action(field_size, fileSize());
action(field_modDate, fileModDate());
action(field_user, fileUser());
action(field_group, fileGroup());
action(field_mode, fileMode());
action(field_perms, filePerms());
action(field_type, fileType());
RowState::foreachAttr(action);
}
VariableType
FsRows::SearchState::fileRelPath() const
{
return curPathStr.substr(fsRoot.string().length());
}
VariableType
FsRows::SearchState::fileSize() const
{
return curStat.st_size;
}
VariableType
FsRows::SearchState::fileModDate() const
{
return boost::posix_time::from_time_t(curStat.st_mtime);
}
VariableType
FsRows::SearchState::fileUser() const
{
struct passwd * p = getpwuid(curStat.st_uid);
if (p) {
return p->pw_name;
}
else {
return curStat.st_uid;
}
}
VariableType
FsRows::SearchState::fileGroup() const
{
struct group * g = getgrgid(curStat.st_gid);
if (g) {
return g->gr_name;
}
else {
return curStat.st_gid;
}
}
VariableType
FsRows::SearchState::fileMode() const
{
throw NotSupported(__PRETTY_FUNCTION__);
}
VariableType
FsRows::SearchState::filePerms() const
{
throw NotSupported(__PRETTY_FUNCTION__);
}
VariableType
FsRows::SearchState::fileType() const
{
throw NotSupported(__PRETTY_FUNCTION__);
}
|