0002-use-futimens-if-available-instead-of-utime.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. From 23510b930ea31f7de8005e2f0ff6cab7062b4e26 Mon Sep 17 00:00:00 2001
  2. From: Kamil Dudka <kdudka@redhat.com>
  3. Date: Thu, 19 Aug 2010 15:23:06 +0200
  4. Subject: [PATCH 2/2] use futimens() if available, instead of utime()
  5. ---
  6. config.h.in | 3 +++
  7. configure | 2 +-
  8. configure.ac | 2 +-
  9. src/files.c | 48 +++++++++++++++++++++++++++++++++++-------------
  10. 4 files changed, 40 insertions(+), 15 deletions(-)
  11. diff --git a/config.h.in b/config.h.in
  12. index 52e13f1..cb17b29 100644
  13. --- a/config.h.in
  14. +++ b/config.h.in
  15. @@ -64,6 +64,9 @@
  16. /* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
  17. #undef HAVE_DOPRNT
  18. +/* Define to 1 if you have the `futimens' function. */
  19. +#undef HAVE_FUTIMENS
  20. +
  21. /* Define to 1 if you have the `getdelim' function. */
  22. #undef HAVE_GETDELIM
  23. diff --git a/configure b/configure
  24. index 02733c7..1805e53 100755
  25. --- a/configure
  26. +++ b/configure
  27. @@ -7484,7 +7484,7 @@ fi
  28. -for ac_func in getdelim getline isblank strcasecmp strcasestr strncasecmp strnlen vsnprintf
  29. +for ac_func in futimens getdelim getline isblank strcasecmp strcasestr strncasecmp strnlen vsnprintf
  30. do :
  31. as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
  32. ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
  33. diff --git a/configure.ac b/configure.ac
  34. index 66f8ee3..f4975d3 100644
  35. --- a/configure.ac
  36. +++ b/configure.ac
  37. @@ -415,7 +415,7 @@ fi])
  38. dnl Checks for functions.
  39. -AC_CHECK_FUNCS(getdelim getline isblank strcasecmp strcasestr strncasecmp strnlen vsnprintf)
  40. +AC_CHECK_FUNCS(futimens getdelim getline isblank strcasecmp strcasestr strncasecmp strnlen vsnprintf)
  41. if test x$enable_utf8 != xno; then
  42. AC_CHECK_FUNCS(iswalnum iswblank iswpunct iswspace nl_langinfo mblen mbstowcs mbtowc wctomb wcwidth)
  43. diff --git a/src/files.c b/src/files.c
  44. index 99cc1b8..9a1bdcc 100644
  45. --- a/src/files.c
  46. +++ b/src/files.c
  47. @@ -1455,6 +1455,29 @@ int copy_file(FILE *inn, FILE *out)
  48. return retval;
  49. }
  50. +#ifdef HAVE_FUTIMENS
  51. +/* set atime/mtime by file descriptor */
  52. +int utime_wrap(int fd, const char *filename, struct utimbuf *ut)
  53. +{
  54. + struct timespec times[2];
  55. + (void) filename;
  56. +
  57. + times[0].tv_sec = ut->actime;
  58. + times[1].tv_sec = ut->modtime;
  59. + times[0].tv_nsec = 0L;
  60. + times[1].tv_nsec = 0L;
  61. +
  62. + return futimens(fd, times);
  63. +}
  64. +#else
  65. +/* set atime/mtime by file name */
  66. +int utime_wrap(int fd, const char *filename, struct utimbuf *ut)
  67. +{
  68. + (void) fd;
  69. + return utime(filename, ut);
  70. +}
  71. +#endif
  72. +
  73. /* Write a file out to disk. If f_open isn't NULL, we assume that it is
  74. * a stream associated with the file, and we don't try to open it
  75. * ourselves. If tmp is TRUE, we set the umask to disallow anyone else
  76. @@ -1694,6 +1717,18 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
  77. fprintf(stderr, "Backing up %s to %s\n", realname, backupname);
  78. #endif
  79. + /* Set backup's file metadata. */
  80. + if (utime_wrap(backup_fd, backupname, &filetime) == -1
  81. + && !ISSET(INSECURE_BACKUP)) {
  82. + statusbar(_("Error writing backup file %s: %s"), backupname,
  83. + strerror(errno));
  84. + /* If we can't write to the backup, DONT go on, since
  85. + whatever caused the backup file to fail (e.g. disk
  86. + full may well cause the real file write to fail, which
  87. + means we could lose both the backup and the original! */
  88. + goto cleanup_and_exit;
  89. + }
  90. +
  91. /* Copy the file. */
  92. copy_status = copy_file(f, backup_file);
  93. @@ -1704,19 +1739,6 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
  94. goto cleanup_and_exit;
  95. }
  96. - /* And set its metadata. */
  97. - if (utime(backupname, &filetime) == -1 && !ISSET(INSECURE_BACKUP)) {
  98. - if (prompt_failed_backupwrite(backupname))
  99. - goto skip_backup;
  100. - statusbar(_("Error writing backup file %s: %s"), backupname,
  101. - strerror(errno));
  102. - /* If we can't write to the backup, DONT go on, since
  103. - whatever caused the backup file to fail (e.g. disk
  104. - full may well cause the real file write to fail, which
  105. - means we could lose both the backup and the original! */
  106. - goto cleanup_and_exit;
  107. - }
  108. -
  109. free(backupname);
  110. }
  111. --
  112. 1.7.4