-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
410 lines (357 loc) · 14.6 KB
/
main.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "lib/user.hpp"
#define yes true
#define no false
// Initialization
class init
{
static void xdg_user_dir(string& value, const string optional){
value = execute_cmd("xdg-user-dir " + optional);
}
static void xdg_user_dir(string& value){
int firstpath, secondpath;
string str;
while(value.find("$") != string::npos){
firstpath = value.find_first_of("$");
str = value.substr(firstpath + 1);
// If the val doesn't contains any slash
if(str.find_first_of("/") != string::npos){
secondpath = str.find_first_of("/") + 1;
} else {
secondpath = str.length() + 1;
}
str.erase(secondpath - 1);
str = execute_cmd("xdg-user-dir " + str);
if(!value.find_first_of("/")){
if(str.substr(0, 1) == "/" && value.substr(firstpath - 1, 1) == "/"){
value.replace(firstpath - 1, secondpath + 1, str);
return;
}
}
value.replace(firstpath, secondpath, str);
}
}
public:
static void getconfig()
{
ini::IniFile conf;
conf.load(*confL);
*path::general = conf["Path"]["General"].as<string>();
*path::documents = conf["Path"]["Documents"].as<string>();
*path::pictures = conf["Path"]["Pictures"].as<string>();
*path::videos = conf["Path"]["Videos"].as<string>();
*path::music = conf["Path"]["Music"].as<string>();
// If no value is set then
if(path::general->empty())
xdg_user_dir(*path::general, "HOME");
else
xdg_user_dir(*path::general);
if(path::documents->empty())
xdg_user_dir(*path::documents, "DOCUMENTS");
else
xdg_user_dir(*path::documents);
if(path::pictures->empty())
xdg_user_dir(*path::pictures, "PICTURES");
else
xdg_user_dir(*path::pictures);
if(path::videos->empty())
xdg_user_dir(*path::videos, "VIDEOS");
else
xdg_user_dir(*path::videos);
if(path::music->empty())
xdg_user_dir(*path::music, "MUSIC");
else
xdg_user_dir(*path::music);
*exclude::extensions = conf["Exclude"]["Extensions"].as<string>();
*exclude::filenames = conf["Exclude"]["Filenames"].as<string>();
}
static void getjson()
{
using nljson = nlohmann::json;
ifstream json_file("/etc/qsort/extensions.json");
nljson jsondata = nljson::parse(json_file);
// Access the values in the JSON file
auto documents = jsondata["Documents"];
for (auto& document : documents) {
json::documents->append(" ");
json::documents->append(document);
}
auto pictures = jsondata["Pictures"];
for (auto& picture : pictures) {
json::pictures->append(" ");
json::pictures->append(picture);
}
auto musics = jsondata["Musics"];
for (auto& music : musics) {
json::musics->append(" ");
json::musics->append(music);
}
auto videos = jsondata["Videos"];
for (auto& video : videos) {
json::videos->append(" ");
json::videos->append(video);
}
}
};
// Main Program
class base : protected init
{
unsigned short thread_count = std::thread::hardware_concurrency();
bool ismime = false;
void version()
{
cout << "qsort v" << ver << "-" << vertype << endl;
}
void help(string exec)
{
cout << exec << " [--thread] [count]\n\n";
cout << "Parameters of qsort are:\n";
cout << "--version Prints the version information\n";
cout << "--help Shows this window\n";
cout << "--edit-conf [cli] Opens qsort configuration file(requires sudo)\n";
cout << " cli Force to cli mode\n";
cout << "--thread [count] Threads count which will be used\n";
cout << " The count range only can between [1-" << thread_count << "]\n";
cout << "--custom-conf [filename] Choose a Custom qsort.conf file\n";
cout << " -cc Same as --custom-conf\n";
cout << "--mime (Beta) This parameter force qsort to sort\n";
cout << " files according to their mime type";
cout << endl;
}
void error(string err)
{
cerr << "Error: Unrecognized parameter " << err << endl;
errorcode = 1;
}
static void operations(const long long int count, const long long int start_point, const bool ismime){
// Listing all folder/files
auto iter = fs::directory_iterator{*current_path};
auto subrange = iter | ranges::views::drop(start_point);
magic_t magic_cookie = magic_open(MAGIC_MIME_TYPE);
if (ismime){
magic_load(magic_cookie, NULL);
}
for (auto const &files : subrange | ranges::views::take(count))
{
if (fs::is_regular_file(files.path()))
{
fs::path pathfile{files.path()};
string filename = files.path().filename().string();
string extension;
// Storing File Names into filename
filename.erase(0, filename.find_last_of("/") + 1);
if (filename.find_last_of(".") != string::npos)
{
extension = filename.substr(filename.find_last_of(".") + 1);
// Check If extension exist into configuration
if (exclude::extensions->find(extension) != string::npos)
continue;
// If filename exist into configuration
if (exclude::filenames->find(filename) != string::npos)
continue;
// Check if extension exist into Catagories
string mvpath;
if(ismime){
string type = magic_file(magic_cookie, files.path().filename().c_str());
type.erase(type.find("/"));
if (type == "image")
mvpath = *path::pictures;
else if (type == "video")
mvpath = *path::videos;
else if (type == "audio")
mvpath = *path::music;
else
mvpath = *path::general;
} else {
if (json::documents->find(extension) != string::npos)
mvpath = *path::documents;
else if (json::pictures->find(extension) != string::npos)
mvpath = *path::pictures;
else if (json::musics->find(extension) != string::npos)
mvpath = *path::music;
else if (json::videos->find(extension) != string::npos)
mvpath = *path::videos;
else
mvpath = *path::general;
}
try {
fs::rename(pathfile, mvpath + "/" + filename);
}
catch(...){
cout << "Unable to rename: " << pathfile << endl;
}
}
else
{
// If no extension and file not found into Exclude list then move to General
if (exclude::filenames->find(filename) == string::npos)
{
fs::path temp_path = *path::general + "/" + filename;
fs::rename(pathfile, temp_path);
}
}
}
}
if(ismime){
magic_close(magic_cookie);
}
}
void work_thread(const bool ismime){
// If no arguments passed then do main operation
// Initializing Variables/Settings
thread g_config(init::getconfig);
thread g_json(init::getjson);
g_config.join();
g_json.join();
const long long unsigned int file_count = distance(fs::directory_iterator(*current_path), fs::directory_iterator{});
thread threads[thread_count];
const long long int thread_process = file_count/thread_count;
bool taskDone = no;
if (thread_process > thread_count){
tempint = 0;
cout << "Using " << thread_count << " Threads\n";
// Creating threads
for(unsigned short i = 0; i < thread_count; ++i){
threads[i] = thread(&base::operations, thread_process, tempint, ismime);
tempint = tempint + thread_process;
if(tempint > file_count){
tempint = file_count;
}
}
// Waiting for thread to complete
for(auto &thread : threads){
thread.join();
}
taskDone = yes;
}
// Checks if any remaining files
tempint = 0;
const unsigned int remaining_count = distance(fs::directory_iterator(*current_path), fs::directory_iterator{});
if (remaining_count){
operations(remaining_count, tempint, ismime);
}
// If operation completed
if(taskDone){
cout << "Operation Completed" << endl;
}
}
public:
base(const int arg, const char **argv)
{
// If arguments passed into the executable
if (arg > 1)
{
// Version Information
*tempstr = argv[1];
if (!tempstr->compare("--version"))
{
if (arg == 2)
version();
else
error(argv[2]);
}
// Help
else if (!tempstr->compare("--help"))
{
if (arg == 2)
help(argv[0]);
else
error(argv[2]);
}
// Edit Configuration
else if (!tempstr->compare("--edit-conf"))
{
const char *editor;
if (arg == 2){
if(access("/usr/bin/xdg-open", X_OK)){
editor = "editor";
} else {
editor = "xdg-open";
}
} else {
*tempstr = argv[2];
if (!tempstr->compare("cli")){
editor = "editor";
}
}
char* const args[] = {const_cast<char*>(editor), const_cast<char*>("/etc/qsort/qsort.conf"), nullptr};
if (geteuid() != 0){
cerr << "Error: Sudo permission required\n";
} else {
execvp(editor, args); // Open default text editor
cerr << "Error: Could not open text editor\n";
}
errorcode = 1;
}
// For Multiple types arguments
else {
// Appending the whole parameters list
for(int i=1; i<arg; ++i)
{
if(i!=1)
args->append("\\");
args->append(argv[i]);
}
args->append("\\");
// Thread Parameter
if (args->find("--thread") != string::npos){
if (arg == 2)
{
work_thread(false);
} else {
*tempstr = args->substr(args->find("--thread") + 8 + 1);
tempstr->erase(tempstr->find_first_of("\\"));
if (stoi(*tempstr) <= thread_count && stoi(*tempstr) >= 1){
this->thread_count = stoi(*tempstr);
} else {
error("Error: Out of range. You can only use any thread between [1-" + to_string(thread_count) + "].");
}
}
}
// Custom qsort.conf
if (args->find("--custom-conf") != string::npos || args->find("-cc") != string::npos){
if (arg == 2){
error("No configuration file name specified");
} else {
if(args->find("-cc") != string::npos)
*tempstr = args->substr(args->find("-cc") + 3 + 1);
else
*tempstr = args->substr(args->find("--custom-conf") + 13 + 1);
tempstr->erase(tempstr->find_first_of("\\"));
if(fs::exists(*tempstr)){
if(fs::is_regular_file(*tempstr) && !fs::is_empty(*tempstr))
*confL = *tempstr;
else
error("Error: " + *tempstr + " is not a valid configuration file");
} else {
error("Error: Configuration file not found");
}
}
}
// Mime type sorter
if (args->find("--mime") != string::npos){
this->ismime = true;
}
// Load the task
work_thread(this->ismime);
}
}
else
{
work_thread(false);
}
}
~base()
{
delete tempstr, current_path, confL, args;
delete exclude::extensions, exclude::filenames;
delete path::documents, path::general, path::music, path::pictures, path::videos;
delete json::documents, json::musics, json::pictures, json::videos;
}
};
int main(const int arg, const char **argv)
{
// Passing parameters to the base class
base *b = new base(arg, argv);
delete b;
return errorcode;
}