-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
444 lines (389 loc) · 11.5 KB
/
utils.c
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
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include "threads.h"
#include "utils.h"
typedef struct
{
unsigned long long size;
pthread_mutex_t *mutex;
Arena *arena;
} WorkerArgs;
void swap(void *a, void *b, int size)
{
char *temp = (char *)malloc(size);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
free(temp);
}
int partition(void *arr, size_t size, int low, int high, int (*cmp)(const void *, const void *))
{
char *array = (char *)arr;
// last element pivot
void *pivot = array + size * high;
int i = low - 1;
for (int j = low; j < high; j++)
{
if (cmp(array + j * size, pivot) <= 0)
{
i++;
swap(array + i * size, array + j * size, size);
}
}
i++;
swap(array + i * size, pivot, size);
return i;
}
void quick_sort(void *arr, size_t size, int low, int high, int (*cmp)(const void *, const void *))
{
if (low < high)
{
int pivot = partition(arr, size, low, high, cmp);
quick_sort(arr, size, low, pivot - 1, cmp);
quick_sort(arr, size, pivot + 1, high, cmp);
}
}
bool is_symlink(mode_t mode)
{
return (mode & S_IFMT) == S_IFLNK;
}
bool check_if_parent_dir(char *dir_name)
{
return strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0;
}
void *work_size(void *arg)
{
WorkerArgs *worker_args = (WorkerArgs *)arg;
struct stat st;
char full_path[1024];
char *path;
while ((path = (char *)dequeue()) != NULL)
{
struct dirent *d;
DIR *dir;
dir = opendir(path);
if (dir == NULL)
{
if (errno == ENOENT)
{
fprintf(stderr, "Directory not found: %s\n", path);
}
if (errno == EACCES)
{
fprintf(stderr, "Permission denied: %s\n", path);
}
if (errno == EPERM)
{
fprintf(stderr, "Operation not permitted: %s\n", path);
}
pthread_mutex_lock(get_queue_mutex());
set_active_tasks(get_active_tasks() - 1); // Decrement active tasks if directory open fails
if (get_active_tasks() == 0 && get_queue_size() == 0)
{
set_done(true);
pthread_cond_broadcast(get_not_empty_condition()); // Notify all waiting threads
}
pthread_mutex_unlock(get_queue_mutex());
continue;
}
while ((d = readdir(dir)) != NULL)
{
bool is_dir = d->d_type == DT_DIR;
char *dir_name = d->d_name;
snprintf(full_path, sizeof(full_path), "%s/%s", path, dir_name);
if (lstat(full_path, &st) == 0)
{
if (is_dir && !check_if_parent_dir(dir_name))
{
char *new_path = join_paths(path, dir_name, worker_args->arena);
enqueue(new_path);
}
else
{
pthread_mutex_lock(worker_args->mutex);
worker_args->size += st.st_blocks * 512;
pthread_mutex_unlock(worker_args->mutex);
}
}
}
closedir(dir);
pthread_mutex_lock(get_queue_mutex());
set_active_tasks(get_active_tasks() - 1);
// If no tasks are left, signal all threads to exit
if (get_active_tasks() == 0 && get_queue_size() == 0)
{
set_done(true);
pthread_cond_broadcast(get_not_empty_condition()); // Notify all waiting threads
}
pthread_mutex_unlock(get_queue_mutex());
}
return NULL;
}
long long get_dir_size_threaded(char *path, int max_depth, Arena *arena)
{
long number_of_processors = sysconf(_SC_NPROCESSORS_ONLN);
pthread_t threads[number_of_processors];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
WorkerArgs args = {.mutex = &mutex, .size = 0, .arena = arena};
enqueue(path);
for (int i = 0; i < number_of_processors; i++)
{
if (pthread_create(&threads[i], NULL, work_size, &args) != 0)
{
fprintf(stderr, "Failed to create thread %d\n", i);
return EXIT_FAILURE;
}
}
for (int i = 0; i < number_of_processors; i++)
{
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(get_queue_mutex());
pthread_cond_destroy(get_not_empty_condition());
pthread_cond_destroy(get_not_full_condition());
return args.size;
}
long long get_dir_size(const char *path, int max_depth)
{
DIR *dir;
struct dirent *entry;
char full_path[1024];
struct stat st;
long long total_size = 0;
if (max_depth < 0)
{
return 0;
}
dir = opendir(path);
if (dir == NULL)
{
return 0;
}
while ((entry = readdir(dir)) != NULL)
{
if (check_if_parent_dir(entry->d_name))
{
continue;
}
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
if (lstat(full_path, &st) == 0)
{
if (S_ISDIR(st.st_mode))
{
total_size += get_dir_size(full_path, max_depth - 1);
}
else
{
// block size of 512 bytes
total_size += st.st_blocks * 512;
}
}
}
closedir(dir);
return total_size;
}
char *get_user_name_from_uid(uid_t uid)
{
struct passwd *pwd;
pwd = getpwuid(uid);
if (pwd != NULL)
{
return pwd->pw_name;
}
return NULL;
}
char *get_group_name_from_gid(gid_t gid)
{
struct group *grp;
grp = getgrgid(gid);
if (grp != NULL)
{
return grp->gr_name;
}
return NULL;
}
char *join_paths(const char *path1, const char *path2, Arena *arena)
{
// Allocate enough space for the new path (with possible slash and null terminator)
size_t len1 = strlen(path1);
size_t len2 = strlen(path2);
char *result = allocate(arena, len1 + len2 + 2); // 1 for potential '/' + 1 for '\0'
if (result == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// Copy the first path
strcpy(result, path1);
// Add a '/' if path1 doesn't end with it
if (len1 > 0 && result[len1 - 1] != '/')
{
strcat(result, "/");
}
// Append the second path
strcat(result, path2);
return result;
}
char *get_user_permissions(mode_t mode, Arena *arena)
{
// Allocate a string for the permission format "rwx" + null terminator
char *permissions = (char *)allocate(arena, 4); // "rwx" is 3 characters, +1 for '\0'
if (permissions == NULL)
{
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
// Set permissions for user (owner) - UID
permissions[0] = (mode & S_IRUSR) ? 'r' : '-'; // Read permission
permissions[1] = (mode & S_IWUSR) ? 'w' : '-'; // Write permission
permissions[2] = (mode & S_IXUSR) ? 'x' : '-'; // Execute permission
permissions[3] = '\0'; // Null terminator for the string
return permissions;
}
void format_size(unsigned long long bytes, char *output, size_t output_size)
{
const char *units[] = {"B", "KB", "MB", "GB", "TB", "PB"};
int unit_index = 0;
double size = (double)bytes;
// Scale the size down to a more suitable unit
while (size >= 1024 && unit_index < 5)
{
size /= 1024;
unit_index++;
}
// Format the result into the output buffer
snprintf(output, output_size, "%.1f%s", size, units[unit_index]);
}
int safe_parse_cli_int(char *num)
{
char *endptr;
errno = 0; // Reset errno before the call to strtol
long value = strtol(num, &endptr, 10);
if (errno == ERANGE)
{
// Error: The number is out of range for a long integer
return 1;
}
if (endptr == num)
{
// Error: No digits were found. Invalid input
return 1;
}
if (*endptr != '\0')
{
// Error: Trailing characters after the number
return 1;
}
if (value < INT_MIN || value > INT_MAX)
{
// Error: The number is out of range for an int
return 1;
}
// Safe to cast to int
return (int)value;
}
char *duplicate_string(char *str, Arena *arena)
{
int length = strlen(str) + 1;
char *new_string = (char *)allocate(arena, length);
memcpy(new_string, str, length);
new_string[length - 1] = '\0';
return new_string;
}
Tuple *parent_path_from_child(char *path, size_t length, Tuple *t, Arena *arena)
{
t->parent = NULL;
t->child = NULL;
char *parent_path = (char *)allocate(arena, length + 1);
char *child_path = (char *)allocate(arena, length + 1);
parent_path[length] = '\0';
if (length == 1 && path[0] == '/')
{
parent_path[0] = '/';
parent_path[1] = '\0';
t->parent = parent_path;
return t;
}
int i = length;
if (path[length - 1] == '/')
{
i--;
}
while (i-- > 0)
{
if (path[i] == '/')
{
break;
}
}
if (i == 0)
{
parent_path[0] = '/';
parent_path[1] = '\0';
t->parent = parent_path;
return t;
}
if (i < 0)
return t;
strncpy(parent_path, path, i);
t->parent = parent_path;
if (length - 1 - i - 1 > 0)
{
strncpy(child_path, path + i + 1, path[length - 1] == '/' ? length - i - 2 : length - i - 1);
t->child = child_path;
}
return t;
}
bool str_ends_with_char(char *str, int length, char ch)
{
return length > 0 && str[length - 1] == ch;
}
char *parse_special_dir(char *path, int length)
{
if (length == 0)
return NULL;
if (length == 1 && str_ends_with_char(path, 1, '.'))
return ".";
if (length == 2 && str_ends_with_char(path, 2, '.') && str_ends_with_char(path, 1, '.'))
return "..";
if (str_ends_with_char(path, length, '/'))
length--;
if (str_ends_with_char(path, length, '.') && str_ends_with_char(path, length - 1, '/'))
return ".";
if (str_ends_with_char(path, length, '.') && str_ends_with_char(path, length - 1, '.') && str_ends_with_char(path, length - 2, '/'))
return "..";
return NULL;
}
void print_help(char *name)
{
printf("Usage: ./%s [command] [options] [path]\n\n", name);
printf("Commands:\n");
printf(" ls - List directory contents\n");
printf(" f - Find files and directories\n");
printf(" cp - Copy files and directories\n");
printf(" mv - Move files and directories\n");
printf(" new - Create new files and directories\n");
printf(" size - Get the size of a file or directory\n\n");
printf("Options:\n");
printf(" -s - Sort by size\n");
printf(" -a - Sort by name\n");
printf(" -nr - No recursion\n");
printf(" --file|-p - Search pattern\n");
printf(" --depth - Maximum depth\n");
printf(" -d - Create directory instead of file with 'new'\n\n");
printf("Examples:\n");
printf(" %s ls -s /sorts/by/size\n", name);
printf(" %s ls -a /sorts/by/name\n", name);
printf(" %s f /path/to/directory -p pattern\n", name);
printf(" %s cp /path/to/source /path/to/destination\n", name);
printf(" %s new /path/to/file1 /path/to/file2\n", name);
printf(" %s new -d /path/to/directory1 /path/to/directory2\n", name);
printf(" %s size /path/to/file\n", name);
}