[PATCH] Close files before renaming or unlinking them

Ulf Hermann ulf.hermann@qt.io
Thu May 4 14:30:00 GMT 2017


On windows we cannot rename or unlink open files.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 libasm/ChangeLog |  4 ++++
 libasm/asm_end.c | 15 +++++++++++----
 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 +
 9 files changed, 35 insertions(+), 7 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/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



More information about the Elfutils-devel mailing list