Skip to content

Commit

Permalink
Merge pull request #8811 from nadavMiz/content-dir-empty-get
Browse files Browse the repository at this point in the history
NSFS | close stream when getting empty content dir
  • Loading branch information
nadavMiz authored Feb 27, 2025
2 parents 6d53494 + 2f27896 commit 7681865
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/sdk/namespace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,12 @@ class NamespaceFS {
// NOTE: don't move this code after the open
// this can lead to ENOENT failures due to file not exists when content size is 0
// if entry is a directory object and its content size = 0 - return empty response
if (await this._is_empty_directory_content(file_path, fs_context, params)) return null;
if (await this._is_empty_directory_content(file_path, fs_context, params)) {
res.end();
// since we don't write anything to the stream wait_finished might not be needed. added just in case there is a delay
await stream_utils.wait_finished(res, { signal: object_sdk.abort_controller.signal });
return null;
}

file = await nb_native().fs.open(
fs_context,
Expand Down
25 changes: 24 additions & 1 deletion src/test/unit_tests/test_namespace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ mocha.describe('namespace_fs', function() {
const dir_2 = '/a/b/';
const upload_key_1 = dir_1 + 'upload_key_1/';
const upload_key_2 = dir_2 + 'upload_key_2/';
const upload_key_empty = 'empty_key/';
const data = crypto.randomBytes(100);

mocha.before(async function() {
Expand All @@ -558,6 +559,22 @@ mocha.describe('namespace_fs', function() {
console.log('upload_object with trailing / response', inspect(upload_res));
});

mocha.it('get empty content dir', async function() {
await ns_tmp.upload_object({
bucket: upload_bkt,
key: upload_key_empty,
source_stream: buffer_utils.buffer_to_read_stream(crypto.randomBytes(0)),
size: 0
}, dummy_object_sdk);

const read_res = buffer_utils.write_stream();
await ns_tmp.read_object_stream({
bucket: upload_bkt,
key: upload_key_empty,
}, dummy_object_sdk, read_res);
assert(read_res.writableEnded);
});

mocha.it(`delete the path - stop when not empty and key with trailing /`, async function() {
const upload_res = await ns_tmp.upload_object({
bucket: upload_bkt,
Expand All @@ -574,11 +591,17 @@ mocha.describe('namespace_fs', function() {
});

mocha.after(async function() {
const delete_res = await ns_tmp.delete_object({
let delete_res = await ns_tmp.delete_object({
bucket: upload_bkt,
key: upload_key_2,
}, dummy_object_sdk);
console.log('delete_object with trailing / (key 2) response', inspect(delete_res));

delete_res = await ns_tmp.delete_object({
bucket: upload_bkt,
key: upload_key_empty,
}, dummy_object_sdk);
console.log('delete_object with trailing / (empty content dir) response', inspect(delete_res));
});
});

Expand Down

0 comments on commit 7681865

Please sign in to comment.