]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/ld: don't delete output binary if not "ordinary" file.
authorMike Andrews <mra@xoba.com>
Sat, 29 Mar 2014 16:50:49 +0000 (09:50 -0700)
committerIan Lance Taylor <iant@golang.org>
Sat, 29 Mar 2014 16:50:49 +0000 (09:50 -0700)
e.g., don't delete /dev/null. this fix inspired by gnu libiberty,
unlink-if-ordinary.c.

Fixes #7563

LGTM=iant
R=golang-codereviews, iant, 0intro
CC=golang-codereviews, r
https://golang.org/cl/76810045

src/cmd/ld/lib.c

index 20383de1e1702e0910048ce341fbb5f5569c41c6..888bc2ce0d733027782f278eee9ccdaf9a4f28a5 100644 (file)
@@ -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) {