Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
pkg/fileutils: add verifications for SymbolicLink
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
  • Loading branch information
Lu Fengqi committed Feb 17, 2020
1 parent a392463 commit 16763d6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ func Link(src string, linkName string) error {

// SymbolicLink creates target as a symbolic link to src.
func SymbolicLink(src string, target string) error {
// TODO Add verifications.
if !PathExist(src) {
return fmt.Errorf("failed to symlink %s to %s: src no such file or directory", target, src)
}
if PathExist(target) {
if IsDir(target) {
return fmt.Errorf("failed to symlink %s to %s: link name already exists and is a directory", target, src)
}
if err := DeleteFile(target); err != nil {
return fmt.Errorf("failed to symlink %s to %s when deleting target file: %v", target, src, err)
}

}
return os.Symlink(src, target)
}

Expand Down

0 comments on commit 16763d6

Please sign in to comment.