-
Notifications
You must be signed in to change notification settings - Fork 1
/
u9fs.c
1838 lines (1608 loc) · 32.9 KB
/
u9fs.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
/* already in plan9.h #include <sys/types.h> *//* for struct passwd, struct group, struct stat ... */
/* plan9.h is first to get the large file support definitions as early as possible */
#include <plan9.h>
#include <sys/stat.h> /* for stat, umask */
#include <stdlib.h> /* for malloc */
#include <string.h> /* for strcpy, memmove */
#include <pwd.h> /* for getpwnam, getpwuid */
#include <grp.h> /* for getgrnam, getgrgid */
#include <unistd.h> /* for gethostname, pread, pwrite, read, write */
#include <utime.h> /* for utime */
#include <dirent.h> /* for readdir */
#include <errno.h> /* for errno */
#include <stdio.h> /* for remove [sic] */
#include <fcntl.h> /* for O_RDONLY, etc. */
#include <limits.h> /* for PATH_MAX */
#include <sys/socket.h> /* various networking crud */
#include <netinet/in.h>
#include <netdb.h>
#include <fcall.h>
#include <oldfcall.h>
#include <u9fs.h>
/* #ifndef because can be given in makefile */
#ifndef DEFAULTLOG
#define DEFAULTLOG "/tmp/u9fs.log"
#endif
char *logfile = DEFAULTLOG;
#define S_ISSPECIAL(m) (S_ISCHR(m) || S_ISBLK(m) || S_ISFIFO(m))
enum {
Tdot = 1,
Tdotdot
};
enum {
P9P1,
P9P2000
};
typedef struct User User;
struct User {
int id;
gid_t defaultgid;
char *name;
char **mem; /* group members */
int nmem;
User *next;
};
struct Fid {
int fid;
char *path;
struct stat st;
User *u;
int omode;
DIR *dir;
int diroffset;
int fd;
struct dirent *dirent;
int direof;
Fid *next;
Fid *prev;
int auth;
void *authmagic;
};
void* emalloc(size_t);
void* erealloc(void*, size_t);
char* estrdup(char*);
char* estrpath(char*, char*, int);
int okuser(char*);
int groupchange(User*, User*, char**);
void rversion(Fcall*, Fcall*);
void rauth(Fcall*, Fcall*);
void rattach(Fcall*, Fcall*);
void rflush(Fcall*, Fcall*);
void rclone(Fcall*, Fcall*);
void rwalk(Fcall*, Fcall*);
void ropen(Fcall*, Fcall*);
void rcreate(Fcall*, Fcall*);
void rread(Fcall*, Fcall*);
void rwrite(Fcall*, Fcall*);
void rclunk(Fcall*, Fcall*);
void rstat(Fcall*, Fcall*);
void rwstat(Fcall*, Fcall*);
void rclwalk(Fcall*, Fcall*);
void rremove(Fcall*, Fcall*);
User* uname2user(char*);
User* gname2user(char*);
User* uid2user(int);
User* gid2user(int);
Fid* newfid(int, char**);
Fid* oldfidex(int, int, char**);
Fid* oldfid(int, char**);
int fidstat(Fid*, char**);
void freefid(Fid*);
int userchange(User*, char**);
int userwalk(User*, char**, char*, Qid*, char**);
int useropen(Fid*, int, char**);
int usercreate(Fid*, char*, int, long, char**);
int userremove(Fid*, char**);
int userperm(User*, char*, int, int);
int useringroup(User*, User*);
Qid stat2qid(struct stat*);
void getfcallold(int, Fcall*, int);
void putfcallold(int, Fcall*);
char Eauth[] = "authentication failed";
char Ebadfid[] = "fid unknown or out of range";
char Ebadoffset[] = "bad offset in directory read";
char Ebadusefid[] = "bad use of fid";
char Edirchange[] = "wstat can't convert between files and directories";
char Eexist[] = "file or directory already exists";
char Efidactive[] = "fid already in use";
char Enotdir[] = "not a directory";
char Enotingroup[] = "not a member of proposed group";
char Enotowner[] = "only owner can change group in wstat";
char Eperm[] = "permission denied";
char Especial0[] = "already attached without access to special files";
char Especial1[] = "already attached with access to special files";
char Especial[] = "no access to special file";
char Etoolarge[] = "i/o count too large";
char Eunknowngroup[] = "unknown group";
char Eunknownuser[] = "unknown user";
char Ewstatbuffer[] = "bogus wstat buffer";
ulong msize = IOHDRSZ+8192;
uchar* rxbuf;
uchar* txbuf;
void* databuf;
int connected;
int devallowed;
char* autharg;
char* defaultuser;
char hostname[256];
char remotehostname[256];
int chatty9p = 0;
int network = 1;
int old9p = -1;
int authed;
char* root;
User* none;
Auth *authmethods[] = { /* first is default */
&authrhosts,
&authp9any,
&authnone,
};
Auth *auth;
/*
* frogs: characters not valid in plan9
* filenames, keep this list in sync with
* /sys/src/9/port/chan.c:1656
*/
char isfrog[256]={
/*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
/*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
/*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
/*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
/*' '*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'('*/ 0, 0, 0, 0, 0, 0, 0, 1, /*'/'*/
/*'0'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'8'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'@'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'H'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'P'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'X'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'`'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'h'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'p'*/ 0, 0, 0, 0, 0, 0, 0, 0,
/*'x'*/ 0, 0, 0, 0, 0, 0, 0, 1, /*DEL*/
};
char*
rootpath(char *path)
{
static char buf[PATH_MAX];
if(root == nil)
return path;
snprint(buf, sizeof buf, "%s%s", root, path);
return buf;
}
void
getfcallnew(int fd, Fcall *fc, int have)
{
int len;
if(have > BIT32SZ)
sysfatal("cannot happen");
if(have < BIT32SZ && readn(fd, rxbuf+have, BIT32SZ-have) != BIT32SZ-have)
sysfatal("couldn't read message");
len = GBIT32(rxbuf);
if(len <= BIT32SZ)
sysfatal("bogus message");
len -= BIT32SZ;
if(readn(fd, rxbuf+BIT32SZ, len) != len)
sysfatal("short message");
if(convM2S(rxbuf, len+BIT32SZ, fc) != len+BIT32SZ)
sysfatal("badly sized message type %d", rxbuf[0]);
}
void
getfcallold(int fd, Fcall *fc, int have)
{
int len, n;
if(have > 3)
sysfatal("cannot happen");
if(have < 3 && readn(fd, rxbuf, 3-have) != 3-have)
sysfatal("couldn't read message");
len = oldhdrsize(rxbuf[0]);
if(len < 3)
sysfatal("bad message %d", rxbuf[0]);
if(len > 3 && readn(fd, rxbuf+3, len-3) != len-3)
sysfatal("couldn't read message");
n = iosize(rxbuf);
if(readn(fd, rxbuf+len, n) != n)
sysfatal("couldn't read message");
len += n;
if(convM2Sold(rxbuf, len, fc) != len)
sysfatal("badly sized message type %d", rxbuf[0]);
}
void
putfcallnew(int wfd, Fcall *tx)
{
uint n;
if((n = convS2M(tx, txbuf, msize)) == 0)
sysfatal("couldn't format message type %d", tx->type);
if(write(wfd, txbuf, n) != n)
sysfatal("couldn't send message");
}
void
putfcallold(int wfd, Fcall *tx)
{
uint n;
if((n = convS2Mold(tx, txbuf, msize)) == 0)
sysfatal("couldn't format message type %d", tx->type);
if(write(wfd, txbuf, n) != n)
sysfatal("couldn't send message");
}
void
getfcall(int fd, Fcall *fc)
{
if(old9p == 1){
getfcallold(fd, fc, 0);
return;
}
if(old9p == 0){
getfcallnew(fd, fc, 0);
return;
}
/* auto-detect */
if(readn(fd, rxbuf, 3) != 3)
sysfatal("couldn't read message");
/* is it an old (9P1) message? */
if(50 <= rxbuf[0] && rxbuf[0] <= 87 && (rxbuf[0]&1)==0 && GBIT16(rxbuf+1) == 0xFFFF){
old9p = 1;
getfcallold(fd, fc, 3);
return;
}
getfcallnew(fd, fc, 3);
old9p = 0;
}
void
seterror(Fcall *f, char *error)
{
f->type = Rerror;
f->ename = error ? error : "programmer error";
}
int
isowner(User *u, Fid *f)
{
return u->id == f->st.st_uid;
}
void
serve(int rfd, int wfd)
{
Fcall rx, tx;
for(;;){
getfcall(rfd, &rx);
if(chatty9p)
fprint(2, "<- %F\n", &rx);
memset(&tx, 0, sizeof tx);
tx.type = rx.type+1;
tx.tag = rx.tag;
switch(rx.type){
case Tflush:
break;
case Tversion:
rversion(&rx, &tx);
break;
case Tauth:
rauth(&rx, &tx);
break;
case Tattach:
rattach(&rx, &tx);
break;
case Twalk:
rwalk(&rx, &tx);
break;
case Tstat:
tx.stat = databuf;
rstat(&rx, &tx);
break;
case Twstat:
rwstat(&rx, &tx);
break;
case Topen:
ropen(&rx, &tx);
break;
case Tcreate:
rcreate(&rx, &tx);
break;
case Tread:
tx.data = databuf;
rread(&rx, &tx);
break;
case Twrite:
rwrite(&rx, &tx);
break;
case Tclunk:
rclunk(&rx, &tx);
break;
case Tremove:
rremove(&rx, &tx);
break;
default:
fprint(2, "unknown message %F\n", &rx);
seterror(&tx, "bad message");
break;
}
if(chatty9p)
fprint(2, "-> %F\n", &tx);
(old9p ? putfcallold : putfcallnew)(wfd, &tx);
}
}
void
rversion(Fcall *rx, Fcall *tx)
{
if(msize > rx->msize)
msize = rx->msize;
tx->msize = msize;
if(strncmp(rx->version, "9P", 2) != 0)
tx->version = "unknown";
else
tx->version = "9P2000";
}
void
rauth(Fcall *rx, Fcall *tx)
{
char *e;
if((e = auth->auth(rx, tx)) != nil)
seterror(tx, e);
}
void
rattach(Fcall *rx, Fcall *tx)
{
char *e;
Fid *fid;
User *u;
if(rx->aname == nil)
rx->aname = "";
if(strcmp(rx->aname, "device") == 0){
if(connected && !devallowed){
seterror(tx, Especial0);
return;
}
devallowed = 1;
}else{
if(connected && devallowed){
seterror(tx, Especial1);
return;
}
}
if(strcmp(rx->uname, "none") == 0){
if(authed == 0){
seterror(tx, Eauth);
return;
}
if(none != nil)
rx->uname = none->name;
} else {
if((e = auth->attach(rx, tx)) != nil){
seterror(tx, e);
return;
}
authed++;
}
if((fid = newfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
fid->path = estrdup("/");
if(fidstat(fid, &e) < 0){
seterror(tx, e);
freefid(fid);
return;
}
if(defaultuser)
rx->uname = defaultuser;
if((u = uname2user(rx->uname)) == nil
|| (!defaultuser && u->id == 0)){
/* we don't know anyone named root... */
seterror(tx, Eunknownuser);
freefid(fid);
return;
}
fid->u = u;
tx->qid = stat2qid(&fid->st);
return;
}
void
rwalk(Fcall *rx, Fcall *tx)
{
int i;
char *path, *e;
Fid *fid, *nfid;
e = nil;
if((fid = oldfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
if(fid->omode != -1){
seterror(tx, Ebadusefid);
return;
}
if(fidstat(fid, &e) < 0){
seterror(tx, e);
return;
}
if(!S_ISDIR(fid->st.st_mode) && rx->nwname){
seterror(tx, Enotdir);
return;
}
nfid = nil;
if(rx->newfid != rx->fid && (nfid = newfid(rx->newfid, &e)) == nil){
seterror(tx, e);
return;
}
path = estrdup(fid->path);
e = nil;
for(i=0; i<rx->nwname; i++)
if(userwalk(fid->u, &path, rx->wname[i], &tx->wqid[i], &e) < 0)
break;
if(i == rx->nwname){ /* successful clone or walk */
tx->nwqid = i;
if(nfid){
nfid->path = path;
nfid->u = fid->u;
}else{
free(fid->path);
fid->path = path;
}
}else{
if(i > 0) /* partial walk? */
tx->nwqid = i;
else
seterror(tx, e);
if(nfid) /* clone implicit new fid */
freefid(nfid);
free(path);
}
return;
}
void
ropen(Fcall *rx, Fcall *tx)
{
char *e;
Fid *fid;
if((fid = oldfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
if(fid->omode != -1){
seterror(tx, Ebadusefid);
return;
}
if(fidstat(fid, &e) < 0){
seterror(tx, e);
return;
}
if(!devallowed && S_ISSPECIAL(fid->st.st_mode)){
seterror(tx, Especial);
return;
}
if(useropen(fid, rx->mode, &e) < 0){
seterror(tx, e);
return;
}
tx->iounit = 0;
tx->qid = stat2qid(&fid->st);
}
void
rcreate(Fcall *rx, Fcall *tx)
{
char *e;
Fid *fid;
if((fid = oldfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
if(fid->omode != -1){
seterror(tx, Ebadusefid);
return;
}
if(fidstat(fid, &e) < 0){
seterror(tx, e);
return;
}
if(!S_ISDIR(fid->st.st_mode)){
seterror(tx, Enotdir);
return;
}
if(usercreate(fid, rx->name, rx->mode, rx->perm, &e) < 0){
seterror(tx, e);
return;
}
if(fidstat(fid, &e) < 0){
seterror(tx, e);
return;
}
tx->iounit = 0;
tx->qid = stat2qid(&fid->st);
}
uchar
modebyte(struct stat *st)
{
uchar b;
b = 0;
if(S_ISDIR(st->st_mode))
b |= QTDIR;
/* no way to test append-only */
/* no real way to test exclusive use, but mark devices as such */
if(S_ISSPECIAL(st->st_mode))
b |= QTEXCL;
return b;
}
ulong
plan9mode(struct stat *st)
{
return ((ulong)modebyte(st)<<24) | (st->st_mode & 0777);
}
/*
* this is for chmod, so don't worry about S_IFDIR
*/
mode_t
unixmode(Dir *d)
{
return (mode_t)(d->mode&0777);
}
Qid
stat2qid(struct stat *st)
{
uchar *p, *ep, *q;
Qid qid;
/*
* For now, ignore the device number.
*/
qid.path = 0;
p = (uchar*)&qid.path;
ep = p+sizeof(qid.path);
q = p+sizeof(ino_t);
if(q > ep){
fprint(2, "warning: inode number too big\n");
q = ep;
}
memmove(p, &st->st_ino, q-p);
q = q+sizeof(dev_t);
if(q > ep){
/*
* fprint(2, "warning: inode number + device number too big %d+%d\n",
* sizeof(ino_t), sizeof(dev_t));
*/
q = ep - sizeof(dev_t);
if(q < p)
fprint(2, "warning: device number too big by itself\n");
else
*(dev_t*)q ^= st->st_dev;
}
qid.vers = st->st_mtime ^ (st->st_size << 8);
qid.type = modebyte(st);
return qid;
}
char *
enfrog(char *src)
{
char *d, *dst;
uchar *s;
d = dst = emalloc(strlen(src)*3 + 1);
for (s = (uchar *)src; *s; s++)
if(isfrog[*s] || *s == '\\')
d += sprintf(d, "\\%02x", *s);
else
*d++ = *s;
*d = 0;
return dst;
}
char *
defrog(char *s)
{
char *d, *dst, buf[3];
d = dst = emalloc(strlen(s) + 1);
for(; *s; s++)
if(*s == '\\' && strlen(s) >= 3){
buf[0] = *++s; /* skip \ */
buf[1] = *++s;
buf[2] = 0;
*d++ = strtoul(buf, NULL, 16);
} else
*d++ = *s;
*d = 0;
return dst;
}
void
stat2dir(char *path, struct stat *st, Dir *d)
{
User *u;
char *q, *p, *npath;
memset(d, 0, sizeof(*d));
d->qid = stat2qid(st);
d->mode = plan9mode(st);
d->atime = st->st_atime;
d->mtime = st->st_mtime;
d->length = st->st_size;
d->uid = (u = uid2user(st->st_uid)) ? u->name : "???";
d->gid = (u = gid2user(st->st_gid)) ? u->name : "???";
d->muid = "";
if((q = strrchr(path, '/')) != nil)
d->name = enfrog(q+1);
else
d->name = enfrog(path);
}
void
rread(Fcall *rx, Fcall *tx)
{
char *e, *path, *rpath;
uchar *p, *ep;
int n;
Fid *fid;
Dir d;
struct stat st;
if(rx->count > msize-IOHDRSZ){
seterror(tx, Etoolarge);
return;
}
if((fid = oldfidex(rx->fid, -1, &e)) == nil){
seterror(tx, e);
return;
}
if (fid->auth) {
char *e;
e = auth->read(rx, tx);
if (e)
seterror(tx, e);
return;
}
if(fid->omode == -1 || (fid->omode&3) == OWRITE){
seterror(tx, Ebadusefid);
return;
}
if(fid->dir){
if(rx->offset != fid->diroffset){
if(rx->offset != 0){
seterror(tx, Ebadoffset);
return;
}
rewinddir(fid->dir);
fid->diroffset = 0;
fid->direof = 0;
}
if(fid->direof){
tx->count = 0;
return;
}
p = (uchar*)tx->data;
ep = (uchar*)tx->data+rx->count;
for(;;){
if(p+BIT16SZ >= ep)
break;
if(fid->dirent == nil) /* one entry cache for when convD2M fails */
if((fid->dirent = readdir(fid->dir)) == nil){
fid->direof = 1;
break;
}
if(strcmp(fid->dirent->d_name, ".") == 0
|| strcmp(fid->dirent->d_name, "..") == 0){
fid->dirent = nil;
continue;
}
rpath = rootpath(fid->path);
path = estrpath(rpath, fid->dirent->d_name, 0);
memset(&st, 0, sizeof st);
if(stat(path, &st) < 0){
fprint(2, "dirread: stat(%s) failed: %s\n", path, strerror(errno));
fid->dirent = nil;
free(path);
continue;
}
free(path);
stat2dir(fid->dirent->d_name, &st, &d);
if((n=(old9p ? convD2Mold : convD2M)(&d, p, ep-p)) <= BIT16SZ)
break;
p += n;
fid->dirent = nil;
}
tx->count = p - (uchar*)tx->data;
fid->diroffset += tx->count;
}else{
if((n = pread(fid->fd, tx->data, rx->count, rx->offset)) < 0){
seterror(tx, strerror(errno));
return;
}
tx->count = n;
}
}
void
rwrite(Fcall *rx, Fcall *tx)
{
char *e;
Fid *fid;
int n;
if(rx->count > msize-IOHDRSZ){
seterror(tx, Etoolarge);
return;
}
if((fid = oldfidex(rx->fid, -1, &e)) == nil){
seterror(tx, e);
return;
}
if (fid->auth) {
char *e;
e = auth->write(rx, tx);
if (e)
seterror(tx, e);
return;
}
if(fid->omode == -1 || (fid->omode&3) == OREAD || (fid->omode&3) == OEXEC){
seterror(tx, Ebadusefid);
return;
}
if((n = pwrite(fid->fd, rx->data, rx->count, rx->offset)) < 0){
seterror(tx, strerror(errno));
return;
}
tx->count = n;
}
void
rclunk(Fcall *rx, Fcall *tx)
{
char *e, *rpath;
Fid *fid;
if((fid = oldfidex(rx->fid, -1, &e)) == nil){
seterror(tx, e);
return;
}
if (fid->auth) {
if (auth->clunk) {
e = (*auth->clunk)(rx, tx);
if (e) {
seterror(tx, e);
return;
}
}
}
else if(fid->omode != -1 && fid->omode&ORCLOSE){
rpath = rootpath(fid->path);
remove(rpath);
}
freefid(fid);
}
void
rremove(Fcall *rx, Fcall *tx)
{
char *e;
Fid *fid;
if((fid = oldfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
if(userremove(fid, &e) < 0)
seterror(tx, e);
freefid(fid);
}
void
rstat(Fcall *rx, Fcall *tx)
{
char *e;
Fid *fid;
Dir d;
if((fid = oldfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
if(fidstat(fid, &e) < 0){
seterror(tx, e);
return;
}
stat2dir(fid->path, &fid->st, &d);
if((tx->nstat=(old9p ? convD2Mold : convD2M)(&d, tx->stat, msize)) <= BIT16SZ)
seterror(tx, "convD2M fails");
}
void
rwstat(Fcall *rx, Fcall *tx)
{
char *e, *opath, *npath;
char *p, *old, *new, *dir;
gid_t gid;
Dir d;
Fid *fid;
if((fid = oldfid(rx->fid, &e)) == nil){
seterror(tx, e);
return;
}
/*
* wstat is supposed to be atomic.
* we check all the things we can before trying anything.
* still, if we are told to truncate a file and rename it and only
* one works, we're screwed. in such cases we leave things
* half broken and return an error. it's hardly perfect.
*/
if((old9p ? convM2Dold : convM2D)(rx->stat, rx->nstat, &d, (char*)rx->stat) <= BIT16SZ){
seterror(tx, Ewstatbuffer);
return;
}
if(fidstat(fid, &e) < 0){
seterror(tx, e);
return;
}
/*
* The casting is necessary because d.mode is ulong and might,
* on some systems, be 64 bits. We only want to compare the
* bottom 32 bits, since that's all that gets sent in the protocol.
*
* Same situation for d.mtime and d.length (although that last check
* is admittedly superfluous, given the current lack of 128-bit machines).
*/
gid = (gid_t)-1;
if(d.gid[0] != '\0'){
User *g;
g = gname2user(d.gid);
if(g == nil){
seterror(tx, Eunknowngroup);
return;
}
gid = (gid_t)g->id;
if(groupchange(fid->u, gid2user(gid), &e) < 0){
seterror(tx, e);
return;
}
}
if((u32int)d.mode != (u32int)~0 && (((d.mode&DMDIR)!=0) ^ (S_ISDIR(fid->st.st_mode)!=0))){
seterror(tx, Edirchange);
return;
}
if(strcmp(fid->path, "/") == 0){
seterror(tx, "no wstat of root");
return;
}
/*
* try things in increasing order of harm to the file.
* mtime should come after truncate so that if you
* do both the mtime actually takes effect, but i'd rather
* leave truncate until last.
* (see above comment about atomicity).
*/
opath = estrdup(rootpath(fid->path));
if((u32int)d.mode != (u32int)~0 && chmod(opath, unixmode(&d)) < 0){
if(chatty9p)
fprint(2, "chmod(%s, 0%luo) failed\n", opath, unixmode(&d));
seterror(tx, strerror(errno));
return;
}
if((u32int)d.mtime != (u32int)~0){
struct utimbuf t;