Cover CleanedAbs

This commit is contained in:
Jeffrey Regan
2019-01-25 12:21:03 -08:00
parent 028724df08
commit c21dfefbdf
7 changed files with 173 additions and 34 deletions

View File

@@ -17,7 +17,9 @@ limitations under the License.
package fs
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
@@ -53,9 +55,41 @@ func (realFS) RemoveAll(name string) error {
// Open delegates to os.Open.
func (realFS) Open(name string) (File, error) { return os.Open(name) }
// EvalSymlinks delegates to filepath.EvalSymlinks.
func (realFS) EvalSymlinks(path string) (string, error) {
return filepath.EvalSymlinks(path)
// CleanedAbs returns a cleaned, absolute path
// with no symbolic links split into directory
// and file components. If the entire path is
// a directory, the file component is an empty
// string.
func (x realFS) CleanedAbs(path string) (string, string, error) {
absRoot, err := filepath.Abs(path)
if err != nil {
return "", "", fmt.Errorf(
"abs path error on '%s' : %v", path, err)
}
deLinked, err := filepath.EvalSymlinks(absRoot)
if err != nil {
return "", "", fmt.Errorf(
"evalsymlink failure on '%s' : %v", path, err)
}
if x.IsDir(deLinked) {
return deLinked, "", nil
}
d := filepath.Dir(deLinked)
if !x.IsDir(d) {
// Programmer/assumption error.
log.Fatalf("first part of '%s' not a directory", deLinked)
}
if d == deLinked {
// Programmer/assumption error.
log.Fatalf("d '%s' should be a subset of deLinked", d)
}
f := filepath.Base(deLinked)
if filepath.Join(d, f) != deLinked {
// Programmer/assumption error.
log.Fatalf("these should be equal: '%s', '%s'",
filepath.Join(d, f), deLinked)
}
return d, f, nil
}
// Exists returns true if os.Stat succeeds.