-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkroot.c
1216 lines (1010 loc) · 24 KB
/
mkroot.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
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 <compiler.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <stdarg.h>
#include <magic.h>
#include <list.h>
#include <hgang.h>
#include <strpool.h>
#include <fobuf.h>
#include <os.h>
#include <vec.h>
#include <webroot-format.h>
#include "trie.h"
#include "sha1.h"
#define WRITE_FILES 1
#define BUFFER_SIZE (1U << 20U)
#define SYMBUF (16U << 10U) /* readlink buffer */
#if 0
#define dprintf printf
#else
#define dprintf(x...) do {} while(0)
#endif
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
static const char * const index_pages[] = {
"index.html",
"default.html",
};
static const char *tmpchunks_pattern = "/tmp/ashttpd.mkroot.XXXXXX";
static const char *cmd = "mkroot";
static int dotfiles; /* whether to include dot files */
static int indexdirs = 1;
struct mime_type {
struct list_head m_list;
const char *m_type;
uint32_t m_strtab_off;
};
#define OBJ_TYPE_FILE 0
#define OBJ_TYPE_REDIRECT 1
struct object {
struct list_head o_list;
gidx_oid_t o_oid;
unsigned int o_type;
union {
struct {
struct mime_type *type;
union {
char *path;
uint64_t tmpoff;
}u;
uint64_t size;
uint64_t off;
dev_t dev;
ino_t ino;
time_t mtime;
#define FILE_PATH 0
#define FILE_TMPCHUNK 1
unsigned int content;
uint8_t digest[WEBROOT_DIGEST_LEN];
} file;
struct {
char *uri;
uint32_t strtab_off;
int code;
} redirect;
}o_u;
};
struct uri {
struct list_head u_list;
struct object *u_obj;
char *u_uri;
};
struct webroot {
struct list_head r_uri;
struct list_head r_redirect;
struct list_head r_file;
struct list_head r_mime_type;
hgang_t r_uri_mem;
hgang_t r_obj_mem;
hgang_t r_mime_mem;
strpool_t r_str_mem;
fobuf_t r_tmpchunks;
const char *r_base;
trie_t r_trie;
struct trie_entry *r_trie_ent;
magic_t r_magic;
uint64_t r_files_sz;
uint64_t r_tmpoff;
unsigned int r_num_uri;
unsigned int r_num_redirect;
unsigned int r_num_file;
unsigned int r_mimetab_sz;
unsigned int r_redirtab_sz;
};
static char *path_splice(const char *dir, const char *path)
{
size_t olen;
char *ret;
olen = snprintf(NULL, 0, "%s/%s", dir, path);
ret = malloc(olen + 2);
if ( NULL == ret ) {
fprintf(stderr, "%s: calloc: %s\n", cmd, os_err());
return NULL;
}
if ( (*dir && dir[strlen(dir) - 1] == '/') || path[0] == '/' )
snprintf(ret, olen + 1, "%s%s", dir, path);
else
snprintf(ret, olen + 1, "%s/%s", dir, path);
return ret;
}
static struct webroot *webroot_new(const char *base)
{
struct webroot *r;
char buf[strlen(tmpchunks_pattern) + 1];
int fd;
r = calloc(1, sizeof(*r));
if ( NULL == r)
goto out;
r->r_uri_mem = hgang_new(sizeof(struct uri), 0);
if ( NULL == r->r_uri_mem )
goto out_free;
r->r_obj_mem = hgang_new(sizeof(struct object), 0);
if ( NULL == r->r_obj_mem )
goto out_free_uri;
r->r_mime_mem = hgang_new(sizeof(struct mime_type), 0);
if ( NULL == r->r_mime_mem )
goto out_free_obj;
r->r_str_mem = strpool_new(0);
if ( NULL == r->r_str_mem )
goto out_free_mime;
r->r_magic = magic_open(MAGIC_MIME_TYPE);
if ( NULL == r->r_magic )
goto out_free_str;
if ( magic_load(r->r_magic, NULL) )
goto out_free_magic;
snprintf(buf, sizeof(buf), "%s", tmpchunks_pattern);
fd = mkstemp(buf);
if ( fd < 0 ) {
fprintf(stderr, "%s: mkstemp: %s\n", cmd, os_err());
goto out_free_magic;
}
printf("%s: Using %s for temp chunks\n", cmd, buf);
if ( unlink(buf) ) {
/* not fatal */
fprintf(stderr, "%s: %s: unlink %s\n", cmd, buf, os_err());
}
r->r_tmpchunks = fobuf_new(fd, 0);
if ( NULL == r->r_tmpchunks )
goto out_close;
INIT_LIST_HEAD(&r->r_uri);
INIT_LIST_HEAD(&r->r_redirect);
INIT_LIST_HEAD(&r->r_file);
INIT_LIST_HEAD(&r->r_mime_type);
r->r_base = base;
goto out;
out_close:
close(fd);
out_free_magic:
magic_close(r->r_magic);
out_free_str:
strpool_free(r->r_str_mem);
out_free_mime:
hgang_free(r->r_mime_mem);
out_free_obj:
hgang_free(r->r_obj_mem);
out_free_uri:
hgang_free(r->r_uri_mem);
out_free:
free(r);
r = NULL;
out:
return r;
}
static void webroot_free(struct webroot *r)
{
if ( r ) {
/* fobuf likes to fsync stuff for safety
* but this is just a tmpfile so let's
* just nuke it quickly. Assumes previous
* code did the right things.
*/
close(fobuf_fd(r->r_tmpchunks));
fobuf_abort(r->r_tmpchunks);
hgang_free(r->r_uri_mem);
hgang_free(r->r_obj_mem);
hgang_free(r->r_mime_mem);
strpool_free(r->r_str_mem);
magic_close(r->r_magic);
free(r->r_trie_ent);
trie_free(r->r_trie);
free(r);
}
}
static int ucmp(const void *A, const void *B)
{
const struct uri * const *a = A;
const struct uri * const *b = B;
#if 0
size_t min, i;
int ret;
min = (strlen((*a)->u_uri) < strlen((*b)->u_uri)) ? strlen((*a)->u_uri) : strlen((*b)->u_uri);
ret = strlen((*a)->u_uri) - strlen((*b)->u_uri);
for(i = 0; i < min; i++) {
int ret;
ret = (*a)->u_uri[i] - (*b)->u_uri[i];
if ( ret )
return ret;
}
return ret;
#else
return strcmp((*a)->u_uri, (*b)->u_uri);
#endif
}
static int sort_uris(struct webroot *r)
{
struct uri *u, *tmp, **s;
unsigned int i = 0;
s = malloc(r->r_num_uri * sizeof(*s));
if ( NULL == s )
return 0;
list_for_each_entry_safe(u, tmp, &r->r_uri, u_list) {
s[i++] = u;
list_del(&u->u_list);
}
assert(i == r->r_num_uri);
assert(list_empty(&r->r_uri));
qsort(s, r->r_num_uri, sizeof(*s), ucmp);
for(i = 0; i < r->r_num_uri; i++) {
list_add_tail(&s[i]->u_list, &r->r_uri);
}
free(s);
return 1;
}
static int sort_objects(struct webroot *r)
{
struct object *obj;
gidx_oid_t oid = 0;
list_for_each_entry(obj, &r->r_redirect, o_list) {
obj->o_oid = oid++;
}
list_for_each_entry(obj, &r->r_file, o_list) {
obj->o_oid = oid++;
}
return 1;
}
static int webroot_prep(struct webroot *r)
{
struct mime_type *m;
struct trie_entry *ent;
struct object *obj;
unsigned int i = 0;
struct uri *u;
uint64_t off;
int ret = 0;
trie_t t;
if ( !sort_uris(r) )
goto out;
if ( !sort_objects(r) )
goto out;
ent = malloc(sizeof(*ent) * r->r_num_uri);
if ( NULL == ent )
goto out;
/* Create the trie index */
list_for_each_entry(u, &r->r_uri, u_list) {
ent[i].t_str.v_ptr = (const uint8_t *)u->u_uri;
ent[i].t_str.v_len = strlen(u->u_uri);
ent[i].t_oid = u->u_obj->o_oid;
// printf(" %d: %.*s (%d)\n", i,
// (int)ent[i].t_str.v_len,
// ent[i].t_str.v_ptr,
// ent[i].t_oid);
i++;
}
t = trie_new(ent, r->r_num_uri);
if ( NULL == t )
goto out_free;
printf("%s: index: trie=%"PRId64" bytes\n",
cmd, trie_trie_size(t));
r->r_trie = t;
r->r_trie_ent = ent;
/* Layout mime and redirect string tables */
off = sizeof(struct webroot_hdr) +
trie_trie_size(r->r_trie) +
sizeof(struct webroot_redirect) * r->r_num_redirect +
sizeof(struct webroot_file) * r->r_num_file;
list_for_each_entry(m, &r->r_mime_type, m_list) {
m->m_strtab_off = off;
off += strlen(m->m_type);
r->r_mimetab_sz += strlen(m->m_type);
}
r->r_redirtab_sz = 0;
list_for_each_entry(obj, &r->r_redirect, o_list) {
obj->o_u.redirect.strtab_off = off;
if ( obj->o_u.redirect.uri ) {
off += strlen(obj->o_u.redirect.uri);
r->r_redirtab_sz += strlen(obj->o_u.redirect.uri);
}
}
/* Calculate files size */
r->r_files_sz = 0;
list_for_each_entry(obj, &r->r_file, o_list) {
obj->o_u.file.off = off;
off += obj->o_u.file.size;
r->r_files_sz += obj->o_u.file.size;
}
/* success */
ret = 1;
goto out;
out_free:
free(ent);
out:
return ret;
}
/* the assumption here being that the webroot digest field is the same size
* as a SHA1 hash.
*/
static void etag(uint8_t digest[WEBROOT_DIGEST_LEN], uint8_t sha[20])
{
memcpy(digest, sha, 20);
}
static int write_file(struct webroot *r, struct object *f, fobuf_t out)
{
uint8_t buf[BUFFER_SIZE];
blk_SHA_CTX ctx;
uint8_t sha[20];
ssize_t ret;
uint64_t len;
int rc = 0;
int fd;
if ( f->o_u.file.content == FILE_PATH ) {
fd = open(f->o_u.file.u.path, O_RDONLY);
if ( fd < 0 ) {
fprintf(stderr, "%s: %s: open: %s\n",
cmd, f->o_u.file.u.path, os_err());
goto out;
}
}else if ( f->o_u.file.content == FILE_TMPCHUNK ) {
fd = fobuf_fd(r->r_tmpchunks);
if ( lseek(fd, f->o_u.file.u.tmpoff, SEEK_SET) < 0 ) {
fprintf(stderr, "%s: lseek: %s\n", cmd, os_err());
return 0;
}
}else{
abort();
}
blk_SHA1_Init(&ctx);
len = f->o_u.file.size;
again:
ret = read(fd, buf, (len > sizeof(buf)) ? sizeof(buf) : len);
if ( ret < 0 ) {
fprintf(stderr, "%s: %s: read: %s\n",
cmd, f->o_u.file.u.path, os_err());
goto out_close;
}
if ( ret && !fobuf_write(out, buf, ret) ) {
fprintf(stderr, "%s: %s: write: %s\n",
cmd, f->o_u.file.u.path, os_err());
goto out_close;
}
blk_SHA1_Update(&ctx, buf, ret);
len -= (size_t)ret;
if ( ret )
goto again;
blk_SHA1_Final(sha, &ctx);
etag(f->o_u.file.digest, sha);
if ( !len ) {
rc = 1;
}else{
/* should never happen to tmpchunks */
assert(f->o_u.file.content == FILE_PATH);
fprintf(stderr, "%s: %s size was modified during scan\n",
cmd, f->o_u.file.u.path);
}
out_close:
if ( f->o_u.file.content == FILE_PATH ) {
close(fd);
}
out:
return rc;
}
static int write_files(struct webroot *r, fobuf_t out)
{
struct object *obj;
/* make sure the tmpchunks data is actually flushed
* from buffers and in to the file, since the fobuf
* cache has no way of doing coherancy because we're
* reading with read(2) behind its back
*/
if ( !fobuf_flush(r->r_tmpchunks) )
return 0;
list_for_each_entry(obj, &r->r_file, o_list) {
if ( !write_file(r, obj, out) )
return 0;
}
return 1;
}
static int write_mimetab(struct webroot *r, fobuf_t out)
{
struct mime_type *m;
list_for_each_entry(m, &r->r_mime_type, m_list) {
if ( !fobuf_write(out, m->m_type, strlen(m->m_type)) )
return 0;
}
return 1;
}
static int write_redirtab(struct webroot *r, fobuf_t out)
{
struct object *obj;
list_for_each_entry(obj, &r->r_redirect, o_list) {
if ( NULL == obj->o_u.redirect.uri )
continue;
if ( !fobuf_write(out, obj->o_u.redirect.uri,
strlen(obj->o_u.redirect.uri)) )
return 0;
}
return 1;
}
static int write_header(struct webroot *r, fobuf_t out)
{
struct webroot_hdr hdr;
hdr.h_num_edges = trie_num_edges(r->r_trie);
hdr.h_num_redirect = r->r_num_redirect;
hdr.h_num_file = r->r_num_file;
hdr.h_strtab_sz = r->r_mimetab_sz +
r->r_redirtab_sz;
hdr.h_magic = WEBROOT_MAGIC;
hdr.h_vers = WEBROOT_CURRENT_VER;
/* provides simple way to map all index data */
hdr.h_files_begin = sizeof(struct webroot_hdr) +
trie_trie_size(r->r_trie) +
sizeof(struct webroot_redirect) * r->r_num_redirect +
sizeof(struct webroot_file) * r->r_num_file +
r->r_mimetab_sz +
r->r_redirtab_sz;
return fobuf_write(out, &hdr, sizeof(hdr));
}
static int write_redirect_objs(struct webroot *r, fobuf_t out)
{
struct webroot_redirect wr;
struct object *obj;
list_for_each_entry(obj, &r->r_redirect, o_list) {
if ( obj->o_u.redirect.uri ) {
wr.r_off = obj->o_u.redirect.strtab_off;
wr.r_len = strlen(obj->o_u.redirect.uri);
}else{
wr.r_off = WEBROOT_INVALID_REDIRECT;
wr.r_len = obj->o_u.redirect.code;
}
if ( !fobuf_write(out, &wr, sizeof(wr)) )
return 0;
}
return 1;
}
static uint32_t modified(time_t mtime)
{
return mtime;
}
static int write_file_objs(struct webroot *r, fobuf_t out)
{
struct webroot_file wf;
struct object *obj;
list_for_each_entry(obj, &r->r_file, o_list) {
wf.f_off = obj->o_u.file.off;
wf.f_len = obj->o_u.file.size;
wf.f_type = obj->o_u.file.type->m_strtab_off;
wf.f_type_len = strlen(obj->o_u.file.type->m_type);
wf.f_modified = modified(obj->o_u.file.mtime);
memcpy(wf.f_digest, obj->o_u.file.digest, WEBROOT_DIGEST_LEN);
if ( !fobuf_write(out, &wf, sizeof(wf)) )
return 0;
}
return 1;
}
static int write_etags(struct webroot *r, fobuf_t out)
{
int fd;
off_t off;
/* flush internal buffers, no fsync */
if ( !fobuf_flush(out) )
return 0;
/* seek back to start of file objects */
fd = fobuf_fd(out);
off = sizeof(struct webroot_hdr) +
trie_trie_size(r->r_trie) +
sizeof(struct webroot_redirect) * r->r_num_redirect;
if ( lseek(fd, off, SEEK_SET) < 0 ) {
fprintf(stderr, "%s: lseek: %s\n", cmd, os_err());
return 0;
}
return write_file_objs(r, out);
}
static int webroot_write(struct webroot *r, fobuf_t out)
{
if ( !write_header(r, out) )
return 0;
if ( !trie_write_trie(r->r_trie, out) )
return 0;
if ( !write_redirect_objs(r, out) )
return 0;
if ( !write_file_objs(r, out) )
return 0;
if ( !write_mimetab(r, out) )
return 0;
if ( !write_redirtab(r, out) )
return 0;
#if WRITE_FILES
printf("%s: Writing files\n", cmd);
if ( !write_files(r, out) )
return 0;
#endif
if ( !write_etags(r, out) )
return 0;
return 1;
}
static char *webroot_lookup(struct webroot *r, const char *path)
{
return path_splice(r->r_base, path);
}
static char *webroot_strdup(struct webroot *r, const char *str)
{
size_t len = strlen(str) + 1;
char *ret;
ret = strpool_alloc(r->r_str_mem, len);
if ( NULL == ret )
return ret;
memcpy(ret, str, len);
return ret;
}
static struct uri *webroot_link(struct webroot *r,
const char *link, struct object *target)
{
struct uri *uri;
if ( !strlen(link) )
link = "/";
uri = hgang_alloc0(r->r_uri_mem);
if ( NULL == uri )
return NULL;
uri->u_uri = webroot_strdup(r, link);
if ( NULL == r ) {
hgang_return(r->r_uri_mem, uri);
return NULL;
}
uri->u_obj = target;
list_add_tail(&uri->u_list, &r->r_uri);
r->r_num_uri++;
return uri;
}
static uint64_t webroot_output_size(struct webroot *r)
{
return sizeof(struct webroot_hdr) +
trie_trie_size(r->r_trie) +
sizeof(struct webroot_redirect) * r->r_num_redirect +
sizeof(struct webroot_file) * r->r_num_file +
r->r_mimetab_sz +
r->r_redirtab_sz +
r->r_files_sz;
}
static struct mime_type *mime_add(struct webroot *r, const char *mime)
{
struct mime_type *m;
list_for_each_entry(m, &r->r_mime_type, m_list) {
if ( !strcmp(m->m_type, mime) )
return m;
}
m = hgang_alloc0(r->r_mime_mem);
if ( NULL == m )
return NULL;
m->m_type = webroot_strdup(r, mime);
if ( NULL == m->m_type ) {
hgang_return(r->r_mime_mem, m);
return NULL;
}
list_add(&m->m_list, &r->r_mime_type);
return m;
}
static struct object *obj_redirect(struct webroot *r, const char *path)
{
struct object *obj;
obj = hgang_alloc0(r->r_obj_mem);
if ( NULL == obj )
return NULL;
obj->o_type = OBJ_TYPE_REDIRECT;
obj->o_u.redirect.uri = webroot_strdup(r, path);
if ( NULL == obj->o_u.redirect.uri ) {
hgang_return(r->r_obj_mem, obj);
return NULL;
}
list_add_tail(&obj->o_list, &r->r_redirect);
r->r_num_redirect++;
return obj;
}
static struct object *obj_code(struct webroot *r, int code)
{
struct object *obj;
obj = hgang_alloc0(r->r_obj_mem);
if ( NULL == obj )
return NULL;
obj->o_type = OBJ_TYPE_REDIRECT;
obj->o_u.redirect.code = code;
list_add_tail(&obj->o_list, &r->r_redirect);
r->r_num_redirect++;
return obj;
}
static struct object *obj_file(struct webroot *r,
dev_t dev,
ino_t ino,
time_t mtime,
uint64_t size)
{
struct object *obj;
/* TODO: use a hash table, benchmarked at causing 200ms
* slowdown for 5,000 files. Dwarfed by I/O and SHA1.
*/
list_for_each_entry(obj, &r->r_file, o_list) {
if ( dev == obj->o_u.file.dev &&
ino == obj->o_u.file.ino ) {
return obj;
}
}
obj = hgang_alloc0(r->r_obj_mem);
if ( NULL == obj )
return NULL;
obj->o_type = OBJ_TYPE_FILE;
obj->o_u.file.size = size;
obj->o_u.file.dev = dev;
obj->o_u.file.ino = ino;
obj->o_u.file.mtime = mtime;
obj->o_u.file.content = FILE_PATH;
list_add_tail(&obj->o_list, &r->r_file);
r->r_num_file++;
return obj;
}
static struct object *obj_file_path(struct webroot *r,
const char *path,
dev_t dev,
ino_t ino,
time_t mtime,
uint64_t size)
{
struct object *obj;
struct mime_type *m;
const char *mime;
obj = obj_file(r, dev, ino, mtime, size);
if ( NULL == obj )
return NULL;
obj->o_u.file.content = FILE_PATH;
obj->o_u.file.u.path = webroot_strdup(r, path);
if ( NULL == obj->o_u.file.u.path ) {
list_del(&obj->o_list);
hgang_return(r->r_obj_mem, obj);
return NULL;
}
mime = magic_file(r->r_magic, path);
m = mime_add(r, mime);
if ( NULL == m ) {
list_del(&obj->o_list);
hgang_return(r->r_obj_mem, obj);
return NULL;
}
obj->o_u.file.type = m;
return obj;
}
static struct object *obj_file_tmpchunks(struct webroot *r,
const char *mime,
dev_t dev,
ino_t ino,
time_t mtime)
{
struct object *obj;
struct mime_type *m;
obj = obj_file(r, dev, ino, mtime, 0);
if ( NULL == obj )
return NULL;
obj->o_u.file.content = FILE_TMPCHUNK;
obj->o_u.file.u.tmpoff = r->r_tmpoff;
m = mime_add(r, mime);
if ( NULL == m ) {
list_del(&obj->o_list);
hgang_return(r->r_obj_mem, obj);
return NULL;
}
obj->o_u.file.type = m;
return obj;
}
static int scan_item(struct webroot *r, const char *dir, const char *u);
static int scan_dir(struct webroot *r, const char *dir, const char *path)
{
struct dirent *e;
int ret = 0;
DIR *d;
d = opendir(path);
if ( NULL == d ) {
fprintf(stderr, "%s: %s: opendir: %s\n", cmd, path, os_err());
goto out;
}
/* TODO: sort entries by inode number for performance */
while( (e = readdir(d)) ) {
char *uri;
if ( !strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") )
continue;
if ( !dotfiles && e->d_name[0] == '.' )
continue;
uri = path_splice(dir, e->d_name);
ret = scan_item(r, dir, uri);
free(uri);
if ( !ret )
goto out;
}
closedir(d);
ret = 1;
out:
return ret;
}
static int try_index(struct webroot *r, const char *u,
const char *page, struct object **obj)
{
struct stat st;
char *uri, *ipath;
uri = path_splice(u, page);
if ( NULL == uri )
return 0;
ipath = webroot_lookup(r, uri);
free(uri);
if ( NULL == ipath )
return 0;
if ( stat(ipath, &st) ) {
if ( errno == ENOENT ) {
free(ipath);
*obj = NULL;
return 1;
}
fprintf(stderr, "%s: %s: stat: %s\n",
cmd, ipath, os_err());
free(ipath);
return 0;
}
if ( S_ISREG(st.st_mode) ) {
dprintf("index: %s -> %s\n", u, ipath);
*obj = obj_file_path(r, ipath, st.st_dev, st.st_ino,
st.st_mtime, st.st_size);
free(ipath);
if ( NULL == *obj )
return 0;
/* found it, OK */
return 1;
}
/* didn't find it but OK */
free(ipath);
*obj = NULL;
return 1;
}
_printf(2, 3) static void tc_printf(struct webroot *r, const char *fmt, ...)
{
static char *abuf;
static size_t abuflen;
int len;
va_list va;
char *new;
if ( NULL == r->r_tmpchunks ) {
/* callers MUST check that this didn't happen */
return;
}
again:
va_start(va, fmt);
len = vsnprintf(abuf, abuflen, fmt, va);
if ( len < 0 ) /* bug in old glibc */
len = 0;
if ( (size_t)len < abuflen )
goto done;
new = realloc(abuf, len + 1);
if ( new == NULL )
goto done;
abuf = new;
abuflen = len + 1;
goto again;
done:
/* if writes fail, we just get rid of all tmpchunks,
* caller must check and abort
*/
if ( !fobuf_write(r->r_tmpchunks, abuf, (size_t)len) ) {
int fd;
fprintf(stderr, "%s: fobuf_write: %s\n", cmd, os_err());
fd = fobuf_fd(r->r_tmpchunks);
fobuf_abort(r->r_tmpchunks);
close(fd);
r->r_tmpchunks = NULL;
}else{
r->r_tmpoff += (size_t)len;
}
va_end(va);
}
static struct object *index_dir(struct webroot *r, struct stat *st,
const char *path, const char *u)
{
struct object *obj;
struct dirent *e;
DIR *d;
printf("%s: indexing %s\n", cmd, path);
obj = obj_file_tmpchunks(r, "text/html", st->st_dev, st->st_ino,
st->st_mtime);
if ( NULL == obj )
return NULL;
tc_printf(r, "<!DOCTYPE HTML PUBLIC "
"\"-//W3C//DTD HTML 3.2 Final//EN\">");
tc_printf(r, "<html>\n\t<head><title>Index of %s</title>\t</head>\n",
u);
tc_printf(r, "<body><h1>Index of %s</h1>\n", u);
tc_printf(r, "</body>\n</html>");
d = opendir(path);
if ( NULL == d ) {
fprintf(stderr, "%s: %s: opendir: %s\n", cmd, path, os_err());
return NULL;
}
while( (e = readdir(d)) ) {
char *uri;
if ( !strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") )
continue;
if ( !dotfiles && e->d_name[0] == '.' )
continue;
uri = path_splice(u, e->d_name);
tc_printf(r, "<a href=\"%s\">%s</a><br>\n", uri, e->d_name);
free(uri);
}
closedir(d);