-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibapi.c
More file actions
262 lines (223 loc) · 7.46 KB
/
Copy pathlibapi.c
File metadata and controls
262 lines (223 loc) · 7.46 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/xattr.h>
#include <dirent.h>
#define MAX_TAG_LEN 20
#define MAX_TAGS 20
#define MAX_FILENAME_LEN 256
// Create a new file with the given name
int create_file(const char *filename) {
int fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (fd == -1) {
perror("Failed to create file");
return -1;
}
close(fd);
return 0;
}
int open_file(const char* filename) {
int fd = open(filename, O_RDWR);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
return fd;
}
// Read the contents of the file with the given name
char* read_file(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("Failed to read file");
return NULL;
}
struct stat st;
fstat(fd, &st);
char *buffer = malloc(st.st_size + 1);
memset(buffer, 0, st.st_size + 1);
if (read(fd, buffer, st.st_size) == -1) {
perror("Failed to read file");
close(fd);
free(buffer);
return NULL;
}
close(fd);
return buffer;
}
// Write the contents to the file with the given name
int write_file(const char *filename, const char *content) {
int fd = open(filename, O_WRONLY);
if (fd == -1) {
perror("Failed to write file");
return -1;
}
if (write(fd, content, strlen(content)) == -1) {
perror("Failed to write file");
close(fd);
return -1;
}
close(fd);
return 0;
}
// Rename the file with the given name to the new name
int rename_file(const char *old_filename, const char *new_filename) {
if (rename(old_filename, new_filename) == -1) {
perror("Failed to rename file");
return -1;
}
return 0;
}
// Add tag to the file with the given name
void add_tag(const char *filename, const char *tag) {
char tag_filename[MAX_FILENAME_LEN];
FILE *tag_file;
char line[MAX_TAGS * (MAX_TAG_LEN + 1)];
int i;
// create the tag filename by appending ".tags" to the original filename
int fd = open(filename, O_WRONLY);
if (fd == -1) {
printf("File does not exist. New file will be created.");
create_file(filename);
snprintf(tag_filename, MAX_FILENAME_LEN, "%s.tags", filename);
}
else {
snprintf(tag_filename, MAX_FILENAME_LEN, "%s.tags", filename);
}
// open the tag file for appending
tag_file = fopen(tag_filename, "a");
if (!tag_file) {
printf("Failed to open tag file for %s\n", filename);
return;
}
// read the existing tags from the file
if (fgets(line, MAX_TAGS * (MAX_TAG_LEN + 1), tag_file)) {
// remove the newline character at the end of the line
line[strcspn(line, "\n")] = 0;
// append the new tag to the end of the line, separated by comma if there are already tags
char *existing_tags = line;
int existing_tags_len = strlen(existing_tags);
if (existing_tags_len > 0) {
existing_tags[existing_tags_len] = ','; // Add a comma after the existing tags
existing_tags_len++;
existing_tags = line + existing_tags_len; // Move the pointer to the next position
line[existing_tags_len] = '\0'; // Add a null terminator at the end of the line
}
strncat(existing_tags, tag, MAX_TAG_LEN); // Concatenate the new tag to the line
// write the updated tags to the file
fseek(tag_file, 0, SEEK_SET);
fputs(line, tag_file);
} else {
// tag file was empty, add new tag to the file
fputs(tag, tag_file);
}
fclose(tag_file);
}
// Search for given tag among all files in specified directory
char *search_tag(const char *tag) {
DIR *dir;
struct dirent *ent;
char filename[MAX_FILENAME_LEN];
char tag_filename[MAX_FILENAME_LEN];
FILE *tag_file;
char line[MAX_TAGS * MAX_TAG_LEN];
char *token;
int tag_found;
char *result = NULL;
// open the current directory
dir = opendir(".");
if (!dir) {
printf("Failed to open directory\n");
return NULL;
}
// iterate over all files in the directory
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_REG) {
// check if the file has a .tags file
snprintf(tag_filename, MAX_FILENAME_LEN, "%s.tags", ent->d_name);
tag_file = fopen(tag_filename, "r");
if (!tag_file) {
continue; // file doesn't have a .tags file
}
// read the tags from the file and check if the specified tag is present
tag_found = 0;
while (fgets(line, MAX_TAGS * MAX_TAG_LEN, tag_file)) {
// tokenize the line by commas and check if the tag is present
token = strtok(line, ",");
while (token) {
if (strcmp(token, tag) == 0) {
tag_found = 1;
break;
}
token = strtok(NULL, ",");
}
if (tag_found) {
// tag found, append the path of the file to the result string
snprintf(filename, MAX_FILENAME_LEN, "%s/%s", ".", ent->d_name);
if (result) {
result = realloc(result, strlen(result) + strlen(filename) + 1);
strcat(result, " ");
} else {
result = malloc(strlen(filename) + 1);
}
strcat(result, filename);
break;
}
}
fclose(tag_file);
}
}
closedir(dir);
// tag not found
return result;
}
// Remove the specified tag from the file with the given name
void remove_tag(const char *filename, const char *tag) {
char tag_filename[MAX_FILENAME_LEN];
FILE *tag_file;
char line[MAX_TAGS * (MAX_TAG_LEN + 1)];
char new_line[MAX_TAGS * (MAX_TAG_LEN + 1)] = "";
int tag_removed = 0;
snprintf(tag_filename, MAX_FILENAME_LEN, "%s.tags", filename);
// Open the tag file for reading and writing
tag_file = fopen(tag_filename, "r+");
if (!tag_file) {
printf("Failed to open tag file for %s\n", filename);
return;
}
// Read the existing tags from the file
if (fgets(line, MAX_TAGS * (MAX_TAG_LEN + 1), tag_file)) {
// Remove the newline character at the end of the line
line[strcspn(line, "\n")] = '\0';
// Tokenize the line by commas and check if the tag is present
char *token = strtok(line, ",");
while (token) {
if (strcmp(token, tag) == 0) {
tag_removed = 1;
} else {
// Append the tag to the new line
if (strlen(new_line) > 0) {
strcat(new_line, ",");
}
strcat(new_line, token);
}
token = strtok(NULL, ",");
}
// Write the updated tags back to the file
fseek(tag_file, 0, SEEK_SET);
if (tag_removed) {
if (strlen(new_line) > 0) {
fputs(new_line, tag_file);
} else {
// No tags remaining, truncate the file
FILE *temp_file = fopen("temp.txt", "w");
fclose(temp_file);
rename("temp.txt", tag_filename);
}
}
}
fclose(tag_file);
}