github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/kbfs_mount.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package service 5 6 import ( 7 "os" 8 "path/filepath" 9 "time" 10 11 "golang.org/x/net/context" 12 13 "github.com/keybase/client/go/libkb" 14 keybase1 "github.com/keybase/client/go/protocol/keybase1" 15 "github.com/keybase/go-framed-msgpack-rpc/rpc" 16 ) 17 18 type KBFSMountHandler struct { 19 *BaseHandler 20 libkb.Contextified 21 } 22 23 func NewKBFSMountHandler(xp rpc.Transporter, g *libkb.GlobalContext) *KBFSMountHandler { 24 return &KBFSMountHandler{ 25 BaseHandler: NewBaseHandler(g, xp), 26 Contextified: libkb.NewContextified(g), 27 } 28 } 29 30 func (h *KBFSMountHandler) GetCurrentMountDir(ctx context.Context) (res string, err error) { 31 return h.G().Env.GetMountDir() 32 } 33 34 const waitForDirectMountTimeout = 10 * time.Second 35 const waitForDirectMountPollInterval = time.Second 36 37 func (h *KBFSMountHandler) WaitForMounts(ctx context.Context) (active bool, err error) { 38 ctx, cancel := context.WithTimeout(ctx, waitForDirectMountTimeout) 39 defer cancel() 40 mount, err := h.GetCurrentMountDir(ctx) 41 if err != nil { 42 return false, err 43 } 44 directMountFileToCheck := filepath.Join(mount, ".kbfs_error") 45 ticker := time.NewTicker(waitForDirectMountPollInterval) 46 defer ticker.Stop() 47 directMountFound, preferredMountFound := false, false 48 for !directMountFound || !preferredMountFound { 49 select { 50 case <-ticker.C: 51 if !directMountFound { 52 fi, err := os.Stat(directMountFileToCheck) 53 if err == nil && !fi.IsDir() { 54 directMountFound = true 55 } 56 // Not check os.IsNotExist here because it can be permission 57 // error too. So just wait it out. 58 } 59 if !preferredMountFound { 60 if len(libkb.FindPreferredKBFSMountDirs()) > 0 { 61 preferredMountFound = true 62 } 63 } 64 case <-ctx.Done(): 65 return false, nil 66 } 67 } 68 return true, nil 69 } 70 71 func (h *KBFSMountHandler) GetPreferredMountDirs(ctx context.Context) (res []string, err error) { 72 res = libkb.FindPreferredKBFSMountDirs() 73 directMount, err := h.G().Env.GetMountDir() 74 if err != nil { 75 return nil, err 76 } 77 res = append(res, directMount) 78 return res, nil 79 } 80 81 func (h *KBFSMountHandler) GetAllAvailableMountDirs(ctx context.Context) (res []string, err error) { 82 return getMountDirs() 83 } 84 85 func (h *KBFSMountHandler) SetCurrentMountDir(_ context.Context, drive string) (err error) { 86 oldMount, _ := h.G().Env.GetMountDir() 87 w := h.G().Env.GetConfigWriter() 88 err = w.SetStringAtPath("mountdir", drive) 89 if err != nil { 90 return err 91 } 92 err = h.G().ConfigReload() 93 if err != nil { 94 return err 95 } 96 return libkb.ChangeMountIcon(oldMount, drive) 97 } 98 99 func (h *KBFSMountHandler) GetKBFSPathInfo(ctx context.Context, standardPath string) (pathInfo keybase1.KBFSPathInfo, err error) { 100 return libkb.GetKBFSPathInfo(standardPath) 101 }