fs.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * @module vow-fs
  3. * @author Filatov Dmitry <dfilatov@yandex-team.ru>
  4. * @version 0.3.2
  5. * @license
  6. * Dual licensed under the MIT and GPL licenses:
  7. * * http://www.opensource.org/licenses/mit-license.php
  8. * * http://www.gnu.org/licenses/gpl.html
  9. */
  10. var vow = require('vow'),
  11. Queue = require('vow-queue'),
  12. openFilesQueue = new Queue(),
  13. fs = require('fs'),
  14. path = require('path'),
  15. os = require('os'),
  16. uuid = require('node-uuid'),
  17. glob = require('glob'),
  18. slice = Array.prototype.slice,
  19. promisify = function(nodeFn) {
  20. return function() {
  21. var defer = vow.defer(),
  22. args = slice.call(arguments);
  23. args.push(function(err) {
  24. err? defer.reject(err) : defer.resolve(arguments[1]);
  25. });
  26. try {
  27. nodeFn.apply(fs, args);
  28. }
  29. catch(err) {
  30. defer.reject(err);
  31. }
  32. return defer.promise();
  33. };
  34. },
  35. tmpDir = os.tmpdir || os.tmpDir || function() { return '/tmp'; },
  36. makeDir = promisify(fs.mkdir),
  37. removeDir = promisify(fs.rmdir),
  38. lstat = promisify(fs.lstat),
  39. emfileTimeout = 1,
  40. emfileFixWrapper = function(method, weight) {
  41. var wrapper = function() {
  42. var callArgs = arguments;
  43. return openFilesQueue.enqueue(function() {
  44. return method.apply(vfs, callArgs).then(
  45. function(res) {
  46. emfileTimeout = Math.max(1, emfileTimeout / 2);
  47. return res;
  48. },
  49. function(err) {
  50. if(err.code === 'EMFILE') {
  51. emfileTimeout++;
  52. return vow.delay(null, emfileTimeout).then(function() {
  53. return wrapper.apply(vfs, callArgs);
  54. });
  55. }
  56. else {
  57. throw err;
  58. }
  59. });
  60. }, weight);
  61. };
  62. return wrapper;
  63. },
  64. undef;
  65. var vfs = module.exports = {
  66. /**
  67. * Reads file by given path
  68. * @param {String} path
  69. * @param {String} [encoding=utf8]
  70. * @returns {vow:Promise}
  71. */
  72. read : emfileFixWrapper(promisify(fs.readFile)),
  73. /**
  74. * Writes data to file by given path
  75. * @param {String} path
  76. * @param {String|Buffer} data
  77. * @param {String} [encoding=utf8]
  78. * @returns {vow:Promise}
  79. */
  80. write : emfileFixWrapper(promisify(fs.writeFile)),
  81. /**
  82. * Appends data to file by given path
  83. * @param {String} path
  84. * @param {String|Buffer} data
  85. * @param {String} [encoding=utf8]
  86. * @returns {vow:Promise}
  87. */
  88. append : emfileFixWrapper(promisify(fs.appendFile)),
  89. /**
  90. * Removes file at given path
  91. * @param {String} pathToRemove
  92. * @returns {vow:Promise}
  93. */
  94. remove : promisify(fs.unlink),
  95. /**
  96. * Copies file from sourcePath to targetPath
  97. * @param {String} sourcePath
  98. * @param {String} targetPath
  99. * @returns {vow:Promise}
  100. */
  101. copy : emfileFixWrapper(function(sourcePath, targetPath) {
  102. return this.isFile(sourcePath).then(function(isFile) {
  103. if(!isFile) {
  104. var err = Error();
  105. err.errno = 28;
  106. err.code = 'EISDIR';
  107. err.path = sourcePath;
  108. throw err;
  109. }
  110. var defer = vow.defer(),
  111. sourceStream = fs.createReadStream(sourcePath),
  112. errFn = function(err) {
  113. defer.reject(err);
  114. };
  115. sourceStream
  116. .on('error', errFn)
  117. .on('open', function() {
  118. var targetStream = fs.createWriteStream(targetPath);
  119. sourceStream.pipe(
  120. targetStream
  121. .on('error', errFn)
  122. .on('close', function() {
  123. defer.resolve();
  124. }));
  125. });
  126. return defer.promise();
  127. });
  128. }, 2),
  129. /**
  130. * Moves from sourcePath to targetPath
  131. * @param {String} sourcePath
  132. * @param {String} targetPath
  133. * @returns {vow:Promise}
  134. */
  135. move : promisify(fs.rename),
  136. /**
  137. * Extracts fs.Stats about a given path
  138. * @param {String} path
  139. * @returns {vow:Promise}
  140. */
  141. stat : promisify(fs.stat),
  142. /**
  143. * Tests whether or not the given path exists
  144. * @param {String} path
  145. * @returns {vow:Promise}
  146. */
  147. exists : fs.exists?
  148. function(path) {
  149. var defer = vow.defer();
  150. fs.exists(path, function(exists) {
  151. defer.resolve(exists);
  152. });
  153. return defer.promise();
  154. } :
  155. function(path) {
  156. var defer = vow.defer();
  157. fs.stat(path, function(err) {
  158. defer.resolve(!err);
  159. });
  160. return defer.promise();
  161. },
  162. /**
  163. * Creates a hard link from the sourcePath to targetPath
  164. * @param {String} sourcePath
  165. * @param {String} targetPath
  166. * @returns {vow:Promise}
  167. */
  168. link : promisify(fs.link),
  169. /**
  170. * Creates a relative symbolic link from the sourcePath to targetPath
  171. * @param {String} sourcePath
  172. * @param {String} targetPath
  173. * @param {String} [type=file] can be either 'dir', 'file', or 'junction'
  174. * @returns {vow:Promise}
  175. */
  176. symLink : promisify(fs.symlink),
  177. /**
  178. * Changes the owner for a given path using Unix user-id and group-id numbers
  179. * @param {String} path
  180. * @param uid
  181. * @param gid
  182. * @returns {vow:Promise}
  183. */
  184. chown : promisify(fs.chown),
  185. /**
  186. * Changes the Unix mode for a path. Returns a promise
  187. * @param {String} path
  188. * @param mode
  189. * @returns {vow:Promise}
  190. */
  191. chmod : promisify(fs.chmod),
  192. /**
  193. * Normalizes a given path to absolute path
  194. * @param {String} path
  195. * @returns {vow:Promise}
  196. */
  197. absolute : promisify(fs.realpath),
  198. /**
  199. * Checks whether the given path is a file
  200. * @param {String} path
  201. * @returns {vow:Promise}
  202. */
  203. isFile : function(path) {
  204. return this.stat(path).then(function(stats) {
  205. return stats.isFile();
  206. });
  207. },
  208. /**
  209. * Checks whether the given path is a directory
  210. * @param {String} path
  211. * @returns {vow:Promise}
  212. */
  213. isDir : function(path) {
  214. return this.stat(path).then(function(stats) {
  215. return stats.isDirectory();
  216. });
  217. },
  218. /**
  219. * Checks whether the given path is a socket
  220. * @param {String} path
  221. * @returns {vow:Promise}
  222. */
  223. isSocket : function(path) {
  224. return this.stat(path).then(function(stats) {
  225. return stats.isSocket();
  226. });
  227. },
  228. /**
  229. * Checks whether the given path is a symbolic link
  230. * @param {String} path
  231. * @returns {vow:Promise}
  232. */
  233. isSymLink : function(path) {
  234. return lstat(path).then(function(stats) {
  235. return stats.isSymbolicLink();
  236. });
  237. },
  238. /**
  239. * Makes a temporary file
  240. * @param {Object} options
  241. * @param {String} [options.prefix]
  242. * @param {String} [options.dir=os.tmpdir()]
  243. * @param {String} [options.ext=tmp]
  244. * @returns {vow:Promise}
  245. */
  246. makeTmpFile : function(options) {
  247. options || (options = {});
  248. var filePath = path.join(
  249. options.dir || tmpDir(),
  250. (options.prefix || '') + uuid.v4() + (options.ext || '.tmp'));
  251. return vfs.write(filePath, '').then(function() {
  252. return filePath;
  253. });
  254. },
  255. /**
  256. * Reads the contents of a directory by given path
  257. * @param {String} path
  258. * @returns {vow:Promise}
  259. */
  260. listDir : emfileFixWrapper(promisify(fs.readdir)),
  261. /**
  262. * Makes a directory at a given path, recursively creating any branches that doesn't exist
  263. * @param {String} dirPath
  264. * @param [mode=0777]
  265. * @param [failIfExist=false]
  266. * @returns {vow:Promise}
  267. */
  268. makeDir : function(dirPath, mode, failIfExist) {
  269. if(typeof mode === 'boolean') {
  270. failIfExist = mode;
  271. mode = undef;
  272. }
  273. var dirName = path.dirname(dirPath),
  274. onFailed = function(e) {
  275. if(e.code !== 'EEXIST' || failIfExist) {
  276. throw e;
  277. }
  278. return vfs.isDir(dirPath).then(function(isDir) {
  279. if(!isDir) {
  280. throw e;
  281. }
  282. });
  283. };
  284. return vfs.exists(dirName).then(function(exists) {
  285. if(exists) {
  286. return makeDir(dirPath, mode).fail(onFailed);
  287. }
  288. else {
  289. failIfExist = false;
  290. return vfs.makeDir(dirName, mode).then(function() {
  291. return makeDir(dirPath, mode).fail(onFailed);
  292. });
  293. }
  294. });
  295. },
  296. /**
  297. * Removes directory
  298. * @param {String} dirPath
  299. * @returns {vow:Promise}
  300. */
  301. removeDir : function(dirPath) {
  302. return vfs.listDir(dirPath)
  303. .then(function(list) {
  304. return list.length && vow.all(
  305. list.map(function(file) {
  306. var fullPath = path.join(dirPath, file);
  307. return vfs.isFile(fullPath).then(function(isFile) {
  308. return isFile?
  309. vfs.remove(fullPath) :
  310. vfs.removeDir(fullPath);
  311. });
  312. }));
  313. })
  314. .then(function() {
  315. return removeDir(dirPath);
  316. });
  317. },
  318. /**
  319. * Matches files using the patterns, see https://github.com/isaacs/node-glob for details
  320. * @param {String} pattern to be matched
  321. * @param {Object} [options] options
  322. * @returns {vow:Promise}
  323. */
  324. glob : promisify(glob),
  325. options : function(opts) {
  326. if(typeof opts.openFileLimit !== 'undefined') {
  327. openFilesQueue.params({ weightLimit : opts.openFileLimit });
  328. }
  329. }
  330. };
  331. openFilesQueue.start();