Skip to content

Commit

Permalink
Make utimens(NULL) work correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
quite authored and Nikratio committed Jun 18, 2018
1 parent fb17470 commit f045211
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

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

Release 3.3.2 (2018-04-29)
Expand Down
12 changes: 10 additions & 2 deletions sshfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,14 @@ static int sshfs_utimens(const char *path, const struct timespec tv[2],
int err;
struct buffer buf;
struct sshfs_file *sf = NULL;
time_t asec = tv[0].tv_sec, msec = tv[1].tv_sec;

struct timeval now;
gettimeofday(&now, NULL);
if (asec == 0)
asec = now.tv_sec;
if (msec == 0)
msec = now.tv_sec;

if (fi != NULL) {
sf = get_sshfs_file(fi);
Expand All @@ -2486,8 +2494,8 @@ static int sshfs_utimens(const char *path, const struct timespec tv[2],
else
buf_add_buf(&buf, &sf->handle);
buf_add_uint32(&buf, SSH_FILEXFER_ATTR_ACMODTIME);
buf_add_uint32(&buf, tv[0].tv_sec);
buf_add_uint32(&buf, tv[1].tv_sec);
buf_add_uint32(&buf, asec);
buf_add_uint32(&buf, msec);

err = sftp_request(sf == NULL ? SSH_FXP_SETSTAT : SSH_FXP_FSETSTAT,
&buf, SSH_FXP_STATUS, NULL);
Expand Down
13 changes: 13 additions & 0 deletions test/test_sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_sshfs(tmpdir, debug, cache_timeout, sync_rd, capfd):
# SSHFS only supports one second resolution when setting
# file timestamps.
tst_utimens(mnt_dir, tol=1)
tst_utimens_now(mnt_dir)

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

def tst_utimens_now(mnt_dir):
fullname = pjoin(mnt_dir, name_generator())

fd = os.open(fullname, os.O_CREAT | os.O_RDWR)
os.close(fd)
os.utime(fullname, None)

fstat = os.lstat(fullname)
# We should get now-timestamps
assert fstat.st_atime != 0
assert fstat.st_mtime != 0

def tst_passthrough(src_dir, mnt_dir, cache_timeout):
name = name_generator()
src_name = pjoin(src_dir, name)
Expand Down

0 comments on commit f045211

Please sign in to comment.