-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmuweb.c
More file actions
executable file
·3453 lines (2800 loc) · 116 KB
/
Copy pathmuweb.c
File metadata and controls
executable file
·3453 lines (2800 loc) · 116 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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <getopt.h>
#include <netinet/in.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <time.h>
#include <unistd.h>
#include <common/base/totp.h>
#include <common/content/core/state_preview.h>
#include <common/content/lookup.h>
#define MUWEB_VERSION "0.1.0"
#define MUWEB_MAX_CONNECTIONS 24
#define MUWEB_HEADER_LIMIT (16 * 1024)
#define MUWEB_UPLOAD_LIMIT (32 * 1024 * 1024)
#define MUWEB_IDLE_SECONDS 30
#define MUWEB_SEND_CHUNK (64 * 1024)
#define MUWEB_SCAN_DEPTH 8
#define MUWEB_CONTENT_SECONDS 60
#define MUWEB_CONTENT_DEPTH 6
#define MUWEB_CONTENT_ROOTS 4
static const char *const catalogue_types[] = {
"box", "grid", "preview", "text", "splash", "manual",
"video", "overlay/base", "overlay/battery", "overlay/bright", "overlay/volume"
};
#define CATALOGUE_TYPE_COUNT (sizeof(catalogue_types) / sizeof(catalogue_types[0]))
void *mux_malloc(const size_t size) {
void *memory = malloc(size);
if (!memory && size) {
fprintf(stderr, "muweb: out of memory (%zu bytes)\n", size);
abort();
}
return memory;
}
static volatile sig_atomic_t running = 1;
static char web_root[PATH_MAX];
static char catalogue_root[PATH_MAX];
static char pickles_root[PATH_MAX];
static char info_root[PATH_MAX];
static char content_roots[MUWEB_CONTENT_ROOTS][PATH_MAX];
static size_t content_root_count = 0;
static int read_only = 0;
static int verbose = 0;
static unsigned char code_secret[TOTP_SECRET_SIZE];
static int code_required = 0;
#define MUWEB_SESSION_SLOTS 4
#define MUWEB_SESSION_IDLE 1800
#define MUWEB_TOKEN_BYTES 16
#define MUWEB_TOKEN_TEXT (MUWEB_TOKEN_BYTES * 2 + 1)
struct session {
char token[MUWEB_TOKEN_TEXT];
time_t expires;
};
static struct session sessions[MUWEB_SESSION_SLOTS];
static void signal_handler(const int signal_number) {
(void) signal_number;
running = 0;
}
static void log_verbose(const char *format, ...) {
if (!verbose) return;
va_list arguments;
va_start(arguments, format);
vfprintf(stderr, format, arguments);
va_end(arguments);
fputc('\n', stderr);
}
struct buffer {
char *data;
size_t length;
size_t capacity;
};
static int buffer_reserve(struct buffer *buffer, const size_t extra) {
if (buffer->length + extra + 1 <= buffer->capacity) return 1;
size_t capacity = buffer->capacity ? buffer->capacity : 1024;
while (capacity < buffer->length + extra + 1) {
if (capacity > SIZE_MAX / 2) return 0;
capacity *= 2;
}
char *data = realloc(buffer->data, capacity);
if (!data) return 0;
buffer->data = data;
buffer->capacity = capacity;
return 1;
}
static int buffer_append(struct buffer *buffer, const char *text, const size_t length) {
if (!buffer_reserve(buffer, length)) return 0;
memcpy(buffer->data + buffer->length, text, length);
buffer->length += length;
buffer->data[buffer->length] = '\0';
return 1;
}
static int buffer_puts(struct buffer *buffer, const char *text) {
return buffer_append(buffer, text, strlen(text));
}
static int buffer_printf(struct buffer *buffer, const char *format, ...) {
va_list arguments;
va_start(arguments, format);
char stack[512];
const int needed = vsnprintf(stack, sizeof(stack), format, arguments);
va_end(arguments);
if (needed < 0) return 0;
if ((size_t) needed < sizeof(stack)) return buffer_append(buffer, stack, (size_t) needed);
if (!buffer_reserve(buffer, (size_t) needed)) return 0;
va_start(arguments, format);
vsnprintf(buffer->data + buffer->length, (size_t) needed + 1, format, arguments);
va_end(arguments);
buffer->length += (size_t) needed;
return 1;
}
static void buffer_free(struct buffer *buffer) {
free(buffer->data);
buffer->data = NULL;
buffer->length = 0;
buffer->capacity = 0;
}
#define MUWEB_CACHE_SECONDS 15
struct cached {
struct buffer body;
time_t made;
};
static struct cached catalogue_cache;
static struct cached pickles_cache;
static struct cached content_cache;
static struct cached folder_cache;
static char folder_cached_name[PATH_MAX];
static int cache_fresh(const struct cached *cache) {
return cache->body.length > 0 && time(NULL) - cache->made < MUWEB_CACHE_SECONDS;
}
static void cache_store(struct cached *cache, const struct buffer *body) {
cache->body.length = 0;
if (buffer_append(&cache->body, body->data ? body->data : "", body->length))
cache->made = time(NULL);
else
cache->body.length = 0;
}
static void cache_drop(void) {
catalogue_cache.body.length = 0;
pickles_cache.body.length = 0;
content_cache.body.length = 0;
folder_cache.body.length = 0;
folder_cached_name[0] = '\0';
}
static int json_string(struct buffer *buffer, const char *value) {
if (!buffer_puts(buffer, "\"")) return 0;
for (const unsigned char *p = (const unsigned char *) (value ? value : ""); *p; ++p) {
switch (*p) {
case '"':
if (!buffer_puts(buffer, "\\\"")) return 0;
break;
case '\\':
if (!buffer_puts(buffer, "\\\\")) return 0;
break;
case '\n':
if (!buffer_puts(buffer, "\\n")) return 0;
break;
case '\r':
if (!buffer_puts(buffer, "\\r")) return 0;
break;
case '\t':
if (!buffer_puts(buffer, "\\t")) return 0;
break;
default:
if (*p < 0x20) {
if (!buffer_printf(buffer, "\\u%04x", *p)) return 0;
} else if (!buffer_append(buffer, (const char *) p, 1)) {
return 0;
}
break;
}
}
return buffer_puts(buffer, "\"");
}
static int json_field(struct buffer *buffer, const char *key, const char *value) {
return json_string(buffer, key) && buffer_puts(buffer, ":") && json_string(buffer, value);
}
static int json_number(struct buffer *buffer, const char *key, const long long value) {
return json_string(buffer, key) && buffer_printf(buffer, ":%lld", value);
}
static int space_json(struct buffer *out, const char *key, const char *path) {
struct statvfs info;
if (!path || !path[0] || statvfs(path, &info) < 0) return 1;
const unsigned long long unit = info.f_frsize ? info.f_frsize : info.f_bsize;
const unsigned long long total = (unsigned long long) info.f_blocks * unit;
const unsigned long long free_space = (unsigned long long) info.f_bavail * unit;
const unsigned long long used = ((unsigned long long) info.f_blocks - info.f_bfree) * unit;
return buffer_printf(out, "\"%s\":{\"total\":%llu,\"free\":%llu,\"used\":%llu},", key, total, free_space, used);
}
static int string_array_add(char ***items, size_t *count, size_t *capacity, const char *value) {
if (*count == *capacity) {
const size_t next = *capacity ? *capacity * 2 : 64;
char **grown = realloc(*items, next * sizeof(char *));
if (!grown) return 0;
*items = grown;
*capacity = next;
}
char *copy = strdup(value);
if (!copy) return 0;
(*items)[(*count)++] = copy;
return 1;
}
static void string_array_free(char **items, const size_t count) {
for (size_t i = 0; i < count; ++i)
free(items[i]);
free(items);
}
static int string_compare(const void *left, const void *right) {
return strcmp(*(const char *const *) left, *(const char *const *) right);
}
static size_t string_array_dedupe(char **items, const size_t count) {
if (count < 2) return count;
qsort(items, count, sizeof(char *), string_compare);
size_t unique = 1;
for (size_t i = 1; i < count; ++i) {
if (strcmp(items[i], items[unique - 1]) == 0) {
free(items[i]);
continue;
}
items[unique++] = items[i];
}
return unique;
}
static void strip_extension(const char *name) {
char *dot = strrchr(name, '.');
if (dot && dot != name) *dot = '\0';
}
static const char *file_extension(const char *name) {
const char *dot = strrchr(name, '.');
return dot && dot != name ? dot + 1 : "";
}
static int url_decode(const char *source, char *out, const size_t out_size) {
size_t written = 0;
for (const char *p = source; *p; ++p) {
if (written + 1 >= out_size) return 0;
if (*p == '%') {
if (!isxdigit((unsigned char) p[1]) || !isxdigit((unsigned char) p[2])) return 0;
const char hex[3] = {p[1], p[2], '\0'};
const long value = strtol(hex, NULL, 16);
if (value == 0) return 0;
out[written++] = (char) value;
p += 2;
} else {
out[written++] = *p;
}
}
out[written] = '\0';
return 1;
}
static int safe_relative_path(const char *relative) {
if (!relative || !*relative || *relative == '/') return 0;
if (strlen(relative) >= PATH_MAX - 1) return 0;
const char *segment = relative;
while (*segment) {
const char *end = strchr(segment, '/');
const size_t length = end ? (size_t) (end - segment) : strlen(segment);
if (length == 0 || segment[0] == '.') return 0;
for (size_t i = 0; i < length; ++i) {
if (segment[i] == '\\' || (unsigned char) segment[i] < 0x20) return 0;
}
if (!end) break;
segment = end + 1;
}
return 1;
}
static int join_path(char *out, const size_t out_size, const char *base, const char *relative) {
const int written =
relative && *relative ? snprintf(out, out_size, "%s/%s", base, relative) : snprintf(out, out_size, "%s", base);
return written > 0 && (size_t) written < out_size;
}
static int resolve_within(const char *root, const char *relative, char *out, const size_t out_size) {
if (relative && *relative && !safe_relative_path(relative)) return 0;
char candidate[PATH_MAX];
if (!join_path(candidate, sizeof(candidate), root, relative)) return 0;
char resolved[PATH_MAX];
if (!realpath(candidate, resolved)) return 0;
char resolved_root[PATH_MAX];
if (!realpath(root, resolved_root)) return 0;
const size_t root_length = strlen(resolved_root);
if (strncmp(resolved, resolved_root, root_length) != 0) return 0;
if (resolved[root_length] != '\0' && resolved[root_length] != '/') return 0;
const int written = snprintf(out, out_size, "%s", resolved);
return written > 0 && (size_t) written < out_size;
}
static int make_directories(const char *path) {
char working[PATH_MAX];
const int written = snprintf(working, sizeof(working), "%s", path);
if (written < 0 || (size_t) written >= sizeof(working)) return 0;
for (char *p = working + 1; *p; ++p) {
if (*p != '/') continue;
*p = '\0';
if (mkdir(working, 0755) < 0 && errno != EEXIST) return 0;
*p = '/';
}
return mkdir(working, 0755) == 0 || errno == EEXIST;
}
static int
resolve_target(const char *root, const char *relative, const int create_parents, char *out, const size_t out_size) {
if (!safe_relative_path(relative)) return 0;
char parent_relative[PATH_MAX];
const char *leaf = strrchr(relative, '/');
if (leaf) {
const size_t parent_length = (size_t) (leaf - relative);
if (parent_length >= sizeof(parent_relative)) return 0;
memcpy(parent_relative, relative, parent_length);
parent_relative[parent_length] = '\0';
leaf += 1;
} else {
parent_relative[0] = '\0';
leaf = relative;
}
if (create_parents && parent_relative[0]) {
char parent_path[PATH_MAX];
if (!join_path(parent_path, sizeof(parent_path), root, parent_relative)) return 0;
if (!make_directories(parent_path)) return 0;
}
char parent_resolved[PATH_MAX];
if (!resolve_within(root, parent_relative[0] ? parent_relative : NULL, parent_resolved, sizeof(parent_resolved)))
return 0;
const int written = snprintf(out, out_size, "%s/%s", parent_resolved, leaf);
return written > 0 && (size_t) written < out_size;
}
static void drop_checksum(const char *path) {
char sum[PATH_MAX];
if ((size_t) snprintf(sum, sizeof(sum), "%s.sum", path) < sizeof(sum)) unlink(sum);
}
static int write_file_atomic(const char *path, const char *data, const size_t length) {
char temporary[PATH_MAX];
const int written = snprintf(temporary, sizeof(temporary), "%s.tmp", path);
if (written < 0 || (size_t) written >= sizeof(temporary)) return 0;
const int descriptor = open(temporary, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0644);
if (descriptor < 0) return 0;
size_t offset = 0;
while (offset < length) {
const ssize_t chunk = write(descriptor, data + offset, length - offset);
if (chunk <= 0) {
if (errno == EINTR) continue;
close(descriptor);
unlink(temporary);
return 0;
}
offset += (size_t) chunk;
}
if (fsync(descriptor) < 0 || close(descriptor) < 0) {
unlink(temporary);
return 0;
}
if (rename(temporary, path) < 0) {
unlink(temporary);
return 0;
}
return 1;
}
static int read_whole_file(const char *path, struct buffer *out) {
const int descriptor = open(path, O_RDONLY | O_CLOEXEC);
if (descriptor < 0) return 0;
char chunk[65536];
for (;;) {
const ssize_t got = read(descriptor, chunk, sizeof(chunk));
if (got == 0) break;
if (got < 0) {
if (errno == EINTR) continue;
close(descriptor);
return 0;
}
if (out->length + (size_t) got > MUWEB_UPLOAD_LIMIT || !buffer_append(out, chunk, (size_t) got)) {
close(descriptor);
return 0;
}
}
close(descriptor);
return 1;
}
static const char *mime_for(const char *name) {
static const struct {
const char *extension;
const char *type;
} table[] = {
{"html", "text/html; charset=utf-8"},
{"css", "text/css; charset=utf-8"},
{"js", "text/javascript; charset=utf-8"},
{"json", "application/json"},
{"svg", "image/svg+xml"},
{"png", "image/png"},
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"webp", "image/webp"},
{"qoi", "image/qoi"},
{"tga", "image/x-targa"},
{"gif", "image/gif"},
{"bmp", "image/bmp"},
{"pcx", "image/x-pcx"},
{"txt", "text/plain; charset=utf-8"},
{"pdf", "application/pdf"},
{"mp4", "video/mp4"},
{"webm", "video/webm"},
{"ini", "text/plain; charset=utf-8"},
{"state", "application/octet-stream"},
{"srm", "application/octet-stream"}
};
const char *extension = file_extension(name);
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); ++i) {
if (strcasecmp(extension, table[i].extension) == 0) return table[i].type;
}
return "application/octet-stream";
}
static int upload_extension_allowed(const char *type, const char *extension) {
static const char *const images[] = {"svg", "png", "jpg", "jpeg", "webp", "qoi", "tga", "gif", "bmp", "pcx", NULL};
static const char *const documents[] = {"txt", NULL};
static const char *const videos[] = {"mp4", NULL};
const char *const *allowed = images;
if (strcmp(type, "text") == 0 || strcmp(type, "manual") == 0)
allowed = documents;
else if (strcmp(type, "video") == 0)
allowed = videos;
for (size_t i = 0; allowed[i]; ++i) {
if (strcasecmp(extension, allowed[i]) == 0) return 1;
}
return 0;
}
struct content_item {
char stem[NAME_MAX + 1];
char catalogue[NAME_MAX + 1];
int folder;
};
struct content_folder {
char name[NAME_MAX + 1];
char catalogue[NAME_MAX + 1];
struct content_item *items;
size_t count;
size_t capacity;
};
static struct content_folder *content_folders = NULL;
static size_t content_folder_count = 0;
static size_t content_folder_capacity = 0;
static time_t content_made = 0;
struct assign_entry {
char key[NAME_MAX + 1];
char catalogue[NAME_MAX + 1];
};
static struct assign_entry *assign_entries = NULL;
static size_t assign_count = 0;
static char **skip_patterns = NULL;
static size_t skip_pattern_count = 0;
static size_t skip_pattern_capacity = 0;
static void lowercase(char *text) {
for (; *text; ++text)
*text = (char) tolower((unsigned char) *text);
}
static void assign_key(const char *name, char *out, const size_t out_size) {
size_t written = 0;
for (const char *read = name; *read && written + 1 < out_size; ++read) {
if (*read == ' ' || *read == '-' || *read == '_' || *read == '+') continue;
out[written++] = (char) tolower((unsigned char) *read);
}
out[written] = '\0';
}
static const char *json_scan_string(const char *cursor, char *out, const size_t out_size) {
while (*cursor && *cursor != '"') {
if (*cursor == '}' || *cursor == ']') return NULL;
++cursor;
}
if (*cursor != '"') return NULL;
++cursor;
size_t written = 0;
while (*cursor && *cursor != '"') {
char character = *cursor++;
if (character == '\\' && *cursor) {
const char escape = *cursor++;
switch (escape) {
case 'n':
character = '\n';
break;
case 't':
character = '\t';
break;
case 'r':
character = '\r';
break;
case 'b':
character = '\b';
break;
case 'f':
character = '\f';
break;
case 'u': {
unsigned value = 0;
for (int digit = 0; digit < 4 && isxdigit((unsigned char) *cursor); ++digit) {
const char hex = *cursor++;
value = value * 16
+ (unsigned) (isdigit((unsigned char) hex) ? hex - '0'
: tolower((unsigned char) hex) - 'a' + 10);
}
character = value && value < 0x80 ? (char) value : '?';
break;
}
default:
character = escape;
break;
}
}
if (written + 1 < out_size) out[written++] = character;
}
if (*cursor != '"') return NULL;
out[written] = '\0';
return cursor + 1;
}
static void assign_free(void) {
free(assign_entries);
assign_entries = NULL;
assign_count = 0;
}
static void assign_load(void) {
assign_free();
if (!info_root[0]) return;
char path[PATH_MAX];
if ((size_t) snprintf(path, sizeof(path), "%s/manifest/assign.json", info_root) >= sizeof(path)) return;
struct buffer text = {0};
if (!read_whole_file(path, &text) || !text.data) {
buffer_free(&text);
return;
}
const char *cursor = strchr(text.data, '{');
size_t capacity = 0;
while (cursor) {
char key[NAME_MAX + 1];
char value[NAME_MAX + 1];
cursor = json_scan_string(cursor, key, sizeof(key));
if (!cursor) break;
cursor = json_scan_string(cursor, value, sizeof(value));
if (!cursor) break;
if (!key[0] || !value[0]) continue;
if (assign_count == capacity) {
const size_t next = capacity ? capacity * 2 : 32;
struct assign_entry *grown = realloc(assign_entries, next * sizeof(*grown));
if (!grown) break;
assign_entries = grown;
capacity = next;
}
assign_key(key, assign_entries[assign_count].key, sizeof(assign_entries[assign_count].key));
snprintf(assign_entries[assign_count].catalogue, sizeof(assign_entries[assign_count].catalogue), "%s", value);
assign_count += 1;
}
buffer_free(&text);
log_verbose("assign.json: %zu directory names mapped", assign_count);
}
static const char *assign_lookup(const char *directory_name) {
char key[NAME_MAX + 1];
assign_key(directory_name, key, sizeof(key));
for (size_t i = 0; i < assign_count; ++i)
if (strcmp(assign_entries[i].key, key) == 0) return assign_entries[i].catalogue;
return NULL;
}
static void skip_free(void) {
string_array_free(skip_patterns, skip_pattern_count);
skip_patterns = NULL;
skip_pattern_count = 0;
skip_pattern_capacity = 0;
}
static void skip_load(void) {
skip_free();
if (!info_root[0]) return;
char path[PATH_MAX];
if ((size_t) snprintf(path, sizeof(path), "%s/skip.ini", info_root) >= sizeof(path)) return;
struct buffer text = {0};
if (!read_whole_file(path, &text) || !text.data) {
buffer_free(&text);
return;
}
for (char *line = text.data, *next; line && *line; line = next) {
next = strchr(line, '\n');
if (next) *next++ = '\0';
size_t length = strlen(line);
while (length && (line[length - 1] == '\r' || line[length - 1] == ' '))
line[--length] = '\0';
if (!length || line[0] == '#') continue;
lowercase(line);
string_array_add(&skip_patterns, &skip_pattern_count, &skip_pattern_capacity, line);
}
buffer_free(&text);
log_verbose("skip.ini: %zu patterns", skip_pattern_count);
}
static int should_skip(const char *name, const int is_directory) {
char folded[NAME_MAX + 1];
snprintf(folded, sizeof(folded), "%s", name);
lowercase(folded);
for (size_t i = 0; i < skip_pattern_count; ++i) {
const char *pattern = skip_patterns[i];
int directory_only = 0;
if (*pattern == '/') {
directory_only = 1;
++pattern;
if (!*pattern) continue;
}
if (directory_only && !is_directory) continue;
if (fnmatch(pattern, folded, 0) == 0) return 1;
}
return 0;
}
#define CATALOGUE_FOLDER "Folder"
static void content_free(void) {
for (size_t i = 0; i < content_folder_count; ++i)
free(content_folders[i].items);
free(content_folders);
content_folders = NULL;
content_folder_count = 0;
content_folder_capacity = 0;
content_made = 0;
}
static struct content_folder *content_folder_for(const char *name) {
for (size_t i = 0; i < content_folder_count; ++i)
if (strcmp(content_folders[i].name, name) == 0) return &content_folders[i];
if (content_folder_count == content_folder_capacity) {
const size_t next = content_folder_capacity ? content_folder_capacity * 2 : 16;
struct content_folder *grown = realloc(content_folders, next * sizeof(*grown));
if (!grown) return NULL;
content_folders = grown;
content_folder_capacity = next;
}
struct content_folder *folder = &content_folders[content_folder_count];
memset(folder, 0, sizeof(*folder));
snprintf(folder->name, sizeof(folder->name), "%s", name);
content_folder_count += 1;
return folder;
}
static int
content_item_add(struct content_folder *folder, const char *stem, const char *catalogue, const int is_folder) {
for (size_t i = 0; i < folder->count; ++i)
if (folder->items[i].folder == is_folder && strcmp(folder->items[i].stem, stem) == 0) return 1;
if (folder->count == folder->capacity) {
const size_t next = folder->capacity ? folder->capacity * 2 : 32;
struct content_item *grown = realloc(folder->items, next * sizeof(*grown));
if (!grown) return 0;
folder->items = grown;
folder->capacity = next;
}
struct content_item *item = &folder->items[folder->count];
memset(item, 0, sizeof(*item));
snprintf(item->stem, sizeof(item->stem), "%s", stem);
snprintf(item->catalogue, sizeof(item->catalogue), "%s", catalogue ? catalogue : "");
item->folder = is_folder;
folder->count += 1;
return 1;
}
static void content_walk(
const char *root, const char *path, struct content_folder *folder, const char *catalogue, const int depth
) {
if (depth > MUWEB_CONTENT_DEPTH) return;
DIR *directory = opendir(path);
if (!directory) return;
const struct dirent *entry;
while ((entry = readdir(directory))) {
if (entry->d_name[0] == '.') continue;
char child[PATH_MAX];
if ((size_t) snprintf(child, sizeof(child), "%s/%s", path, entry->d_name) >= sizeof(child)) continue;
int is_directory = entry->d_type == DT_DIR;
if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
struct stat info;
if (stat(child, &info) < 0) continue;
is_directory = S_ISDIR(info.st_mode);
}
if (should_skip(entry->d_name, is_directory)) continue;
if (is_directory) {
if (entry->d_type == DT_LNK) {
char resolved[PATH_MAX];
const size_t root_length = strlen(root);
if (!realpath(child, resolved)) continue;
if (strncmp(resolved, root, root_length) != 0
|| (resolved[root_length] && resolved[root_length] != '/'))
continue;
}
content_item_add(folder, entry->d_name, CATALOGUE_FOLDER, 1);
const char *assigned = assign_lookup(entry->d_name);
content_walk(root, child, folder, assigned ? assigned : catalogue, depth + 1);
continue;
}
char stem[NAME_MAX + 1];
snprintf(stem, sizeof(stem), "%s", entry->d_name);
strip_extension(stem);
if (stem[0]) content_item_add(folder, stem, catalogue, 0);
}
closedir(directory);
}
static int content_item_compare(const void *left, const void *right) {
const struct content_item *a = left;
const struct content_item *b = right;
if (a->folder != b->folder) return b->folder - a->folder;
return strcasecmp(a->stem, b->stem);
}
static void content_index_build(void) {
if (!content_root_count) return;
if (content_made && time(NULL) - content_made < MUWEB_CONTENT_SECONDS) return;
content_free();
assign_load();
skip_load();
for (size_t root = 0; root < content_root_count; ++root) {
DIR *directory = opendir(content_roots[root]);
if (!directory) continue;
const struct dirent *entry;
while ((entry = readdir(directory))) {
if (entry->d_name[0] == '.') continue;
char child[PATH_MAX];
if ((size_t) snprintf(child, sizeof(child), "%s/%s", content_roots[root], entry->d_name) >= sizeof(child))
continue;
int is_directory = entry->d_type == DT_DIR;
if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
struct stat info;
if (stat(child, &info) < 0) continue;
is_directory = S_ISDIR(info.st_mode);
}
if (!is_directory || should_skip(entry->d_name, 1)) continue;
if (entry->d_type == DT_LNK) {
char resolved[PATH_MAX];
const size_t root_length = strlen(content_roots[root]);
if (!realpath(child, resolved)) continue;
if (strncmp(resolved, content_roots[root], root_length) != 0
|| (resolved[root_length] && resolved[root_length] != '/'))
continue;
}
struct content_folder *folder = content_folder_for(entry->d_name);
if (!folder) continue;
const char *assigned = assign_lookup(entry->d_name);
if (assigned && !folder->catalogue[0])
snprintf(folder->catalogue, sizeof(folder->catalogue), "%s", assigned);
content_walk(content_roots[root], child, folder, assigned, 0);
}
closedir(directory);
}
size_t total = 0;
for (size_t i = 0; i < content_folder_count; ++i) {
qsort(content_folders[i].items, content_folders[i].count, sizeof(struct content_item), content_item_compare);
total += content_folders[i].count;
}
content_made = time(NULL);
log_verbose("content: %zu items across %zu folders", total, content_folder_count);
}
static int content_holds(const char *catalogue, const char *stem) {
content_index_build();
for (size_t i = 0; i < content_folder_count; ++i)
for (size_t j = 0; j < content_folders[i].count; ++j)
if (strcmp(content_folders[i].items[j].catalogue, catalogue) == 0
&& strcmp(content_folders[i].items[j].stem, stem) == 0)
return 1;
return 0;
}
static size_t content_count_for(const char *catalogue) {
size_t count = 0;
content_index_build();
for (size_t i = 0; i < content_folder_count; ++i)
for (size_t j = 0; j < content_folders[i].count; ++j)
if (strcmp(content_folders[i].items[j].catalogue, catalogue) == 0) count += 1;
return count;
}
struct catalogue_file {
char relative[PATH_MAX];
char stem[NAME_MAX + 1];
long long bytes;
long long modified;
};
struct catalogue_scan {
struct catalogue_file *files;
size_t count;
size_t capacity;
long long bytes;
};
static int
catalogue_scan_add(struct catalogue_scan *scan, const char *relative, const char *name, const struct stat *info) {
if (scan->count == scan->capacity) {
const size_t next = scan->capacity ? scan->capacity * 2 : 64;
struct catalogue_file *grown = realloc(scan->files, next * sizeof(struct catalogue_file));
if (!grown) return 0;
scan->files = grown;
scan->capacity = next;
}
struct catalogue_file *file = &scan->files[scan->count];
if ((size_t) snprintf(file->relative, sizeof(file->relative), "%s", relative) >= sizeof(file->relative)) return 0;
if ((size_t) snprintf(file->stem, sizeof(file->stem), "%s", name) >= sizeof(file->stem)) return 0;
strip_extension(file->stem);
file->bytes = (long long) info->st_size;
file->modified = (long long) info->st_mtime;
scan->bytes += file->bytes;
scan->count += 1;
return 1;
}
static void catalogue_scan_type(const char *type_path, struct catalogue_scan *scan, const int want_sizes) {
static const struct stat unsized;
DIR *directory = opendir(type_path);
if (!directory) return;
const struct dirent *entry;
while ((entry = readdir(directory))) {
if (entry->d_name[0] == '.') continue;
char child[PATH_MAX];
if (!join_path(child, sizeof(child), type_path, entry->d_name)) continue;
struct stat info;
int kind = entry->d_type;
if (kind == DT_UNKNOWN || want_sizes) {
if (stat(child, &info) < 0) continue;
kind = S_ISDIR(info.st_mode) ? DT_DIR : S_ISREG(info.st_mode) ? DT_REG : DT_UNKNOWN;
}
if (kind == DT_REG) {
catalogue_scan_add(scan, entry->d_name, entry->d_name, want_sizes ? &info : &unsized);
continue;
}
if (kind != DT_DIR) continue;
DIR *nested = opendir(child);