github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libfuse/prefetch_file.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  //
     5  //go:build !windows
     6  // +build !windows
     7  
     8  package libfuse
     9  
    10  import (
    11  	"bazil.org/fuse"
    12  	"bazil.org/fuse/fs"
    13  	"github.com/keybase/client/go/kbfs/libkbfs"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  // PrefetchFile represents a write-only file where any write of at least one
    18  // byte triggers either disabling or enabling prefetching.  It is mainly useful
    19  // for testing.
    20  type PrefetchFile struct {
    21  	fs     *FS
    22  	enable bool
    23  }
    24  
    25  var _ fs.Node = (*PrefetchFile)(nil)
    26  
    27  // Attr implements the fs.Node interface for PrefetchFile.
    28  func (f *PrefetchFile) Attr(ctx context.Context, a *fuse.Attr) error {
    29  	a.Size = 0
    30  	a.Mode = 0222
    31  	return nil
    32  }
    33  
    34  var _ fs.Handle = (*PrefetchFile)(nil)
    35  
    36  var _ fs.HandleWriter = (*PrefetchFile)(nil)
    37  
    38  // Write implements the fs.HandleWriter interface for PrefetchFile.
    39  func (f *PrefetchFile) Write(ctx context.Context, req *fuse.WriteRequest,
    40  	resp *fuse.WriteResponse) (err error) {
    41  	f.fs.log.CDebugf(ctx, "PrefetchFile (enable: %t) Write", f.enable)
    42  	defer func() { err = f.fs.processError(ctx, libkbfs.WriteMode, err) }()
    43  	if len(req.Data) == 0 {
    44  		return nil
    45  	}
    46  
    47  	f.fs.config.BlockOps().TogglePrefetcher(f.enable)
    48  
    49  	resp.Size = len(req.Data)
    50  	return nil
    51  }