Skip to content

Commit 8cec911

Browse files
committed
File system API fixed
1 parent b3f37a0 commit 8cec911

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

js/core/fs/index.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const utils = new Utils(llfs);
2323
const { log, success, error, warn } = $$.logger;
2424

2525
module.exports = {
26-
readdir (path, options = () => {}, callback = options) {
26+
readdir (path, options = () => { }, callback = options) {
2727
let resolved = null;
2828

2929
if (utils.isSystemPath(path)) {
@@ -80,10 +80,10 @@ module.exports = {
8080
return filesystem.readdir(resolved.parts.slice(2).join('/'), callback);
8181
})
8282

83-
/* .then(list => {
84-
if (resolved.level <= 1) return;
85-
callback(null, list.map(file => file.name), list);
86-
})*/
83+
/* .then(list => {
84+
if (resolved.level <= 1) return;
85+
callback(null, list.map(file => file.name), list);
86+
})*/
8787
.catch((err) => {
8888
callback(err);
8989
});
@@ -95,17 +95,24 @@ module.exports = {
9595
* @param {object} [options] - Options
9696
* @param {function} callback - Callback(error, result)
9797
*/
98-
readFile (path, options = () => {}, callback = options) {
98+
readFile (path, options = () => { }, callback = options) {
9999
let resolved = null;
100100

101+
const encoding = typeof options === 'string'
102+
? options
103+
: 'buffer';
104+
101105
if (utils.isSystemPath(path)) {
102106
log(`${path} is a system path`, { level: 'fs' });
103107

104108
const extpath = utils.extractSystemPath(path);
105109

106110
log(`${path} extracted to ${extpath}`, { level: 'fs' });
107111

108-
callback(null, __SYSCALL.initrdReadFileBuffer(extpath));
112+
if (encoding === 'buffer')
113+
callback(null, Buffer.from(__SYSCALL.initrdReadFileBuffer(extpath)));
114+
else
115+
callback(null, Buffer.from(__SYSCALL.initrdReadFileBuffer(extpath)).toString(encoding));
109116
success('OK!', { from: 'FS->readFile->System', level: 'fs' });
110117

111118
return;
@@ -133,11 +140,10 @@ module.exports = {
133140

134141
/** Returns file content (or buffer if encoding = 'buffer')
135142
* @param {string} path - Path to file
136-
* @param {string} [encoding='ascii'] - File encoding
143+
* @param {string} [encoding='buffer'] - File encoding
137144
* @returns {string} or Buffer
138145
*/
139-
readFileSync (path, encoding = 'ascii') {
140-
// TODO: buffer => binary
146+
readFileSync (path, encoding = 'buffer') {
141147
if (utils.isSystemPath(path)) {
142148
log(`${path} is a system path`, { level: 'fs' });
143149

@@ -147,17 +153,19 @@ module.exports = {
147153

148154
success('OK!', { from: 'FS->readFile->System', level: 'fs' });
149155

156+
const buffer = Buffer.from(__SYSCALL.initrdReadFileBuffer(extpath));
157+
150158
return encoding === 'buffer'
151-
? __SYSCALL.initrdReadFileBuffer(extpath)
152-
: __SYSCALL.initrdReadFile(extpath);
159+
? buffer
160+
: buffer.toString(encoding);
153161
}
154162

155-
warn('readFileSync for external pathes doesn\'t implemented!', { from: 'fs->readFileSync' });
163+
warn('readFileSync for external pathes doesn\'t implemented! Use readFile.', { from: 'fs->readFileSync' });
156164

157-
return ''; // TODO:
165+
return new Buffer(0); // TODO:
158166
},
159167

160-
writeFile (path, data, options = () => {}, callback = options) {
168+
writeFile (path, data, options = () => { }, callback = options) {
161169
let resolved = null;
162170

163171
try {
@@ -179,7 +187,8 @@ module.exports = {
179187
callback(new Error('Is a directory'));
180188
}
181189
},
182-
mkdir (path, options = () => {}, callback = options) {
190+
191+
mkdir (path, options = () => { }, callback = options) {
183192
let resolved = null;
184193

185194
try {

js/modules/fs.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ function readFileImpl (fnName, path, opts) {
8080
return [makeErrorNotFound(path, fnName), null];
8181
}
8282

83-
const buf = __SYSCALL.initrdReadFileBuffer(absolute);
83+
const buf = Buffer.from(__SYSCALL.initrdReadFileBuffer(absolute));
8484

8585
if (!buf) {
8686
return [makeErrorNotFound(path, fnName), null];
8787
}
8888

8989
if (encoding) {
90-
return [null, new Buffer(buf).toString(encoding)];
90+
return [null, buf.toString(encoding)];
9191
}
9292

93-
return [null, new Buffer(buf)];
93+
return [null, buf];
9494
}
9595

9696
exports.readFileImpl = (path, opts, cb) => {
@@ -115,6 +115,7 @@ exports.readFileImplSync = (path, opts) => {
115115

116116
return buf;
117117
};
118+
118119
exports.readFile = fsmod.readFile;
119120
exports.readFileSync = fsmod.readFileSync;
120121

0 commit comments

Comments
 (0)