github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/ext/disklayout/dirent_new.go (about) 1 // Copyright 2019 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package disklayout 16 17 import ( 18 "fmt" 19 20 "github.com/SagerNet/gvisor/pkg/sentry/fs" 21 ) 22 23 // DirentNew represents the ext4 directory entry struct. This emulates Linux's 24 // ext4_dir_entry_2 struct. The FileName can not be more than 255 bytes so we 25 // only need 8 bits to store the NameLength. As a result, NameLength has been 26 // shortened and the other 8 bits are used to encode the file type. Use the 27 // FileTypeRaw field only if the SbDirentFileType feature is set. 28 // 29 // Note: This struct can be of variable size on disk. The one described below 30 // is of maximum size and the FileName beyond NameLength bytes might contain 31 // garbage. 32 // 33 // +marshal 34 type DirentNew struct { 35 InodeNumber uint32 36 RecordLength uint16 37 NameLength uint8 38 FileTypeRaw uint8 39 FileNameRaw [MaxFileName]byte `marshal:"unaligned"` 40 } 41 42 // Compiles only if DirentNew implements Dirent. 43 var _ Dirent = (*DirentNew)(nil) 44 45 // Inode implements Dirent.Inode. 46 func (d *DirentNew) Inode() uint32 { return d.InodeNumber } 47 48 // RecordSize implements Dirent.RecordSize. 49 func (d *DirentNew) RecordSize() uint16 { return d.RecordLength } 50 51 // FileName implements Dirent.FileName. 52 func (d *DirentNew) FileName() string { 53 return string(d.FileNameRaw[:d.NameLength]) 54 } 55 56 // FileType implements Dirent.FileType. 57 func (d *DirentNew) FileType() (fs.InodeType, bool) { 58 if inodeType, ok := inodeTypeByFileType[d.FileTypeRaw]; ok { 59 return inodeType, true 60 } 61 62 panic(fmt.Sprintf("unknown file type %v", d.FileTypeRaw)) 63 }