Skip to content

Commit

Permalink
Support for non-UTF8 filenames by using Buffers in calls to fs.stat a…
Browse files Browse the repository at this point in the history
…nd fs.readdir

The names won't be properly encoded (i.e. they'll be mis-converted back to UTF-8),
but node-dir won't abort now when seeing these names, which was happening before this patch.
  • Loading branch information
miguelzf committed Dec 22, 2016
1 parent cba03d9 commit b40a44a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
20 changes: 12 additions & 8 deletions lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,24 @@ exports.files = function files(dir, type, callback, options) {
}
}

var bufdir = Buffer.from(dir);

const onDirRead = function(err, list) {
if (err) return callback(err);

pending = list.length;
if (!pending) return done();

for (var file, i = 0, l = list.length; i < l; i++) {
file = path.join(dir, list[i]);
var fname = list[i].toString();
file = path.join(dir, fname);
var buffile = Buffer.concat([bufdir, Buffer.from(path.sep), list[i]]);

if(options.sync){
var res = fs.statSync(file);
getStatHandler(file,list[i])(null, res)
var res = fs.statSync(buffile);
getStatHandler(file,fname)(null, res)
}else{
fs.stat(file, getStatHandler(file,list[i]));
fs.stat(buffile, getStatHandler(file,fname));
}
}

Expand All @@ -146,18 +150,18 @@ exports.files = function files(dir, type, callback, options) {
if (stat && stat.mode === 17115) return done();

if(options.sync){
const list = fs.readdirSync(dir)
const list = fs.readdirSync(bufdir, {encoding: 'buffer'})
return onDirRead(null, list)
}else{
fs.readdir(dir, onDirRead)
fs.readdir(bufdir, {encoding: 'buffer'}, onDirRead)
}
}

if(options.sync){
const stat = fs.statSync(dir);
const stat = fs.statSync(bufdir);
return onStat(null, stat)
}else{
fs.stat(dir, onStat);
fs.stat(bufdir, onStat);
}
};

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/testdir5/testuções.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bla
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
tdir = path.join(fixturesDir, 'testdir'),
tdir2 = path.join(fixturesDir, 'testdir2'),
tdir3 = path.join(fixturesDir, 'testdir3'),
tdir4 = path.join(fixturesDir, 'testdir4');
tdir4 = path.join(fixturesDir, 'testdir4'),
tdir5 = path.join(fixturesDir, 'testdir5');

describe('readfiles method', function() {

Expand Down Expand Up @@ -1201,6 +1202,11 @@ describe("files method", function() {
});
});

it("support non-UTF8 file names", function() {
var files = dir.files(tdir5,'file',()=>{},{sync:true});
var cmp = Buffer.from('testdir5/testuções.txt', 'latin1').toString();
path.relative(fixturesDir, files[0]).should.eql(cmp);
});
});


Expand Down

0 comments on commit b40a44a

Please sign in to comment.