-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysif.c
3190 lines (2757 loc) · 84.6 KB
/
sysif.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
/* Copyright 2010-2024
* Kaz Kylheku <kaz@kylheku.com>
* Vancouver, Canada
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define UTF8_DECL_OPENDIR
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <wchar.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include "config.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_FCNTL
#include <fcntl.h>
#endif
#if HAVE_SYS_WAIT
#include <sys/wait.h>
#endif
#if HAVE_SYS_STAT
#include <sys/stat.h>
#endif
#if HAVE_WINDOWS_H
#include <windows.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_TIME
#include <sys/time.h>
#endif
#if HAVE_SYS_SYSMACROS_H
#include <sys/sysmacros.h>
#endif
#if HAVE_POLL
#include <poll.h>
#endif
#if HAVE_PWUID
#include <pwd.h>
#endif
#if HAVE_GRGID
#include <grp.h>
#endif
#if HAVE_FNMATCH
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <fnmatch.h>
#endif
#if HAVE_UNAME
#include <sys/utsname.h>
#endif
#if HAVE_DLOPEN
#include <dlfcn.h>
#endif
#if HAVE_CRYPT_R
#include <crypt.h>
#endif
#if HAVE_UTIME
#include <utime.h>
#endif
#if HAVE_RLIMIT
#include <sys/resource.h>
#endif
#include "alloca.h"
#include "lib.h"
#include "stream.h"
#include "hash.h"
#include "signal.h"
#include "utf8.h"
#include "unwind.h"
#include "gc.h"
#include "eval.h"
#include "args.h"
#include "struct.h"
#include "itypes.h"
#include "txr.h"
#include "sysif.h"
#if CONFIG_LARGE_FILE_OFFSET
#include "arith.h"
#endif
#ifndef DT_DIR
#undef DT_UNKNOWN
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4
#define DT_BLK 6
#define DT_REG 8
#define DT_LNK 10
#define DT_SOCK 12
#endif
val stat_s;
val dev_k, ino_k, mode_k, nlink_k, uid_k;
val gid_k, rdev_k, size_k, blksize_k, blocks_k;
val atime_k, mtime_k, ctime_k;
val dev_s, ino_s, mode_s, nlink_s, uid_s;
val gid_s, rdev_s, size_s, blksize_s, blocks_s;
val atime_s, mtime_s, ctime_s;
val atime_nsec_s, mtime_nsec_s, ctime_nsec_s;
val path_s, dir_s, dirent_s;
val child_env_s;
#if HAVE_PWUID || HAVE_GRGID
val passwd_s;
#endif
#if HAVE_PWUID
val gecos_s, shell_s;
#endif
#if HAVE_GRGID
val group_s, mem_s;
#endif
#if HAVE_UNAME
val utsname_s, sysname_s, nodename_s, release_s, version_s, machine_s;
val domainname_s;
#endif
#if HAVE_FCNTL
val flock_s, type_s, whence_s, start_s, len_s, pid_s;
#endif
#if HAVE_DLOPEN
val dlhandle_s, dlsym_s;
#endif
val rlim_s, cur_s, max_s;
static val rlim_st;
struct cobj_class *dir_cls;
static val at_exit_list;
static val dirent_st;
static val env_list, env_hash;
static val errno_wrap(val newval)
{
val self = lit("errno");
val oldval = num(errno);
if (default_null_arg(newval))
errno = c_num(newval, self);
return oldval;
}
val errno_to_str(int eno)
{
#if HAVE_STRERROR_POSIX
char buf[128];
return strerror_r(eno, buf, sizeof buf) >= 0 ? string_utf8(buf) : nil;
#elif HAVE_STRERROR_GNU
char buf[128];
return string_utf8(strerror_r(eno, buf, sizeof buf));
#else
return string_utf8(strerror(eno));
#endif
}
static val strerror_wrap(val errnum)
{
val self = lit("strerror");
int eno = c_int(errnum, self);
return errno_to_str(eno);
}
#if HAVE_STRSIGNAL
static val strsignal_wrap(val signum)
{
val self = lit("strsignal");
int sig = c_int(signum, self);
const char *str = strsignal(sig);
return if3(str,
string_utf8(strsignal(sig)),
format(nil, lit("Unknown signal %s"), signum, nao));
}
#endif
#if HAVE_DAEMON
static val daemon_wrap(val nochdir, val noclose)
{
int result = daemon(nochdir ? 1 : 0, noclose ? 1 : 0);
return result == 0 ? t : nil;
}
#endif
val exit_wrap(val status)
{
val self = lit("exit");
int stat;
if missingp(status)
stat = EXIT_SUCCESS;
else if (status == nil)
stat = EXIT_FAILURE;
else if (status == t)
stat = EXIT_SUCCESS;
else
stat = c_num(status, self);
exit(stat);
/* notreached */
return nil;
}
val at_exit_call(val func)
{
push(func, &at_exit_list);
return func;
}
val at_exit_do_not_call(val func)
{
val old = at_exit_list;
at_exit_list = remq(func, old, nil);
return tnil(old != at_exit_list);
}
static void at_exit_handler(void)
{
val iter;
for (iter = at_exit_list; iter; iter = cdr(iter))
funcall(car(iter));
}
static val abort_wrap(void)
{
abort();
}
val usleep_wrap(val usec)
{
val self = lit("usleep");
val retval;
cnum u = c_num(usec, self);
sig_save_enable;
#if HAVE_POSIX_NANOSLEEP
{
struct timespec ts;
ts.tv_sec = u / 1000000;
ts.tv_nsec = (u % 1000000) * 1000;
retval = if3(nanosleep(&ts, 0) == 0, t, nil);
}
#elif HAVE_POSIX_SLEEP && HAVE_POSIX_USLEEP
retval = if2(sleep(u / 1000000) == 0 &&
usleep(u % 1000000) == 0, t);
#elif HAVE_WINDOWS_H
Sleep(u / 1000);
retval = t;
#else
#error port me!
#endif
sig_restore_enable;
return retval;
}
#if HAVE_UNISTD_H
static val getpid_wrap(void)
{
return num(getpid());
}
#if HAVE_GETPPID
static val getppid_wrap(void)
{
return num(getppid());
}
#endif
#endif
val env(void)
{
if (env_list) {
return env_list;
} else {
list_collect_decl (out, ptail);
#if HAVE_ENVIRON
extern char **environ;
char **iter = environ;
for (; *iter != 0; iter++)
ptail = list_collect(ptail, string_utf8(*iter));
return env_list = out;
#elif HAVE_GETENVIRONMENTSTRINGS
wchar_t *env = GetEnvironmentStringsW();
wchar_t *iter = env;
if (iter == 0)
oom();
for (; *iter; iter += wcslen(iter) + 1)
ptail = list_collect(ptail, string(iter));
FreeEnvironmentStringsW(env);
return env_list = out;
#else
uw_throwf(error_s, lit("environment strings not available"), nao);
#endif
}
}
val replace_env(val env_list)
{
#if HAVE_ENVIRON && HAVE_SETENV
val self = lit("replace-env");
extern char **environ;
val iter;
static char *empty_env[1];
environ = empty_env;
for (iter = env_list; iter; iter = cdr(iter)) {
const wchar_t *pair = c_str(car(iter), self);
char *pair8 = utf8_dup_to(pair);
char *eq = strchr(pair8, '=');
int res;
if (eq != 0) {
char *name = chk_substrdup_utf8(pair8, 0, eq - pair8);
res = setenv(name, eq + 1, 1);
free(name);
} else {
res = setenv(pair8, "", 1);
}
free(pair8);
if (res < 0)
uw_ethrowf(system_error_s, lit("~a: setenv failed: ~d/~s"),
self, num(errno), errno_to_str(errno), nao);
}
return env_list;
#else
uw_throwf(error_s, lit("environ mechanism not available"), nao);
#endif
}
static val get_env_hash(void)
{
if (env_hash) {
return env_hash;
} else {
val env_strings = env();
val hash = make_hash(hash_weak_none, t);
for (; env_strings; env_strings = cdr(env_strings)) {
val estr = car(env_strings);
val eqpos = break_str(estr, lit("="));
val key = sub(estr, 0, eqpos);
val val = sub(estr, succ(eqpos), t);
sethash(hash, key, val);
}
if (!opt_compat || opt_compat > 244)
env_hash = hash;
return hash;
}
}
val errno_to_file_error(int err)
{
switch (err) {
#ifdef ENOENT
case ENOENT: return path_not_found_s;
#endif
#ifdef EEXIST
case EEXIST: return path_exists_s;
#endif
#ifdef EPERM
case EPERM: return path_permission_s;
#endif
#ifdef EACCES
case EACCES: return path_permission_s;
#endif
default: return file_error_s;
}
}
#if HAVE_MKDIR
static val mkdir_wrap(val path, val mode)
{
val self = lit("mkdir");
cnum cmode = c_num(default_arg(mode, num_fast(0777)), self);
char *u8path = utf8_dup_to(c_str(path, self));
int err = mkdir(u8path, cmode);
free(u8path);
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("mkdir ~a: ~d/~s"),
path, num(eno), errno_to_str(eno), nao);
}
return t;
}
#elif HAVE_WINDOWS_H
static val mkdir_wrap(val path, val mode)
{
int err = _wmkdir(c_str(path, self));
(void) mode;
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("mkdir ~a: ~d/~s"),
path, num(eno), errno_to_str(eno), nao);
}
return t;
}
#endif
#if HAVE_CHMOD || HAVE_CHOWN || HAVE_FCHDIR || HAVE_SYS_STAT || HAVE_FILE_STAMP_CHANGE
static int get_fd(val stream, val self)
{
val fd_in = if3(integerp(stream), stream, stream_fd(stream));
if (stream && !fd_in)
uw_throwf(file_error_s,
lit("~a: stream ~s has no :fd property"),
self, stream, nao);
if (!stream)
uw_throwf(file_error_s,
lit("~a: ~s isn't a stream object"),
self, stream, nao);
return c_int(fd_in, self);
}
#endif
#if HAVE_SYS_STAT
static int do_stat(val wpath, struct stat *buf, val self)
{
if (stringp(wpath)) {
char *path = utf8_dup_to(c_str(wpath, self));
int res = stat(path, buf);
free(path);
return res;
} else {
val self = lit("stat");
int fd = get_fd(wpath, self);
return fstat(fd, buf);
}
}
#ifdef S_IFLNK
static int do_lstat(val wpath, struct stat *buf, val self)
{
char *path = utf8_dup_to(c_str(wpath, self));
int res = lstat(path, buf);
free(path);
return res;
}
#else
#define do_lstat do_stat
#endif
#endif
#if HAVE_MKDIR || HAVE_WINDOWS_H
static val mkdir_nothrow_exists(val path, val mode, val self)
{
val ret = t;
uw_catch_begin(cons(file_error_s, nil), esym, eobj);
ret = mkdir_wrap(path, mode);
uw_catch (esym, eobj) {
switch (errno) {
case EACCES:
case EPERM:
ret = num(errno);
break;
case EEXIST:
ret = nil;
#if HAVE_SYS_STAT
{
struct stat st;
int err = do_stat(path, &st, self);
if (err == 0 && !S_ISDIR(st.st_mode))
ret = num(EEXIST);
}
#endif
break;
default:
uw_throw(esym, eobj);
}
}
uw_unwind;
uw_catch_end;
return ret;
}
static val ensure_dir(val path, val mode)
{
val self = lit("ensure-dir");
#if HAVE_WINDOWS_H
val sep = lit("\\");
val sep_set = lit("\\/");
#else
val sep = lit("/");
val sep_set = lit("/");
#endif
val split_path = split_str_set(path, sep_set);
val partial_path = pop(&split_path);
val ret = t;
for (;;) {
if (length(partial_path) != zero)
ret = mkdir_nothrow_exists(partial_path, mode, self);
if (!split_path)
break;
partial_path = scat3(partial_path, sep, pop(&split_path));
}
if (integerp(ret)) {
int eno = c_num(ret, self);
uw_ethrowf(errno_to_file_error(eno),
lit("ensure-dir: ~a: ~d/~s"), path, ret,
errno_to_str(eno), nao);
}
return ret;
}
#endif
#if HAVE_UNISTD_H
static val chdir_wrap(val path)
{
val self = lit("chdir");
int err;
#if HAVE_FCHDIR
if (!stringp(path)) {
int fd = get_fd(path, self);
err = fchdir(fd);
} else
#endif
{
char *u8path = utf8_dup_to(c_str(path, self));
err = chdir(u8path);
free(u8path);
}
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("chdir ~a: ~d/~s"),
path, num(eno), errno_to_str(eno), nao);
}
return t;
}
val getcwd_wrap(void)
{
size_t guess = 256;
for (;;) {
char *u8buf = coerce(char *, chk_malloc(guess));
if (getcwd(u8buf, guess) == 0) {
int eno = errno;
free(u8buf);
if (eno != ERANGE) {
uw_ethrowf(errno_to_file_error(eno), lit("getcwd: ~d/~s"),
num(errno), errno_to_str(errno), nao);
}
if (2 * guess > guess)
guess *= 2;
else
uw_ethrowf(file_error_s, lit("getcwd: weird problem"), nao);
} else {
val out = string_utf8(u8buf);
free(u8buf);
return out;
}
}
}
static val rmdir_wrap(val path)
{
val self = lit("rmdir");
char *u8path = utf8_dup_to(c_str(path, self));
int err = rmdir(u8path);
free(u8path);
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("rmdir ~a: ~d/~s"),
path, num(eno), errno_to_str(eno), nao);
}
return t;
}
#endif
#if HAVE_MAKEDEV
static val makedev_wrap(val major, val minor)
{
val self = lit("makedev");
return num(makedev(c_num(major, self), c_num(minor, self)));
}
static val minor_wrap(val dev)
{
val self = lit("minor");
return num(minor(c_num(dev, self)));
}
static val major_wrap(val dev)
{
val self = lit("major");
return num(major(c_num(dev, self)));
}
#endif
#if HAVE_MKNOD
static val mknod_wrap(val path, val mode, val dev)
{
val self = lit("mknod");
cnum cmode = c_num(mode, self);
cnum cdev = c_num(default_arg(dev, zero), self);
char *u8path = utf8_dup_to(c_str(path, self));
int err = mknod(u8path, cmode, cdev);
free(u8path);
if (err < 0) {
int eno = errno;
#if HAVE_MAKEDEV
uw_ethrowf(errno_to_file_error(eno), lit("mknod ~a ~a ~a (~d:~d): ~d/~s"),
path, mode, dev, major_wrap(dev), minor_wrap(dev), num(eno),
errno_to_str(eno), nao);
#else
uw_ethrowf(errno_to_file_error(eno), lit("mknod ~a ~a ~a: ~d/~s"),
path, mode, dev, num(eno),
errno_to_str(eno), nao);
#endif
}
return t;
}
#endif
#if HAVE_MKFIFO
static val mkfifo_wrap(val path, val mode)
{
val self = lit("mkfifo");
cnum cmode = c_num(mode, self);
char *u8path = utf8_dup_to(c_str(path, self));
int err = mkfifo(u8path, cmode);
free(u8path);
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("mknod ~a ~a: ~d/~s"),
path, mode, num(eno),
errno_to_str(eno), nao);
}
return t;
}
#endif
#if HAVE_CHMOD
#define CHM_O 4
#define CHM_G 2
#define CHM_U 1
enum chm_state { chm_who, chm_perm, chm_nxtop, chm_comma };
enum chm_op { chm_add, chm_sub, chm_set };
static val chmod_wrap(val target, val mode)
{
val self = lit("chmod");
cnum cmode = 0;
int err = 0;
char *u8path = if3(stringp(target), utf8_dup_to(c_str(target, self)), 0);
int fd = if3(u8path, -1, get_fd(target, self));
if (integerp(mode)) {
cmode = c_num(mode, self);
} else if (stringp(mode)) {
#if HAVE_SYS_STAT
struct stat st;
unsigned who = 0;
enum chm_state cs = chm_who;
enum chm_op op = chm_add;
if (u8path)
err = stat(u8path, &st);
else
err = fstat(fd, &st);
if (err == 0) {
const wchar_t *cm = c_str(mode, self);
wchar_t ch;
mode_t srcm = 0, oldm = st.st_mode;
cmode = oldm;
while ((ch = *cm++) != 0) {
switch (cs) {
case chm_who:
switch (ch) {
case 'u': who |= CHM_U; continue;
case 'g': who |= CHM_G; continue;
case 'o': who |= CHM_O; continue;
case 'a': who |= CHM_U | CHM_G | CHM_O; continue;
case '+': op = chm_add; cs = chm_perm; continue;
case '-': op = chm_sub; cs = chm_perm; continue;
case '=': op = chm_set; cs = chm_perm; break;
default:
goto inval;
}
break;
case chm_nxtop:
srcm = 0;
switch (ch) {
case '+': op = chm_add; cs = chm_perm; continue;
case '-': op = chm_sub; cs = chm_perm; continue;
case '=': op = chm_set; cs = chm_perm; break;
default: goto perm;
}
break;
perm:
case chm_perm:
switch (ch) {
case 'u': srcm |= (oldm & 0700) >> 6; cs = chm_comma; break;
case 'g': srcm |= (oldm & 0070) >> 3; cs = chm_comma; break;
case 'o': srcm |= (oldm & 0007); cs = chm_comma; break;
case 'r': srcm |= 4; cs = chm_nxtop; break;
case 'w': srcm |= 2; cs = chm_nxtop; break;
case 'x': srcm |= 1; cs = chm_nxtop; break;
case 's': srcm |= 010; cs = chm_nxtop; break;
case 't': srcm |= 020; cs = chm_nxtop; break;
case 'X': srcm |= ((cmode & 0111) != 0 ||
S_ISDIR(cmode)); cs = chm_nxtop; break;
case ',': goto nextmode;
default:
goto inval;
}
break;
case chm_comma:
if (ch != ',')
goto inval;
nextmode:
srcm = 0; who = 0; oldm = cmode; cs = chm_who;
continue;
}
{
mode_t bits = 0;
mode_t mask = 0;
int implicit_all = (who == 0);
if ((srcm & 020))
bits |= S_ISVTX;
if (implicit_all || (who & CHM_U) != 0) {
mask |= (0700 | S_ISUID);
if ((srcm & 010))
bits |= S_ISUID;
bits |= (srcm & 7) << 6;
}
if (implicit_all || (who & CHM_G) != 0) {
mask |= (0070 | S_ISGID);
if ((srcm & 010))
bits |= S_ISGID;
bits |= (srcm & 7) << 3;
}
if (implicit_all || (who & CHM_O) != 0) {
mask |= 0007;
bits |= (srcm & 7);
}
if (implicit_all) {
mode_t um = umask(0777);
umask(um);
bits &= ~um;
}
switch (op) {
case chm_add: cmode |= bits; break;
case chm_sub: cmode &= ~bits; break;
case chm_set:
if (cs == chm_perm) {
oldm = cmode;
cmode &= ~mask;
if (implicit_all || (who & CHM_O) != 0)
cmode &= ~S_ISVTX; /* GNU Coreutils 8.28 chmod behavior */
}
cmode |= bits;
break;
}
}
}
if (cs == chm_who)
goto inval;
}
#else
free(u8path);
uw_throwf(file_error_s, lit("~s: ~s mode requires stat"),
self, mode, nao);
#endif
} else {
inval:
free(u8path);
uw_throwf(file_error_s, lit("~s: invalid mode ~s"),
self, mode, nao);
}
if (err == 0) {
if (u8path) {
err = chmod(u8path, cmode);
} else {
int fd = get_fd(target, self);
err = fchmod(fd, cmode);
}
}
free(u8path);
if (err < 0) {
int eno = errno;
val error = errno_to_file_error(eno);
val errstr = errno_to_str(eno);
if (stringp(mode))
uw_ethrowf(error, lit("~a ~a ~a: ~d/~s"),
self, target, mode, num(eno), errstr, nao);
else
uw_ethrowf(error, lit("~a ~a #o~o: ~d/~s"),
self, target, mode, num(eno), errstr, nao);
}
return t;
}
#endif
#if HAVE_CHOWN
static val do_chown(val target, val uid, val gid, val link_p, val self)
{
cnum cuid = c_num(uid, self);
cnum cgid = c_num(gid, self);
int err;
if (stringp(target)) {
char *u8path = utf8_dup_to(c_str(target, self));
err = if3(link_p, lchown, chown)(u8path, cuid, cgid);
free(u8path);
} else {
int fd = get_fd(target, self);
err = fchown(fd, cuid, cgid);
}
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("~a ~a ~a ~a: ~d/~s"),
self, target, uid, gid, num(eno),
errno_to_str(eno), nao);
}
return t;
}
static val chown_wrap(val target, val uid, val gid)
{
return do_chown(target, uid, gid, nil, lit("chown"));
}
static val lchown_wrap(val target, val uid, val gid)
{
return do_chown(target, uid, gid, t, lit("lchown"));
}
#endif
#if HAVE_SYMLINK
static val symlink_wrap(val target, val to)
{
val self = lit("symlink");
const wchar_t *wtarget = c_str(target, self);
const wchar_t *wto = c_str(to, self);
char *u8target = utf8_dup_to(wtarget);
char *u8to = utf8_dup_to(wto);
int err = symlink(u8target, u8to);
free(u8target);
free(u8to);
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("symlink ~a ~a: ~d/~s"),
target, to, num(eno), errno_to_str(eno), nao);
}
return t;
}
static val link_wrap_common(val target, val to, val follow_link, val self)
{
const wchar_t *wtarget = c_str(target, self);
const wchar_t *wto = c_str(to, self);
char *u8target = utf8_dup_to(wtarget);
char *u8to = utf8_dup_to(wto);
#if HAVE_LINKAT
int err = linkat(AT_FDCWD, u8target, AT_FDCWD, u8to,
if3(follow_link, AT_SYMLINK_FOLLOW, 0));
#else
int err = link(u8target, u8to);
(void) follow_link;
#endif
free(u8target);
free(u8to);
if (err < 0) {
int eno = errno;
uw_ethrowf(errno_to_file_error(eno), lit("link ~a ~a: ~d/~s"),
target, to, num(eno), errno_to_str(eno), nao);
}
return t;
}
static val link_wrap(val target, val to)
{
return link_wrap_common(target, to, nil, lit("link"));
}
#if HAVE_LINKAT
static val rlink_wrap(val target, val to)
{
return link_wrap_common(target, to, t, lit("rlink"));
}
#endif
static val readlink_wrap(val path)
{
val self = lit("readlink");
char *u8path = utf8_dup_to(c_str(path, self));