mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
refactor(dir) getSize
This commit is contained in:
parent
0b989990fa
commit
24cf39ca95
1 changed files with 44 additions and 55 deletions
|
|
@ -19,85 +19,74 @@
|
|||
Util = main.util,
|
||||
path = main.path;
|
||||
|
||||
exports.getSize = function(pDir, pCallBack) {
|
||||
var lTotal = 0;
|
||||
exports.getSize = function(dir, callback) {
|
||||
var total = 0;
|
||||
|
||||
function calcSize(pParams){
|
||||
var lStat = pParams.stat,
|
||||
lSize = lStat && lStat.size || 0;
|
||||
function calcSize(stat) {
|
||||
var size = stat && stat.size || 0;
|
||||
|
||||
lTotal += lSize;
|
||||
total += size;
|
||||
}
|
||||
|
||||
processDir(pDir, calcSize, function() {
|
||||
Util.exec(pCallBack, null, lTotal);
|
||||
processDir(dir, calcSize, function() {
|
||||
Util.exec(callback, null, total);
|
||||
});
|
||||
};
|
||||
|
||||
function processDir(pDir, pFunc, pCallBack){
|
||||
var lAsyncRunning = 0,
|
||||
lFileCounter = 1;
|
||||
function processDir(dir, func, callback) {
|
||||
var asyncRunning = 0,
|
||||
fileCounter = 1;
|
||||
|
||||
function getDirInfo(pDir) {
|
||||
function getDirInfo(dir) {
|
||||
/* 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. */
|
||||
information about the file the link references.
|
||||
*/
|
||||
|
||||
fs.lstat(pDir, Util.call(getStat, {
|
||||
name: pDir
|
||||
}));
|
||||
fs.lstat(dir, getStat.bind(null, dir));
|
||||
}
|
||||
|
||||
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 (!p.error) {
|
||||
if ( lStat.isFile() )
|
||||
Util.exec(pFunc, {
|
||||
name: d.name,
|
||||
stat: lStat
|
||||
});
|
||||
else if ( lStat.isDirectory() ) {
|
||||
++lAsyncRunning;
|
||||
function getStat(dir, error, stat) {
|
||||
--fileCounter;
|
||||
|
||||
if (!error) {
|
||||
if (stat.isFile())
|
||||
Util.exec(func, stat);
|
||||
else if (stat.isDirectory()) {
|
||||
++asyncRunning;
|
||||
|
||||
fs.readdir(dir, function(error, files) {
|
||||
var dirPath, file, n, i;
|
||||
|
||||
fs.readdir(lPath, function(pError, pFiles) {
|
||||
lAsyncRunning--;
|
||||
asyncRunning--;
|
||||
|
||||
if (!error) {
|
||||
n = files.length;
|
||||
fileCounter += n;
|
||||
|
||||
var lDirPath, n;
|
||||
|
||||
if (!pError){
|
||||
n = pFiles.length;
|
||||
lFileCounter += n;
|
||||
for (i = 0; i < n; i++) {
|
||||
file = files[i];
|
||||
dirPath = path.join(dir, file);
|
||||
|
||||
for (var i = 0; i < n; i++) {
|
||||
lDirPath = path.join(lPath, pFiles[i]);
|
||||
process.nextTick(getDirInfo.bind(null, lDirPath));
|
||||
}
|
||||
process.nextTick(getDirInfo.bind(null, dirPath));
|
||||
}
|
||||
|
||||
if(!n)
|
||||
execCallBack();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if(!n)
|
||||
execCallBack();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
execCallBack();
|
||||
}
|
||||
|
||||
function execCallBack(){
|
||||
if (!lFileCounter && !lAsyncRunning)
|
||||
Util.exec(pCallBack);
|
||||
function execCallBack() {
|
||||
if (!fileCounter && !asyncRunning)
|
||||
Util.exec(callback);
|
||||
}
|
||||
|
||||
getDirInfo(pDir);
|
||||
getDirInfo(dir);
|
||||
}
|
||||
})();
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue