github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libfuse/rekey_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  // RekeyFile represents a write-only file when any write of at least
    18  // one byte triggers a rekey of the folder.
    19  type RekeyFile struct {
    20  	folder *Folder
    21  }
    22  
    23  var _ fs.Node = (*RekeyFile)(nil)
    24  
    25  // Attr implements the fs.Node interface for RekeyFile.
    26  func (f *RekeyFile) Attr(ctx context.Context, a *fuse.Attr) error {
    27  	a.Size = 0
    28  	a.Mode = 0222
    29  	return nil
    30  }
    31  
    32  var _ fs.Handle = (*RekeyFile)(nil)
    33  
    34  var _ fs.HandleWriter = (*RekeyFile)(nil)
    35  
    36  // Write implements the fs.HandleWriter interface for RekeyFile.
    37  func (f *RekeyFile) Write(ctx context.Context, req *fuse.WriteRequest,
    38  	resp *fuse.WriteResponse) (err error) {
    39  	f.folder.fs.log.CDebugf(ctx, "RekeyFile Write")
    40  	defer func() { err = f.folder.processError(ctx, libkbfs.WriteMode, err) }()
    41  	if len(req.Data) == 0 {
    42  		return nil
    43  	}
    44  	_, err = libkbfs.RequestRekeyAndWaitForOneFinishEvent(ctx,
    45  		f.folder.fs.config.KBFSOps(), f.folder.getFolderBranch().Tlf)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	f.folder.fs.NotificationGroupWait()
    50  	resp.Size = len(req.Data)
    51  	return nil
    52  }