UNPKG

alinex-fs

Version:

Extension of nodes filesystem tools.

115 lines (96 loc) 3.03 kB
/* Change Ownership ================================================= Recursive change file ownership like {@link fs.chown}. The options object is the same as used for {@link find.coffee} with the additional mode: - `user` - `String|Integer` - user name or id to set - `group` - `String|Integer` - group name or id to set - `dereference` - `Boolean` dereference symbolic links and go into them - `ìgnoreErrors` - `Boolean` go on and ignore IO errors */ (function() { var async, debug, find, fs, getGid, getUid, parallel, posix; debug = require('debug')('fs:chowns'); fs = require('fs'); async = require('async'); posix = require('posix'); find = require('./find'); parallel = require('../helper/parallel'); /* @param {String} source file path or directory to search @param {Object} options selection of files to search and user/group id @param {function(Error)} cb callback with error if something went wrong - No file to change owner for found! */ module.exports.chmods = function(source, options, cb) { if (cb == null) { cb = function() {}; } return find.find(source, options, function(err, list) { var error, gid, uid; if (err) { return cb(err); } if (!(list.length || options.ignoreErrors)) { return cb(new Error("No file to change owner for found!")); } try { uid = getUid(options); gid = getGid(options); } catch (error1) { error = error1; if (!options.ignoreErrors) { return cb(error); } } return async.eachLimit(list, parallel(options), function(file, cb) { if (debug.enabled) { debug("chown of " + file); } return fs.chown(file, uid, gid, cb); }, function(err) { return cb(err, list); }); }); }; /* @param {String} source file path or directory to search @param {Object} options selection of files to search and user/group id @throws {Error} if something went wrong - No file to change owner for found! */ module.exports.chmodsSync = function(source, options) { var error, file, gid, i, len, list, uid; list = find.findSync(source, options); if (!(list.length || options.ignoreErrors)) { return new Error("No file to change owner for found!"); } try { uid = getUid(options); gid = getGid(options); } catch (error1) { error = error1; if (!options.ignoreErrors) { throw error; } } for (i = 0, len = list.length; i < len; i++) { file = list[i]; fs.chownSync(file, uid, gid); } return list; }; getUid = function(options) { if (options.user && !isNaN(options.user)) { return posix.getpwnam(options.user).uid; } return options.user; }; getGid = function(options) { if (options.group && !isNaN(options.group)) { return posix.getgrnam(options.group).gid; } return options.group; }; }).call(this); //# sourceMappingURL=chowns.map