Skip to content

Commit a57c3b1

Browse files
authored
Merge pull request fshost#44 from miguelzf/master
Support for non-UTF8 filenames by using Buffers in calls to fs
2 parents cba03d9 + b40a44a commit a57c3b1

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

lib/paths.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,24 @@ exports.files = function files(dir, type, callback, options) {
121121
}
122122
}
123123

124+
var bufdir = Buffer.from(dir);
125+
124126
const onDirRead = function(err, list) {
125127
if (err) return callback(err);
126128

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

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

133137
if(options.sync){
134-
var res = fs.statSync(file);
135-
getStatHandler(file,list[i])(null, res)
138+
var res = fs.statSync(buffile);
139+
getStatHandler(file,fname)(null, res)
136140
}else{
137-
fs.stat(file, getStatHandler(file,list[i]));
141+
fs.stat(buffile, getStatHandler(file,fname));
138142
}
139143
}
140144

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

148152
if(options.sync){
149-
const list = fs.readdirSync(dir)
153+
const list = fs.readdirSync(bufdir, {encoding: 'buffer'})
150154
return onDirRead(null, list)
151155
}else{
152-
fs.readdir(dir, onDirRead)
156+
fs.readdir(bufdir, {encoding: 'buffer'}, onDirRead)
153157
}
154158
}
155159

156160
if(options.sync){
157-
const stat = fs.statSync(dir);
161+
const stat = fs.statSync(bufdir);
158162
return onStat(null, stat)
159163
}else{
160-
fs.stat(dir, onStat);
164+
fs.stat(bufdir, onStat);
161165
}
162166
};
163167

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bla

test/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
tdir = path.join(fixturesDir, 'testdir'),
66
tdir2 = path.join(fixturesDir, 'testdir2'),
77
tdir3 = path.join(fixturesDir, 'testdir3'),
8-
tdir4 = path.join(fixturesDir, 'testdir4');
8+
tdir4 = path.join(fixturesDir, 'testdir4'),
9+
tdir5 = path.join(fixturesDir, 'testdir5');
910

1011
describe('readfiles method', function() {
1112

@@ -1201,6 +1202,11 @@ describe("files method", function() {
12011202
});
12021203
});
12031204

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

12061212

0 commit comments

Comments
 (0)