"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/touch/touch.js" (11 Apr 2017, 4179 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var fs = require("fs")
    2   , cons = require("constants")
    3 
    4 module.exports = touch
    5 touch.touchSync = touch.sync = function (f, options) {
    6   return touch(f, options)
    7 }
    8 
    9 touch.ftouch = ftouch
   10 touch.ftouchSync = function (fd, options) {
   11   return ftouch(fd, options)
   12 }
   13 
   14 function validOpts (options) {
   15   options = Object.create(options || {})
   16 
   17   // {mtime: true}, {ctime: true}
   18   // If set to something else, then treat as epoch ms value
   19   var now = new Date(options.time || Date.now())
   20   if (!options.atime && !options.mtime) {
   21     options.atime = options.mtime = now
   22   } else if (true === options.atime) {
   23     options.atime = now
   24   } else if (true === options.mtime) {
   25     options.mtime = now
   26   }
   27 
   28   var oflags = 0
   29   if (!options.force) {
   30     oflags = oflags | cons.O_RDWR
   31   }
   32   if (!options.nocreate) {
   33     oflags = oflags | cons.O_CREAT
   34   }
   35 
   36   options.oflags = oflags
   37   return options
   38 }
   39 
   40 function optionsRef (then, arg, options, cb) {
   41   if (!options.ref) return then(arg, options, cb)
   42 
   43   return cb
   44        ? fs.stat(options.ref, optionsRefcb(then, arg, options, cb))
   45        : optionsRefcb(then, arg, options)(null, fs.statSync(options.ref))
   46 }
   47 
   48 function optionsRefcb (then, arg, options, cb) { return function (er, s) {
   49   if (er) {
   50     er.path = er.file = options.ref
   51     return cb(er)
   52   }
   53   options.atime = options.atime && s.atime.getTime()
   54   options.mtime = options.mtime && s.mtime.getTime()
   55 
   56   // so we don't keep doing this.
   57   options.ref = null
   58 
   59   return then(arg, options, cb)
   60 }}
   61 
   62 function touch (f, options, cb) {
   63   if (typeof options === "function") cb = options, options = null
   64   options = validOpts(options)
   65   return optionsRef(touch_, f, validOpts(options), cb)
   66 }
   67 
   68 function touch_ (f, options, cb) {
   69   return openThenF(f, options, cb)
   70 }
   71 
   72 function openThenF (f, options, cb) {
   73   options.closeAfter = true
   74   return cb
   75        ? fs.open(f, options.oflags, openThenFcb(options, cb))
   76        : openThenFcb(options)(null, fs.openSync(f, options.oflags))
   77 }
   78 
   79 function openThenFcb (options, cb) { return function (er, fd) {
   80   if (er) {
   81     if (fd && options.closeAfter) fs.close(fd, function () {})
   82     return cb(er)
   83   }
   84   return ftouch(fd, options, cb)
   85 }}
   86 
   87 function ftouch (fd, options, cb) {
   88   if (typeof options === "function") cb = options, options = null
   89   return optionsRef(ftouch_, fd, validOpts(options), cb)
   90 }
   91 
   92 function ftouch_ (fd, options, cb) {
   93   // still not set.  leave as what the file already has.
   94   return fstatThenFutimes(fd, options, cb)
   95 }
   96 
   97 function fstatThenFutimes (fd, options, cb) {
   98   if (options.atime && options.mtime) return thenFutimes(fd, options, cb)
   99 
  100   return cb
  101        ? fs.fstat(fd, fstatThenFutimescb(fd, options, cb))
  102        : fstatThenFutimescb(fd, options)(null, fs.fstatSync(fd))
  103 }
  104 
  105 function fstatThenFutimescb (fd, options, cb) { return function (er, s) {
  106   if (er) {
  107     if (options.closeAfter) fs.close(fd, function () {})
  108     return cb(er)
  109   }
  110   options.atime = options.atime || s.atime.getTime()
  111   options.mtime = options.mtime || s.mtime.getTime()
  112   return thenFutimes(fd, options, cb)
  113 }}
  114 
  115 function thenFutimes (fd, options, cb) {
  116   if (typeof options.atime === "object") {
  117     options.atime = options.atime.getTime()
  118   }
  119   if (typeof options.mtime === "object") {
  120     options.mtime = options.mtime.getTime()
  121   }
  122 
  123   var a = parseInt(options.atime / 1000, 10)
  124     , m = parseInt(options.mtime / 1000, 10)
  125   return cb
  126        ? fs.futimes(fd, a, m, thenFutimescb(fd, options, cb))
  127        : thenFutimescb(fd, options)(null, fs.futimesSync(fd, a, m))
  128 }
  129 
  130 function thenFutimescb (fd, options, cb) { return function (er, res) {
  131   if (er) {
  132     if (options.closeAfter) fs.close(fd, function () {})
  133     return cb(er)
  134   }
  135   return finish(fd, options, res, cb)
  136 }}
  137 
  138 function finish (fd, options, res, cb) {
  139   return options.closeAfter ? finishClose(fd, options, res, cb)
  140        : cb ? cb(null, res)
  141        : res
  142 }
  143 
  144 function finishClose (fd, options, res, cb) {
  145   return cb
  146        ? fs.close(fd, finishClosecb(res, options, cb))
  147        : finishClosecb(res, options)(null, fs.closeSync(fd))
  148 }
  149 
  150 function finishClosecb (res, options, cb) { return function (er) {
  151   if (er) return cb(er)
  152   options.closeAfter = null
  153   return finish(null, options, res, cb)
  154 }}