From e1137a2ab9b9a58119fb067f0f36bf3915276168 Mon Sep 17 00:00:00 2001 From: mitchell Date: Thu, 22 Aug 2024 11:16:11 -0400 Subject: [PATCH] Smartlink.Link() should create directories as needed. We no longer have to avoid removing files, which could have removed needed directories. --- internal/smartlink/smartlink.go | 19 ++++++++----------- pkg/runtime/depot.go | 9 +-------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/internal/smartlink/smartlink.go b/internal/smartlink/smartlink.go index c3eca28464..245b725d7c 100644 --- a/internal/smartlink/smartlink.go +++ b/internal/smartlink/smartlink.go @@ -62,6 +62,12 @@ func Link(src, dest string) error { return nil } + if destDir := filepath.Dir(dest); !fileutils.DirExists(destDir) { + if err := os.MkdirAll(destDir, 0755); err != nil { + return errs.Wrap(err, "could not create directory %s", destDir) + } + } + // Multiple artifacts can supply the same file. We do not have a better solution for this at the moment other than // favouring the first one encountered. if fileutils.TargetExists(dest) { @@ -78,7 +84,7 @@ func Link(src, dest string) error { // UnlinkContents will unlink the contents of src to dest if the links exist // WARNING: on windows smartlinks are hard links, and relating hard links back to their source is non-trivial, so instead // we just delete the target path. If the user modified the target in any way their changes will be lost. -func UnlinkContents(src, dest string, ignorePaths ...string) error { +func UnlinkContents(src, dest string) error { if !fileutils.DirExists(dest) { return errs.New("dest dir does not exist: %s", dest) } @@ -89,11 +95,6 @@ func UnlinkContents(src, dest string, ignorePaths ...string) error { return errs.Wrap(err, "Could not resolve src and dest paths") } - ignore := make(map[string]bool) - for _, path := range ignorePaths { - ignore[path] = true - } - entries, err := os.ReadDir(src) if err != nil { return errs.Wrap(err, "Reading dir %s failed", dest) @@ -106,12 +107,8 @@ func UnlinkContents(src, dest string, ignorePaths ...string) error { continue } - if _, yes := ignore[destPath]; yes { - continue - } - if fileutils.IsDir(destPath) { - if err := UnlinkContents(srcPath, destPath, ignorePaths...); err != nil { + if err := UnlinkContents(srcPath, destPath); err != nil { return err // Not wrapping here cause it'd just repeat the same error due to the recursion } } else { diff --git a/pkg/runtime/depot.go b/pkg/runtime/depot.go index e5a1cc2d69..67437e225f 100644 --- a/pkg/runtime/depot.go +++ b/pkg/runtime/depot.go @@ -285,21 +285,14 @@ func (d *depot) Undeploy(id strfmt.UUID, relativeSrc, path string) error { if err != nil { return errs.Wrap(err, "failed to get shared files") } - sharedFiles := make([]string, 0) - for file := range redeploys { - sharedFiles = append(sharedFiles, file) - } // Perform uninstall based on deployment type - if err := smartlink.UnlinkContents(filepath.Join(d.Path(id), relativeSrc), path, sharedFiles...); err != nil { + if err := smartlink.UnlinkContents(filepath.Join(d.Path(id), relativeSrc), path); err != nil { return errs.Wrap(err, "failed to unlink artifact") } // Re-link or re-copy any files provided by other artifacts. for sharedFile, relinkSrc := range redeploys { - if err := os.Remove(sharedFile); err != nil { - return errs.Wrap(err, "failed to remove file") - } switch deploy.Type { case deploymentTypeLink: if err := smartlink.Link(relinkSrc, sharedFile); err != nil {