refactored

This commit is contained in:
coderaiser 2013-04-08 10:29:04 -04:00
parent 65b51f730f
commit bde849b28b

View file

@ -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);
}
})();