From d91c8dc97c9244ce2fd2a6e9c9b362696b9b66d4 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sun, 7 Apr 2013 06:54:29 -0400 Subject: [PATCH] added module dir --- lib/server/dir.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/server/dir.js diff --git a/lib/server/dir.js b/lib/server/dir.js new file mode 100644 index 00000000..22d91658 --- /dev/null +++ b/lib/server/dir.js @@ -0,0 +1,64 @@ +/* inspired by http://procbits.com/2011/10/29/a-node-js-experiment-thinking-asynchronously-recursion-calculate-file-size-directory */ +(function(){ + 'use strict'; + + if(!global.cloudcmd) + return console.log( + '# dirsize.js' + '\n' + + '# -----------' + '\n' + + '# Module is part of Cloud Commander,' + '\n' + + '# used for getting dir size.' + '\n' + + '# If you wont to see at work' + '\n' + + '# try GET /api/v1/fs/etc?size or' + '\n' + + '# dir.getSize(\'/etc, function(err, size){' + '\n' + + '# });' + '\n' + + '# http://coderaiser.github.com/cloudcmd' + '\n'); + + var main = global.cloudcmd.main, + fs = main.fs, + path = main.path; + + exports.getSize = function(pDir, pCallBack) { + var lAsyncRunning = 0, + lFileCounter = 1, + lTotal = 0; + + function getDirSize(pDir) { + /* The lstat() function shall be equivalent to stat(), + except when path refers to a symbolic link. In that case lstat() + shall return information about the link, while stat() shall return + information about the file the link references. */ + + fs.lstat(pDir, function(pError, pStat) { + --lFileCounter; + + if (!pError) { + if ( pStat.isFile() ) + lTotal += pStat.size; + else if ( pStat.isDirectory() ) { + ++lAsyncRunning; + + fs.readdir(pDir, function(pError, pFiles) { + lAsyncRunning--; + + if (!pError){ + lFileCounter += pFiles.length; + + var lDirPath; + for (var i = 0, n = pFiles.length; i < n; i++) { + lDirPath = path.join(pDir, pFiles[i]); + getDirSize(lDirPath); + } + } + }); + } + } + + if (!lFileCounter && !lAsyncRunning) + pCallBack(null, lTotal); + }); + } + + return getDirSize(pDir); + }; +})(); \ No newline at end of file