From ed9191e1f0c5ba1d24f2deb330c3bfe2b729a8db Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Thu, 12 Jun 2025 09:50:29 +0200 Subject: [PATCH] fix: do not crash when a symlink we try to create is already present and with the designated target, fix #603 --- local/php/executor_posix.go | 13 ++++++++++++- local/php/executor_windows.go | 8 +++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/local/php/executor_posix.go b/local/php/executor_posix.go index 9a28b8d7..0ff8b656 100644 --- a/local/php/executor_posix.go +++ b/local/php/executor_posix.go @@ -24,7 +24,10 @@ package php import ( "os" + "path/filepath" "syscall" + + "github.com/pkg/errors" ) func shouldSignalBeIgnored(sig os.Signal) bool { @@ -34,5 +37,13 @@ func shouldSignalBeIgnored(sig os.Signal) bool { } func symlink(oldname, newname string) error { - return os.Symlink(oldname, newname) + err := errors.WithStack(os.Symlink(oldname, newname)) + + if os.IsExist(errors.Cause(err)) { + if target, _ := filepath.EvalSymlinks(newname); target == oldname { + return nil + } + } + + return err } diff --git a/local/php/executor_windows.go b/local/php/executor_windows.go index f5145733..5694b83b 100644 --- a/local/php/executor_windows.go +++ b/local/php/executor_windows.go @@ -22,6 +22,8 @@ package php import ( "io" "os" + + "github.com/pkg/errors" ) func shouldSignalBeIgnored(sig os.Signal) bool { @@ -31,14 +33,14 @@ func shouldSignalBeIgnored(sig os.Signal) bool { func symlink(oldname, newname string) error { source, err := os.Open(oldname) if err != nil { - return err + return errors.WithStack(err) } defer source.Close() destination, err := os.Create(newname) if err != nil { - return err + return errors.WithStack(err) } defer destination.Close() _, err = io.Copy(destination, source) - return err + return errors.WithStack(err) }