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