Skip to content

Commit f045211

Browse files
quiteNikratio
authored andcommitted
Make utimens(NULL) work correctly
1 parent fb17470 commit f045211

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

ChangeLog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Make utimens(NULL) result in timestamp "now" -- no more touched files
5+
dated 1970-01-01
46
* New `createmode` workaround.
57

68
Release 3.3.2 (2018-04-29)

sshfs.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,14 @@ static int sshfs_utimens(const char *path, const struct timespec tv[2],
24732473
int err;
24742474
struct buffer buf;
24752475
struct sshfs_file *sf = NULL;
2476+
time_t asec = tv[0].tv_sec, msec = tv[1].tv_sec;
2477+
2478+
struct timeval now;
2479+
gettimeofday(&now, NULL);
2480+
if (asec == 0)
2481+
asec = now.tv_sec;
2482+
if (msec == 0)
2483+
msec = now.tv_sec;
24762484

24772485
if (fi != NULL) {
24782486
sf = get_sshfs_file(fi);
@@ -2486,8 +2494,8 @@ static int sshfs_utimens(const char *path, const struct timespec tv[2],
24862494
else
24872495
buf_add_buf(&buf, &sf->handle);
24882496
buf_add_uint32(&buf, SSH_FILEXFER_ATTR_ACMODTIME);
2489-
buf_add_uint32(&buf, tv[0].tv_sec);
2490-
buf_add_uint32(&buf, tv[1].tv_sec);
2497+
buf_add_uint32(&buf, asec);
2498+
buf_add_uint32(&buf, msec);
24912499

24922500
err = sftp_request(sf == NULL ? SSH_FXP_SETSTAT : SSH_FXP_FSETSTAT,
24932501
&buf, SSH_FXP_STATUS, NULL);

test/test_sshfs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def test_sshfs(tmpdir, debug, cache_timeout, sync_rd, capfd):
102102
# SSHFS only supports one second resolution when setting
103103
# file timestamps.
104104
tst_utimens(mnt_dir, tol=1)
105+
tst_utimens_now(mnt_dir)
105106

106107
tst_link(mnt_dir, cache_timeout)
107108
tst_truncate_path(mnt_dir)
@@ -403,6 +404,18 @@ def tst_utimens(mnt_dir, tol=0):
403404
assert abs(fstat.st_atime_ns - atime_ns) < tol*1e9
404405
assert abs(fstat.st_mtime_ns - mtime_ns) < tol*1e9
405406

407+
def tst_utimens_now(mnt_dir):
408+
fullname = pjoin(mnt_dir, name_generator())
409+
410+
fd = os.open(fullname, os.O_CREAT | os.O_RDWR)
411+
os.close(fd)
412+
os.utime(fullname, None)
413+
414+
fstat = os.lstat(fullname)
415+
# We should get now-timestamps
416+
assert fstat.st_atime != 0
417+
assert fstat.st_mtime != 0
418+
406419
def tst_passthrough(src_dir, mnt_dir, cache_timeout):
407420
name = name_generator()
408421
src_name = pjoin(src_dir, name)

0 commit comments

Comments
 (0)