github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/fsrpc/fs.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  package fsrpc
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/keybase/client/go/kbfs/data"
    11  	"github.com/keybase/client/go/kbfs/libkbfs"
    12  	"github.com/keybase/client/go/logger"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  type fs struct {
    18  	config libkbfs.Config
    19  	log    logger.Logger
    20  }
    21  
    22  // NewFS returns a new FS protocol implementation
    23  func NewFS(config libkbfs.Config, log logger.Logger) keybase1.FsInterface {
    24  	return &fs{config: config, log: log}
    25  }
    26  
    27  func (f fs) favorites(ctx context.Context, path Path) (keybase1.ListResult, error) {
    28  	f.config.GetPerfLog().CDebugf(ctx, "GetFavorites fsrpc")
    29  	favs, err := f.config.KBFSOps().GetFavorites(ctx)
    30  	if err != nil {
    31  		return keybase1.ListResult{}, err
    32  	}
    33  	files := []keybase1.File{}
    34  	for _, fav := range favs {
    35  		if fav.Type == path.TLFType {
    36  			favPath, err := path.Join(fav.Name)
    37  			if err != nil {
    38  				return keybase1.ListResult{}, err
    39  			}
    40  			files = append(files, keybase1.File{Path: favPath.String()})
    41  		}
    42  	}
    43  	return keybase1.ListResult{Files: files}, nil
    44  }
    45  
    46  func (f fs) tlf(ctx context.Context, path Path) (keybase1.ListResult, error) {
    47  	files := []keybase1.File{}
    48  
    49  	node, de, err := path.GetNode(ctx, f.config)
    50  	if err != nil {
    51  		return keybase1.ListResult{}, err
    52  	}
    53  
    54  	if node == nil {
    55  		return keybase1.ListResult{}, fmt.Errorf("Node not found for path: %s", path)
    56  	}
    57  
    58  	if de.Type == data.Dir {
    59  		children, err := f.config.KBFSOps().GetDirChildren(ctx, node)
    60  		if err != nil {
    61  			return keybase1.ListResult{}, err
    62  		}
    63  
    64  		// For entryInfo: for name, entryInfo := range children
    65  		for name := range children {
    66  			dirPath, err := path.Join(name.Plaintext())
    67  			if err != nil {
    68  				return keybase1.ListResult{}, err
    69  			}
    70  			files = append(files, keybase1.File{Path: dirPath.String()})
    71  		}
    72  	} else {
    73  		_, name, err := path.DirAndBasename()
    74  		if err != nil {
    75  			return keybase1.ListResult{}, err
    76  		}
    77  		filePath, err := path.Join(name)
    78  		if err != nil {
    79  			return keybase1.ListResult{}, err
    80  		}
    81  		files = append(files, keybase1.File{Path: filePath.String()})
    82  	}
    83  	return keybase1.ListResult{Files: files}, nil
    84  }
    85  
    86  func (f fs) keybase(ctx context.Context) (keybase1.ListResult, error) {
    87  	return keybase1.ListResult{
    88  		Files: []keybase1.File{
    89  			{Path: "/keybase/public"},
    90  			{Path: "/keybase/private"},
    91  		},
    92  	}, nil
    93  }
    94  
    95  func (f fs) root(ctx context.Context) (keybase1.ListResult, error) {
    96  	return keybase1.ListResult{
    97  		Files: []keybase1.File{
    98  			{Path: "/keybase"},
    99  		},
   100  	}, nil
   101  }
   102  
   103  // List implements keybase1.FsInterface
   104  func (f *fs) List(ctx context.Context, arg keybase1.ListArg) (keybase1.ListResult, error) {
   105  	f.log.CDebugf(ctx, "Listing %q", arg.Path)
   106  
   107  	kbfsPath, err := NewPath(arg.Path)
   108  	if err != nil {
   109  		return keybase1.ListResult{}, err
   110  	}
   111  
   112  	var result keybase1.ListResult
   113  	switch kbfsPath.PathType {
   114  	case RootPathType:
   115  		result, err = f.root(ctx)
   116  	case KeybasePathType:
   117  		result, err = f.keybase(ctx)
   118  	case KeybaseChildPathType:
   119  		result, err = f.favorites(ctx, kbfsPath)
   120  	default:
   121  		result, err = f.tlf(ctx, kbfsPath)
   122  	}
   123  	if err != nil {
   124  		f.log.CErrorf(ctx, "Error listing path %q: %s", arg.Path, err)
   125  	}
   126  	return result, err
   127  }