Repository navigation
Expand file tree
/
Copy pathunix.c
More file actions
1485 lines (1286 loc) · 34.2 KB
/
Copy pathunix.c
File metadata and controls
1485 lines (1286 loc) · 34.2 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
/* vere/unix.c
**
*/
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <uv.h>
#include <termios.h>
#include <term.h>
#include <errno.h>
#include <libgen.h>
#include <ftw.h>
#include "all.h"
#include "vere/vere.h"
/* _unix_down(): descend path.
*/
static c3_c*
_unix_down(c3_c* pax_c, c3_c* sub_c)
{
c3_w pax_w = strlen(pax_c);
c3_w sub_w = strlen(sub_c);
c3_c* don_c = c3_malloc(pax_w + sub_w + 2);
strncpy(don_c, pax_c, pax_w);
don_c[pax_w] = '/';
strncpy(don_c + pax_w + 1, sub_c, sub_w);
don_c[pax_w + 1 + sub_w] = '\0';
return don_c;
}
/* _unix_string_to_path(): convert c string to u3_noun path
*
* c string must begin with the pier path plus mountpoint
*/
static u3_noun
_unix_string_to_path_helper(c3_c* pax_c) {
c3_assert(pax_c[-1] == '/');
c3_c* end_w = strchr(pax_c, '/');
if ( !end_w ) {
end_w = strrchr(pax_c, '.');
if ( !end_w ) {
return u3nc(u3i_string(pax_c), u3_nul);
}
else {
return u3nt(u3i_bytes(end_w - pax_c, (c3_y*) pax_c),
u3i_string(end_w + 1),
u3_nul);
}
}
else {
return u3nc(u3i_bytes(end_w - pax_c, (c3_y*) pax_c),
_unix_string_to_path_helper(end_w + 1));
}
}
static u3_noun
_unix_string_to_path(c3_c* pax_c) {
pax_c += strlen(u3_Host.dir_c) + 1;
c3_c* pox_c = strchr(pax_c, '/');
if ( !pox_c ) {
pox_c = strchr(pax_c, '.');
if ( !pox_c ) {
return u3_nul;
}
else {
return u3nc(u3i_string(pox_c + 1), u3_nul);
}
}
else {
return _unix_string_to_path_helper(pox_c + 1);
}
}
/* _unix_rm_r_cb(): callback to delete individiual files/directories
*/
static c3_i
_unix_rm_r_cb(const c3_c* pax_c,
const struct stat* buf_u,
c3_i typeflag,
struct FTW* ftw_u)
{
switch ( typeflag ) {
default:
uL(fprintf(uH, "bad file type in rm_r: %s\r\n", pax_c));
break;
case FTW_F:
if ( 0 != unlink(pax_c) && ENOENT != errno ) {
uL(fprintf(uH, "error unlinking (in rm_r) %s: %s\n",
pax_c, strerror(errno)));
c3_assert(0);
}
break;
case FTW_D:
uL(fprintf(uH, "shouldn't have gotten pure directory: %s\r\n", pax_c));
break;
case FTW_DNR:
uL(fprintf(uH, "couldn't read directory: %s\r\n", pax_c));
break;
case FTW_NS:
uL(fprintf(uH, "couldn't stat path: %s\r\n", pax_c));
break;
case FTW_DP:
if ( 0 != rmdir(pax_c) && ENOENT != errno ) {
uL(fprintf(uH, "error rmdiring %s: %s\n", pax_c, strerror(errno)));
c3_assert(0);
}
break;
case FTW_SL:
uL(fprintf(uH, "got symbolic link: %s\r\n", pax_c));
break;
case FTW_SLN:
uL(fprintf(uH, "got nonexistent symbolic link: %s\r\n", pax_c));
break;
}
return 0;
}
/* _unix_rm_r(): rm -r directory
*/
static void
_unix_rm_r(c3_c* pax_c)
{
if ( 0 > nftw(pax_c, _unix_rm_r_cb, 100, FTW_DEPTH | FTW_PHYS )
&& ENOENT != errno) {
uL(fprintf(uH, "rm_r error on %s: %s\r\n", pax_c, strerror(errno)));
}
}
/* _unix_mkdir(): mkdir, asserting.
*/
static void
_unix_mkdir(c3_c* pax_c)
{
if ( 0 != mkdir(pax_c, 0755) && EEXIST != errno) {
uL(fprintf(uH, "error mkdiring %s: %s\n", pax_c, strerror(errno)));
c3_assert(0);
}
}
/* _unix_write_file_hard(): write to a file, overwriting what's there
*/
static c3_w
_unix_write_file_hard(c3_c* pax_c, u3_noun mim)
{
c3_i fid_i = open(pax_c, O_WRONLY | O_CREAT | O_TRUNC, 0666);
c3_w len_w, rit_w, siz_w, mug_w = 0;
c3_y* dat_y;
u3_noun dat = u3t(u3t(mim));
if ( fid_i < 0 ) {
uL(fprintf(uH, "error opening %s for writing: %s\r\n",
pax_c, strerror(errno)));
u3z(mim);
return 0;
}
siz_w = u3h(u3t(mim));
len_w = u3r_met(3, dat);
dat_y = c3_malloc(siz_w);
memset(dat_y, 0, siz_w);
u3r_bytes(0, len_w, dat_y, dat);
u3z(mim);
rit_w = write(fid_i, dat_y, siz_w);
if ( rit_w != siz_w ) {
uL(fprintf(uH, "error writing %s: %s\r\n",
pax_c, strerror(errno)));
mug_w = 0;
}
else {
mug_w = u3r_mug_bytes(dat_y, len_w);
}
close(fid_i);
free(dat_y);
return mug_w;
}
/* _unix_write_file_soft(): write to a file, not overwriting if it's changed
*/
static void
_unix_write_file_soft(u3_ufil* fil_u, u3_noun mim)
{
struct stat buf_u;
c3_i fid_i = open(fil_u->pax_c, O_RDONLY, 0644);
c3_ws len_ws, red_ws;
c3_w old_w;
c3_y* old_y;
if ( fid_i < 0 || fstat(fid_i, &buf_u) < 0 ) {
if ( ENOENT == errno ) {
goto _unix_write_file_soft_go;
}
else {
uL(fprintf(uH, "error opening file (soft) %s: %s\r\n",
fil_u->pax_c, strerror(errno)));
u3z(mim);
return;
}
}
len_ws = buf_u.st_size;
old_y = c3_malloc(len_ws);
red_ws = read(fid_i, old_y, len_ws);
if ( close(fid_i) < 0 ) {
uL(fprintf(uH, "error closing file (soft) %s: %s\r\n",
fil_u->pax_c, strerror(errno)));
}
if ( len_ws != red_ws ) {
if ( red_ws < 0 ) {
uL(fprintf(uH, "error reading file (soft) %s: %s\r\n",
fil_u->pax_c, strerror(errno)));
}
else {
uL(fprintf(uH, "wrong # of bytes read in file %s: %d %d\r\n",
fil_u->pax_c, len_ws, red_ws));
}
free(old_y);
u3z(mim);
return;
}
old_w = u3r_mug_bytes(old_y, len_ws);
if ( old_w != fil_u->gum_w ) {
fil_u->gum_w = u3r_mug(u3t(u3t(mim))); // XXX this might fail with
free(old_y); // trailing zeros
u3z(mim);
return;
}
free(old_y);
_unix_write_file_soft_go:
fil_u->gum_w = _unix_write_file_hard(fil_u->pax_c, mim);
}
static void
_unix_watch_dir(u3_udir* dir_u, u3_udir* par_u, c3_c* pax_c);
static void
_unix_watch_file(u3_ufil* fil_u, u3_udir* par_u, c3_c* pax_c);
/* _unix_get_mount_point(): retrieve or create mount point
*/
static u3_umon*
_unix_get_mount_point(u3_noun mon)
{
if ( c3n == u3ud(mon) ) {
c3_assert(!"mount point must be an atom");
u3z(mon);
return NULL;
}
c3_c* nam_c = u3r_string(mon);
u3_umon* mon_u;
for ( mon_u = u3_Host.unx_u.mon_u;
mon_u && 0 != strcmp(nam_c, mon_u->nam_c);
mon_u = mon_u->nex_u )
{
}
if ( !mon_u ) {
mon_u = malloc(sizeof(u3_umon));
mon_u->nam_c = nam_c;
mon_u->dir_u.dir = c3y;
mon_u->dir_u.dry = c3n;
mon_u->dir_u.pax_c = strdup(u3_Host.dir_c);
mon_u->dir_u.par_u = NULL;
mon_u->dir_u.nex_u = NULL;
mon_u->dir_u.kid_u = NULL;
mon_u->nex_u = u3_Host.unx_u.mon_u;
u3_Host.unx_u.mon_u = mon_u;
}
else {
free(nam_c);
}
u3z(mon);
return mon_u;
}
/* _unix_scan_mount_point(): scan unix for already-existing mount point
*/
static void
_unix_scan_mount_point(u3_umon* mon_u)
{
DIR* rid_u = opendir(mon_u->dir_u.pax_c);
if ( !rid_u ) {
uL(fprintf(uH, "error opening pier directory: %s: %s\r\n",
mon_u->dir_u.pax_c, strerror(errno)));
return;
}
c3_w len_w = strlen(mon_u->nam_c);
while ( 1 ) {
struct dirent ent_u;
struct dirent* out_u;
c3_w err_w;
if ( 0 != (err_w = readdir_r(rid_u, &ent_u, &out_u)) ) {
uL(fprintf(uH, "erroring loading pier directory %s: %s\r\n",
mon_u->dir_u.pax_c, strerror(errno)));
c3_assert(0);
}
else if ( !out_u ) {
break;
}
else if ( '.' == out_u->d_name[0] ) { // unnecessary, but consistency
continue;
}
else if ( 0 != strncmp(mon_u->nam_c, out_u->d_name, len_w) ) {
continue;
}
else {
c3_c* pax_c = _unix_down(mon_u->dir_u.pax_c, out_u->d_name);
struct stat buf_u;
if ( 0 != stat(pax_c, &buf_u) ) {
uL(fprintf(uH, "can't stat pier directory %s: %s\r\n",
mon_u->dir_u.pax_c, strerror(errno)));
free(pax_c);
continue;
}
if ( S_ISDIR(buf_u.st_mode) ) {
if ( out_u->d_name[len_w] != '\0' ) {
free(pax_c);
continue;
}
else {
u3_udir* dir_u = c3_malloc(sizeof(u3_udir));
_unix_watch_dir(dir_u, &mon_u->dir_u, pax_c);
}
}
else {
if ( '.' != out_u->d_name[len_w]
|| '\0' == out_u->d_name[len_w + 1]
|| '~' == out_u->d_name[strlen(out_u->d_name) - 1]
|| ('#' == out_u->d_name[0] &&
'#' == out_u->d_name[strlen(out_u->d_name) - 1])
) {
free(pax_c);
continue;
}
else {
u3_ufil* fil_u = c3_malloc(sizeof(u3_ufil));
_unix_watch_file(fil_u, &mon_u->dir_u, pax_c);
}
}
free(pax_c);
}
}
}
static u3_noun _unix_free_node(u3_unod* nod_u);
/* _unix_free_file(): free file, unlinking it
*/
static void
_unix_free_file(uv_handle_t* was_u)
{
u3_ufil* fil_u = (void*) was_u;
if ( 0 != unlink(fil_u->pax_c) && ENOENT != errno ) {
uL(fprintf(uH, "error unlinking %s: %s\n", fil_u->pax_c, strerror(errno)));
c3_assert(0);
}
free(fil_u->pax_c);
free(fil_u);
}
/* _unix_free_dir(): free directory, deleting everything within
*/
static void
_unix_free_dir(uv_handle_t* was_u)
{
u3_udir* dir_u = (void*) was_u;
_unix_rm_r(dir_u->pax_c);
if ( dir_u->kid_u ) {
fprintf(stderr, "don't kill me, i've got a family %s\r\n", dir_u->pax_c);
}
else {
// fprintf(stderr, "i'm a lone, lonely loner %s\r\n", dir_u->pax_c);
}
free(dir_u->pax_c);
free(dir_u); // XXX this might be too early, how do we
// know we've freed all the children?
// i suspect we should do this only if
// our kid list is empty
}
/* _unix_free_node(): free node, deleting everything within
*
* also deletes from parent list if in it
*/
static u3_noun
_unix_free_node(u3_unod* nod_u)
{
u3_noun can;
if ( nod_u->par_u ) {
u3_unod* don_u = nod_u->par_u->kid_u;
if ( !don_u ) {
}
else if ( nod_u == don_u ) {
nod_u->par_u->kid_u = nod_u->par_u->kid_u->nex_u;
}
else {
for ( ; don_u->nex_u && nod_u != don_u->nex_u; don_u = don_u->nex_u ) {
}
if ( don_u->nex_u ) {
don_u->nex_u = don_u->nex_u->nex_u;
}
}
}
if ( c3y == nod_u->dir ) {
can = u3_nul;
u3_unod* nud_u = ((u3_udir*) nod_u)->kid_u;
while ( nud_u ) {
u3_unod* nex_u = nud_u->nex_u;
can = u3kb_weld(_unix_free_node(nud_u), can);
nud_u = nex_u;
}
uv_close((uv_handle_t*)&nod_u->was_u, _unix_free_dir);
}
else {
can = u3nc(u3nc(_unix_string_to_path(nod_u->pax_c), u3_nul),
u3_nul);
uv_close((uv_handle_t*)&nod_u->was_u, _unix_free_file);
}
return can;
}
/* _unix_free_mount_point(): free mount point
*
* this process needs to happen in a very careful order. in particular,
* we must recurse before we get to the callback, so that libuv does all
* the child directories before it does us.
*
* tread carefully
*/
static void
_unix_free_mount_point(u3_umon* mon_u)
{
u3_unod* nod_u;
for ( nod_u = mon_u->dir_u.kid_u; nod_u; ) {
u3_unod* nex_u = nod_u->nex_u;
u3z(_unix_free_node(nod_u));
nod_u = nex_u;
}
free(mon_u->dir_u.pax_c);
free(mon_u->nam_c);
free(mon_u);
}
/* _unix_delete_mount_point(): remove mount point from list and free
*/
static void
_unix_delete_mount_point(u3_noun mon)
{
if ( c3n == u3ud(mon) ) {
c3_assert(!"mount point must be an atom");
u3z(mon);
return;
}
c3_c* nam_c = u3r_string(mon);
u3_umon* mon_u;
u3_umon* tem_u;
mon_u = u3_Host.unx_u.mon_u;
if ( !mon_u ) {
uL(fprintf(uH, "mount point already gone: %s\r\n", nam_c));
goto _delete_mount_point_out;
}
if ( 0 == strcmp(nam_c, mon_u->nam_c) ) {
u3_Host.unx_u.mon_u = mon_u->nex_u;
_unix_free_mount_point(mon_u);
goto _delete_mount_point_out;
}
for ( ;
mon_u->nex_u && 0 != strcmp(nam_c, mon_u->nex_u->nam_c);
mon_u = mon_u->nex_u )
{
}
if ( !mon_u->nex_u ) {
uL(fprintf(uH, "mount point already gone: %s\r\n", nam_c));
goto _delete_mount_point_out;
}
tem_u = mon_u->nex_u;
mon_u->nex_u = mon_u->nex_u->nex_u;
_unix_free_mount_point(tem_u);
_delete_mount_point_out:
free(nam_c);
u3z(mon);
}
/* _unix_time_cb: timer callback
*/
static void
_unix_time_cb(uv_timer_t* tim_u)
{
u3_lo_open();
{
u3_Host.unx_u.alm = c3n;
u3_Host.unx_u.dyr = c3y;
}
u3_lo_shut(c3y);
}
/* _unix_fs_event_cb(): filesystem event callback.
*/
static void
_unix_fs_event_cb(uv_fs_event_t* was_u,
const c3_c* pax_c,
c3_i evt_i,
c3_i sas_i)
{
// note that we're doing something tricky and weird here.
//
// * libuv passes around a pointer to a uv_fs_event_t
// * we define a struct that STARTS with a uv_fs_event_t and then has
// more fields after it
// * this is what we pass into libuv up top
// * this is what we get out of libuv down below
// * thus a cast is cool
u3_unod* nod_u = (u3_unod*) was_u;
while ( nod_u ) {
nod_u->dry = c3n;
nod_u = (u3_unod*) nod_u->par_u;
}
// we start a timer so that in 100 ms we check the fs.
// the extra time is so that the fs "settles down".
// vim, for example, tends to delete and re-add files
// for safety purposes.
if ( c3y == u3_Host.unx_u.alm ) {
uv_timer_stop(&u3_Host.unx_u.tim_u);
}
else {
u3_Host.unx_u.alm = c3y;
}
u3_Host.unx_u.dyr = c3n;
uv_timer_start(&u3_Host.unx_u.tim_u, _unix_time_cb, 100, 0);
}
/* _unix_watch_file(): initialize file
*/
static void
_unix_watch_file(u3_ufil* fil_u, u3_udir* par_u, c3_c* pax_c)
{
// initialize fil_u
fil_u->dir = c3n;
fil_u->dry = c3n;
fil_u->pax_c = c3_malloc(1 + strlen(pax_c));
strcpy(fil_u->pax_c, pax_c);
fil_u->par_u = par_u;
fil_u->nex_u = NULL;
fil_u->mug_w = 0;
fil_u->gum_w = 0;
if ( par_u ) {
fil_u->nex_u = par_u->kid_u;
par_u->kid_u = (u3_unod*) fil_u;
}
// stuff fil_u into libuv
// note that we're doing something tricky here
// see comment in _unix_fs_event_cb
c3_w ret_w = uv_fs_event_init(u3L, &fil_u->was_u);
if (0 != ret_w){
uL(fprintf(uH, "file event init: %s\n", uv_strerror(ret_w)));
c3_assert(0);
}
ret_w = uv_fs_event_start(&fil_u->was_u, _unix_fs_event_cb, pax_c, 0);
if ( 0 != ret_w ){
uL(fprintf(uH, "file event start %s: %s\n", fil_u->pax_c, uv_strerror(ret_w)));
c3_assert(0);
}
}
/* _unix_watch_dir(): initialize directory
*/
static void
_unix_watch_dir(u3_udir* dir_u, u3_udir* par_u, c3_c* pax_c)
{
// initialize dir_u
dir_u->dir = c3y;
dir_u->dry = c3n;
dir_u->pax_c = c3_malloc(1 + strlen(pax_c));
strcpy(dir_u->pax_c, pax_c);
dir_u->par_u = par_u;
dir_u->nex_u = NULL;
dir_u->kid_u = NULL;
if ( par_u ) {
dir_u->nex_u = par_u->kid_u;
par_u->kid_u = (u3_unod*) dir_u;
}
// stuff dir_u into libuv
// note that we're doing something tricky here
// see comment in _unix_fs_event_cb
c3_w ret_w = uv_fs_event_init(u3L, &dir_u->was_u);
if (0 != ret_w){
uL(fprintf(uH, "directory event init: %s\n", uv_strerror(ret_w)));
c3_assert(0);
}
ret_w = uv_fs_event_start(&dir_u->was_u, _unix_fs_event_cb, pax_c, 0);
if (0 != ret_w){
uL(fprintf(uH, "directory event start: %s\n", uv_strerror(ret_w)));
c3_assert(0);
}
}
/* _unix_create_dir(): create unix directory and watch it
*/
static void
_unix_create_dir(u3_udir* dir_u, u3_udir* par_u, u3_noun nam)
{
c3_c* nam_c = u3r_string(nam);
c3_w nam_w = strlen(nam_c);
c3_w pax_w = strlen(par_u->pax_c);
c3_c* pax_c = c3_malloc(pax_w + 1 + nam_w + 1);
strncpy(pax_c, par_u->pax_c, pax_w);
pax_c[pax_w] = '/';
strncpy(pax_c + pax_w + 1, nam_c, nam_w);
pax_c[pax_w + 1 + nam_w] = '\0';
free(nam_c);
u3z(nam);
_unix_mkdir(pax_c);
_unix_watch_dir(dir_u, par_u, pax_c);
}
static u3_noun _unix_update_node(u3_unod* nod_u);
/* _unix_update_file(): update file, producing list of changes
*
* when scanning through files, if dry, do nothing. otherwise, mark as
* dry, then check if file exists. if not, remove self from node list
* and add path plus sig to %into event. otherwise, read the file and
* get a mug checksum. if same as mug_w, move on. otherwise, overwrite
* mug_w with new mug and add path plus data to %into event.
*/
static u3_noun
_unix_update_file(u3_ufil* fil_u)
{
c3_assert( c3n == fil_u->dir );
if ( c3y == fil_u->dry ) {
return u3_nul;
}
fil_u->dry = c3y;
struct stat buf_u;
c3_i fid_i = open(fil_u->pax_c, O_RDONLY, 0644);
c3_ws len_ws, red_ws;
c3_y* dat_y;
if ( fid_i < 0 || fstat(fid_i, &buf_u) < 0 ) {
if ( ENOENT == errno ) {
return u3nc(u3nc(_unix_string_to_path(fil_u->pax_c), u3_nul), u3_nul);
}
else {
uL(fprintf(uH, "error opening file %s: %s\r\n",
fil_u->pax_c, strerror(errno)));
return u3_nul;
}
}
// So, if file gets deleted and then quickly re-added, like vim and
// other editors do, we lose the notification. This is a bad thing,
// so we always stop and restart the notification.
uv_fs_event_stop(&fil_u->was_u);
c3_w ret_w = uv_fs_event_start(&fil_u->was_u,
_unix_fs_event_cb,
fil_u->pax_c,
0);
if ( 0 != ret_w ){
uL(fprintf(uH, "update file event start: %s\n", uv_strerror(ret_w)));
c3_assert(0);
}
len_ws = buf_u.st_size;
dat_y = c3_malloc(len_ws);
red_ws = read(fid_i, dat_y, len_ws);
if ( close(fid_i) < 0 ) {
uL(fprintf(uH, "error closing file %s: %s\r\n",
fil_u->pax_c, strerror(errno)));
}
if ( len_ws != red_ws ) {
if ( red_ws < 0 ) {
uL(fprintf(uH, "error reading file %s: %s\r\n",
fil_u->pax_c, strerror(errno)));
}
else {
uL(fprintf(uH, "wrong # of bytes read in file %s: %d %d\r\n",
fil_u->pax_c, len_ws, red_ws));
}
free(dat_y);
return u3_nul;
}
else {
c3_w mug_w = u3r_mug_bytes(dat_y, len_ws);
if ( mug_w == fil_u->mug_w ) {
free(dat_y);
return u3_nul;
}
else if ( mug_w == fil_u->gum_w ) {
fil_u->mug_w = mug_w;
free(dat_y);
return u3_nul;
}
else {
fil_u->mug_w = mug_w;
u3_noun pax = _unix_string_to_path(fil_u->pax_c);
u3_noun mim = u3nt(c3__text, u3i_string("plain"), u3_nul);
u3_noun dat = u3nt(mim, len_ws, u3i_bytes(len_ws, dat_y));
free(dat_y);
return u3nc(u3nt(pax, u3_nul, dat), u3_nul);
}
}
}
/* _unix_update_dir(): update directory, producing list of changes
*
* when changing this, consider whether to also change
* _unix_initial_update_dir()
*/
static u3_noun
_unix_update_dir(u3_udir* dir_u)
{
u3_noun can = u3_nul;
c3_assert( c3y == dir_u->dir );
if ( c3y == dir_u->dry ) {
return u3_nul;
}
dir_u->dry = c3y;
// Check that old nodes are still there
u3_unod* nod_u = dir_u->kid_u;
if ( nod_u ) {
while ( nod_u ) {
if ( c3y == nod_u->dry ) {
nod_u = nod_u->nex_u;
}
else {
if ( c3y == nod_u->dir ) {
DIR* red_u = opendir(nod_u->pax_c);
if ( 0 == red_u ) {
u3_unod* nex_u = nod_u->nex_u;
can = u3kb_weld(_unix_free_node(nod_u), can);
nod_u = nex_u;
}
else {
closedir(red_u);
nod_u = nod_u->nex_u;
}
}
else {
struct stat buf_u;
c3_i fid_i = open(nod_u->pax_c, O_RDONLY, 0644);
if ( (fid_i < 0) || (fstat(fid_i, &buf_u) < 0) ) {
if ( ENOENT != errno ) {
uL(fprintf(uH, "_unix_update_dir: error opening file %s: %s\r\n",
nod_u->pax_c, strerror(errno)));
}
u3_unod* nex_u = nod_u->nex_u;
can = u3kb_weld(_unix_free_node(nod_u), can);
nod_u = nex_u;
}
else {
if ( close(fid_i) < 0 ) {
uL(fprintf(uH, "_unix_update_dir: error closing file %s: %s\r\n",
nod_u->pax_c, strerror(errno)));
}
nod_u = nod_u->nex_u;
}
}
}
}
}
// Check for new nodes
DIR* rid_u = opendir(dir_u->pax_c);
if ( !rid_u ) {
uL(fprintf(uH, "error opening directory %s: %s\r\n",
dir_u->pax_c, strerror(errno)));
c3_assert(0);
}
while ( 1 ) {
struct dirent ent_u;
struct dirent* out_u;
c3_w err_w;
if ( (err_w = readdir_r(rid_u, &ent_u, &out_u)) != 0 ) {
uL(fprintf(uH, "error loading directory %s: %s\r\n",
dir_u->pax_c, strerror(err_w)));
c3_assert(0);
}
else if ( !out_u ) {
break;
}
else if ( '.' == out_u->d_name[0] ) {
continue;
}
else {
c3_c* pax_c = _unix_down(dir_u->pax_c, out_u->d_name);
struct stat buf_u;
if ( 0 != stat(pax_c, &buf_u) ) {
uL(fprintf(uH, "can't stat %s: %s\r\n", pax_c, strerror(errno)));
free(pax_c);
continue;
}
else {
u3_unod* nod_u;
for ( nod_u = dir_u->kid_u; nod_u; nod_u = nod_u->nex_u ) {
if ( 0 == strcmp(pax_c, nod_u->pax_c) ) {
if ( S_ISDIR(buf_u.st_mode) ) {
if ( c3n == nod_u->dir ) {
uL(fprintf(uH, "not a directory: %s\r\n", nod_u->pax_c));
c3_assert(0);
}
}
else {
if ( c3y == nod_u->dir ) {
uL(fprintf(uH, "not a file: %s\r\n", nod_u->pax_c));
c3_assert(0);
}
}
break;
}
}
if ( !nod_u ) {
if ( !S_ISDIR(buf_u.st_mode) ) {
if ( !strchr(out_u->d_name,'.')
|| '~' == out_u->d_name[strlen(out_u->d_name) - 1]
|| ('#' == out_u->d_name[0] &&
'#' == out_u->d_name[strlen(out_u->d_name) - 1])
) {
free(pax_c);
continue;
}
u3_ufil* fil_u = c3_malloc(sizeof(u3_ufil));
_unix_watch_file(fil_u, dir_u, pax_c);
}
else {
u3_udir* dis_u = c3_malloc(sizeof(u3_udir));
_unix_watch_dir(dis_u, dir_u, pax_c);
can = u3kb_weld(_unix_update_dir(dis_u), can); // XXX unnecessary?
}
}
}
free(pax_c);
}
}
if ( closedir(rid_u) < 0 ) {
uL(fprintf(uH, "error closing directory %s: %s\r\n",
dir_u->pax_c, strerror(errno)));
}
if ( !dir_u->kid_u ) {
return u3kb_weld(_unix_free_node((u3_unod*) dir_u), can);
}
// get change list
for ( nod_u = dir_u->kid_u; nod_u; nod_u = nod_u->nex_u ) {
can = u3kb_weld(_unix_update_node(nod_u), can);
}
return can;
}
/* _unix_update_node(): update node, producing list of changes
*/
static u3_noun
_unix_update_node(u3_unod* nod_u)
{
if ( c3y == nod_u->dir ) {
return _unix_update_dir((void*)nod_u);
}
else {
return _unix_update_file((void*)nod_u);
}
}
/* _unix_update_mount(): update mount point
*/
static void
_unix_update_mount(u3_umon* mon_u, u3_noun all)
{
if ( c3n == mon_u->dir_u.dry ) {
u3_noun can = u3_nul;
u3_unod* nod_u;
for ( nod_u = mon_u->dir_u.kid_u; nod_u; nod_u = nod_u->nex_u ) {
can = u3kb_weld(_unix_update_node(nod_u), can);
}
u3v_plan(u3nq(u3_blip, c3__sync, u3k(u3A->sen), u3_nul),
u3nq(c3__into, u3i_string(mon_u->nam_c), all, can));
}
}
/* _unix_initial_update_file(): read file, but don't watch
*/
static u3_noun
_unix_initial_update_file(c3_c* pax_c)
{
struct stat buf_u;
c3_i fid_i = open(pax_c, O_RDONLY, 0644);
c3_ws len_ws, red_ws;
c3_y* dat_y;
if ( fid_i < 0 || fstat(fid_i, &buf_u) < 0 ) {
if ( ENOENT == errno ) {
return u3_nul;
}
else {
uL(fprintf(uH, "error opening initial file %s: %s\r\n",
pax_c, strerror(errno)));
return u3_nul;
}
}
len_ws = buf_u.st_size;
dat_y = c3_malloc(len_ws);
red_ws = read(fid_i, dat_y, len_ws);
if ( close(fid_i) < 0 ) {
uL(fprintf(uH, "error closing initial file %s: %s\r\n",
pax_c, strerror(errno)));
}
if ( len_ws != red_ws ) {
if ( red_ws < 0 ) {
uL(fprintf(uH, "error reading initial file %s: %s\r\n",
pax_c, strerror(errno)));