Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm methods #8

Merged
merged 17 commits into from
Jan 9, 2023
Prev Previous commit
Next Next commit
fix(dir methods): rm
  • Loading branch information
korostelevm committed Jan 9, 2023
commit 80514286ac16a52913cfd2b5e95087e2691feff2
37 changes: 25 additions & 12 deletions src/CyclicS3FSPromises.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class CyclicS3FSPromises{
}));
}catch(e){
if(e.name === 'NotFound'){
throw new Error(`Error: ENOENT: no such file or directory, stat '${fileName}'`)
throw new Error(`ENOENT: no such file or directory, stat '${fileName}'`)
}else{
throw e
}
Expand Down Expand Up @@ -147,7 +147,7 @@ class CyclicS3FSPromises{
result = folders.concat(files).filter(r=>{return r.length})
}catch(e){
if(e.name === 'NotFound' || e.message === 'NotFound'){
throw new Error(`Error: ENOENT: no such file or directory, scandir '${path}'`)
throw new Error(`ENOENT: no such file or directory, scandir '${path}'`)
}else{
throw e
}
Expand All @@ -157,7 +157,18 @@ class CyclicS3FSPromises{

async rm(path){
try{
let f = await this.stat(path)
let f = await Promise.allSettled([
this.stat(path),
this.readdir(path)
])

if(f[0].status == 'rejected' && f[1].status == 'fulfilled'){
throw new Error(`SystemError [ERR_FS_EISDIR]: Path is a directory: rm returned EISDIR (is a directory) ${path}`)
}
if(f[0].status == 'rejected' && f[1].status == 'rejected'){
throw f[0].reason
}

}catch(e){
throw e
}
Expand All @@ -175,15 +186,17 @@ class CyclicS3FSPromises{
}


// async rmdir(path){
// path = util.normalize_dir(path)
// try{
// let await readdir(path)
// // await this.s3.send(cmd)
// }catch(e){
// throw e
// }
// }
async rmdir(path){
try{
let contents = await this.readdir(path)
if(contents.length){
throw new Error(`ENOTEMPTY: directory not empty, rmdir '${path}'`)
}
}catch(e){
throw e
}
// path = util.normalize_dir(path)
}

}

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class CyclicS3FS extends CyclicS3FSPromises {
new Promise(async (resolve,reject)=>{
try{
this.stat = super.stat
this.readdir = super.readdir
let res = await super.rm(...arguments)
return resolve(callback(null,res))
}catch(e){
Expand Down
11 changes: 10 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,21 @@ describe("Basic smoke tests", () => {
resolve()
})
})
})

test("rmSync(directory)", async () => {
const fs = s3fs(BUCKET)
fs.mkdirSync('/dir')


try{
fs.rmSync('/dir')
}catch(e){
expect(e).toContain(`[ERR_FS_EISDIR]: Path is a directory: rm returned EISDIR (is a directory)`)
}

})





Expand Down