Move Follow symlink to pkg
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
4086e37e27
commit
e8a8022ba3
7 changed files with 185 additions and 0 deletions
72
symlink/fs.go
Normal file
72
symlink/fs.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package symlink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FollowSymlink will follow an existing link and scope it to the root
|
||||
// path provided.
|
||||
func FollowSymlinkInScope(link, root string) (string, error) {
|
||||
prev := "/"
|
||||
|
||||
root, err := filepath.Abs(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
link, err = filepath.Abs(link)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(filepath.Dir(link), root) {
|
||||
return "", fmt.Errorf("%s is not within %s", link, root)
|
||||
}
|
||||
|
||||
for _, p := range strings.Split(link, "/") {
|
||||
prev = filepath.Join(prev, p)
|
||||
prev = filepath.Clean(prev)
|
||||
|
||||
for {
|
||||
if !strings.HasPrefix(prev, root) {
|
||||
// Don't resolve symlinks outside of root. For example,
|
||||
// we don't have to check /home in the below.
|
||||
//
|
||||
// /home -> usr/home
|
||||
// FollowSymlinkInScope("/home/bob/foo/bar", "/home/bob/foo")
|
||||
break
|
||||
}
|
||||
|
||||
stat, err := os.Lstat(prev)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
break
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if stat.Mode()&os.ModeSymlink == os.ModeSymlink {
|
||||
dest, err := os.Readlink(prev)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
switch dest[0] {
|
||||
case '/':
|
||||
prev = filepath.Join(root, dest)
|
||||
case '.':
|
||||
prev, _ = filepath.Abs(prev)
|
||||
|
||||
if prev = filepath.Clean(filepath.Join(filepath.Dir(prev), dest)); len(prev) < len(root) {
|
||||
prev = filepath.Join(root, filepath.Base(dest))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return prev, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue