This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] Close files before renaming or unlinking them


On windows we cannot rename or unlink open files.

(strip.c was missing)

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 libasm/ChangeLog |  4 ++++
 libasm/asm_end.c | 15 +++++++++++----
 src/ChangeLog    |  4 ++++
 src/strip.c      |  5 ++++-
 tests/ChangeLog  |  9 +++++++++
 tests/newfile.c  |  7 +++++--
 tests/newscn.c   |  3 ++-
 tests/update1.c  |  1 +
 tests/update2.c  |  1 +
 tests/update3.c  |  1 +
 tests/update4.c  |  1 +
 11 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 2b499c7..0e67657 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* asm_end.c: Rename the output file only after freeing resources.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* asm_end.c: Don't fchmod the new file if fchmod is unavailable.
 
 2017-02-28  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index 7fabe94..7891fbb 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -521,16 +521,23 @@ asm_end (AsmCtx_t *ctx)
     }
 #endif
 
+  char *tmp_fname = strdup (ctx->tmp_fname);
+  char *fname = strdup (ctx->fname);
+
+  /* Free the resources.  */
+  __libasm_finictx (ctx);
+
   /* Rename output file.  */
-  if (rename (ctx->tmp_fname, ctx->fname) != 0)
+  result = rename (tmp_fname, fname);
+  free (tmp_fname);
+  free (fname);
+
+  if (result != 0)
     {
       __libasm_seterrno (ASM_E_CANNOT_RENAME);
       return -1;
     }
 
-  /* Free the resources.  */
-  __libasm_finictx (ctx);
-
   return 0;
 }
 
diff --git a/src/ChangeLog b/src/ChangeLog
index 0d1e57d..a474331 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* strip.c: Close and reopen file when renaming.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* addr2line.c: Don't assume unix file system conventions.
 	* size.c: Likewise.
 	* strip.c: Likewise.
diff --git a/src/strip.c b/src/strip.c
index 60f6700..99d7dd1 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -2006,7 +2006,10 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 
       /* Create the real output file.  First rename, then change the
 	 mode.  */
-      if (rename (tmp_debug_fname, debug_fname) != 0
+      close (debug_fd);
+      int rename_result = rename (tmp_debug_fname, debug_fname);
+      debug_fd = open (debug_fname, O_RDONLY | O_BINARY);
+      if (rename_result != 0 || debug_fd == -1
 #if HAVE_DECL_FCHMOD
 	  || fchmod (debug_fd, mode) != 0
 #endif
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 5e29f82..b8de138 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,14 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* newfile.c: Close the file when we're done and unlink it afterwards.
+	* newscn.c: Likewise.
+	* update1.c: Likewise.
+	* update2.c: Likewise.
+	* update3.c: Likewise.
+	* update4.c: Likewise.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* asm-tst4.c: Don't assume unix file system conventions.
 	* asm-tst5.c: Likewise.
 	* asm-tst6.c: Likewise.
diff --git a/tests/newfile.c b/tests/newfile.c
index 5eabdcb..a279317 100644
--- a/tests/newfile.c
+++ b/tests/newfile.c
@@ -63,8 +63,6 @@ main (int argc, char *argv[] __attribute__ ((unused)))
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  /* Remove the file when we exit.  */
-  unlink (fname);
 
   elf_version (EV_CURRENT);
   elf = elf_begin (fd, ELF_C_WRITE, NULL);
@@ -166,5 +164,10 @@ main (int argc, char *argv[] __attribute__ ((unused)))
       (void) elf_end (elf);
     }
 
+  close (fd);
+
+  /* Remove the file when we exit.  */
+  unlink (fname);
+
   return result;
 }
diff --git a/tests/newscn.c b/tests/newscn.c
index 466f2f6..de8951d 100644
--- a/tests/newscn.c
+++ b/tests/newscn.c
@@ -46,7 +46,6 @@ main (void)
       fprintf (stderr, "Failed to open fdput file: %s\n", name);
       exit (1);
     }
-  unlink (name);
 
   elf = elf_begin (fd, ELF_C_WRITE, NULL);
   if (elf == NULL)
@@ -62,5 +61,7 @@ main (void)
   elf_end (elf);
   close (fd);
 
+  unlink (name);
+
   return 0;
 }
diff --git a/tests/update1.c b/tests/update1.c
index a571618..548c6d8 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -121,6 +121,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
       exit (1);
     }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update2.c b/tests/update2.c
index 3e22879..1dcff3f 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -144,6 +144,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
       exit (1);
     }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update3.c b/tests/update3.c
index d619bed..9d4f880 100644
--- a/tests/update3.c
+++ b/tests/update3.c
@@ -199,6 +199,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
       exit (1);
     }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update4.c b/tests/update4.c
index 8196b8c..a762e0a 100644
--- a/tests/update4.c
+++ b/tests/update4.c
@@ -351,6 +351,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
       exit (1);
     }
 
+  close (fd);
   unlink (fname);
 
   return 0;
-- 
2.1.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]