1 | 'use strict'
|
---|
2 |
|
---|
3 | const fs = require('graceful-fs')
|
---|
4 | const os = require('os')
|
---|
5 | const path = require('path')
|
---|
6 |
|
---|
7 | // HFS, ext{2,3}, FAT do not, Node.js v0.10 does not
|
---|
8 | function hasMillisResSync () {
|
---|
9 | let tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2))
|
---|
10 | tmpfile = path.join(os.tmpdir(), tmpfile)
|
---|
11 |
|
---|
12 | // 550 millis past UNIX epoch
|
---|
13 | const d = new Date(1435410243862)
|
---|
14 | fs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141')
|
---|
15 | const fd = fs.openSync(tmpfile, 'r+')
|
---|
16 | fs.futimesSync(fd, d, d)
|
---|
17 | fs.closeSync(fd)
|
---|
18 | return fs.statSync(tmpfile).mtime > 1435410243000
|
---|
19 | }
|
---|
20 |
|
---|
21 | function hasMillisRes (callback) {
|
---|
22 | let tmpfile = path.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2))
|
---|
23 | tmpfile = path.join(os.tmpdir(), tmpfile)
|
---|
24 |
|
---|
25 | // 550 millis past UNIX epoch
|
---|
26 | const d = new Date(1435410243862)
|
---|
27 | fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => {
|
---|
28 | if (err) return callback(err)
|
---|
29 | fs.open(tmpfile, 'r+', (err, fd) => {
|
---|
30 | if (err) return callback(err)
|
---|
31 | fs.futimes(fd, d, d, err => {
|
---|
32 | if (err) return callback(err)
|
---|
33 | fs.close(fd, err => {
|
---|
34 | if (err) return callback(err)
|
---|
35 | fs.stat(tmpfile, (err, stats) => {
|
---|
36 | if (err) return callback(err)
|
---|
37 | callback(null, stats.mtime > 1435410243000)
|
---|
38 | })
|
---|
39 | })
|
---|
40 | })
|
---|
41 | })
|
---|
42 | })
|
---|
43 | }
|
---|
44 |
|
---|
45 | function timeRemoveMillis (timestamp) {
|
---|
46 | if (typeof timestamp === 'number') {
|
---|
47 | return Math.floor(timestamp / 1000) * 1000
|
---|
48 | } else if (timestamp instanceof Date) {
|
---|
49 | return new Date(Math.floor(timestamp.getTime() / 1000) * 1000)
|
---|
50 | } else {
|
---|
51 | throw new Error('fs-extra: timeRemoveMillis() unknown parameter type')
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | function utimesMillis (path, atime, mtime, callback) {
|
---|
56 | // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback)
|
---|
57 | fs.open(path, 'r+', (err, fd) => {
|
---|
58 | if (err) return callback(err)
|
---|
59 | fs.futimes(fd, atime, mtime, futimesErr => {
|
---|
60 | fs.close(fd, closeErr => {
|
---|
61 | if (callback) callback(futimesErr || closeErr)
|
---|
62 | })
|
---|
63 | })
|
---|
64 | })
|
---|
65 | }
|
---|
66 |
|
---|
67 | function utimesMillisSync (path, atime, mtime) {
|
---|
68 | const fd = fs.openSync(path, 'r+')
|
---|
69 | fs.futimesSync(fd, atime, mtime)
|
---|
70 | return fs.closeSync(fd)
|
---|
71 | }
|
---|
72 |
|
---|
73 | module.exports = {
|
---|
74 | hasMillisRes,
|
---|
75 | hasMillisResSync,
|
---|
76 | timeRemoveMillis,
|
---|
77 | utimesMillis,
|
---|
78 | utimesMillisSync
|
---|
79 | }
|
---|