fix: correctly check for chart directory conflict

This commit move the check for directory conflict
to the Expand function.

This let us check if we will overwrite the actual
chart directory. The previous implementation would
check if a directory suffixed with the chart version exist.

This would results in:
- Creating a spuerfluous directory with the chart name
suffixed by the chart version.
- Not preventing the overwrite of an existing chart with the same
name but with a different version.

Signed-off-by: foyerunix <foyerunix@foyer.lu>
pull/30868/head
foyerunix 4 months ago
parent dd40316660
commit 82b0baddbc

@ -150,23 +150,6 @@ func (p *Pull) Run(chartRef string) (string, error) {
if !filepath.IsAbs(ud) {
ud = filepath.Join(p.DestDir, ud)
}
// Let udCheck to check conflict file/dir without replacing ud when untarDir is the current directory(.).
udCheck := ud
if udCheck == "." {
_, udCheck = filepath.Split(chartRef)
} else {
_, chartName := filepath.Split(chartRef)
udCheck = filepath.Join(udCheck, chartName)
}
if _, err := os.Stat(udCheck); err != nil {
if err := os.MkdirAll(udCheck, 0755); err != nil {
return out.String(), fmt.Errorf("failed to untar (mkdir): %w", err)
}
} else {
return out.String(), fmt.Errorf("failed to untar: a file or directory with the name %s already exists", udCheck)
}
return out.String(), chartutil.ExpandFile(ud, saved)
}
return out.String(), nil

@ -61,6 +61,15 @@ func Expand(dir string, r io.Reader) error {
return err
}
// Check if chartdir conflict with an already existing directory
if _, err := os.Stat(chartdir); err != nil {
if err := os.MkdirAll(chartdir, 0755); err != nil {
return fmt.Errorf("failed to untar (mkdir): %w", err)
}
} else {
return fmt.Errorf("failed to untar: a file or directory with the name %s already exists", chartdir)
}
// Copy all files verbatim. We don't parse these files because parsing can remove
// comments.
for _, file := range files {

Loading…
Cancel
Save