Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/clone: check clone path #3369

Merged
merged 6 commits into from
Mar 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ func clone(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("abs of %s: %s", dst, err)
}

srcMp, err := findMountpoint(srcAbsPath)
if err != nil {
return err
}
dstMp, err := findMountpoint(filepath.Dir(dstAbsPath))
davies marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
if srcMp != dstMp {
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)) {
zhijian-pro marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("the clone DST path should not be under the SRC path")
}

dstParent := filepath.Dir(dstAbsPath)
dstName := filepath.Base(dstAbsPath)
dstParentIno, err := utils.GetFileInode(dstParent)
Expand Down Expand Up @@ -127,3 +144,20 @@ func clone(ctx *cli.Context) error {
}
return nil
}
func findMountpoint(dir string) (string, error) {
zhijian-pro marked this conversation as resolved.
Show resolved Hide resolved
odir := dir
for dir != "" {
inode, err := utils.GetFileInode(dir)
if err != nil {
return "", fmt.Errorf("get inode of %s: %s", dir, err)
}
if inode == 1 {
zhijian-pro marked this conversation as resolved.
Show resolved Hide resolved
return dir, nil
}
dir = filepath.Dir(dir)
if dir == "/" {
return "", fmt.Errorf("%s is not inside JuiceFS", odir)
}
}
return "", fmt.Errorf("%s is not inside JuiceFS", odir)
}