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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <pwd.h>
#include <string.h>
#include <libgen.h>
#include <dirent.h>
#include <stdarg.h>
#define USAGE "to be decided"
#define MODE_NORMAL -1
#define MODE_YES 0
#define MODE_NO 1
#define MODE_FORCE 2
#define ENVVAR_HOME "HOME"
#define NOFILE 3
bool v_cvm_fprintf = false;
int choice_mode = MODE_NORMAL;
struct trashsys_log_info {
uint64_t ts_log_id;
char ts_log_filename[FILENAME_MAX]; // doublecheck this!
size_t ts_log_filesize;
time_t ts_log_trashtime;
char ts_log_originalpath[PATH_MAX];
bool ts_log_tmp;
};
struct trashsys_additional_paths {
char ts_add_logfilename[PATH_MAX];
char ts_add_trashed_filename[PATH_MAX];
};
struct initial_path_info { // Initial useful strings to create before we start checking if certain directories or files exist.
char ts_path_user_home[PATH_MAX];
char ts_path_trashsys[PATH_MAX];
char ts_path_log[PATH_MAX];
char ts_path_trashed[PATH_MAX];
};
int handle_ynf(bool y_used, bool n_used, bool f_used) { // Will handle cases for y, n and f. Exits if any of these are used together.
int choice_mode_ynf = MODE_NORMAL;
if (n_used == true && y_used == true) { // If both YES and NO are used print usage and exit
fprintf(stderr, "%s", USAGE);
exit(EXIT_FAILURE);
} else if (n_used == true && f_used == true) { fprintf(stderr, "%s", USAGE); exit(EXIT_FAILURE); }
else if (y_used == true && f_used == true) { fprintf(stderr, "%s", USAGE); exit(EXIT_FAILURE); }
if (n_used == true) { choice_mode_ynf = MODE_NO; }
if (y_used == true) { choice_mode_ynf = MODE_YES; }
if (f_used == true) { choice_mode_ynf = MODE_FORCE; }
return choice_mode_ynf;
}
int cvm_fprintf(bool ONOROFF, FILE *stream, const char *format, ...) {
// a sort of debug fprintf
if (ONOROFF == false) {
return 0; // Return 0 to indicate no characters were printed
}
// If condition is true, proceed with fprintf
va_list args;
va_start(args, format);
int result = vfprintf(stream, format, args);
va_end(args);
return result; // Return the result from fprintf
}
char *concat_str(char *final, ssize_t rem_size, const char *from) {
// IF you use this function PLEASE know this:
// rem_size is the amount of characters left in final
// rem_size should NOT include \0 in the size
// So if you were to have 5 remaining characters then 5 is what you put as the argument
// from is calculated and then we add +1 to account for the \0 character.
if (final == NULL || from == NULL) {
return NULL;
}
ssize_t from_len = strlen(from);
if (from_len+1 > rem_size) {
cvm_fprintf(v_cvm_fprintf, stderr, "IF: from_len: %li\nIF: rem_size: %li\n", from_len+1, rem_size);
return NULL;
}
cvm_fprintf(v_cvm_fprintf, stderr, "Ffrom_len: %li\nRrem_size: %li\n", from_len+1, rem_size);
strcat(final, from);
return final;
}
void free_ipi(struct initial_path_info *ipi) { // Free all info in initial_path_info created from fill_ipi
free(ipi);
cvm_fprintf(v_cvm_fprintf, stderr, "initial_path_info free'd\n");
}
struct initial_path_info *fill_ipi() { // Function for filling out initial_path_info so it can be used later
#define MY_PATH_MAX PATH_MAX
char *ts_toplevel = "/.trashsys";
char *ts_log = "/log";
char *ts_trashed = "/trashed";
char *homepath;
struct initial_path_info *ipi = malloc(sizeof(struct initial_path_info)); // malloc memory to struct
ipi->ts_path_user_home[0] = '\0'; // Add null character to all of them because we'll be using concat_str (basically strcat) later
ipi->ts_path_trashsys[0] = '\0';
ipi->ts_path_log[0] = '\0';
ipi->ts_path_trashed[0] = '\0';
homepath = getenv(ENVVAR_HOME); // Get the home path of the current user
if (homepath == NULL) {
fprintf(stderr, "fill_ipi(): getenv failed");
free_ipi(ipi);
exit(EXIT_FAILURE);
}
//char *concat_str(char *final, ssize_t rem_size, const char *from);
concat_str(ipi->ts_path_user_home, MY_PATH_MAX, homepath); // we are only doing it once so PATH_MAX is fine
if(concat_str(ipi->ts_path_trashsys, MY_PATH_MAX, homepath) == NULL) {
fprintf(stderr, "fill_ipi: path is too long\n");
free_ipi(ipi);
exit(EXIT_FAILURE);
}
ssize_t remaining_size = MY_PATH_MAX;
remaining_size = remaining_size - strlen(ts_toplevel); // Because we are using concat_str more than once we gotta calculate the space left
/*We do this same calculation process further down below as well*/
if(concat_str(ipi->ts_path_trashsys, remaining_size, ts_toplevel) == NULL) {
fprintf(stderr, "fill_ipi: path is too long\n");
free_ipi(ipi);
exit(EXIT_FAILURE);
}
if(concat_str(ipi->ts_path_log, MY_PATH_MAX, ipi->ts_path_trashsys) == NULL) {
fprintf(stderr, "fill_ipi: path is too long\n");
exit(EXIT_FAILURE);
}
remaining_size = MY_PATH_MAX;
remaining_size = remaining_size - strlen(ts_log);
if(concat_str(ipi->ts_path_log, remaining_size, ts_log) == NULL) {
fprintf(stderr, "fill_ipi: path is too long\n");
free_ipi(ipi);
exit(EXIT_FAILURE);
}
if(concat_str(ipi->ts_path_trashed, MY_PATH_MAX, ipi->ts_path_trashsys) == NULL) {
fprintf(stderr, "fill_ipi: path is too long\n");
exit(EXIT_FAILURE);
}
remaining_size = MY_PATH_MAX;
remaining_size = remaining_size - strlen(ts_trashed);
if(concat_str(ipi->ts_path_trashed, remaining_size, ts_trashed) == NULL) {
fprintf(stderr, "fill_ipi: path is too long\n");
free_ipi(ipi);
exit(EXIT_FAILURE);
}
cvm_fprintf(v_cvm_fprintf, stdout, "%s\n%s\n%s\n%s\n", ipi->ts_path_user_home, ipi->ts_path_trashsys, ipi->ts_path_log, ipi->ts_path_trashed);
return ipi;
}
int check_create_ts_dirs(struct initial_path_info *ipi) { // 1. Check if trashsys toplevel exists 2. Check if log exists 3. Check if trashed exists
int mkd;
mkd = mkdir(ipi->ts_path_trashsys, 0755);
if (mkd < 0) {
if (errno == EEXIST) { cvm_fprintf(v_cvm_fprintf, stderr, ".trashsys exists\n"); } else { return -1; }
} else { cvm_fprintf(v_cvm_fprintf, stderr, "%s was created\n", ipi->ts_path_trashsys); }
mkd = mkdir(ipi->ts_path_log, 0755);
if (mkd < 0) {
if (errno == EEXIST) { cvm_fprintf(v_cvm_fprintf, stderr, "log exists\n"); } else { return -1; }
} else { cvm_fprintf(v_cvm_fprintf, stderr, "%s was created\n", ipi->ts_path_log); }
mkd = mkdir(ipi->ts_path_trashed, 0755);
if (mkd < 0) {
if (errno == EEXIST) { cvm_fprintf(v_cvm_fprintf, stderr, "trashed exists\n"); } else { return -1; }
} else { cvm_fprintf(v_cvm_fprintf, stderr, "%s was created\n", ipi->ts_path_trashed); }
return 0;
}
uint64_t find_highest_id (struct initial_path_info *ipi) { // Find highest id and then return it, because we will create the new log entry as highestID + 1
// We need to check whether a file is a directory or just a file.
uint64_t id = 1;
struct dirent *ddd;
DIR *dir = opendir(ipi->ts_path_log);
if (dir == NULL) {
free_ipi(ipi);
exit(EXIT_FAILURE);
} // Return here or exit() ??
while ((ddd = readdir(dir)) != NULL) {
// MUST BE TESTED MORE!!!
int path_max_int = PATH_MAX; // Temporary variable, good for testing
char stat_fullpath[path_max_int];
stat_fullpath[0] = '\0';
ssize_t sf_sz = path_max_int;
if(concat_str(stat_fullpath, sf_sz, ipi->ts_path_log) == NULL) {
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
free_ipi(ipi);
exit(EXIT_FAILURE);
}
sf_sz = sf_sz - strlen(stat_fullpath);
//fprintf(stdout, "%ld\n", sf_sz);
if(concat_str(stat_fullpath, sf_sz, "/") == NULL) {
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
free_ipi(ipi);
exit(EXIT_FAILURE);
}
sf_sz = sf_sz - strlen(stat_fullpath);
//fprintf(stdout, "%ld\n", sf_sz);
if(concat_str(stat_fullpath, sf_sz, ddd->d_name) == NULL) {
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
free_ipi(ipi);
exit(EXIT_FAILURE);
}
struct stat d_or_f;
stat(stat_fullpath, &d_or_f);
if(S_ISREG(d_or_f.st_mode)) { // check if given file is actually a file
cvm_fprintf(v_cvm_fprintf, stdout, "is regular file: %s\n", ddd->d_name);
char *endptr;
uint64_t strtoull_ID = strtoull(ddd->d_name, &endptr, 10);
if(ddd->d_name == endptr) {
cvm_fprintf(v_cvm_fprintf, stdout, "d_name == endptr | d_name: %p | endptr: %p | d_name string: %s\n", ddd->d_name, endptr, ddd->d_name);
continue;
}
if(*endptr != ':') {
cvm_fprintf(v_cvm_fprintf, stdout, "':' not found for file: %s\n", ddd->d_name);
continue;
}
if(strtoull_ID > id) { // If id is bigger then update it
id = strtoull_ID;
cvm_fprintf(v_cvm_fprintf, stdout, "found higher ID: %d\n", id);
}
}
}
closedir(dir);
return id;
}
int tli_fill_info (struct trashsys_log_info *tli, char* filename, bool log_tmp, struct initial_path_info *ipi) {
// This function will be the main function that gathers and fills out info that will be in the log file for a file a user wants to trash
char *rp;
rp = realpath(filename, NULL); // get full entire path of the file
if (rp == NULL) {
return NOFILE;
}
tli->ts_log_originalpath[0] = '\0';
tli->ts_log_filename[0] = '\0';
if(concat_str(tli->ts_log_originalpath, PATH_MAX, rp) == NULL) {
free_ipi(ipi);
exit(EXIT_FAILURE);
}
free(rp);
if(concat_str(tli->ts_log_filename, FILENAME_MAX, basename(filename)) == NULL) {
free_ipi(ipi);
exit(EXIT_FAILURE);
}
tli->ts_log_tmp = log_tmp; // tmp or not?
tli->ts_log_trashtime = time(NULL); // record current time
FILE *file = fopen(filename, "r"); // We get the filesize in bytes
fseek(file, 0, SEEK_END);
long filesize = ftell(file);
fclose(file);
tli->ts_log_filesize = (size_t)filesize;
uint64_t ID = find_highest_id(ipi);
tli->ts_log_id = ID + 1; // +1 because if we are making a new file we need to give it one above highest ID.
return 0;
}
int write_log_file(struct initial_path_info *ipi, struct trashsys_log_info *tli, bool t_used_aka_tmp, struct trashsys_additional_paths *tap) {
/*
struct trashsys_log_info {
uint64_t ts_log_id;
char ts_log_filename[FILENAME_MAX]; // doublecheck this!
size_t ts_log_filesize;
time_t ts_log_trashtime;
char ts_log_originalpath[PATH_MAX];
bool ts_log_tmp;
};
struct initial_path_info { // Initial useful strings to create before we start checking if certain directories or files exist.
char ts_path_user_home[PATH_MAX];
char ts_path_trashsys[PATH_MAX];
char ts_path_log[PATH_MAX];
char ts_path_trashed[PATH_MAX];
};
*/
#define LOG_PATH_MAX PATH_MAX
char idstr[23];
char path_log_cur_file[LOG_PATH_MAX];
char *tmp_path = "/tmp/";
int rem_sz = LOG_PATH_MAX;
if (t_used_aka_tmp == true) {
fprintf(stdout, "%s", tmp_path);
}
snprintf(idstr, 23, "/%ld:", tli->ts_log_id); // Take the ID and make it in to a string
concat_str(path_log_cur_file, LOG_PATH_MAX, ipi->ts_path_log); // Add log path to temporary file path
rem_sz = rem_sz - strlen(path_log_cur_file);
concat_str(path_log_cur_file, rem_sz, idstr); // Add the ID to start of filename
rem_sz = rem_sz - strlen(path_log_cur_file);
concat_str(path_log_cur_file, rem_sz, tli->ts_log_filename); // Add the filename of the file we are trashing after the ID
concat_str(tap->ts_add_logfilename, PATH_MAX, path_log_cur_file);
fprintf(stdout, "%s\n", path_log_cur_file);
FILE *file = fopen(path_log_cur_file, "w");
if(file == NULL) { return -1; }
fprintf(file, ";\n%ld\n%s\n%ld\n%ld\n%s\n%d\n;\n", tli->ts_log_id, tli->ts_log_filename, tli->ts_log_filesize, tli->ts_log_trashtime, tli->ts_log_originalpath, tli->ts_log_tmp);
fclose(file);
return 0;
}
int choice(int mode) {
char choice;
char modechoice;
do {
if (mode == MODE_NORMAL) { fputs("[Y / N] ? ", stdout); }
if (mode == MODE_YES) { fputs("[Y / n] ? ", stdout); }
if (mode == MODE_NO) { fputs("[y / N] ? ", stdout); }
if (mode == MODE_FORCE) { return 0; }
choice = getchar();
if (choice == '\n' && mode == MODE_YES) { modechoice = 'Y'; choice = modechoice; goto modeskip;}
if (choice == '\n' && mode == MODE_NO) { modechoice = 'N'; choice = modechoice; goto modeskip;}
if (choice == '\n' && mode == MODE_NORMAL) { continue; }
while ('\n' != getchar());
} while ( (choice != 'Y') && (choice != 'y') && (choice != 'N') && (choice != 'n') );
modeskip:
if ( (choice == 'Y') || (choice == 'y') )
{
return 0;
}
if ((choice == 'N') || (choice == 'n') )
{
return 1;
}
return EXIT_FAILURE;
}
int main (int argc, char *argv[]) {
if (argc == 1) {
fprintf(stderr, "%s: please specify a filename\n%s\n", argv[0], USAGE);
return EXIT_FAILURE;
}
bool y_used = false;
bool n_used = false;
bool v_used = false;
bool f_used = false;
bool a_used = false;
bool t_used = false;
bool l_used = false;
bool L_used = false;
bool c_used = false;
bool C_used = false;
bool R_used = false;
int opt;
//int returnval;
//char *optarg_copy = NULL; // We will copy optarg to this
while ((opt = getopt(argc, argv, "ynvfatlLcCR:")) != -1) {
switch (opt) {
case 'y':
y_used = true; // YES on enter
break;
case 'n':
n_used = true; // NO on enter
break;
case 'v':
v_used = true; // Verbose debug mode
break;
case 'f':
f_used = true; // choice will not ask, it will just say yes by default thus basically "forcing" it
break;
case 'a':
a_used = true;
break;
case 't':
t_used = true;
break;
case 'l':
l_used = true;
break;
case 'L':
L_used = true;
break;
case 'c':
c_used = true;
break;
case 'C':
C_used = true;
break;
case 'R':
R_used = true;
break;
}
}
if(false) { // This is just so the compiler wont complain
fprintf(stdout, "%d%d%d%d%d%d%d%d%d%d%d", R_used, C_used, c_used, L_used, l_used, t_used, a_used, f_used, v_used, n_used, y_used);
}
if (v_used == true) { v_cvm_fprintf = true; } // Verbose mode
choice_mode = handle_ynf(y_used, n_used, f_used);
choice(choice_mode);
struct initial_path_info *ipi_m; // _m because i just want to keep in mind that we're in main which is a bit easier for me
struct trashsys_log_info tli_m;
struct trashsys_additional_paths tap;
int cctd;
ipi_m = fill_ipi(); // Fill out ipi struct
cctd = check_create_ts_dirs(ipi_m); // check for or create directories
if(cctd == -1) {
fprintf(stderr, "check_create_ts_dirs(): Cannot create directories\n");
free_ipi(ipi_m);
return EXIT_FAILURE;
}
int index;
for (index = optind ; index < argc ; index++) {
cvm_fprintf(v_cvm_fprintf, stdout, "%s\n", argv[index]);
if(tli_fill_info(&tli_m , argv[index], false, ipi_m) == NOFILE) {
fprintf(stderr, "%s: error '%s': No such file or directory\n", basename(argv[0]), basename(argv[index]));
continue;
}
if(write_log_file(ipi_m, &tli_m, t_used, &tap) == -1) {
fprintf(stderr, "%s: cannot create logfile", basename(argv[0]));
continue;
}
/*struct trashsys_additinal_paths {
char ts_add_logfilename[PATH_MAX];
char ts_add_trashed_filename[PATH_MAX]
};
*/
concat_str(tap.ts_add_trashed_filename, PATH_MAX, ipi_m->ts_path_trashed);
int tap_maxsz = PATH_MAX;
tap_maxsz = tap_maxsz - strlen("/");
concat_str(tap.ts_add_trashed_filename, tap_maxsz, "/");
tap_maxsz = tap_maxsz - strlen(tli_m.ts_log_filename);
concat_str(tap.ts_add_trashed_filename, tap_maxsz, tli_m.ts_log_filename);
if(rename(tli_m.ts_log_originalpath, tap.ts_add_trashed_filename) == -1) {
continue;
}
cvm_fprintf(v_cvm_fprintf, stdout, "ID: %ld\nfullpath: %s\nfilename: %s\ntime: %ld\ntmp: %d\nsize: %ld\n", tli_m.ts_log_id, tli_m.ts_log_originalpath, tli_m.ts_log_filename, tli_m.ts_log_trashtime, tli_m.ts_log_tmp, tli_m.ts_log_filesize);
}
free_ipi(ipi_m);
return 0;
}
|