-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.file_system.c
648 lines (536 loc) · 15.3 KB
/
interpreter.file_system.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 - 2022 TheVice
*
*/
#include "file_system.h"
#include "buffer.h"
#include "common.h"
#include "conversion.h"
#include "date_time.h"
#include "hash.h"
#include "interpreter.h"
#include "path.h"
#include "range.h"
enum file_function
{
file_exists_,
file_get_checksum_,
file_get_creation_time_,
file_get_creation_time_utc_,
file_get_last_access_time_,
file_get_last_access_time_utc_,
file_get_last_write_time_,
file_get_last_write_time_utc_,
file_get_length_,
file_up_to_date_,
file_replace_,
UNKNOWN_FILE_FUNCTION,
file_set_creation_time_,
file_set_creation_time_utc_,
file_set_last_access_time_,
file_set_last_access_time_utc_,
file_set_last_write_time_,
file_set_last_write_time_utc_
};
enum dir_function
{
enumerate_file_system_entries,
dir_exists,
dir_get_creation_time,
dir_get_creation_time_utc,
get_current_directory,
get_directory_root,
dir_get_last_access_time,
dir_get_last_access_time_utc,
dir_get_last_write_time,
dir_get_last_write_time_utc,
get_logical_drives,
get_parent_directory,
UNKNOWN_DIR_FUNCTION
};
enum enumerate_entry_types
{
directory_entry,
file_entry,
all_entries,
UNKNOWN_ENTRY_TYPE
};
uint8_t file_get_id_of_file_set_creation_time_function()
{
return file_set_creation_time_;
}
uint8_t file_get_id_of_file_set_creation_time_utc_function()
{
return file_set_creation_time_utc_;
}
uint8_t file_get_id_of_file_set_last_access_time_function()
{
return file_set_last_access_time_;
}
uint8_t file_get_id_of_file_set_last_access_time_utc_function()
{
return file_set_last_access_time_utc_;
}
uint8_t file_get_id_of_file_set_last_write_time_function()
{
return file_set_last_write_time_;
}
uint8_t file_get_id_of_file_set_last_write_time_utc_function()
{
return file_set_last_write_time_utc_;
}
uint8_t dir_get_id_of_get_current_directory_function()
{
return get_current_directory;
}
uint8_t file_system_get_id_of_directory_entry()
{
return directory_entry;
}
uint8_t file_system_get_id_of_file_entry()
{
return file_entry;
}
uint8_t file_system_get_id_of_all_entries()
{
return all_entries;
}
uint8_t dir_get_function(const uint8_t* name_start, const uint8_t* name_finish)
{
static const uint8_t* dir_function_str[] =
{
(const uint8_t*)"enumerate-file-system-entries",
(const uint8_t*)"exists",
(const uint8_t*)"get-creation-time",
(const uint8_t*)"get-creation-time-utc",
(const uint8_t*)"get-current-directory",
(const uint8_t*)"get-directory-root",
(const uint8_t*)"get-last-access-time",
(const uint8_t*)"get-last-access-time-utc",
(const uint8_t*)"get-last-write-time",
(const uint8_t*)"get-last-write-time-utc",
(const uint8_t*)"get-logical-drives",
(const uint8_t*)"get-parent-directory"
};
/**/
return common_string_to_enum(name_start, name_finish, dir_function_str, UNKNOWN_DIR_FUNCTION);
}
uint8_t dir_exec_function(uint8_t function, const void* arguments, uint8_t arguments_count,
void* output)
{
if (UNKNOWN_DIR_FUNCTION <= function ||
get_current_directory == function ||
NULL == arguments ||
3 < arguments_count ||
NULL == output)
{
return 0;
}
struct range values[3];
if (arguments_count && !common_get_arguments(arguments, arguments_count, values, 1))
{
return 0;
}
switch (function)
{
case enumerate_file_system_entries:
{
if (3 != arguments_count &&
2 != arguments_count)
{
break;
}
static const uint8_t* entry_types_str[] =
{
(const uint8_t*)"directory",
(const uint8_t*)"file",
(const uint8_t*)"all"
};
/**/
const uint8_t entry_type = common_string_to_enum(
values[1].start, values[1].finish, entry_types_str, UNKNOWN_ENTRY_TYPE);
if (UNKNOWN_ENTRY_TYPE == entry_type)
{
break;
}
uint8_t recurse = 0;
if (3 == arguments_count &&
!bool_parse(values[2].start, values[2].finish, &recurse))
{
break;
}
return directory_enumerate_file_system_entries(
buffer_buffer_data(arguments, 0), entry_type, recurse, output, 1);
}
case dir_exists:
return 1 == arguments_count && bool_to_string(directory_exists(values[0].start), output);
case dir_get_creation_time:
return 1 == arguments_count && int64_to_string(directory_get_creation_time(values[0].start), output);
case dir_get_creation_time_utc:
return 1 == arguments_count && int64_to_string(directory_get_creation_time_utc(values[0].start), output);
case get_directory_root:
return 1 == arguments_count &&
directory_get_directory_root(values[0].start, &values[0].finish) &&
buffer_append_data_from_range(output, &values[0]);
case dir_get_last_access_time:
return 1 == arguments_count && int64_to_string(directory_get_last_access_time(values[0].start), output);
case dir_get_last_access_time_utc:
return 1 == arguments_count && int64_to_string(directory_get_last_access_time_utc(values[0].start), output);
case dir_get_last_write_time:
return 1 == arguments_count && int64_to_string(directory_get_last_write_time(values[0].start), output);
case dir_get_last_write_time_utc:
return 1 == arguments_count && int64_to_string(directory_get_last_write_time_utc(values[0].start), output);
case get_logical_drives:
return 0 == arguments_count && directory_get_logical_drives(output);
case get_parent_directory:
return 1 == arguments_count &&
directory_get_parent_directory(values[0].start, &values[0].finish) &&
buffer_append_data_from_range(output, &values[0]);
case UNKNOWN_DIR_FUNCTION:
default:
break;
}
return 0;
}
uint8_t file_get_function(const uint8_t* name_start, const uint8_t* name_finish)
{
static const uint8_t* file_function_str[] =
{
(const uint8_t*)"exists",
(const uint8_t*)"get-checksum",
(const uint8_t*)"get-creation-time",
(const uint8_t*)"get-creation-time-utc",
(const uint8_t*)"get-last-access-time",
(const uint8_t*)"get-last-access-time-utc",
(const uint8_t*)"get-last-write-time",
(const uint8_t*)"get-last-write-time-utc",
(const uint8_t*)"get-length",
(const uint8_t*)"up-to-date",
(const uint8_t*)"replace"
};
/**/
return common_string_to_enum(name_start, name_finish, file_function_str, UNKNOWN_FILE_FUNCTION);
}
uint8_t file_exec_function(uint8_t function, const void* arguments, uint8_t arguments_count,
void* output)
{
if (UNKNOWN_FILE_FUNCTION <= function ||
NULL == arguments ||
!arguments_count ||
3 < arguments_count ||
NULL == output)
{
return 0;
}
struct range values[3];
if (!common_get_arguments(arguments, arguments_count, values, 1))
{
return 0;
}
for (uint8_t i = arguments_count, count = COUNT_OF(values); i < count; ++i)
{
values[i].start = values[i].finish = NULL;
}
switch (function)
{
case file_exists_:
return 1 == arguments_count &&
bool_to_string(file_exists(values[0].start), output);
case file_get_checksum_:
return (2 == arguments_count || 3 == arguments_count) &&
file_get_checksum(values[0].start, &values[1], &values[2], output);
case file_get_creation_time_:
return 1 == arguments_count &&
int64_to_string(file_get_creation_time(values[0].start), output);
case file_get_creation_time_utc_:
return 1 == arguments_count &&
int64_to_string(file_get_creation_time_utc(values[0].start), output);
case file_get_last_access_time_:
return 1 == arguments_count &&
int64_to_string(file_get_last_access_time(values[0].start), output);
case file_get_last_access_time_utc_:
return 1 == arguments_count &&
int64_to_string(file_get_last_access_time_utc(values[0].start), output);
case file_get_last_write_time_:
return 1 == arguments_count &&
int64_to_string(file_get_last_write_time(values[0].start), output);
case file_get_last_write_time_utc_:
return 1 == arguments_count &&
int64_to_string(file_get_last_write_time_utc(values[0].start), output);
case file_get_length_:
return 1 == arguments_count &&
int64_to_string(file_get_length(values[0].start), output);
case file_up_to_date_:
return 2 == arguments_count &&
bool_to_string(file_up_to_date(values[0].start, values[1].start), output);
case file_replace_:
return 3 == arguments_count &&
bool_to_string(
file_replace(values[0].start,
values[1].start, values[1].finish,
values[2].start, values[2].finish), output);
case UNKNOWN_FILE_FUNCTION:
default:
break;
}
return 0;
}
#define ATTRIB_ARCHIVE_POSITION 0
#define ATTRIB_FILE_POSITION 1
#define ATTRIB_HIDDEN_POSITION 2
#define ATTRIB_NORMAL_POSITION 3
#define ATTRIB_READ_ONLY_POSITION 4
#define ATTRIB_SYSTEM_POSITION 5
#define ATTRIB_MAX_POSITION (ATTRIB_SYSTEM_POSITION + 1)
uint8_t attrib_get_attributes_and_arguments_for_task(
const uint8_t*** task_attributes, const uint8_t** task_attributes_lengths,
uint8_t* task_attributes_count, void* task_arguments)
{
static const uint8_t* attrib_attributes[] =
{
(const uint8_t*)"archive",
(const uint8_t*)"file",
(const uint8_t*)"hidden",
(const uint8_t*)"normal",
(const uint8_t*)"readonly",
(const uint8_t*)"system"
};
/**/
static const uint8_t attrib_attributes_lengths[] = { 7, 4, 6, 6, 8, 6 };
/**/
return common_get_attributes_and_arguments_for_task(
attrib_attributes, attrib_attributes_lengths,
COUNT_OF(attrib_attributes),
task_attributes, task_attributes_lengths,
task_attributes_count, task_arguments);
}
uint8_t attrib_evaluate_task(void* task_arguments, uint8_t verbose)
{
(void)verbose;
if (NULL == task_arguments)
{
return 0;
}
void* file_path_in_a_buffer = buffer_buffer_data(task_arguments, ATTRIB_FILE_POSITION);
if (buffer_size(file_path_in_a_buffer))
{
if (!buffer_push_back(file_path_in_a_buffer, 0))
{
return 0;
}
}
else
{
return 1;
}
uint8_t attributes[ATTRIB_MAX_POSITION];
for (uint8_t i = 0, count = ATTRIB_MAX_POSITION; i < count; ++i)
{
if (ATTRIB_FILE_POSITION == i)
{
continue;
}
const void* data = buffer_buffer_data(task_arguments, i);
if (NULL == data)
{
return 0;
}
attributes[ATTRIB_FILE_POSITION] = (uint8_t)buffer_size(data);
if (!attributes[ATTRIB_FILE_POSITION])
{
attributes[i] = 0;
continue;
}
const uint8_t* value = buffer_uint8_t_data(data, 0);
if (!bool_parse(value, value + attributes[ATTRIB_FILE_POSITION], &(attributes[i])))
{
return 0;
}
}
return file_set_attributes(buffer_uint8_t_data(file_path_in_a_buffer, 0),
attributes[ATTRIB_ARCHIVE_POSITION], attributes[ATTRIB_HIDDEN_POSITION],
attributes[ATTRIB_NORMAL_POSITION], attributes[ATTRIB_READ_ONLY_POSITION],
attributes[ATTRIB_SYSTEM_POSITION]);
}
#define DELETE_DIR_POSITION 0
#define DELETE_FILE_POSITION 1
static const uint8_t* delete_attributes[] =
{
(const uint8_t*)"dir",
(const uint8_t*)"file"
};
static const uint8_t delete_attributes_lengths[] = { 3, 4 };
uint8_t delete_get_attributes_and_arguments_for_task(
const uint8_t*** task_attributes, const uint8_t** task_attributes_lengths,
uint8_t* task_attributes_count, void* task_arguments)
{
return common_get_attributes_and_arguments_for_task(
delete_attributes, delete_attributes_lengths,
COUNT_OF(delete_attributes),
task_attributes, task_attributes_lengths,
task_attributes_count, task_arguments);
}
uint8_t delete_evaluate_task(void* task_arguments, uint8_t verbose)
{
if (NULL == task_arguments)
{
return 0;
}
void* dir_path_in_buffer = buffer_buffer_data(task_arguments, DELETE_DIR_POSITION);
void* file_path_in_buffer = buffer_buffer_data(task_arguments, DELETE_FILE_POSITION);
const uint8_t* dir_ = NULL;
if (buffer_size(dir_path_in_buffer))
{
if (!buffer_push_back(dir_path_in_buffer, 0))
{
return 0;
}
dir_ = buffer_uint8_t_data(dir_path_in_buffer, 0);
if (file_exists(dir_))
{
return 0;
}
}
const uint8_t* file = NULL;
if (buffer_size(file_path_in_buffer))
{
if (!buffer_push_back(file_path_in_buffer, 0))
{
return 0;
}
file = buffer_uint8_t_data(file_path_in_buffer, 0);
if (directory_exists(file))
{
return 0;
}
}
if (NULL == dir_ && NULL == file)
{
return 0;
}
if (file && file_exists(file))
{
verbose = file_delete(file);
}
else
{
verbose = 1;
}
if (dir_ && directory_exists(dir_))
{
verbose = directory_delete(dir_) && verbose;
}
return verbose;
}
#define MKDIR_DIR_POSITION 0
uint8_t mkdir_get_attributes_and_arguments_for_task(
const uint8_t*** task_attributes, const uint8_t** task_attributes_lengths,
uint8_t* task_attributes_count, void* task_arguments)
{
return common_get_attributes_and_arguments_for_task(
delete_attributes, delete_attributes_lengths, 1,
task_attributes, task_attributes_lengths,
task_attributes_count, task_arguments);
}
uint8_t mkdir_evaluate_task(void* task_arguments, uint8_t verbose)
{
(void)verbose;
if (NULL == task_arguments)
{
return 0;
}
void* dir_path_in_buffer = buffer_buffer_data(task_arguments, MKDIR_DIR_POSITION);
if (!buffer_size(dir_path_in_buffer))
{
return 0;
}
if (!buffer_push_back(dir_path_in_buffer, 0))
{
return 0;
}
const uint8_t* dir_ = buffer_uint8_t_data(dir_path_in_buffer, 0);
if (file_exists(dir_))
{
return 0;
}
return directory_create(dir_);
}
#define TOUCH_DATE_TIME_POSITION 0
#define TOUCH_FILE_POSITION 1
#define TOUCH_MILLIS_POSITION 2
uint8_t touch_get_attributes_and_arguments_for_task(
const uint8_t*** task_attributes, const uint8_t** task_attributes_lengths,
uint8_t* task_attributes_count, void* task_arguments)
{
static const uint8_t* touch_attributes[] =
{
(const uint8_t*)"datetime",
(const uint8_t*)"file",
(const uint8_t*)"millis"
};
/**/
static const uint8_t touch_attributes_lengths[] = { 8, 4, 6 };
/**/
return common_get_attributes_and_arguments_for_task(
touch_attributes, touch_attributes_lengths,
COUNT_OF(touch_attributes),
task_attributes, task_attributes_lengths,
task_attributes_count, task_arguments);
}
uint8_t touch_evaluate_task(void* task_arguments, uint8_t verbose)
{
(void)verbose;
if (NULL == task_arguments)
{
return 0;
}
void* file_path_in_buffer = buffer_buffer_data(task_arguments, TOUCH_FILE_POSITION);
if (!buffer_size(file_path_in_buffer))
{
return 1;
}
if (!buffer_push_back(file_path_in_buffer, 0) ||
directory_exists(buffer_uint8_t_data(file_path_in_buffer, 0)))
{
return 0;
}
void* date_time_in_buffer = buffer_buffer_data(task_arguments, TOUCH_DATE_TIME_POSITION);
void* millis_in_buffer = buffer_buffer_data(task_arguments, TOUCH_MILLIS_POSITION);
if (!file_exists(buffer_uint8_t_data(file_path_in_buffer, 0)))
{
if (!file_create(buffer_uint8_t_data(file_path_in_buffer, 0)))
{
return 0;
}
if (!buffer_size(date_time_in_buffer) &&
!buffer_size(millis_in_buffer))
{
return 1;
}
}
int64_t seconds = 0;
if (buffer_size(date_time_in_buffer))
{
struct range date_time_in_a_range;
date_time_in_a_range.start = buffer_uint8_t_data(date_time_in_buffer, 0);
date_time_in_a_range.finish = date_time_in_a_range.start + buffer_size(date_time_in_buffer);
if (!datetime_parse_range(&date_time_in_a_range, &seconds))
{
return 0;
}
}
else if (buffer_size(millis_in_buffer))
{
const uint8_t* start = buffer_uint8_t_data(millis_in_buffer, 0);
const uint8_t* finish = start + buffer_size(millis_in_buffer);
seconds = int64_parse(start, finish);
seconds = date_time_millisecond_to_second(seconds);
}
else
{
seconds = datetime_now();
}
return file_set_last_write_time(buffer_uint8_t_data(file_path_in_buffer, 0), seconds);
}