static void mode_list(struct cpio *);
static void mode_out(struct cpio *);
static void mode_pass(struct cpio *, const char *);
-static void restore_time(struct cpio *, struct archive_entry *,
+static int restore_time(struct cpio *, struct archive_entry *,
const char *, int fd);
static void usage(void);
static void version(void);
}
}
- restore_time(cpio, entry, srcpath, fd);
+ fd = restore_time(cpio, entry, srcpath, fd);
cleanup:
if (cpio->verbose)
return (0);
}
-static void
+static int
restore_time(struct cpio *cpio, struct archive_entry *entry,
const char *name, int fd)
{
(void)cpio; /* UNUSED */
(void)entry; /* UNUSED */
(void)name; /* UNUSED */
- (void)fd; /* UNUSED */
if (!warned)
cpio_warnc(0, "Can't restore access times on this platform");
warned = 1;
- return;
+ return (fd);
#else
#ifdef _WIN32
struct __timeval times[2];
#endif
if (!cpio->option_atime_restore)
- return;
+ return (fd);
times[1].tv_sec = archive_entry_mtime(entry);
times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000;
#ifdef HAVE_FUTIMES
if (fd >= 0 && futimes(fd, times) == 0)
- return;
+ return (fd);
#endif
+ /*
+ * Some platform cannot restore access times if the file descriptor
+ * is still opened.
+ */
+ if (fd >= 0) {
+ close(fd);
+ fd = -1;
+ }
#ifdef HAVE_LUTIMES
if (lutimes(name, times) != 0)
#endif
cpio_warnc(errno, "Can't update time for %s", name);
#endif
+ return (fd);
}
int r;
int f;
char buff[64];
+ const char *p;
/* Create all of the test files. */
test_create();
/* Copy the file without -a; should change the atime. */
r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
assertEqualInt(r, 0);
- assertFileContents("1 block\n", 8, "copy-no-a.err");
+ p = "1 block" NL;
+ assertFileContents(p, strlen(p), "copy-no-a.err");
assertEmptyFile("copy-no-a.out");
assertEqualInt(0, stat(files[1].name, &st));
failure("Copying file without -a should have changed atime.");
/* Archive the file without -a; should change the atime. */
r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
assertEqualInt(r, 0);
- assertFileContents("1 block\n", 8, "copy-no-a.err");
+ assertFileContents(p, strlen(p), "copy-no-a.err");
assertEqualInt(0, stat(files[2].name, &st));
failure("Archiving file without -a should have changed atime.");
assert(st.st_atime != files[2].atime_sec);
r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
files[3].name, testprog);
assertEqualInt(r, 0);
- assertFileContents("1 block\n", 8, "copy-a.err");
+ p = "1 block" NL;
+ assertFileContents(p, strlen(p), "copy-a.err");
assertEmptyFile("copy-a.out");
assertEqualInt(0, stat(files[3].name, &st));
failure("Copying file with -a should not have changed atime.");
r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
files[4].name, testprog);
assertEqualInt(r, 0);
- assertFileContents("1 block\n", 8, "copy-a.err");
+ assertFileContents(p, strlen(p), "copy-a.err");
assertEqualInt(0, stat(files[4].name, &st));
failure("Archiving file with -a should not have changed atime.");
assertEqualInt(st.st_atime, files[4].atime_sec);