-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsel.c
417 lines (357 loc) · 10.8 KB
/
fsel.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
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <glob.h>
#include <libgen.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#define HASH_SIZE SHA256_DIGEST_LENGTH
#define LOCK_FILE_TEMPLATE "/tmp/fsel_%d.lock"
#define TEMP_FILE_TEMPLATE "/tmp/fsel_%d.tmp"
#define INDEX_FILE_TEMPLATE "/tmp/fsel_%d.idx"
// Command line flags
#define FORCE_FLAG 0x01
#define QUIET_FLAG 0x02
#define SORT_FLAG 0x04
#define CLEAR_FLAG 0x08
char lock_filename[256];
char temp_filename[256];
char index_filename[256];
void compute_hash(const char* path, unsigned char* hash) {
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
const EVP_MD* md = EVP_sha256();
EVP_DigestInit_ex(ctx, md, NULL);
EVP_DigestUpdate(ctx, path, strlen(path));
EVP_DigestFinal_ex(ctx, hash, NULL);
EVP_MD_CTX_free(ctx);
}
int hash_exists(FILE* index_file, unsigned char* hash) {
rewind(index_file);
unsigned char buffer[HASH_SIZE];
while (fread(buffer, HASH_SIZE, 1, index_file)) {
if (memcmp(buffer, hash, HASH_SIZE) == 0) {
return 1;
}
}
return 0;
}
int process_path(const char* path, FILE* temp_file, FILE* index_file) {
struct stat st;
if (stat(path, &st) == -1) {
fprintf(stderr, "Path does not exist: %s\n", path);
return 0;
}
char* abs_path = realpath(path, NULL);
if (!abs_path) {
fprintf(stderr, "Invalid path: %s\n", path);
return 0;
}
unsigned char hash[HASH_SIZE];
compute_hash(abs_path, hash);
if (hash_exists(index_file, hash)) {
free(abs_path);
return 0;
}
fprintf(temp_file, "%s\n", abs_path);
fwrite(hash, HASH_SIZE, 1, index_file);
free(abs_path);
return 1;
}
int lock_file_exists() {
return access(lock_filename, F_OK) == 0;
}
int create_lock_file(int force) {
if (force && lock_file_exists()) {
if (unlink(lock_filename) == -1) {
perror("Failed to remove lock file");
return -1;
}
}
int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (fd == -1) {
if (errno == EEXIST) {
fprintf(stderr, "Error: Lock file exists\n");
} else {
perror("Failed to create lock file");
}
return -1;
}
close(fd);
return 0;
}
void remove_lock_file() {
unlink(lock_filename);
}
char* safe_strdup(const char* str) {
char* new_str = strdup(str);
if (!new_str) {
perror("Failed to allocate memory");
exit(EXIT_FAILURE);
}
return new_str;
}
int append_mode(int argc, char** argv, int flags) {
if (lock_file_exists() && !(flags & FORCE_FLAG)) {
fprintf(stderr, "Error: Lock file exists\n");
return -1;
}
if (create_lock_file(flags & FORCE_FLAG) != 0) {
return -1;
}
FILE* temp_file = fopen(temp_filename, "a");
if (!temp_file) {
perror("Failed to open temp file");
return -1;
}
FILE* index_file = fopen(index_filename, "a+");
if (!index_file) {
perror("Failed to open index file");
fclose(temp_file);
return -1;
}
int count = 0;
// 1. Обработка аргументов командной строки
for (int i = 0; i < argc; i++) {
glob_t glob_result;
if (glob(argv[i], GLOB_TILDE | GLOB_MARK, NULL, &glob_result) == 0) {
for (size_t j = 0; j < glob_result.gl_pathc; j++) {
count += process_path(glob_result.gl_pathv[j], temp_file, index_file);
}
globfree(&glob_result);
}
}
// 2. Чтение из stdin ТОЛЬКО если он не пустой
if (!isatty(fileno(stdin))) {
char* line = NULL;
size_t len = 0;
while (getline(&line, &len, stdin) != -1) {
line[strcspn(line, "\n")] = '\0';
count += process_path(line, temp_file, index_file);
}
free(line);
}
fclose(temp_file);
fclose(index_file);
if (!(flags & QUIET_FLAG)) {
struct stat st;
int total = 0;
if (stat(index_filename, &st) == 0) {
total = st.st_size / HASH_SIZE;
}
printf("%d paths added / %d paths total\n", count, total);
}
remove_lock_file();
return 0;
}
int replace_mode(int argc, char** argv, int flags) {
if (lock_file_exists() && !(flags & FORCE_FLAG)) {
fprintf(stderr, "Error: Lock file exists\n");
return -1;
}
if (create_lock_file(flags & FORCE_FLAG) != 0) {
return -1;
}
FILE* temp_file = fopen(temp_filename, "w");
if (!temp_file) {
perror("Failed to create temp file");
remove_lock_file();
return -1;
}
fclose(temp_file);
FILE* index_file = fopen(index_filename, "wb");
if (!index_file) {
perror("Failed to create index file");
remove_lock_file();
return -1;
}
fclose(index_file);
remove_lock_file();
return append_mode(argc, argv, flags);
}
int output_mode(int _, char** __, int flags) {
(void)_;
(void)__;
if (lock_file_exists() && !(flags & FORCE_FLAG)) {
fprintf(stderr, "Error: Lock file exists\n");
return -1;
}
if (flags & CLEAR_FLAG && create_lock_file(flags & FORCE_FLAG) != 0) {
return -1;
}
if (access(temp_filename, F_OK) == -1) {
return 0;
}
FILE* temp_file = fopen(temp_filename, "r");
if (!temp_file) {
perror("Failed to open temp file");
if (flags & CLEAR_FLAG)
remove_lock_file();
return -1;
}
char* line = NULL;
size_t len = 0;
ssize_t read;
if (flags & SORT_FLAG) {
char** lines = NULL;
size_t count = 0;
while ((read = getline(&line, &len, temp_file)) != -1) {
lines = realloc(lines, (count + 1) * sizeof(char*));
lines[count] = safe_strdup(line);
count++;
}
qsort(lines, count, sizeof(char*), (int (*)(const void*, const void*))strcmp);
for (size_t i = 0; i < count; i++) {
printf("%s", lines[i]);
free(lines[i]);
}
free(lines);
} else {
while ((read = getline(&line, &len, temp_file)) != -1) {
printf("%s", line);
}
}
free(line);
fclose(temp_file);
if (flags & CLEAR_FLAG) {
unlink(temp_filename);
unlink(index_filename);
remove_lock_file();
}
return 0;
}
int clear_mode(int _, char** __, int flags) {
(void)_;
(void)__;
if (lock_file_exists() && !(flags & FORCE_FLAG)) {
fprintf(stderr, "Error: Lock file exists\n");
return -1;
}
if (create_lock_file(flags & FORCE_FLAG) != 0) {
return -1;
}
unlink(temp_filename);
unlink(index_filename);
remove_lock_file();
return 0;
}
int unlock_mode() {
if (!lock_file_exists()) {
printf("No lock file found\n");
return 0;
}
printf("Other instance of \"fsel\" acquired lock. Release existing lock? [Y/N] ");
char response = getchar();
if (response == 'Y' || response == 'y') {
if (unlink(lock_filename) == 0) {
printf("Lock file removed\n");
} else {
perror("Failed to remove lock file");
return -1;
}
}
return 0;
}
void print_help() {
printf("Usage: fsel [options] <command> [paths...]\n"
"Commands:\n"
" save, s Save paths to the selection\n"
" replace, r Replace the selection with new paths\n"
" out, o Output the selection\n"
" clear, c Clear the selection\n"
" unlock, u Remove the lock file\n"
" help Show this help\n"
"Options:\n"
" -q Suppress info messages\n"
" -s Sort files in selection on output\n"
" -c Clear selection after output\n"
" -f Force operation (ignore lock)\n"
" -h Show this help\n");
}
// For simple mode handling all modes use the same signature
typedef struct {
const char* name;
int (*func)(int, char**, int);
} Command;
Command commands[] = {{"save", append_mode}, {"s", append_mode}, {"replace", replace_mode},
{"r", replace_mode}, {"out", output_mode}, {"o", output_mode},
{"clear", clear_mode}, {"c", clear_mode}, {"unlock", unlock_mode},
{"u", unlock_mode}, {NULL, NULL}};
Command* find_command(const char* name) {
for (int i = 0; commands[i].name; i++) {
if (strcmp(commands[i].name, name) == 0) {
return &commands[i];
}
}
return NULL;
}
int main(int argc, char** argv) {
if (argc < 2) {
print_help();
return EXIT_FAILURE;
}
int uid = getuid();
snprintf(temp_filename, sizeof(temp_filename), TEMP_FILE_TEMPLATE, uid);
snprintf(index_filename, sizeof(index_filename), INDEX_FILE_TEMPLATE, uid);
snprintf(lock_filename, sizeof(index_filename), LOCK_FILE_TEMPLATE, uid);
int opt;
int flags = 0;
while ((opt = getopt(argc, argv, "qofch")) != -1) {
switch (opt) {
case 'q':
flags |= QUIET_FLAG;
break;
case 'o':
flags |= SORT_FLAG;
break;
case 'f':
flags |= FORCE_FLAG;
break;
case 'c':
flags |= CLEAR_FLAG;
break;
case 'h':
print_help();
return 0;
default:
fprintf(stderr, "Invalid option\n");
return EXIT_FAILURE;
}
}
if (optind >= argc) {
fprintf(stderr, "No command specified\n");
print_help();
return EXIT_FAILURE;
}
char* command = argv[optind];
argc -= optind + 1;
argv += optind + 1;
Command* cmd = find_command(command);
if (cmd) {
return cmd->func(argc, argv, flags);
} else {
fprintf(stderr, "Invalid command\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}