grim/convey

Bump the version for release
v0.14.0-alpha3
2018-02-20, Gary Kramlich
166a6d1979fa
Bump the version for release
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package path provides utilities for handling filesystem paths.
package path
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// Traverses check whether path traverses outside of root. Returns error if
// root is traversed.
func Traverses(root, path string) (string, error) {
if path == "." {
return root, nil
}
if !filepath.IsAbs(path) {
path = filepath.Join(root, path)
}
realPath, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
if !strings.HasPrefix(realPath, root) {
return "", fmt.Errorf("'%s' is outside of the build context '%s'", path, root)
}
return realPath, nil
}
// TraversesNonExistent works like Traverses but will walk up the path until it
// finds a directory that does exist.
func TraversesNonExistent(root, path string) (string, error) {
// attempt to do a normal traversal
realPath, err := Traverses(root, path)
// if it failed with a os.PathError that means the path doesn't exist.
// so we will strip off the filename and walk up to see if we can still
// find a match.
if _, ok := err.(*os.PathError); ok {
// reset realPath as the original call to Traverses will return an
// empty string for it on error
realPath = path
// create a variable to hold the path that we've removed
stripped := ""
i := 0
for {
// split the directory and file name, we only hit this on error,
// so we can't actually eval symlinks completely, so it's safe-ish
// to split.
dir, file := filepath.Split(realPath)
if file == "" {
dir = filepath.Dir(dir)
file = filepath.Base(dir)
}
// join the filename to the beginning for stripped as we're walking
// up the directory tree.
stripped = filepath.Join(file, stripped)
// call Traverses again
realPath, err = Traverses(root, dir)
// if there was no error, we're done
if err == nil {
return filepath.Join(realPath, stripped), nil
}
realPath = dir
i++
if i >= 10 {
break
}
}
} else if err != nil {
return "", err
}
return realPath, nil
}