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
|
#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>
#define USAGE "to be decided"
#define MODE_NORMAL -1
#define MODE_YES 0
#define MODE_NO 1
#define ENVVAR_HOME "HOME"
struct trashsys_log_info {
uint64_t ts_log_id;
char ts_log_filename[255];
size_t ts_log_filesize;
time_t ts_log_trashtime;
char ts_log_originalpath[4096];
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;
char *ts_path_trashsys;
char *ts_path_log;
char *ts_path_trashed;
};
void free_ipi(struct initial_path_info *ipi) { // Free all info in initial_path_info created from fill_ipi
free(ipi->ts_path_user_home);
free(ipi->ts_path_trashsys);
free(ipi->ts_path_log);
free(ipi->ts_path_trashed);
free(ipi);
}
struct initial_path_info *fill_ipi() { // Function for filling out initial_path_info so it can be used later
struct initial_path_info *ipi = malloc(sizeof(struct initial_path_info));
ipi->ts_path_user_home = malloc(sizeof(char) * 4096);
ipi->ts_path_trashsys = malloc(sizeof(char) * 4096);
ipi->ts_path_log = malloc(sizeof(char) * 4096);
ipi->ts_path_trashed = malloc(sizeof(char) * 4096);
ipi->ts_path_user_home[0] = '\0';
ipi->ts_path_trashsys[0] = '\0';
ipi->ts_path_log[0] = '\0';
ipi->ts_path_trashed[0] = '\0';
char *homepath;
homepath = getenv(ENVVAR_HOME);
if (homepath == NULL) {
fprintf(stderr, "fill_ipi(): getenv failed");
free_ipi(ipi);
exit(EXIT_FAILURE);
}
// top level = "/.trashsys"
// log = "/log"
// trashed = "/trashed"
strcat(ipi->ts_path_user_home, homepath); // Fill home path
strcat(ipi->ts_path_trashsys, homepath); // fill toplevel ts path
strcat(ipi->ts_path_trashsys, "/.trashsys"); // 2nd step to fill toplevel ts path
strcat(ipi->ts_path_log, ipi->ts_path_trashsys); // fill log path
strcat(ipi->ts_path_log, "/log"); // 2nd step fill log path
strcat(ipi->ts_path_trashed, ipi->ts_path_trashsys); // fill trashed path
strcat(ipi->ts_path_trashed, "/trashed"); // 2nd step fill trashed path
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) { fprintf(stderr, ".trashsys exists\n"); } else { return -1; }
}
mkd = mkdir(ipi->ts_path_log, 0755);
if (mkd < 0) {
if (errno == EEXIST) { fprintf(stderr, "log exists\n"); } else { return -1; }
}
mkd = mkdir(ipi->ts_path_trashed, 0755);
if (mkd < 0) {
if (errno == EEXIST) { fprintf(stderr, "trashed exists\n"); } else { return -1; }
}
return 0;
}
int tli_fill_info (struct trashsys_log_info *tli, char* filename, bool log_tmp) {
/*
struct trashsys_log_info {
uint64_t ts_log_id;
char ts_log_filename[255]; X
size_t ts_log_filesize; X
time_t ts_log_trashtime; X
char ts_log_originalpath[4096]; X
bool ts_log_tmp; X
};
*/
char *rp;
rp = realpath(filename, NULL); // get full entire path of the file
tli->ts_log_originalpath[0] = '\0';
tli->ts_log_filename[0] = '\0';
strcat(tli->ts_log_originalpath, rp);
free(rp);
strcat(tli->ts_log_filename, filename);
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;
fprintf(stdout, "fullpath: %s\nfilename: %s\ntime: %ld\ntmp: %d\nsize: %ld\n", tli->ts_log_originalpath, tli->ts_log_filename, tli->ts_log_trashtime, tli->ts_log_tmp, tli->ts_log_filesize);
return 0;
}
/*
int trash_file(struct trashsys_log_info tli, struct initial_path_info *ipi) {
// name
// full path
// filesize
// time of deletion
// other info?
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); }
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;
}
struct initial_path_info *ipi_m;
int cctd;
ipi_m = fill_ipi();
fprintf(stdout, "%s\n%s\n%s\n%s\n", ipi_m->ts_path_user_home, ipi_m->ts_path_trashsys, ipi_m->ts_path_log, ipi_m->ts_path_trashed);
cctd = check_create_ts_dirs(ipi_m);
if(cctd == -1) {
fprintf(stderr, "check_create_ts_dirs(): Cannot create directories\n");
free_ipi(ipi_m);
return EXIT_FAILURE;
}
struct trashsys_log_info tli_m;
tli_fill_info(&tli_m , "myfile.img", false);
free_ipi(ipi_m);
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;
break;
case 'n':
n_used = true;
break;
case 'v':
v_used = true;
break;
case 'f':
f_used = true;
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 (n_used == true && y_used == true) { // If both YES and NO are used print usage and exit
fprintf(stderr, "%s", USAGE);
return EXIT_FAILURE;
}
return 0;
}
|