Skip to content

Commit

Permalink
Smartlink.Link() should create directories as needed.
Browse files Browse the repository at this point in the history
We no longer have to avoid removing files, which could have removed needed directories.
  • Loading branch information
mitchell-as committed Aug 22, 2024
1 parent 4eda2f2 commit e1137a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
19 changes: 8 additions & 11 deletions internal/smartlink/smartlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions pkg/runtime/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e1137a2

Please sign in to comment.