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