简体   繁体   中英

Calling fs.access before fs.stat

I am trying to list a tree of all my folders on C. Some of the files and folders on c require admin rights and fs.stat throws an error because of this. I would like to somehow check first with fs.access for the rights and if no admin rights are needed to continue with fs.stat. All my files and folders are in an array and I'm iterating through it. But sadly I don't get it with the callbacks to work. Can someone help me with this? I appreciate every little help.

fs.readdir(seeDir, function (err, files) {
    if (err) {
        throw err;
    };
    var fileName = '';
    var path = '';
    var statCallback;

    for (var i = 0; i < files.length; i++) {
        fileName = files[i];
        path = util.format('%s%s', seeDir, fileName);
        var isLast = (i === (files.length - 1));

        fs.access(path, fs.F_OK | fs.R_OK, function(err) {
            if (err) {
                console.log(err);
            } else {
                fs.stat(path, function (err, stats) {
                    console.log(path);
                    if (err) {
                        throw err;
                    };
                    if (stats.isDirectory()) {
                        res.write(util.format(formatDirectory, path, fileName));
                    }
                    if (isLast) {
                        res.end(stringFooter);
                    }
                });
            }
            });

    }
});

async call is always problem. :(

anyway, You have two options.

first use call-back recursive function

or

use Promise way. (recommend blurbird primise)

make some change for res.send and res.end (I tested it for console.)

var fs = require('fs');
var util = require('util');
var Promise = require("bluebird");

var formatDirectory = '%s%s';


// CallBack
var getDir = function(seeDir) {
    var files = [];
    var rv = [];
    var done = function() {
        console.log(rv);
    }
    var getFile = function() {
        if(files.length == 0) { return done(); } // files empty, recursive done.
        var file = files.shift(); // pick first file
        var path = util.format('%s%s', seeDir, file);
        fs.access(path, fs.F_OK | fs.R_OK, function(err) {
            if(err) { console.log(err); getFile(); }
            else {
                fs.stat(path, function(err, stats) {
                    if(err) { console.log(err); }
                    else if(stats.isDirectory()) { rv.push(path); } // push result
                    getFile(); // recursive call
                });
            }
        });
    }
    fs.readdir(seeDir, function(err, data) {
        if(err) { throw err; }
        files = data; // initialize
        getFile();
    });
}

// Promise
var getDirPromise = function(seeDir) {
    fs.readdir(seeDir, function (err, files) {
        if (err) { throw err; };
        Promise.reduce(files, function(total, file) {
            return new Promise(function(resolve, reject) {
                var path = util.format('%s%s', seeDir, file);
                fs.access(path, fs.F_OK | fs.R_OK, function(err) {
                    if (err) { console.log(err); return resolve(total); }
                    else {
                        return fs.stat(path, function (err, stats) {
                            console.log(path);
                            if (err) { console.error(err); }
                            else if (stats.isDirectory()) {
                                console.log(util.format(formatDirectory, path, file));
                                total.push(util.format(formatDirectory, path, file));
                            }
                            resolve(total);
                        });
                    }
                });
            });
        }, []).then(function(result) {
            console.log("done", result);
        });
    });
}
//getDir('C:/');
//getDirPromise('C:/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM