summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Andrews <mra@xoba.com>2014-03-29 09:50:49 -0700
committerMike Andrews <mra@xoba.com>2014-03-29 09:50:49 -0700
commit040a191a69eb6ad6b91abbbe9ae5ee977e2988ca (patch)
treeafd65be242fea18f8b6ce43c96172b6575ea0e97
parentfdf8fe2e56111753358140abdbbe7d48f7b8e832 (diff)
downloadgo-040a191a69eb6ad6b91abbbe9ae5ee977e2988ca.tar.gz
cmd/ld: don't delete output binary if not "ordinary" file.
e.g., don't delete /dev/null. this fix inspired by gnu libiberty, unlink-if-ordinary.c. Fixes issue 7563 LGTM=iant R=golang-codereviews, iant, 0intro CC=golang-codereviews, r https://codereview.appspot.com/76810045 Committer: Ian Lance Taylor <iant@golang.org>
-rw-r--r--src/cmd/ld/lib.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/cmd/ld/lib.c b/src/cmd/ld/lib.c
index 20383de1e..888bc2ce0 100644
--- a/src/cmd/ld/lib.c
+++ b/src/cmd/ld/lib.c
@@ -37,6 +37,9 @@
#include "../../pkg/runtime/funcdata.h"
#include <ar.h>
+#if !(defined(_WIN32) || defined(PLAN9))
+#include <sys/stat.h>
+#endif
enum
{
@@ -106,8 +109,13 @@ libinit(void)
// Unix doesn't like it when we write to a running (or, sometimes,
// recently run) binary, so remove the output file before writing it.
// On Windows 7, remove() can force the following create() to fail.
-#ifndef _WIN32
- remove(outfile);
+ // S_ISREG() does not exist on Plan 9.
+#if !(defined(_WIN32) || defined(PLAN9))
+ {
+ struct stat st;
+ if(lstat(outfile, &st) == 0 && S_ISREG(st.st_mode))
+ remove(outfile);
+ }
#endif
cout = create(outfile, 1, 0775);
if(cout < 0) {