Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

utimes.js 2.3KB

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