aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Andrews <mra@xoba.com>2014-04-18 15:37:55 -0700
committerIan Lance Taylor <iant@golang.org>2014-04-18 15:37:55 -0700
commit7c7aaa4156a280960749467dfd3651b8798d420e (patch)
tree65924fd4aa76b4723cdf2d8e59600aeb014fe289
parent75b74ea8ff0ee62828c04e9567ccc5238cec5d49 (diff)
downloadgo-7c7aaa4156a280960749467dfd3651b8798d420e.tar.gz
go-7c7aaa4156a280960749467dfd3651b8798d420e.zip
cmd/ld: don't delete output binary if not "ordinary" file (redux).
following on CL https://golang.org/cl/76810045 and issue 7563, i now see there's another "remove(outfile)" a few dozen lines down that also needs fixing. LGTM=iant R=golang-codereviews, iant CC=0intro, golang-codereviews, r https://golang.org/cl/89030043
-rw-r--r--src/cmd/ld/lib.c31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/cmd/ld/lib.c b/src/cmd/ld/lib.c
index 29de54e3cf..2975b2327a 100644
--- a/src/cmd/ld/lib.c
+++ b/src/cmd/ld/lib.c
@@ -83,6 +83,23 @@ Lflag(char *arg)
ctxt->libdir[ctxt->nlibdir++] = arg;
}
+/*
+ * 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 a subsequent create() to fail.
+ * S_ISREG() does not exist on Plan 9.
+ */
+static void
+mayberemoveoutfile(void)
+{
+#if !(defined(_WIN32) || defined(PLAN9))
+ struct stat st;
+ if(lstat(outfile, &st) == 0 && !S_ISREG(st.st_mode))
+ return;
+#endif
+ remove(outfile);
+}
+
void
libinit(void)
{
@@ -106,17 +123,7 @@ libinit(void)
}
Lflag(smprint("%s/pkg/%s_%s%s%s", goroot, goos, goarch, suffixsep, suffix));
- // 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.
- // 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
+ mayberemoveoutfile();
cout = create(outfile, 1, 0775);
if(cout < 0) {
diag("cannot create %s: %r", outfile);
@@ -139,7 +146,7 @@ errorexit(void)
{
if(nerrors) {
if(cout >= 0)
- remove(outfile);
+ mayberemoveoutfile();
exits("error");
}
exits(0);