github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/fsimpl/fuse/directory.go (about) 1 // Copyright 2020 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 fuse 16 17 import ( 18 "github.com/metacubex/gvisor/pkg/abi/linux" 19 "github.com/metacubex/gvisor/pkg/context" 20 "github.com/metacubex/gvisor/pkg/errors/linuxerr" 21 "github.com/metacubex/gvisor/pkg/sentry/kernel/auth" 22 "github.com/metacubex/gvisor/pkg/sentry/vfs" 23 "github.com/metacubex/gvisor/pkg/usermem" 24 ) 25 26 // +stateify savable 27 type directoryFD struct { 28 fileDescription 29 } 30 31 // Allocate implements directoryFD.Allocate. 32 func (*directoryFD) Allocate(ctx context.Context, mode, offset, length uint64) error { 33 return linuxerr.EISDIR 34 } 35 36 // PRead implements vfs.FileDescriptionImpl.PRead. 37 func (*directoryFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) { 38 return 0, linuxerr.EISDIR 39 } 40 41 // Read implements vfs.FileDescriptionImpl.Read. 42 func (*directoryFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) { 43 return 0, linuxerr.EISDIR 44 } 45 46 // PWrite implements vfs.FileDescriptionImpl.PWrite. 47 func (*directoryFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) { 48 return 0, linuxerr.EISDIR 49 } 50 51 // Write implements vfs.FileDescriptionImpl.Write. 52 func (*directoryFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) { 53 return 0, linuxerr.EISDIR 54 } 55 56 // IterDirents implements vfs.FileDescriptionImpl.IterDirents. 57 func (dir *directoryFD) IterDirents(ctx context.Context, callback vfs.IterDirentsCallback) error { 58 fusefs := dir.inode().fs 59 60 in := linux.FUSEReadIn{ 61 Fh: dir.Fh, 62 Offset: uint64(dir.off.Load()), 63 Size: linux.FUSE_PAGE_SIZE, 64 Flags: dir.statusFlags(), 65 } 66 67 // TODO(gVisor.dev/issue/3404): Support FUSE_READDIRPLUS. 68 req := fusefs.conn.NewRequest(auth.CredentialsFromContext(ctx), pidFromContext(ctx), dir.inode().nodeID, linux.FUSE_READDIR, &in) 69 res, err := fusefs.conn.Call(ctx, req) 70 if err != nil { 71 return err 72 } 73 if err := res.Error(); err != nil { 74 return err 75 } 76 77 var out linux.FUSEDirents 78 if err := res.UnmarshalPayload(&out); err != nil { 79 return err 80 } 81 82 for _, fuseDirent := range out.Dirents { 83 nextOff := int64(fuseDirent.Meta.Off) 84 dirent := vfs.Dirent{ 85 Name: fuseDirent.Name, 86 Type: uint8(fuseDirent.Meta.Type), 87 Ino: fuseDirent.Meta.Ino, 88 NextOff: nextOff, 89 } 90 91 if err := callback.Handle(dirent); err != nil { 92 return err 93 } 94 dir.off.Store(nextOff) 95 } 96 97 return nil 98 }