From cfa935d8fd3b94d3e566c0d41193754a77ddb43c Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 8 Apr 2013 10:29:04 -0400 Subject: [PATCH] refactored --- lib/server/dir.js | 70 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/lib/server/dir.js b/lib/server/dir.js index 8a8585a5..8e4f70da 100644 --- a/lib/server/dir.js +++ b/lib/server/dir.js @@ -20,26 +20,56 @@ path = main.path; exports.getSize = function(pDir, pCallBack) { - var lAsyncRunning = 0, - lFileCounter = 1, - lTotal = 0; + var lTotal = 0; - function getDirSize(pDir) { + function calcSize(pParams){ + var lStat = pParams.stat, + lSize = lStat && lStat.size || 0; + + lTotal += lSize; + } + + processDir(pDir, calcSize, function(){ + console.log(lTotal); + pCallBack(null, lTotal); + }); + }; + + function processDir(pDir, pFunc, pCallBack){ + var lAsyncRunning = 0, + lFileCounter = 1; + + function getDirInfo(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) { + fs.lstat(pDir, Util.call(getStat, { + name: pDir + })); + } + + function getStat(pParams) { + var lRet = Util.checkObj(pParams, ['params']); + if(lRet){ + var p = pParams, + d = p.params, + lStat = p.data, + lPath = d.name; + --lFileCounter; - if (!pError) { - if ( pStat.isFile() ) - lTotal += pStat.size; - else if ( pStat.isDirectory() ) { + if (!p.error) { + if ( lStat.isFile() ) + Util.exec(pFunc, { + name: d.name, + stat: lStat + }); + else if ( lStat.isDirectory() ) { ++lAsyncRunning; - fs.readdir(pDir, function(pError, pFiles) { + fs.readdir(lPath, function(pError, pFiles) { lAsyncRunning--; var lDirPath, n; @@ -49,8 +79,8 @@ lFileCounter += n; for (var i = 0; i < n; i++) { - lDirPath = path.join(pDir, pFiles[i]); - process.nextTick( Util.retExec(getDirSize, lDirPath) ); + lDirPath = path.join(lPath, pFiles[i]); + process.nextTick( Util.retExec(getDirInfo, lDirPath) ); } } @@ -59,18 +89,16 @@ }); } } - - execCallBack(); - }); + } + + execCallBack(); } function execCallBack(){ - if (!lFileCounter && !lAsyncRunning){ - console.log(lTotal); - pCallBack(null, lTotal); - } + if (!lFileCounter && !lAsyncRunning) + Util.exec(pCallBack); } - return getDirSize(pDir); - }; + getDirInfo(pDir); + } })(); \ No newline at end of file