-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fsutil: Add Dir() and Clean() functions
From the code: These functions exists because we work with slash terminated directory paths that come from deb package tarballs but standard library path functions treat slash terminated paths as unclean. We use ad-hoc code to make filepath.Dir() slash terminated in two places right now. For example this code targetDir := filepath.Dir(strings.TrimRight(targetPath, "/")) + "/" is not strictly correct as "/a/b/.///" will be turned into "/a/b/./" which is still "/a/b". Since we deal with slash terminated directory paths everywhere, create and use dedicated helper functions to process them.
- Loading branch information
Showing
3 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fsutil | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// These functions exists because we work with slash terminated directory paths | ||
// that come from deb package tarballs but standard library path functions | ||
// treat slash terminated paths as unclean. | ||
|
||
// Dir is like filepath.Dir() but trailing slash on input is ignored and the | ||
// return value always includes trailing slash. | ||
// | ||
// Comparison of filepath.Dir() with fsutil.Dir(): | ||
// | ||
// filepath.Dir("/foo/bar/") == "/foo/bar" | ||
// filepath.Dir("/foo/bar") == "/foo" | ||
// | ||
// fsutil.Dir("/foo/bar") == "/foo/" | ||
// fsutil.Dir("/foo/bar/") == "/foo/" | ||
func Dir(path string) string { | ||
parent := filepath.Dir(filepath.Clean(path)) | ||
if parent != "/" { | ||
parent += "/" | ||
} | ||
return parent | ||
} | ||
|
||
// Clean is like filepath.Clean() but trailing slash is kept. | ||
// | ||
// Comparison of filepath.Clean() with fsutil.Clean(): | ||
// | ||
// filepath.Clean("/foo/bar") == "/foo/bar" | ||
// filepath.Clean("/foo/bar/") == "/foo/bar" | ||
// filepath.Clean("/foo/bar/.//) == "/foo/bar" | ||
// | ||
// fsutil.Clean("/foo/bar") == "/foo/bar" | ||
// fsutil.Clean("/foo/bar/") == "/foo/bar/" | ||
// fsutil.Clean("/foo/bar/.//") == "/foo/bar/" | ||
func Clean(path string) string { | ||
clean := filepath.Clean(path) | ||
if strings.HasSuffix(path, "/") { | ||
clean += "/" | ||
} | ||
return clean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fsutil_test | ||
|
||
import ( | ||
. "gopkg.in/check.v1" | ||
|
||
"github.com/canonical/chisel/internal/fsutil" | ||
) | ||
|
||
var dirTests = []struct { | ||
input string | ||
output string | ||
}{ | ||
{"/a/b/c", "/a/b/"}, | ||
{"/a/b/c/", "/a/b/"}, | ||
{"/a/b/c//", "/a/b/"}, | ||
{"/a/b/c/././", "/a/b/"}, | ||
{"/a/b/c/.././", "/a/"}, | ||
{"/a/b//c", "/a/b/"}, | ||
{"/a/b/./c", "/a/b/"}, | ||
{"a/b/./c", "a/b/"}, | ||
{"./a/b/./c", "a/b/"}, | ||
{"/", "/"}, | ||
{"///.///", "/"}, | ||
{".", "./"}, | ||
{"", "./"}, | ||
} | ||
|
||
var cleanTests = []struct { | ||
input string | ||
output string | ||
}{ | ||
{"/a/b/c", "/a/b/c"}, | ||
{"/a/b/c/", "/a/b/c/"}, | ||
{"/a/b/c/.//", "/a/b/c/"}, | ||
{"/a/b//./c", "/a/b/c"}, | ||
{"/a/b//./c/", "/a/b/c/"}, | ||
{"/a/b/.././c/", "/a/c/"}, | ||
} | ||
|
||
func (s *S) TestDir(c *C) { | ||
for _, t := range dirTests { | ||
c.Logf("%s => %s", t.input, t.output) | ||
c.Assert(fsutil.Dir(t.input), Equals, t.output) | ||
} | ||
} | ||
|
||
func (s *S) TestClean(c *C) { | ||
for _, t := range cleanTests { | ||
c.Logf("%s => %s", t.input, t.output) | ||
c.Assert(fsutil.Clean(t.input), Equals, t.output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters