Skip to content

Commit 7385ebb

Browse files
committed
fs bug fixes and improved fault tolarance
1 parent d579937 commit 7385ebb

2 files changed

Lines changed: 60 additions & 29 deletions

File tree

src/fslib_mounts.js

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -201,34 +201,57 @@ function mountNativeFolder(optionalDirHandle, callback) {
201201
});
202202
}
203203

204+
async function _verifyDirNodeCanBeRead(handle) {
205+
try {
206+
if(handle.kind === Constants.KIND_DIRECTORY){
207+
let entries = handle.entries();
208+
await entries.next();
209+
}
210+
return null;
211+
} catch (e) {
212+
if(e.code === e.NOT_FOUND_ERR){
213+
return new Errors.ENOENT(`Dir does not exist ${handle.name}`, e);
214+
} else {
215+
return new Errors.EIO(`Phoenix fs could not read directory ${handle.name}`, e);
216+
}
217+
}
218+
}
219+
204220
async function _findLeafNode(currentNode, pathArray, currentIndex, callback) {
221+
let error = await _verifyDirNodeCanBeRead(currentNode);
222+
if(error){
223+
callback(error);
224+
return;
225+
}
226+
205227
let pathLength = pathArray.length;
206228
if(currentIndex === pathLength) {
207229
callback(null, currentNode);
208-
} else {
209-
let childName = pathArray[currentIndex];
210-
let childDirHandle = null;
211-
let childFileHandle = null;
212-
try {
213-
childDirHandle = await currentNode.getDirectoryHandle(childName);
214-
} catch (e) {
215-
// do nothing
216-
}
217-
try {
218-
childFileHandle = await currentNode.getFileHandle(childName);
219-
} catch (e) {
220-
// do nothing
221-
}
230+
return;
231+
}
222232

223-
if(childFileHandle && currentIndex === pathLength - 1) {
224-
// the last node is a file
225-
callback(null, childFileHandle);
226-
} else if(childDirHandle) {
227-
_findLeafNode(childDirHandle, pathArray, currentIndex + 1, callback);
228-
} else {
229-
let path= pathArray.join('/');
230-
callback(new Errors.ENOENT('File/Dir does not exist: ', path));
231-
}
233+
let childName = pathArray[currentIndex];
234+
let childDirHandle = null;
235+
let childFileHandle = null;
236+
try {
237+
childDirHandle = await currentNode.getDirectoryHandle(childName);
238+
} catch (e) {
239+
// do nothing
240+
}
241+
try {
242+
childFileHandle = await currentNode.getFileHandle(childName);
243+
} catch (e) {
244+
// do nothing
245+
}
246+
247+
if(childFileHandle && currentIndex === pathLength - 1) {
248+
// the last node is a file
249+
callback(null, childFileHandle);
250+
} else if(childDirHandle) {
251+
_findLeafNode(childDirHandle, pathArray, currentIndex + 1, callback);
252+
} else {
253+
let path= pathArray.join('/');
254+
callback(new Errors.ENOENT('File/Dir does not exist: ', path));
232255
}
233256
}
234257

src/fslib_native.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,21 @@ const {Utils} =require('./utils');
2929

3030
async function _listDir(handle, callback) {
3131
let dirEntryNames = [];
32-
for await (const [key] of handle.entries()) {
33-
dirEntryNames.push(key);
34-
}
35-
if(callback){
36-
callback(null, dirEntryNames);
32+
try {
33+
for await (const [key] of handle.entries()) {
34+
dirEntryNames.push(key);
35+
}
36+
if(callback){
37+
callback(null, dirEntryNames);
38+
}
39+
return dirEntryNames;
40+
} catch (e) {
41+
if(e.code === e.NOT_FOUND_ERR){
42+
callback(new Errors.ENOENT(`Dir does not exist ${handle.name}`, e));
43+
} else {
44+
callback(new Errors.EIO(`Phoenix fs could not read directory ${handle.name}`, e));
45+
}
3746
}
38-
return dirEntryNames;
3947
}
4048

4149

0 commit comments

Comments
 (0)