github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libfuse/external_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  	"fmt"
    12  	"os"
    13  	"sync"
    14  	"time"
    15  
    16  	"github.com/keybase/client/go/kbfs/ioutil"
    17  	"golang.org/x/net/context"
    18  )
    19  
    20  func newExternalFile(path string) (*SpecialReadFile, error) { // nolint
    21  	if path == "" {
    22  		return nil, fmt.Errorf("No path for external file")
    23  	}
    24  
    25  	var once sync.Once
    26  	var data []byte
    27  	var err error
    28  	var fileTime time.Time
    29  	return &SpecialReadFile{
    30  		read: func(context.Context) ([]byte, time.Time, error) {
    31  			once.Do(func() {
    32  				var info os.FileInfo
    33  				info, err = ioutil.Stat(path)
    34  				if err != nil {
    35  					return
    36  				}
    37  				fileTime = info.ModTime()
    38  				data, err = os.ReadFile(path)
    39  			})
    40  			return data, fileTime, err
    41  		},
    42  	}, nil
    43  }