qid.go (1602B)
1 package lib9p 2 3 import ( 4 "fmt" 5 ) 6 7 // QidType represents the type of Qid. 8 type QidType uint8 9 10 const ( 11 QTDIR QidType = 0x80 /* type bit for directories */ 12 QTAPPEND = 0x40 /* type bit for append only files */ 13 QTEXCL = 0x20 /* type bit for exclusive use files */ 14 QTMOUNT = 0x10 /* type bit for mounted channel */ 15 QTAUTH = 0x08 /* type bit for authentication file */ 16 QTTMP = 0x04 /* type bit for non-backed-up file */ 17 QTSYMLINK = 0x02 /* type bit for symbolic link */ 18 QTFILE = 0x00 /* type bits for plain file */ 19 ) 20 21 // Qid is the identifier of each file in 9P server. 22 type Qid struct { 23 Type QidType // type of the file. 24 Vers uint32 // version of the file. 25 Path uint64 // uniq number of each file. 26 } 27 28 // unmarshalQid converts a byte array of Qid defined by 9P protocol 29 // into a Qid struct. 30 func unmarshalQid(b []byte) Qid { 31 return Qid{ 32 Type: QidType(b[0]), 33 Vers: gbit32(b[1:5]), 34 Path: gbit64(b[5:13]), 35 } 36 } 37 38 // marshal converts the Qid into a byte array of Qid defined by 9P protocol. 39 func (q Qid) marshal() []byte { 40 buf := make([]byte, 13) 41 buf[0] = uint8(q.Type) 42 pbit32(buf[1:5], q.Vers) 43 pbit64(buf[5:13], q.Path) 44 return buf 45 } 46 47 func (q Qid) String() string { 48 return fmt.Sprintf("(%016d %d %s)", q.Path, q.Vers, q.typeStr()) 49 } 50 51 // TypeStr returns the q.Type() as string for debugging information. 52 func (q Qid) typeStr() string { 53 var s string 54 t := q.Type 55 if t&QTDIR != 0 { 56 s += "d" 57 } 58 if t&QTAPPEND != 0 { 59 s += "a" 60 } 61 if t&QTEXCL != 0 { 62 s += "l" 63 } 64 if t&QTAUTH != 0 { 65 s += "A" 66 } 67 return s 68 }