-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsift.cpp
379 lines (312 loc) · 11.5 KB
/
sift.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (C) 2019 Anders Larsen [email protected]
*
* 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 <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/system/error_code.hpp>
#include <vector>
#include <string>
namespace ba = boost::algorithm;
namespace fs = boost::filesystem;
using namespace std;
char usage[] = R"~~~(
sift 0.1 - a file sifter
Usage: sift [OPTIONS] source destination
-m --move : Move items. (Files and/or folders.)
-l --link : Make folders in destination, hardlink files from source.
-t --test : Test and report any items failing to matching anything.
-d --deep : Sift deeper using source folder sub-subfolders.
-h --help : Display more help.
-q --quiet : Donẗ show warnings about files already existing.
-v --verbose : Output info about moved/linked items.)~~~";
char help[] = R"~~~(
sift move or link items (files or folders) in a source folder to matching sub-
folders in a destination "sieve" folder. A destination subfolder match if the
name of the folder contains a word-group where all words are found in the name
of the item being tested. if --move then the item will be moved to the first
matching folder.
Word-groups are separated using ',', ')' or '('. Prefix a word in the word-
group with '!' to specify that it must NOT match the item. Word order, spaces
and case (for a-z/A-Z) in word groups are ignored when attempting to match.
Example destination subfolder names:
/destination sieve path/Science Fiction (sci-fi, space opera)/
/destination sieve path/Science/
/destination sieve path/E-books, (epub, pdf, cbr, cbz, djvu, mobi, azw3)/
Matches between items and destination subfolder word-groups are tried in order
of decreasing complexity/difficulty as measured by length of wordgroup minus
number of words. Fewer longer words are considered more complex and harder to
match than the same words split into several shorter words.
Destination and source folders must be on the same filesystem.
Using the --deep option it is possible to use a previous destination sieve
folder as the new source folder to further refine the sifting process in
several chained steps.
)~~~";
class Sifter
{
/*
* Data structure notes
*
* Groups of words are associated with paths in the destination sieve
* folder. The groups of words are "encoded" as text in the filenames of
* the paths itself. Word-groups are delimited by ',', '(' and ')'.
* Words are delimited by spaces.
*
* Paths are stored in a vector, vPath, as they are discovered. Indices
* into vPath are used instead of the path itself. That saves some memory
* if the same path is used by several word-groups. And it speeds up
* sorting.
*
* A word-group is stored as a vectors of words. And a word is a string.
*
* Word-groups and paths are associated in pairs. That is a mapping.
*
* Mappings are "scored" by how complex they are. Complexity is simply
* measured as the combined length of the word-group - the number of words.
* The score allows mappings to be sorted so that the most complex or
* difficult mappings can be tried first. This might be important for moves.
*
* The scoring and sorting ensures that "science fiction" match "science
* fiction" before it matches "science".
*
* The scores and the mappings are combined in pairs. That is a TMapping.
*
* All TMapping pairs of scores and pairs of path indices and word-groups
* are stored in a vector vMapping, this is the main datastructure of sift.
*/
private:
boost::system::error_code ec;
vector<fs::path> vPath;
typedef pair<int, pair<int, vector<string> > > TMapping;
vector<TMapping> vMapping;
bool bDeep, bVerbose, bMove, bCopy, bLink, bTest, bQuiet;
public:
Sifter()
{
bVerbose = bDeep = bMove = bLink = bCopy = bTest = bQuiet = false;
};
void setVerbose(void)
{
bVerbose = true;
};
void setDeep(void)
{
bDeep = true;
};
void setQuiet(void)
{
bQuiet = true;
};
void setMove(void)
{
bMove = true;
bLink = bCopy = bTest = false;
};
void setLink(void)
{
bLink = true;
bMove = bCopy = bTest = false;
};
void setCopy(void)
{
bCopy = true;
bMove = bLink = bTest = false;
};
void setTest(void)
{
bTest = true;
bMove = bLink = bCopy = false;
};
void sift(const fs::path& pDestination, const fs::path& pSource)
{
setSieve(pDestination);
vector<fs::path> vItems;
if (bDeep == false)
for (auto& dEntry : fs::directory_iterator(pSource))
vItems.push_back(dEntry.path());
else
for (auto& dEntry : fs::directory_iterator(pSource))
if (fs::is_directory(dEntry))
for (auto& dSubEntry : fs::directory_iterator(dEntry))
vItems.push_back(dSubEntry.path());
sort(vItems.begin(), vItems.end());
for (auto& pItem : vItems)
siftItem(pItem);
}
private:
void setSieve(const fs::path& pSieve)
{
for (auto& dSieveEntry : fs::directory_iterator(pSieve))
if (fs::is_directory(dSieveEntry))
vPath.push_back(dSieveEntry.path());
// Improve predictability?
sort(vPath.begin(), vPath.end());
for (int i = 0; i != vPath.size(); i++)
{
string sSieveEntryName = vPath[i].filename().string();
vector<string> vsWordGroup;
ba::trim(sSieveEntryName);
ba::to_lower(sSieveEntryName);
ba::split(vsWordGroup, sSieveEntryName, boost::is_any_of("(),"), boost::token_compress_on);
for (auto& sWordGroup : vsWordGroup)
{
ba::trim(sWordGroup);
int iScore = sWordGroup.length();
if (iScore)
{
vector<string> vsWord;
ba::split(vsWord, sWordGroup, boost::is_any_of(" "), boost::token_compress_on);
iScore = iScore - vsWord.size();
vMapping.push_back(make_pair(iScore, make_pair(i, vsWord)));
}
}
}
sort(vMapping.begin(), vMapping.end());
}
void hardlink(const fs::path& pItem, const fs::path& pDest, bool bShowError = true)
{
if (fs::is_regular_file(pItem))
{
fs::create_hard_link(pItem, pDest, ec);
if (!bQuiet && (ec && (bVerbose || (bShowError || bVerbose))))
cerr << "Warning! " << ec.message() << ": " << pDest << endl;
}
else if (fs::is_directory(pItem))
{
fs::copy_directory(pItem, pDest, ec);
if (!bQuiet && (ec && (bShowError || bVerbose)))
cerr << "Warning! " << ec.message() << ": " << pDest << endl;
for (auto& dSubItem : fs::directory_iterator(pItem))
{
fs::path pNewDest = pDest / dSubItem.path().filename();
hardlink(dSubItem.path(), pNewDest, false); // Recursion!
}
}
}
void move(const fs::path& pItem, const fs::path& pDest)
{
fs::rename(pItem, pDest, ec);
if (!bQuiet && (ec && bVerbose))
cerr << "Warning: " << ec.message() << pDest << endl;
}
void siftItem(const fs::path& pItem)
{
bool bNoMatch = true;
string sItemName = pItem.filename().string();
ba::to_lower(sItemName);
/*
* Horribly inefficient linear search. But it is in
* memory, so it is despite this reasonably fast.
*/
for (auto& mapping : vMapping)
{
bool bFound = false;
for (auto& vsWordGroup : mapping.second.second)
{
bFound = true;
for (auto& sWord : mapping.second.second)
{
// Normal match without ! first in word
if (sWord[0] != '!')
{
if (sItemName.find(sWord) == string::npos)
{
bFound = false;
break;
}
}
// Negated match
else if (sItemName.find(sWord.substr(1)) != string::npos)
{
bFound = false;
break;
}
}
}
if (bFound == true)
{
const fs::path pDest = vPath[mapping.second.first] / pItem.filename();
bNoMatch = false;
if (bMove)
{
if (bVerbose)
cout << "move\t" << pItem << "\\\n\t" << pDest << endl;
move(pItem, pDest);
return;
}
else if (bLink)
{
if (bVerbose)
cout << "link\t" << pItem << "\\\n\t" << pDest << endl;
hardlink(pItem, pDest, true);
}
}
}
if (bTest && bNoMatch)
cout << "No match: " << pItem << endl;
}
};
int main(int argc, char* argv[])
{
string sSource, sDestination;
bool bError = false, bHelp = false;
Sifter sifter;
sifter.setTest(); // Default setting
for (int i = 1; i < argc; i++)
{
string sArg = string(argv[i]);
if ((sArg == "-m") || (sArg == "--move"))
sifter.setMove();
else if ((sArg == "-c") || (sArg == "--copy"))
sifter.setCopy();
else if ((sArg == "-l") || (sArg == "--link"))
sifter.setLink();
else if ((sArg == "-t") || (sArg == "--test"))
sifter.setTest();
else if ((sArg == "-d") || (sArg == "--deep"))
sifter.setDeep();
else if ((sArg == "-q") || (sArg == "--quiet"))
sifter.setQuiet();
else if ((sArg == "-h") || (sArg == "--help"))
bHelp = true;
else if ((sArg == "-v") || (sArg == "--verbose"))
sifter.setVerbose();
else if ((i == (argc - 2)) && fs::is_directory(sArg))
sSource = sArg;
else if ((i == (argc - 1)) && fs::is_directory(sArg))
sDestination = sArg;
else
{
cerr << "ERROR: " << sArg << endl;
bError = true;
}
}
if (!bHelp && ((sSource == "") || (sDestination == "")))
{
cerr << "Error: source and destination must be specified and exist!" << endl;
bError = true;
}
if (bError || bHelp)
{
cerr << usage << endl;
if (bHelp)
cout << help << endl;
return 1;
}
// Everything seems fine! Time to sift!
sifter.sift(sDestination, sSource);
return 0;
}