Improve pathRel to handle edge cases correctly

- Handle case where basepath equals targpath by returning "."
- Ensure basepath is treated as directory with trailing slash to avoid false prefix matches
- Fix linter issue by using += operator

Addresses code review feedback.

Co-authored-by: rchincha <45800463+rchincha@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-15 19:51:57 +00:00
parent d464313d60
commit a1d3dcf345
+11 -2
View File
@@ -80,14 +80,23 @@ func (is *ImageStore) pathRel(basepath, targpath string) (string, error) {
basepath = path.Clean(basepath)
targpath = path.Clean(targpath)
// Handle case where paths are equal
if basepath == targpath {
return ".", nil
}
// Ensure basepath is treated as a directory by adding trailing slash
if !strings.HasSuffix(basepath, "/") {
basepath += "/"
}
// Check if targpath starts with basepath
if !strings.HasPrefix(targpath, basepath) {
return "", fmt.Errorf("%w: path %s is not under base path %s", zerr.ErrBadConfig, targpath, basepath)
}
// Remove basepath prefix and leading slash
// Remove basepath prefix (which now includes trailing slash)
rel := strings.TrimPrefix(targpath, basepath)
rel = strings.TrimPrefix(rel, "/")
return rel, nil
}