Debian Codegraph @

fs2/chmod.js

raw
1 "use strict";
2
3 if (process.platform === "win32") {
4 module.exports = null;
5 return;
6 }
7
8 var isValue = require("type/value/is")
9 , isObject = require("type/object/is")
10 , isPlainFunction = require("type/plain-function/is")
11 , deferred = require("deferred")
12 , resolve = require("path").resolve
13 , original = require("fs").chmod
14 , lstat = require("./lstat").lstat;
15
16 var bareChmod = function (path, mode) {
17 var def = deferred();
18 original(path, mode, function (err, stats) {
19 if (err) def.reject(err);
20 else def.resolve(stats);
21 });
22 return def.promise;
23 };
24
25 var chmod = function (path, mode, options) {
26 if (!options.append) return bareChmod(path, mode);
27 return lstat(path).then(function (stats) {
28 var targetMode = stats.mode | mode;
29 if (targetMode === stats.mode) return stats;
30 return bareChmod(path, targetMode);
31 });
32 };
33 chmod.returnsPromise = true;
34
35 module.exports = exports = function (path, mode/*, options, callback*/) {
36 var options = arguments[2], callback = arguments[3];
37 if (!isValue(callback) && isPlainFunction(options)) {
38 callback = options;
39 options = {};
40 } else if (!isObject(options)) {
41 options = {};
42 }
43 return chmod(resolve(String(path)), mode, options).cb(callback);
44 };
45 exports.returnsPromise = true;
46 exports.chmod = chmod;