-
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 SlashedPathDir() and SlashedPathClean() 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
127 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,66 @@ | ||
package fsutil | ||
|
||
import ( | ||
"path/filepath" | ||
) | ||
|
||
// isDirPath returns whether the path refers a directory. | ||
// The path refers to a directory when it ends with "/", "/." or "/..", or when | ||
// it equals "." or "..". | ||
func isDirPath(path string) bool { | ||
i := len(path) - 1 | ||
if i < 0 { | ||
return true | ||
} | ||
if path[i] == '.' { | ||
i-- | ||
if i < 0 { | ||
return true | ||
} | ||
if path[i] == '.' { | ||
i-- | ||
if i < 0 { | ||
return true | ||
} | ||
} | ||
} | ||
if path[i] == '/' { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// Debian package tarballs present paths slightly differently to what we would | ||
// normally classify as clean paths. While a traditional clean file path is identical | ||
// to a clean deb package file path, the deb package directory path always ends | ||
// with a slash. Although the change only affects directory paths, the implication | ||
// is that a directory path without a slash is interpreted as a file path. For this | ||
// reason, we need to be very careful and handle both file and directory paths using | ||
// a new set of functions. We call this new path type a Slashed Path. A slashed path | ||
// allows us to identify a file or directory simply using lexical analysis. | ||
|
||
// SlashedPathDir takes a file or slashed directory path as input, cleans the | ||
// path and returns the parent directory. An input path ending without a slash | ||
// will be interpreted as a file path. Directory paths should always end with a slash. | ||
// Clean is like filepath.Clean() but trailing slash is kept. | ||
func SlashedPathClean(path string) string { | ||
clean := filepath.Clean(path) | ||
if clean != "/" && isDirPath(path) { | ||
clean += "/" | ||
} | ||
return clean | ||
} | ||
|
||
// SlashedPathClean takes a file or slashed directory path as input, and produces | ||
// the shortest equivalent as output. An input path ending without a slash will be | ||
// interpreted as a file path. Directory paths should always end with a slash. | ||
// 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. | ||
func SlashedPathDir(path string) string { | ||
parent := filepath.Dir(filepath.Clean(path)) | ||
if parent != "/" { | ||
parent += "/" | ||
} | ||
return parent | ||
} |
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,54 @@ | ||
package fsutil_test | ||
|
||
import ( | ||
. "gopkg.in/check.v1" | ||
|
||
"github.com/canonical/chisel/internal/fsutil" | ||
) | ||
|
||
var cleanAndDirTestCases = []struct { | ||
inputPath string | ||
resultClean string | ||
resultDir string | ||
}{ | ||
{"/a/b/c", "/a/b/c", "/a/b/"}, | ||
{"/a/b/c/", "/a/b/c/", "/a/b/"}, | ||
{"/a/b/c//", "/a/b/c/", "/a/b/"}, | ||
{"/a/b//c", "/a/b/c", "/a/b/"}, | ||
{"/a/b/c/.", "/a/b/c/", "/a/b/"}, | ||
{"/a/b/c/.///.", "/a/b/c/", "/a/b/"}, | ||
{"/a/b/./c/", "/a/b/c/", "/a/b/"}, | ||
{"/a/b/.///./c", "/a/b/c", "/a/b/"}, | ||
{"/a/b/c/..", "/a/b/", "/a/"}, | ||
{"/a/b/c/..///./", "/a/b/", "/a/"}, | ||
{"/a/b/c/../.", "/a/b/", "/a/"}, | ||
{"/a/b/../c/", "/a/c/", "/a/"}, | ||
{"/a/b/..///./c", "/a/c", "/a/"}, | ||
{"a/b/./c", "a/b/c", "a/b/"}, | ||
{"./a/b/./c", "a/b/c", "a/b/"}, | ||
{"/", "/", "/"}, | ||
{"///", "/", "/"}, | ||
{"///.///", "/", "/"}, | ||
{"/././.", "/", "/"}, | ||
{".", "./", "./"}, | ||
{".///", "./", "./"}, | ||
{"..", "../", "./"}, | ||
{"..///.", "../", "./"}, | ||
{"../../..", "../../../", "../../"}, | ||
{"..///.///../..", "../../../", "../../"}, | ||
{"", "./", "./"}, | ||
} | ||
|
||
func (s *S) TestSlashedPathClean(c *C) { | ||
for _, t := range cleanAndDirTestCases { | ||
c.Logf("%s => %s", t.inputPath, t.resultClean) | ||
c.Assert(fsutil.SlashedPathClean(t.inputPath), Equals, t.resultClean) | ||
} | ||
} | ||
|
||
func (s *S) TestSlashedPathDir(c *C) { | ||
for _, t := range cleanAndDirTestCases { | ||
c.Logf("%s => %s", t.inputPath, t.resultDir) | ||
c.Assert(fsutil.SlashedPathDir(t.inputPath), Equals, t.resultDir) | ||
} | ||
} |
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