github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/repo/fs/files.go (about)

     1  package fsrepo
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  type basepath string
    11  
    12  func (bp basepath) filepath(f File) string {
    13  	return filepath.Join(string(bp), Filepath(f))
    14  }
    15  
    16  func (bp basepath) readBytes(f File) ([]byte, error) {
    17  	return ioutil.ReadFile(bp.filepath(f))
    18  }
    19  
    20  func (bp basepath) saveFile(d interface{}, f File) error {
    21  	data, err := json.Marshal(d)
    22  	if err != nil {
    23  		log.Debug(err.Error())
    24  		return err
    25  	}
    26  	return ioutil.WriteFile(bp.filepath(f), data, os.ModePerm)
    27  }
    28  
    29  // File represents a type file in a qri repository
    30  type File int
    31  
    32  const (
    33  	// FileUnknown makes the default file value invalid
    34  	FileUnknown File = iota
    35  	// FileLockfile is the on-disk mutex lock
    36  	FileLockfile
    37  	// FileInfo stores information about this repository
    38  	// like version number, size of repo, etc.
    39  	FileInfo
    40  	// FileConfig holds configuration specific to this repo
    41  	FileConfig
    42  	// FileDatasets holds the list of datasets
    43  	FileDatasets
    44  	// FileEventLogs is a log of all queries in order they're run
    45  	FileEventLogs
    46  	// FileJSONRefs is a file for the user's local namespace
    47  	// No longer in use
    48  	FileJSONRefs
    49  	// FileDscache is a flatbuffer file of this repo's dataset cache
    50  	FileDscache
    51  	// FileRefs is a flatbuffer file of this repo's dataset references
    52  	FileRefs
    53  	// FilePeers holds peer repositories
    54  	// Ideally this won't stick around for long
    55  	FilePeers
    56  	// FileAnalytics holds analytics data
    57  	FileAnalytics
    58  	// FileSearchIndex is the path to a search index
    59  	FileSearchIndex
    60  	// FileSelectedRefs is the path to the current ref selection
    61  	FileSelectedRefs
    62  	// FileChangeRequests is a file of change requests
    63  	FileChangeRequests
    64  )
    65  
    66  var paths = map[File]string{
    67  	FileUnknown:        "",
    68  	FileLockfile:       "/repo.lock",
    69  	FileInfo:           "/info.json",
    70  	FileConfig:         "/config.json",
    71  	FileDatasets:       "/datasets.json",
    72  	FileEventLogs:      "/events.json",
    73  	FileJSONRefs:       "/ds_refs.json",
    74  	FileDscache:        "/dscache.fbs",
    75  	FileRefs:           "/refs.fbs",
    76  	FilePeers:          "/peers.json",
    77  	FileAnalytics:      "/analytics.json",
    78  	FileSearchIndex:    "/index.bleve",
    79  	FileSelectedRefs:   "/selected_refs.json",
    80  	FileChangeRequests: "/change_requests.json",
    81  }
    82  
    83  // Filepath gives the relative filepath to a repofiles
    84  // in a given repository
    85  func Filepath(rf File) string {
    86  	return paths[rf]
    87  }