@@ -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+
204220async 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
0 commit comments