2014-05-13 17:34:30 +00:00
|
|
|
package symlink
|
2013-11-07 20:34:01 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-12 01:12:53 +00:00
|
|
|
"fmt"
|
2013-11-07 20:34:01 +00:00
|
|
|
"os"
|
2014-05-15 22:25:38 +00:00
|
|
|
"path"
|
2013-11-07 20:34:01 +00:00
|
|
|
"path/filepath"
|
2013-12-12 00:31:02 +00:00
|
|
|
"strings"
|
2013-11-07 20:34:01 +00:00
|
|
|
)
|
|
|
|
|
2014-05-15 22:25:38 +00:00
|
|
|
const maxLoopCounter = 100
|
|
|
|
|
2013-12-12 00:31:02 +00:00
|
|
|
// FollowSymlink will follow an existing link and scope it to the root
|
|
|
|
// path provided.
|
|
|
|
func FollowSymlinkInScope(link, root string) (string, error) {
|
|
|
|
root, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-12 01:12:53 +00:00
|
|
|
|
|
|
|
link, err = filepath.Abs(link)
|
2013-12-12 00:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-12 01:12:53 +00:00
|
|
|
|
2014-06-24 16:50:44 +00:00
|
|
|
if link == root {
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
2013-12-12 01:12:53 +00:00
|
|
|
if !strings.HasPrefix(filepath.Dir(link), root) {
|
|
|
|
return "", fmt.Errorf("%s is not within %s", link, root)
|
|
|
|
}
|
2013-12-12 00:31:02 +00:00
|
|
|
|
2014-06-24 16:50:44 +00:00
|
|
|
prev := "/"
|
2014-06-24 16:53:53 +00:00
|
|
|
|
2013-12-12 00:31:02 +00:00
|
|
|
for _, p := range strings.Split(link, "/") {
|
|
|
|
prev = filepath.Join(prev, p)
|
|
|
|
prev = filepath.Clean(prev)
|
|
|
|
|
2014-05-15 22:25:38 +00:00
|
|
|
loopCounter := 0
|
2013-12-12 01:12:53 +00:00
|
|
|
for {
|
2014-05-15 22:25:38 +00:00
|
|
|
loopCounter++
|
|
|
|
|
|
|
|
if loopCounter >= maxLoopCounter {
|
|
|
|
return "", fmt.Errorf("loopCounter reached MAX: %v", loopCounter)
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:47:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-12 01:12:53 +00:00
|
|
|
stat, err := os.Lstat(prev)
|
2013-12-12 00:31:02 +00:00
|
|
|
if err != nil {
|
2013-12-12 01:12:53 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
2013-12-12 00:31:02 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2013-12-12 01:12:53 +00:00
|
|
|
if stat.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
dest, err := os.Readlink(prev)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:54:20 +00:00
|
|
|
if path.IsAbs(dest) {
|
2013-12-12 01:12:53 +00:00
|
|
|
prev = filepath.Join(root, dest)
|
2014-04-25 03:54:20 +00:00
|
|
|
} else {
|
2013-12-12 01:12:53 +00:00
|
|
|
prev, _ = filepath.Abs(prev)
|
2013-12-12 00:31:02 +00:00
|
|
|
|
2013-12-12 01:12:53 +00:00
|
|
|
if prev = filepath.Clean(filepath.Join(filepath.Dir(prev), dest)); len(prev) < len(root) {
|
|
|
|
prev = filepath.Join(root, filepath.Base(dest))
|
|
|
|
}
|
2013-12-12 00:31:02 +00:00
|
|
|
}
|
2013-12-12 01:12:53 +00:00
|
|
|
} else {
|
|
|
|
break
|
2013-12-12 00:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prev, nil
|
|
|
|
}
|