Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro committed Mar 24, 2023
1 parent e7ca8c8 commit ca67d98
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
4 changes: 2 additions & 2 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func clone(ctx *cli.Context) error {
return fmt.Errorf("the clone DST path should be at the same mount point as the SRC path")
}

if strings.HasPrefix(strings.TrimLeft(dstAbsPath, dstMp), strings.TrimLeft(srcAbsPath, srcMp)) {
if strings.HasPrefix(dstAbsPath, srcAbsPath) {
return fmt.Errorf("the clone DST path should not be under the SRC path")
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func clone(ctx *cli.Context) error {
wb.Put16(uint16(umask))
wb.Put8(cmode)
var f *os.File
f, err = openController(srcMp)
f, err = findAndOpenControlFile(srcMp)
if err == nil {
return err
}
Expand Down
20 changes: 4 additions & 16 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,9 @@ func info(ctx *cli.Context) error {
if inode < uint64(meta.RootInode) {
logger.Fatalf("inode number shouldn't be less than %d", meta.RootInode)
}
mp, err := findMountpoint(d)
f, err := findAndOpenControlFile(d)
if err != nil {
logger.Errorf("find mountpoint: %s", err)
continue
}
var f *os.File
f, err = openController(mp)
if err != nil {
logger.Errorf("open controller: %s", err)
logger.Errorf("open control file for: %s", err)
continue
}

Expand Down Expand Up @@ -250,15 +244,9 @@ func ltypeToString(t uint32) string {
}

func legacyInfo(d, path string, inode uint64, recursive, raw uint8) {
mp, err := findMountpoint(d)
if err != nil {
logger.Errorf("find mountpoint: %s", err)
return
}
var f *os.File
f, err = openController(mp)
f, err := findAndOpenControlFile(d)
if err != nil {
logger.Errorf("open controller: %s", err)
logger.Errorf("open control file for: %s", err)
return
}
defer f.Close()
Expand Down
17 changes: 7 additions & 10 deletions cmd/rmr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ $ juicefs rmr /mnt/jfs/foo`,
}
}

func openController(mp string) (*os.File, error) {
func findAndOpenControlFile(path string) (*os.File, error) {
mp, err := findMountpoint(path)
if err != nil {
return nil, err
}
return os.OpenFile(filepath.Join(mp, ".control"), os.O_RDWR, 0)
}

Expand All @@ -67,16 +71,9 @@ func rmr(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("lookup inode for %s: %s", d, err)
}
var mp string
mp, err = findMountpoint(d)
if err != nil {
logger.Errorf("find mountpoint for %s: %s", d, err)
continue
}
var f *os.File
f, err = openController(mp)
f, err := findAndOpenControlFile(d)
if err != nil {
logger.Errorf("open controller: %s", err)
logger.Errorf("open control file for: %s", err)
continue
}
wb := utils.NewBuffer(8 + 8 + 1 + uint32(len(name)))
Expand Down
14 changes: 2 additions & 12 deletions cmd/warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,12 @@ func warmup(ctx *cli.Context) error {
if err != nil {
return err
}
controller, err := openController(mp)
controller, err := findAndOpenControlFile(mp)
if err != nil {
return fmt.Errorf("open control file for %s", first)
return fmt.Errorf("open control file for: %s", first)
}
defer controller.Close()

for ; mp != "/"; mp = filepath.Dir(mp) {
inode, err := utils.GetFileInode(mp)
if err != nil {
logger.Fatalf("lookup inode for %s: %s", mp, err)
}
if inode == uint64(meta.RootInode) {
break
}
}

threads := ctx.Uint("threads")
if threads == 0 {
logger.Warnf("threads should be larger than 0, reset it to 1")
Expand Down

0 comments on commit ca67d98

Please sign in to comment.