Skip to content

Commit

Permalink
properly support sub-second stat times
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed May 14, 2024
1 parent 8808bfb commit 6fe73b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const validOpts = (options, path, fd) => {

// {mtime: true}, {ctime: true}
// If set to something else, then treat as epoch ms value
const now = parseInt(new Date(options.time || Date.now()).getTime() / 1000)
const now = new Date(options.time || Date.now()).getTime() / 1000
if (!options.atime && !options.mtime)
options.atime = options.mtime = now
else {
Expand Down Expand Up @@ -129,8 +129,8 @@ class Touch extends EE {
}

onstatref (st) {
this.atime = this.atime && parseInt(st.atime.getTime()/1000, 10)
this.mtime = this.mtime && parseInt(st.mtime.getTime()/1000, 10)
this.atime = this.atime && st.atime.getTime()/1000
this.mtime = this.mtime && st.mtime.getTime()/1000
if (!this.atime || !this.mtime)
this.fstat()
else
Expand All @@ -150,10 +150,10 @@ class Touch extends EE {

onfstat (st) {
if (typeof this.atime !== 'number')
this.atime = parseInt(st.atime.getTime()/1000, 10)
this.atime = st.atime.getTime()/1000

if (typeof this.mtime !== 'number')
this.mtime = parseInt(st.mtime.getTime()/1000, 10)
this.mtime = st.mtime.getTime()/1000

this.futimes()
}
Expand Down
23 changes: 14 additions & 9 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dirs.forEach(d => {
try { fs.mkdirSync(d) } catch (e) {}
})

const now = Math.floor(Date.now() / 1000) * 1000
const now = Date.now()
const then = now - 1000000000 // now - 1Msec

t.teardown(() => {
Expand All @@ -50,10 +50,12 @@ t.test('set both to now', t => {
const asm = astat.mtime.getTime()
const ssm = sstat.mtime.getTime()

t.equal(asm, asa)
t.equal(ssm, ssa)
t.equal(ssa, now)
t.equal(asa, now)
// can actually end up a few ms apart on systems with support
// for subsecond stat times.
t.equal(Math.abs(asm - asa) < 1000, true)
t.equal(Math.abs(ssm - ssa) < 1000, true)
t.equal(Math.abs(ssa - now) < 1000, true)
t.equal(Math.abs(asa - now) < 1000, true)

// ctime should always be now-ish
t.ok(Math.abs(Date.now() - sstat.ctime.getTime()) < 10000)
Expand Down Expand Up @@ -273,8 +275,8 @@ t.test('use one file as ref for another, only mtime', t => {
fs.writeFileSync('sync', '')
fs.writeFileSync('async', '')

fs.utimesSync('sync-ref', then, then)
fs.utimesSync('async-ref', then, then)
fs.utimesSync('sync-ref', then/1000, then/1000)
fs.utimesSync('async-ref', then/1000, then/1000)
fs.utimesSync('sync', now, now)
fs.utimesSync('async', now, now)

Expand All @@ -301,8 +303,8 @@ t.test('use one file as ref for another, only mtime', t => {
t.equal(asm, arsm)
t.equal(ssm, srsm)

t.notEqual(asa, arsa)
t.notEqual(ssa, srsa)
t.not(asa, arsa)
t.not(ssa, srsa)

t.ok(Math.abs(Date.now() - srsc) < 1000)
t.ok(Math.abs(Date.now() - arsc) < 1000)
Expand Down Expand Up @@ -423,8 +425,11 @@ t.test('futimes fail, close after', t => {

touch.ftouch(fd, { closeAfter: true }, er => {
t.equal(er, poop)
t.equal(closes, 1, '1 close')
t.equal(closeSyncs, 1, '1 closeSync')
t.end()
})

})

t.test('ref stat fail', t => {
Expand Down

0 comments on commit 6fe73b8

Please sign in to comment.