javascript - Calling fs.access before fs.stat -


i trying list tree of folders on c. of files , folders on c require admin rights , fs.stat throws error because of this. somehow check first fs.access rights , if no admin rights needed continue fs.stat. files , folders in array , i'm iterating through it. sadly don't callbacks work. can me this? appreciate every little help.

fs.readdir(seedir, function (err, files) {     if (err) {         throw err;     };     var filename = '';     var path = '';     var statcallback;      (var = 0; < 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 problem. :(

anyway, have 2 options.

first use call-back recursive function

or

use promise way. (recommend blurbird primise)

make change res.send , res.end (i tested 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:/'); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -