mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-24 03:05:41 +00:00
refactored
This commit is contained in:
parent
65b51f730f
commit
bde849b28b
1 changed files with 49 additions and 21 deletions
|
|
@ -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);
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue