generated from Stuycs-K/Systems-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.c
116 lines (97 loc) · 2.5 KB
/
data.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
#include "data.h"
#include <limits.h>
int compare(const void *a, const void *b) {
char *name_a = (*(struct finfo **)a)->name;
char *name_b = (*(struct finfo **)b)->name;
return strcmp(name_a, name_b);
}
struct finfo **get_file_listing(char *dir, int *size) {
DIR *d = opendir(dir);
struct dirent *entry;
*size = 0;
while ((entry = readdir(d))) ++(*size);
--*size;
closedir(d);
d = opendir(dir);
struct finfo **list = malloc(*size * sizeof(struct finfo *));
struct finfo **itr = list;
while ((entry = readdir(d))) {
if (strcmp(entry->d_name, ".") == 0) continue;
struct finfo *f = malloc(sizeof(struct finfo));
f->name = malloc(256 * sizeof(char));
strcpy(f->name, entry->d_name);
f->type = entry->d_type;
if (f->type == DT_DIR) f->stat_buffer = NULL;
else {
f->stat_buffer = malloc(sizeof(struct stat));
stat(f->name, f->stat_buffer);
}
*itr = f;
++itr;
}
closedir(d);
qsort(list, *size, sizeof(struct finfo *), compare);
return list;
}
void free_finfo_list(struct finfo **list, int size) {
for (int i = 0; i < size; i++) {
free(list[i]->name);
free(list[i]->stat_buffer);
free(list[i]);
}
free(list);
}
//FILE FUNCTIONS
bool create_file(char *name) {
int fd = open(name, O_CREAT | O_WRONLY, 0644);
if (fd == -1) return false;
close(fd);
return true;
}
bool delete_file(char *name) {
if (remove(name) == 0) {
return true;
}
else {
perror("Error deleting file");
return false;
}
}
// DIR FUNCTIONS
bool create_dir(char *name) {
if (mkdir(name, 0755) == -1) return false;
return true;
}
bool delete_dir(char *name) {
DIR *dir = opendir(name);
if (dir == NULL) {
// Handle error opening directory
return false;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
if (entry->d_type == DT_DIR) {
if (!delete_dir(path)) {
closedir(dir);
return false;
}
} else {
if (remove(path) != 0) {
// Handle error deleting file
closedir(dir);
return false;
}
}
}
closedir(dir);
if (remove(name) != 0) {
// Handle error deleting empty directory
return false;
}
return true;
}