path

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2021 License: Apache-2.0, MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BtfsPathKey  = "BTFS_PATH"
	BtfsPathFlag = "BTFS_PATH_FLAG"
)

Variables

View Source
var (
	StorePath  string
	OriginPath string
)
View Source
var Executable = func() string {
	if ex, err := os.Executable(); err == nil {
		return ex
	}
	return "btfs"
}()
View Source
var PathCapacityCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "Get free space of passed path.",
		ShortDescription: "Get free space of passed path.",
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("path-name", true, true,
			"New BTFS Path. Should be absolute path."),
	},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		path := strings.Trim(req.Arguments[0], " ")
		if path == "" {
			return fmt.Errorf("path is not defined")
		}
		var err error
		if !filepath.IsAbs(path) {
			path, err = filepath.Abs(path)
			if err != nil {
				return err
			}
		}
		if btfsPath != "" {
			if btfsPath != StorePath {
				OriginPath = btfsPath
			} else {
				return fmt.Errorf("specifed path is same with current path")
			}
		} else if envBtfsPath := os.Getenv(BtfsPathKey); envBtfsPath != "" {
			OriginPath = envBtfsPath
		} else if home, err := homedir.Expand(defaultPath); err == nil && home != "" {
			OriginPath = home
		} else {
			return fmt.Errorf("can not find the original stored path")
		}
		if err := validatePath(OriginPath, path); err != nil {
			return err
		}
		if !CheckDirEmpty(path) {
			return fmt.Errorf("path %s is not empty", path)
		}
		valid := true
		usage, err := disk.UsageWithContext(req.Context, filepath.Dir(path))
		if err != nil {
			return err
		}
		humanizedFreeSpace := humanize.Bytes(usage.Free)
		return cmds.EmitOnce(res, &PathCapacity{
			FreeSpace:          usage.Free,
			Valid:              valid,
			HumanizedFreeSpace: humanizedFreeSpace,
		})
	},
	Type: &PathCapacity{},
}
View Source
var PathCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Modify the Host storage folder path for BTFS client.",
		ShortDescription: `
The default local repository path is located at ~/.btfs folder, in order to
improve the hard disk space usage, provide the function to change the original 
storage location, a specified path as a parameter need to be passed.
`,
	},
	Subcommands: map[string]*cmds.Command{
		"status":   PathStatusCmd,
		"capacity": PathCapacityCmd,
		"migrate":  PathMigrateCmd,
		"list":     PathListCmd,
		"mkdir":    PathMkdirCmd,
		"volumes":  PathVolumesCmd,
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("path-name", true, false,
			"New BTFS Path.Should be absolute path."),
		cmds.StringArg("storage-size", true, false, "Storage Commitment Size"),
	},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		locked := lock.TryLock()
		if locked {
			defer lock.Unlock()
		} else {
			return errors.New("Cannot set path concurrently.")
		}
		StorePath = strings.Trim(req.Arguments[0], " ")

		if StorePath == "" {
			return fmt.Errorf("path is not defined")
		}
		var err error
		if StorePath, err = homedir.Expand(StorePath); err != nil {
			return err
		}
		if !filepath.IsAbs(StorePath) {
			StorePath, err = filepath.Abs(StorePath)
			if err != nil {
				return err
			}
		}
		if btfsPath != "" {
			if btfsPath != StorePath {
				OriginPath = btfsPath
			} else {
				return fmt.Errorf("specifed path is same with current path")
			}
		} else if envBtfsPath := os.Getenv(BtfsPathKey); envBtfsPath != "" {
			OriginPath = envBtfsPath
		} else if home, err := homedir.Expand(defaultPath); err == nil && home != "" {
			OriginPath = home
		} else {
			return fmt.Errorf("can not find the original stored path")
		}

		if err := validatePath(OriginPath, StorePath); err != nil {
			return err
		}

		usage, err := disk.UsageWithContext(req.Context, filepath.Dir(StorePath))
		if err != nil {
			return err
		}
		promisedStorageSize, err := humanize.ParseBytes(req.Arguments[1])
		if err != nil {
			return err
		}
		if usage.Free < promisedStorageSize {
			return fmt.Errorf("Not enough disk space, expect: ge %v bytes, actual: %v bytes",
				promisedStorageSize, usage.Free)
		}
		go func() {
			if e := os.Unsetenv(BtfsPathFlag); e != nil {
				log.Error(e)
			}
			DoRestart(true)
		}()
		return nil
	},
}
View Source
var PathListCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "list directories",
		ShortDescription: "list directories",
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("parent", true, true,
			"parent path, should be absolute path."),
	},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		list, err := list(req.Arguments[0])
		if err != nil {
			return err
		}
		return cmds.EmitOnce(res, stringList{Strings: list})
	},
	Type: stringList{},
}
View Source
var PathMigrateCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "path migrate. e.x.: btfs storage path migrate /Users/tron/.btfs.new",
		ShortDescription: "path migrate.",
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("btfs-dir", true, true,
			"Current BTFS Path. Should be absolute path."),
	},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		if _, k := os.LookupEnv(BtfsPathKey); k || CheckExist(srcProperties) || CheckExist(PropertiesFileName) {
			return errors.New("no need to migrate")
		}
		fmt.Printf("Writing \"%s\" to %s\n", req.Arguments[0], PropertiesFileName)
		return ioutil.WriteFile(PropertiesFileName, []byte(req.Arguments[0]), os.ModePerm)
	},
}
View Source
var PathMkdirCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "add folder",
		ShortDescription: "add folder",
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("parent", true, false,
			"parent path, should be absolute path."),
		cmds.StringArg("name", true, false,
			"path name"),
	},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		return add(req.Arguments[0], req.Arguments[1])
	},
}
View Source
var PathStatusCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "Get status of resetting path.",
		ShortDescription: "Get status of resetting path.",
	},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		tryLock := lock.TryLock()
		if tryLock {
			lock.Unlock()
		}
		return cmds.EmitOnce(res, PathStatus{
			Resetting: !tryLock,
			Path:      StorePath,
		})
	},
	Type: PathStatus{},
}
View Source
var PathVolumesCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline:          "list disk volumes",
		ShortDescription: "list disk volumes",
	},
	Arguments: []cmds.Argument{},
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		ps, err := volumes()
		if err != nil {
			return err
		}
		return cmds.EmitOnce(res, ps)
	},
	Type: []volume{},
}
View Source
var (
	PropertiesFileName string
)

Functions

func CheckDirEmpty

func CheckDirEmpty(dirname string) bool

func CheckExist

func CheckExist(pathName string) bool

func DoRestart

func DoRestart(p bool)

func MoveFolder

func MoveFolder() error

func ReadProperties

func ReadProperties(filePath string) string

func SetEnvVariables

func SetEnvVariables()

func WriteProperties

func WriteProperties() error

Types

type Mutex

type Mutex struct {
	sync.Mutex
}

func (*Mutex) TryLock

func (m *Mutex) TryLock() bool

type PathCapacity

type PathCapacity struct {
	FreeSpace          uint64
	Valid              bool
	HumanizedFreeSpace string
}

type PathStatus

type PathStatus struct {
	Resetting bool
	Path      string
}

type Storage

type Storage struct {
	Name       string
	FileSystem string
	Total      uint64
	Free       uint64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL