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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package p9 is a 9P2000.L implementation.
package p9
import (
"fmt"
"math"
"os"
"strings"
"sync/atomic"
"syscall"
"golang.org/x/sys/unix"
)
// OpenFlags is the mode passed to Open and Create operations.
//
// These correspond to bits sent over the wire.
type OpenFlags uint32
const (
// ReadOnly is a Tlopen and Tlcreate flag indicating read-only mode.
ReadOnly OpenFlags = 0
// WriteOnly is a Tlopen and Tlcreate flag indicating write-only mode.
WriteOnly OpenFlags = 1
// ReadWrite is a Tlopen flag indicates read-write mode.
ReadWrite OpenFlags = 2
// OpenFlagsModeMask is a mask of valid OpenFlags mode bits.
OpenFlagsModeMask OpenFlags = 3
// OpenTruncate is a Tlopen flag indicating that the opened file should be
// truncated.
OpenTruncate OpenFlags = 01000
)
// ConnectFlags is the mode passed to Connect operations.
//
// These correspond to bits sent over the wire.
type ConnectFlags uint32
const (
// StreamSocket is a Tlconnect flag indicating SOCK_STREAM mode.
StreamSocket ConnectFlags = 0
// DgramSocket is a Tlconnect flag indicating SOCK_DGRAM mode.
DgramSocket ConnectFlags = 1
// SeqpacketSocket is a Tlconnect flag indicating SOCK_SEQPACKET mode.
SeqpacketSocket ConnectFlags = 2
// AnonymousSocket is a Tlconnect flag indicating that the mode does not
// matter and that the requester will accept any socket type.
AnonymousSocket ConnectFlags = 3
)
// OSFlags converts a p9.OpenFlags to an int compatible with open(2).
func (o OpenFlags) OSFlags() int {
// "flags contains Linux open(2) flags bits" - 9P2000.L
return int(o)
}
// String implements fmt.Stringer.
func (o OpenFlags) String() string {
var buf strings.Builder
switch mode := o & OpenFlagsModeMask; mode {
case ReadOnly:
buf.WriteString("ReadOnly")
case WriteOnly:
buf.WriteString("WriteOnly")
case ReadWrite:
buf.WriteString("ReadWrite")
default:
fmt.Fprintf(&buf, "%#o", mode)
}
otherFlags := o &^ OpenFlagsModeMask
if otherFlags&OpenTruncate != 0 {
buf.WriteString("|OpenTruncate")
otherFlags &^= OpenTruncate
}
if otherFlags != 0 {
fmt.Fprintf(&buf, "|%#o", otherFlags)
}
return buf.String()
}
// Tag is a message tag.
type Tag uint16
// FID is a file identifier.
type FID uint64
// FileMode are flags corresponding to file modes.
//
// These correspond to bits sent over the wire.
// These also correspond to mode_t bits.
type FileMode uint32
const (
// FileModeMask is a mask of all the file mode bits of FileMode.
FileModeMask FileMode = 0170000
// ModeSocket is an (unused) mode bit for a socket.
ModeSocket FileMode = 0140000
// ModeSymlink is a mode bit for a symlink.
ModeSymlink FileMode = 0120000
// ModeRegular is a mode bit for regular files.
ModeRegular FileMode = 0100000
// ModeBlockDevice is a mode bit for block devices.
ModeBlockDevice FileMode = 060000
// ModeDirectory is a mode bit for directories.
ModeDirectory FileMode = 040000
// ModeCharacterDevice is a mode bit for a character device.
ModeCharacterDevice FileMode = 020000
// ModeNamedPipe is a mode bit for a named pipe.
ModeNamedPipe FileMode = 010000
// Read is a mode bit indicating read permission.
Read FileMode = 04
// Write is a mode bit indicating write permission.
Write FileMode = 02
// Exec is a mode bit indicating exec permission.
Exec FileMode = 01
// AllPermissions is a mask with rwx bits set for user, group and others.
AllPermissions FileMode = 0777
// Sticky is a mode bit indicating sticky directories.
Sticky FileMode = 01000
// permissionsMask is the mask to apply to FileModes for permissions. It
// includes rwx bits for user, group and others, and sticky bit.
permissionsMask FileMode = 01777
)
// QIDType is the most significant byte of the FileMode word, to be used as the
// Type field of p9.QID.
func (m FileMode) QIDType() QIDType {
switch {
case m.IsDir():
return TypeDir
case m.IsSocket(), m.IsNamedPipe(), m.IsCharacterDevice():
// Best approximation.
return TypeAppendOnly
case m.IsSymlink():
return TypeSymlink
default:
return TypeRegular
}
}
// FileType returns the file mode without the permission bits.
func (m FileMode) FileType() FileMode {
return m & FileModeMask
}
// Permissions returns just the permission bits of the mode.
func (m FileMode) Permissions() FileMode {
return m & permissionsMask
}
// Writable returns the mode with write bits added.
func (m FileMode) Writable() FileMode {
return m | 0222
}
// IsReadable returns true if m represents a file that can be read.
func (m FileMode) IsReadable() bool {
return m&0444 != 0
}
// IsWritable returns true if m represents a file that can be written to.
func (m FileMode) IsWritable() bool {
return m&0222 != 0
}
// IsExecutable returns true if m represents a file that can be executed.
func (m FileMode) IsExecutable() bool {
return m&0111 != 0
}
// IsRegular returns true if m is a regular file.
func (m FileMode) IsRegular() bool {
return m&FileModeMask == ModeRegular
}
// IsDir returns true if m represents a directory.
func (m FileMode) IsDir() bool {
return m&FileModeMask == ModeDirectory
}
// IsNamedPipe returns true if m represents a named pipe.
func (m FileMode) IsNamedPipe() bool {
return m&FileModeMask == ModeNamedPipe
}
// IsCharacterDevice returns true if m represents a character device.
func (m FileMode) IsCharacterDevice() bool {
return m&FileModeMask == ModeCharacterDevice
}
// IsBlockDevice returns true if m represents a character device.
func (m FileMode) IsBlockDevice() bool {
return m&FileModeMask == ModeBlockDevice
}
// IsSocket returns true if m represents a socket.
func (m FileMode) IsSocket() bool {
return m&FileModeMask == ModeSocket
}
// IsSymlink returns true if m represents a symlink.
func (m FileMode) IsSymlink() bool {
return m&FileModeMask == ModeSymlink
}
// ModeFromOS returns a FileMode from an os.FileMode.
func ModeFromOS(mode os.FileMode) FileMode {
m := FileMode(mode.Perm())
switch {
case mode.IsDir():
m |= ModeDirectory
case mode&os.ModeSymlink != 0:
m |= ModeSymlink
case mode&os.ModeSocket != 0:
m |= ModeSocket
case mode&os.ModeNamedPipe != 0:
m |= ModeNamedPipe
case mode&os.ModeCharDevice != 0:
m |= ModeCharacterDevice
case mode&os.ModeDevice != 0:
m |= ModeBlockDevice
default:
m |= ModeRegular
}
return m
}
// OSMode converts a p9.FileMode to an os.FileMode.
func (m FileMode) OSMode() os.FileMode {
var osMode os.FileMode
osMode |= os.FileMode(m.Permissions())
switch {
case m.IsDir():
osMode |= os.ModeDir
case m.IsSymlink():
osMode |= os.ModeSymlink
case m.IsSocket():
osMode |= os.ModeSocket
case m.IsNamedPipe():
osMode |= os.ModeNamedPipe
case m.IsCharacterDevice():
osMode |= os.ModeCharDevice | os.ModeDevice
case m.IsBlockDevice():
osMode |= os.ModeDevice
}
return osMode
}
// UID represents a user ID.
type UID uint32
// Ok returns true if uid is not NoUID.
func (uid UID) Ok() bool {
return uid != NoUID
}
// GID represents a group ID.
type GID uint32
// Ok returns true if gid is not NoGID.
func (gid GID) Ok() bool {
return gid != NoGID
}
const (
// NoTag is a sentinel used to indicate no valid tag.
NoTag Tag = math.MaxUint16
// NoFID is a sentinel used to indicate no valid FID.
NoFID FID = math.MaxUint32
// NoUID is a sentinel used to indicate no valid UID.
NoUID UID = math.MaxUint32
// NoGID is a sentinel used to indicate no valid GID.
NoGID GID = math.MaxUint32
)
// MsgType is a type identifier.
type MsgType uint8
// MsgType declarations.
const (
MsgTlerror MsgType = 6
MsgRlerror = 7
MsgTstatfs = 8
MsgRstatfs = 9
MsgTlopen = 12
MsgRlopen = 13
MsgTlcreate = 14
MsgRlcreate = 15
MsgTsymlink = 16
MsgRsymlink = 17
MsgTmknod = 18
MsgRmknod = 19
MsgTrename = 20
MsgRrename = 21
MsgTreadlink = 22
MsgRreadlink = 23
MsgTgetattr = 24
MsgRgetattr = 25
MsgTsetattr = 26
MsgRsetattr = 27
MsgTxattrwalk = 30
MsgRxattrwalk = 31
MsgTxattrcreate = 32
MsgRxattrcreate = 33
MsgTgetxattr = 34
MsgRgetxattr = 35
MsgTsetxattr = 36
MsgRsetxattr = 37
MsgTreaddir = 40
MsgRreaddir = 41
MsgTfsync = 50
MsgRfsync = 51
MsgTlink = 70
MsgRlink = 71
MsgTmkdir = 72
MsgRmkdir = 73
MsgTrenameat = 74
MsgRrenameat = 75
MsgTunlinkat = 76
MsgRunlinkat = 77
MsgTversion = 100
MsgRversion = 101
MsgTauth = 102
MsgRauth = 103
MsgTattach = 104
MsgRattach = 105
MsgTflush = 108
MsgRflush = 109
MsgTwalk = 110
MsgRwalk = 111
MsgTread = 116
MsgRread = 117
MsgTwrite = 118
MsgRwrite = 119
MsgTclunk = 120
MsgRclunk = 121
MsgTremove = 122
MsgRremove = 123
MsgTflushf = 124
MsgRflushf = 125
MsgTwalkgetattr = 126
MsgRwalkgetattr = 127
MsgTucreate = 128
MsgRucreate = 129
MsgTumkdir = 130
MsgRumkdir = 131
MsgTumknod = 132
MsgRumknod = 133
MsgTusymlink = 134
MsgRusymlink = 135
MsgTlconnect = 136
MsgRlconnect = 137
MsgTallocate = 138
MsgRallocate = 139
MsgTchannel = 250
MsgRchannel = 251
)
// QIDType represents the file type for QIDs.
//
// QIDType corresponds to the high 8 bits of a Plan 9 file mode.
type QIDType uint8
const (
// TypeDir represents a directory type.
TypeDir QIDType = 0x80
// TypeAppendOnly represents an append only file.
TypeAppendOnly QIDType = 0x40
// TypeExclusive represents an exclusive-use file.
TypeExclusive QIDType = 0x20
// TypeMount represents a mounted channel.
TypeMount QIDType = 0x10
// TypeAuth represents an authentication file.
TypeAuth QIDType = 0x08
// TypeTemporary represents a temporary file.
TypeTemporary QIDType = 0x04
// TypeSymlink represents a symlink.
TypeSymlink QIDType = 0x02
// TypeLink represents a hard link.
TypeLink QIDType = 0x01
// TypeRegular represents a regular file.
TypeRegular QIDType = 0x00
)
// QID is a unique file identifier.
//
// This may be embedded in other requests and responses.
type QID struct {
// Type is the highest order byte of the file mode.
Type QIDType
// Version is an arbitrary server version number.
Version uint32
// Path is a unique server identifier for this path (e.g. inode).
Path uint64
}
// String implements fmt.Stringer.
func (q QID) String() string {
return fmt.Sprintf("QID{Type: %d, Version: %d, Path: %d}", q.Type, q.Version, q.Path)
}
// Decode implements encoder.Decode.
func (q *QID) Decode(b *buffer) {
q.Type = b.ReadQIDType()
q.Version = b.Read32()
q.Path = b.Read64()
}
// Encode implements encoder.Encode.
func (q *QID) Encode(b *buffer) {
b.WriteQIDType(q.Type)
b.Write32(q.Version)
b.Write64(q.Path)
}
// QIDGenerator is a simple generator for QIDs that atomically increments Path
// values.
type QIDGenerator struct {
// uids is an ever increasing value that can be atomically incremented
// to provide unique Path values for QIDs.
uids uint64
}
// Get returns a new 9P unique ID with a unique Path given a QID type.
//
// While the 9P spec allows Version to be incremented every time the file is
// modified, we currently do not use the Version member for anything. Hence,
// it is set to 0.
func (q *QIDGenerator) Get(t QIDType) QID {
return QID{
Type: t,
Version: 0,
Path: atomic.AddUint64(&q.uids, 1),
}
}
// FSStat is used by statfs.
type FSStat struct {
// Type is the filesystem type.
Type uint32
// BlockSize is the blocksize.
BlockSize uint32
// Blocks is the number of blocks.
Blocks uint64
// BlocksFree is the number of free blocks.
BlocksFree uint64
// BlocksAvailable is the number of blocks *available*.
BlocksAvailable uint64
// Files is the number of files available.
Files uint64
// FilesFree is the number of free file nodes.
FilesFree uint64
// FSID is the filesystem ID.
FSID uint64
// NameLength is the maximum name length.
NameLength uint32
}
// Decode implements encoder.Decode.
func (f *FSStat) Decode(b *buffer) {
f.Type = b.Read32()
f.BlockSize = b.Read32()
f.Blocks = b.Read64()
f.BlocksFree = b.Read64()
f.BlocksAvailable = b.Read64()
f.Files = b.Read64()
f.FilesFree = b.Read64()
f.FSID = b.Read64()
f.NameLength = b.Read32()
}
// Encode implements encoder.Encode.
func (f *FSStat) Encode(b *buffer) {
b.Write32(f.Type)
b.Write32(f.BlockSize)
b.Write64(f.Blocks)
b.Write64(f.BlocksFree)
b.Write64(f.BlocksAvailable)
b.Write64(f.Files)
b.Write64(f.FilesFree)
b.Write64(f.FSID)
b.Write32(f.NameLength)
}
// AttrMask is a mask of attributes for getattr.
type AttrMask struct {
Mode bool
NLink bool
UID bool
GID bool
RDev bool
ATime bool
MTime bool
CTime bool
INo bool
Size bool
Blocks bool
BTime bool
Gen bool
DataVersion bool
}
// Contains returns true if a contains all of the attributes masked as b.
func (a AttrMask) Contains(b AttrMask) bool {
if b.Mode && !a.Mode {
return false
}
if b.NLink && !a.NLink {
return false
}
if b.UID && !a.UID {
return false
}
if b.GID && !a.GID {
return false
}
if b.RDev && !a.RDev {
return false
}
if b.ATime && !a.ATime {
return false
}
if b.MTime && !a.MTime {
return false
}
if b.CTime && !a.CTime {
return false
}
if b.INo && !a.INo {
return false
}
if b.Size && !a.Size {
return false
}
if b.Blocks && !a.Blocks {
return false
}
if b.BTime && !a.BTime {
return false
}
if b.Gen && !a.Gen {
return false
}
if b.DataVersion && !a.DataVersion {
return false
}
return true
}
// Empty returns true if no fields are masked.
func (a AttrMask) Empty() bool {
return !a.Mode && !a.NLink && !a.UID && !a.GID && !a.RDev && !a.ATime && !a.MTime && !a.CTime && !a.INo && !a.Size && !a.Blocks && !a.BTime && !a.Gen && !a.DataVersion
}
// AttrMaskAll returns an AttrMask with all fields masked.
func AttrMaskAll() AttrMask {
return AttrMask{
Mode: true,
NLink: true,
UID: true,
GID: true,
RDev: true,
ATime: true,
MTime: true,
CTime: true,
INo: true,
Size: true,
Blocks: true,
BTime: true,
Gen: true,
DataVersion: true,
}
}
// String implements fmt.Stringer.
func (a AttrMask) String() string {
var masks []string
if a.Mode {
masks = append(masks, "Mode")
}
if a.NLink {
masks = append(masks, "NLink")
}
if a.UID {
masks = append(masks, "UID")
}
if a.GID {
masks = append(masks, "GID")
}
if a.RDev {
masks = append(masks, "RDev")
}
if a.ATime {
masks = append(masks, "ATime")
}
if a.MTime {
masks = append(masks, "MTime")
}
if a.CTime {
masks = append(masks, "CTime")
}
if a.INo {
masks = append(masks, "INo")
}
if a.Size {
masks = append(masks, "Size")
}
if a.Blocks {
masks = append(masks, "Blocks")
}
if a.BTime {
masks = append(masks, "BTime")
}
if a.Gen {
masks = append(masks, "Gen")
}
if a.DataVersion {
masks = append(masks, "DataVersion")
}
return fmt.Sprintf("AttrMask{with: %s}", strings.Join(masks, " "))
}
// Decode implements encoder.Decode.
func (a *AttrMask) Decode(b *buffer) {
mask := b.Read64()
a.Mode = mask&0x00000001 != 0
a.NLink = mask&0x00000002 != 0
a.UID = mask&0x00000004 != 0
a.GID = mask&0x00000008 != 0
a.RDev = mask&0x00000010 != 0
a.ATime = mask&0x00000020 != 0
a.MTime = mask&0x00000040 != 0
a.CTime = mask&0x00000080 != 0
a.INo = mask&0x00000100 != 0
a.Size = mask&0x00000200 != 0
a.Blocks = mask&0x00000400 != 0
a.BTime = mask&0x00000800 != 0
a.Gen = mask&0x00001000 != 0
a.DataVersion = mask&0x00002000 != 0
}
// Encode implements encoder.Encode.
func (a *AttrMask) Encode(b *buffer) {
var mask uint64
if a.Mode {
mask |= 0x00000001
}
if a.NLink {
mask |= 0x00000002
}
if a.UID {
mask |= 0x00000004
}
if a.GID {
mask |= 0x00000008
}
if a.RDev {
mask |= 0x00000010
}
if a.ATime {
mask |= 0x00000020
}
if a.MTime {
mask |= 0x00000040
}
if a.CTime {
mask |= 0x00000080
}
if a.INo {
mask |= 0x00000100
}
if a.Size {
mask |= 0x00000200
}
if a.Blocks {
mask |= 0x00000400
}
if a.BTime {
mask |= 0x00000800
}
if a.Gen {
mask |= 0x00001000
}
if a.DataVersion {
mask |= 0x00002000
}
b.Write64(mask)
}
// Attr is a set of attributes for getattr.
type Attr struct {
Mode FileMode
UID UID
GID GID
NLink uint64
RDev uint64
Size uint64
BlockSize uint64
Blocks uint64
ATimeSeconds uint64
ATimeNanoSeconds uint64
MTimeSeconds uint64
MTimeNanoSeconds uint64
CTimeSeconds uint64
CTimeNanoSeconds uint64
BTimeSeconds uint64
BTimeNanoSeconds uint64
Gen uint64
DataVersion uint64
}
// String implements fmt.Stringer.
func (a Attr) String() string {
return fmt.Sprintf("Attr{Mode: 0o%o, UID: %d, GID: %d, NLink: %d, RDev: %d, Size: %d, BlockSize: %d, Blocks: %d, ATime: {Sec: %d, NanoSec: %d}, MTime: {Sec: %d, NanoSec: %d}, CTime: {Sec: %d, NanoSec: %d}, BTime: {Sec: %d, NanoSec: %d}, Gen: %d, DataVersion: %d}",
a.Mode, a.UID, a.GID, a.NLink, a.RDev, a.Size, a.BlockSize, a.Blocks, a.ATimeSeconds, a.ATimeNanoSeconds, a.MTimeSeconds, a.MTimeNanoSeconds, a.CTimeSeconds, a.CTimeNanoSeconds, a.BTimeSeconds, a.BTimeNanoSeconds, a.Gen, a.DataVersion)
}
// Encode implements encoder.Encode.
func (a *Attr) Encode(b *buffer) {
b.WriteFileMode(a.Mode)
b.WriteUID(a.UID)
b.WriteGID(a.GID)
b.Write64(a.NLink)
b.Write64(a.RDev)
b.Write64(a.Size)
b.Write64(a.BlockSize)
b.Write64(a.Blocks)
b.Write64(a.ATimeSeconds)
b.Write64(a.ATimeNanoSeconds)
b.Write64(a.MTimeSeconds)
b.Write64(a.MTimeNanoSeconds)
b.Write64(a.CTimeSeconds)
b.Write64(a.CTimeNanoSeconds)
b.Write64(a.BTimeSeconds)
b.Write64(a.BTimeNanoSeconds)
b.Write64(a.Gen)
b.Write64(a.DataVersion)
}
// Decode implements encoder.Decode.
func (a *Attr) Decode(b *buffer) {
a.Mode = b.ReadFileMode()
a.UID = b.ReadUID()
a.GID = b.ReadGID()
a.NLink = b.Read64()
a.RDev = b.Read64()
a.Size = b.Read64()
a.BlockSize = b.Read64()
a.Blocks = b.Read64()
a.ATimeSeconds = b.Read64()
a.ATimeNanoSeconds = b.Read64()
a.MTimeSeconds = b.Read64()
a.MTimeNanoSeconds = b.Read64()
a.CTimeSeconds = b.Read64()
a.CTimeNanoSeconds = b.Read64()
a.BTimeSeconds = b.Read64()
a.BTimeNanoSeconds = b.Read64()
a.Gen = b.Read64()
a.DataVersion = b.Read64()
}
// StatToAttr converts a Linux syscall stat structure to an Attr.
func StatToAttr(s *syscall.Stat_t, req AttrMask) (Attr, AttrMask) {
attr := Attr{
UID: NoUID,
GID: NoGID,
}
if req.Mode {
// p9.FileMode corresponds to Linux mode_t.
attr.Mode = FileMode(s.Mode)
}
if req.NLink {
attr.NLink = uint64(s.Nlink)
}
if req.UID {
attr.UID = UID(s.Uid)
}
if req.GID {
attr.GID = GID(s.Gid)
}
if req.RDev {
attr.RDev = s.Dev
}
if req.ATime {
attr.ATimeSeconds = uint64(s.Atim.Sec)
attr.ATimeNanoSeconds = uint64(s.Atim.Nsec)
}
if req.MTime {
attr.MTimeSeconds = uint64(s.Mtim.Sec)
attr.MTimeNanoSeconds = uint64(s.Mtim.Nsec)
}
if req.CTime {
attr.CTimeSeconds = uint64(s.Ctim.Sec)
attr.CTimeNanoSeconds = uint64(s.Ctim.Nsec)
}
if req.Size {
attr.Size = uint64(s.Size)
}
if req.Blocks {
attr.BlockSize = uint64(s.Blksize)
attr.Blocks = uint64(s.Blocks)
}
// Use the req field because we already have it.
req.BTime = false
req.Gen = false
req.DataVersion = false
return attr, req
}
// SetAttrMask specifies a valid mask for setattr.
type SetAttrMask struct {
Permissions bool
UID bool
GID bool
Size bool
ATime bool
MTime bool
CTime bool
ATimeNotSystemTime bool
MTimeNotSystemTime bool
}
// IsSubsetOf returns whether s is a subset of m.
func (s SetAttrMask) IsSubsetOf(m SetAttrMask) bool {
sb := s.bitmask()
sm := m.bitmask()
return sm|sb == sm
}
// String implements fmt.Stringer.
func (s SetAttrMask) String() string {
var masks []string
if s.Permissions {
masks = append(masks, "Permissions")
}
if s.UID {
masks = append(masks, "UID")
}
if s.GID {
masks = append(masks, "GID")
}
if s.Size {
masks = append(masks, "Size")
}
if s.ATime {
masks = append(masks, "ATime")
}
if s.MTime {
masks = append(masks, "MTime")
}
if s.CTime {
masks = append(masks, "CTime")
}
if s.ATimeNotSystemTime {
masks = append(masks, "ATimeNotSystemTime")
}
if s.MTimeNotSystemTime {
masks = append(masks, "MTimeNotSystemTime")
}
return fmt.Sprintf("SetAttrMask{with: %s}", strings.Join(masks, " "))
}
// Empty returns true if no fields are masked.
func (s SetAttrMask) Empty() bool {
return !s.Permissions && !s.UID && !s.GID && !s.Size && !s.ATime && !s.MTime && !s.CTime && !s.ATimeNotSystemTime && !s.MTimeNotSystemTime
}
// Decode implements encoder.Decode.
func (s *SetAttrMask) Decode(b *buffer) {
mask := b.Read32()
s.Permissions = mask&0x00000001 != 0
s.UID = mask&0x00000002 != 0
s.GID = mask&0x00000004 != 0
s.Size = mask&0x00000008 != 0
s.ATime = mask&0x00000010 != 0
s.MTime = mask&0x00000020 != 0
s.CTime = mask&0x00000040 != 0
s.ATimeNotSystemTime = mask&0x00000080 != 0
s.MTimeNotSystemTime = mask&0x00000100 != 0
}
func (s SetAttrMask) bitmask() uint32 {
var mask uint32
if s.Permissions {
mask |= 0x00000001
}
if s.UID {
mask |= 0x00000002
}
if s.GID {
mask |= 0x00000004
}
if s.Size {
mask |= 0x00000008
}
if s.ATime {
mask |= 0x00000010
}
if s.MTime {
mask |= 0x00000020
}
if s.CTime {
mask |= 0x00000040
}
if s.ATimeNotSystemTime {
mask |= 0x00000080
}
if s.MTimeNotSystemTime {
mask |= 0x00000100
}
return mask
}
// Encode implements encoder.Encode.
func (s *SetAttrMask) Encode(b *buffer) {
b.Write32(s.bitmask())
}
// SetAttr specifies a set of attributes for a setattr.
type SetAttr struct {
Permissions FileMode
UID UID
GID GID
Size uint64
ATimeSeconds uint64
ATimeNanoSeconds uint64
MTimeSeconds uint64
MTimeNanoSeconds uint64
}
// String implements fmt.Stringer.
func (s SetAttr) String() string {
return fmt.Sprintf("SetAttr{Permissions: 0o%o, UID: %d, GID: %d, Size: %d, ATime: {Sec: %d, NanoSec: %d}, MTime: {Sec: %d, NanoSec: %d}}", s.Permissions, s.UID, s.GID, s.Size, s.ATimeSeconds, s.ATimeNanoSeconds, s.MTimeSeconds, s.MTimeNanoSeconds)
}
// Decode implements encoder.Decode.
func (s *SetAttr) Decode(b *buffer) {
s.Permissions = b.ReadPermissions()
s.UID = b.ReadUID()
s.GID = b.ReadGID()
s.Size = b.Read64()
s.ATimeSeconds = b.Read64()
s.ATimeNanoSeconds = b.Read64()
s.MTimeSeconds = b.Read64()
s.MTimeNanoSeconds = b.Read64()
}
// Encode implements encoder.Encode.
func (s *SetAttr) Encode(b *buffer) {
b.WritePermissions(s.Permissions)
b.WriteUID(s.UID)
b.WriteGID(s.GID)
b.Write64(s.Size)
b.Write64(s.ATimeSeconds)
b.Write64(s.ATimeNanoSeconds)
b.Write64(s.MTimeSeconds)
b.Write64(s.MTimeNanoSeconds)
}
// Apply applies this to the given Attr.
func (a *Attr) Apply(mask SetAttrMask, attr SetAttr) {
if mask.Permissions {
a.Mode = a.Mode&^permissionsMask | (attr.Permissions & permissionsMask)
}
if mask.UID {
a.UID = attr.UID
}
if mask.GID {
a.GID = attr.GID
}
if mask.Size {
a.Size = attr.Size
}
if mask.ATime {
a.ATimeSeconds = attr.ATimeSeconds
a.ATimeNanoSeconds = attr.ATimeNanoSeconds
}
if mask.MTime {
a.MTimeSeconds = attr.MTimeSeconds
a.MTimeNanoSeconds = attr.MTimeNanoSeconds
}
}
// Dirent is used for readdir.
type Dirent struct {
// QID is the entry QID.
QID QID
// Offset is the offset in the directory.
//
// This will be communicated back the original caller.
Offset uint64
// Type is the 9P type.
Type QIDType
// Name is the name of the entry (i.e. basename).
Name string
}
// String implements fmt.Stringer.
func (d Dirent) String() string {
return fmt.Sprintf("Dirent{QID: %d, Offset: %d, Type: 0x%X, Name: %s}", d.QID, d.Offset, d.Type, d.Name)
}
// Decode implements encoder.Decode.
func (d *Dirent) Decode(b *buffer) {
d.QID.Decode(b)
d.Offset = b.Read64()
d.Type = b.ReadQIDType()
d.Name = b.ReadString()
}
// Encode implements encoder.Encode.
func (d *Dirent) Encode(b *buffer) {
d.QID.Encode(b)
b.Write64(d.Offset)
b.WriteQIDType(d.Type)
b.WriteString(d.Name)
}
// AllocateMode are possible modes to p9.File.Allocate().
type AllocateMode struct {
KeepSize bool
PunchHole bool
NoHideStale bool
CollapseRange bool
ZeroRange bool
InsertRange bool
Unshare bool
}
// ToLinux converts to a value compatible with fallocate(2)'s mode.
func (a *AllocateMode) ToLinux() uint32 {
rv := uint32(0)
if a.KeepSize {
rv |= unix.FALLOC_FL_KEEP_SIZE
}
if a.PunchHole {
rv |= unix.FALLOC_FL_PUNCH_HOLE
}
if a.NoHideStale {
rv |= unix.FALLOC_FL_NO_HIDE_STALE
}
if a.CollapseRange {
rv |= unix.FALLOC_FL_COLLAPSE_RANGE
}
if a.ZeroRange {
rv |= unix.FALLOC_FL_ZERO_RANGE
}
if a.InsertRange {
rv |= unix.FALLOC_FL_INSERT_RANGE
}
if a.Unshare {
rv |= unix.FALLOC_FL_UNSHARE_RANGE
}
return rv
}
// Decode implements encoder.Decode.
func (a *AllocateMode) Decode(b *buffer) {
mask := b.Read32()
a.KeepSize = mask&0x01 != 0
a.PunchHole = mask&0x02 != 0
a.NoHideStale = mask&0x04 != 0
a.CollapseRange = mask&0x08 != 0
a.ZeroRange = mask&0x10 != 0
a.InsertRange = mask&0x20 != 0
a.Unshare = mask&0x40 != 0
}
// Encode implements encoder.Encode.
func (a *AllocateMode) Encode(b *buffer) {
mask := uint32(0)
if a.KeepSize {
mask |= 0x01
}
if a.PunchHole {
mask |= 0x02
}
if a.NoHideStale {
mask |= 0x04
}
if a.CollapseRange {
mask |= 0x08
}
if a.ZeroRange {
mask |= 0x10
}
if a.InsertRange {
mask |= 0x20
}
if a.Unshare {
mask |= 0x40
}
b.Write32(mask)
}
|