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