Skip to content

Commit f746c37

Browse files
committed
Use File Descriptor in Setting Stat on Output File
Note that the `fd` is only valid while the file is still open. So we need to move the setting calls to before we close the file. However! We cannot do so with the `utime()` call (even though `futimens()` exists) because the follow- ing `close()` call to the `fd` will reset the atime of the file. So it seems the `utime()` call has to happen after the file is closed.
1 parent a5a2418 commit f746c37

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

programs/fileio.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,7 @@ static int FIO_compressFilename_dstFile(FIO_ctx_t* const fCtx,
16811681
int result;
16821682
int transferStat = 0;
16831683
FILE *dstFile;
1684+
int dstFd = -1;
16841685

16851686
assert(AIO_ReadPool_getFile(ress.readCtx) != NULL);
16861687
if (AIO_WritePool_getFile(ress.writeCtx) == NULL) {
@@ -1696,6 +1697,7 @@ static int FIO_compressFilename_dstFile(FIO_ctx_t* const fCtx,
16961697
DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: opening dst: %s \n", dstFileName);
16971698
dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFileInitialPermissions);
16981699
if (dstFile==NULL) return 1; /* could not open dstFileName */
1700+
dstFd = fileno(dstFile);
16991701
AIO_WritePool_setFile(ress.writeCtx, dstFile);
17001702
/* Must only be added after FIO_openDstFile() succeeds.
17011703
* Otherwise we may delete the destination file if it already exists,
@@ -1709,14 +1711,20 @@ static int FIO_compressFilename_dstFile(FIO_ctx_t* const fCtx,
17091711
if (closeDstFile) {
17101712
clearHandler();
17111713

1714+
if (transferStat) {
1715+
UTIL_setFDStat(dstFd, dstFileName, srcFileStat);
1716+
}
1717+
17121718
DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: closing dst: %s \n", dstFileName);
17131719
if (AIO_WritePool_closeFile(ress.writeCtx)) { /* error closing file */
17141720
DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
17151721
result=1;
17161722
}
1723+
17171724
if (transferStat) {
1718-
UTIL_setFileStat(dstFileName, srcFileStat);
1725+
UTIL_utime(dstFileName, srcFileStat);
17191726
}
1727+
17201728
if ( (result != 0) /* operation failure */
17211729
&& strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */
17221730
) {
@@ -2540,6 +2548,7 @@ static int FIO_decompressDstFile(FIO_ctx_t* const fCtx,
25402548
int result;
25412549
int releaseDstFile = 0;
25422550
int transferStat = 0;
2551+
int dstFd = 0;
25432552

25442553
if ((AIO_WritePool_getFile(ress.writeCtx) == NULL) && (prefs->testMode == 0)) {
25452554
FILE *dstFile;
@@ -2555,6 +2564,7 @@ static int FIO_decompressDstFile(FIO_ctx_t* const fCtx,
25552564

25562565
dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFilePermissions);
25572566
if (dstFile==NULL) return 1;
2567+
dstFd = fileno(dstFile);
25582568
AIO_WritePool_setFile(ress.writeCtx, dstFile);
25592569

25602570
/* Must only be added after FIO_openDstFile() succeeds.
@@ -2568,13 +2578,18 @@ static int FIO_decompressDstFile(FIO_ctx_t* const fCtx,
25682578

25692579
if (releaseDstFile) {
25702580
clearHandler();
2581+
2582+
if (transferStat) {
2583+
UTIL_setFDStat(dstFd, dstFileName, srcFileStat);
2584+
}
2585+
25712586
if (AIO_WritePool_closeFile(ress.writeCtx)) {
25722587
DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
25732588
result = 1;
25742589
}
25752590

25762591
if (transferStat) {
2577-
UTIL_setFileStat(dstFileName, srcFileStat);
2592+
UTIL_utime(dstFileName, srcFileStat);
25782593
}
25792594

25802595
if ( (result != 0) /* operation failure */

programs/util.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,6 @@ int UTIL_setFDStat(const int fd, const char *filename, const stat_t *statbuf)
295295
return -1;
296296
}
297297

298-
/* set access and modification times */
299-
res += UTIL_utime(filename, statbuf);
300-
301298
/* Mimic gzip's behavior:
302299
*
303300
* "Change the group first, then the permissions, then the owner.

tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.exact

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ Trace:FileStat: > UTIL_getFileSize(file)
3030
Trace:FileStat: > UTIL_stat(-1, file)
3131
Trace:FileStat: < 1
3232
Trace:FileStat: < 65537
33-
Trace:FileStat: > UTIL_setFileStat(-1, file.zst)
34-
Trace:FileStat: > UTIL_stat(-1, file.zst)
33+
Trace:FileStat: > UTIL_setFileStat(4, file.zst)
34+
Trace:FileStat: > UTIL_stat(4, file.zst)
3535
Trace:FileStat: < 1
36-
Trace:FileStat: > UTIL_utime(file.zst)
37-
Trace:FileStat: < 0
3836
Trace:FileStat: > UTIL_chmod(file.zst, 0642)
39-
Trace:FileStat: > chmod
37+
Trace:FileStat: > fchmod
4038
Trace:FileStat: < 0
4139
Trace:FileStat: < 0
4240
Trace:FileStat: < 0
41+
Trace:FileStat: > UTIL_utime(file.zst)
42+
Trace:FileStat: < 0

tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.exact

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Trace:FileStat: > UTIL_isRegularFile(file)
2626
Trace:FileStat: > UTIL_stat(-1, file)
2727
Trace:FileStat: < 1
2828
Trace:FileStat: < 1
29-
Trace:FileStat: > UTIL_setFileStat(-1, file)
30-
Trace:FileStat: > UTIL_stat(-1, file)
29+
Trace:FileStat: > UTIL_setFileStat(4, file)
30+
Trace:FileStat: > UTIL_stat(4, file)
3131
Trace:FileStat: < 1
32-
Trace:FileStat: > UTIL_utime(file)
33-
Trace:FileStat: < 0
3432
Trace:FileStat: > UTIL_chmod(file, 0642)
35-
Trace:FileStat: > chmod
33+
Trace:FileStat: > fchmod
3634
Trace:FileStat: < 0
3735
Trace:FileStat: < 0
3836
Trace:FileStat: < 0
37+
Trace:FileStat: > UTIL_utime(file)
38+
Trace:FileStat: < 0

0 commit comments

Comments
 (0)