forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
fs: refactor unlinksync logic in rimraf #1
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
Open
JonasBa
wants to merge
7
commits into
main
Choose a base branch
from
jb/rimraf/_unlinksync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b98d2f0
fs: refactor unlinksync logic in rimraf
JonasBa 797fb93
fs: add rmsync recursive benchmark
JonasBa 7aadd36
fs: break cpp loop in rimrafSync
JonasBa eca4a3a
fs: return on enoent
JonasBa c70dd8d
fs: rename benchmark of rimrafUnlinkSync
JonasBa c284a90
fs: split rimraf logic to separate file
JonasBa d527844
fs: partially port FixWinEpermSync fn to cpp
JonasBa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"binary","filename","configuration","rate","time" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['existing', 'non-existing'], | ||
n: [1e3], | ||
}); | ||
|
||
function main({ n, type }) { | ||
switch (type) { | ||
case 'existing': { | ||
for (let i = 0; i < n; i++) { | ||
fs.writeFileSync(tmpdir.resolve(`rimrafunlinksync-bench-dir-${i}`), ''); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.rmSync(tmpdir.resolve(`rimrafunlinksync-bench-dir-${i}`), { | ||
recursive: true, // required to enter rimraf path | ||
maxRetries: 3, | ||
}); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
bench.end(n); | ||
break; | ||
} | ||
case 'non-existing': { | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.rmSync(tmpdir.resolve(`.non-existent-folder-${i}`), { | ||
recursive: true, // required to enter rimraf path | ||
maxRetries: 3, | ||
}); | ||
} catch (err) { | ||
assert.ok(err); | ||
} | ||
} | ||
bench.end(n); | ||
break; | ||
} | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['existing', 'non-existing'], | ||
n: [1e3], | ||
}); | ||
|
||
function main({ n, type }) { | ||
switch (type) { | ||
case 'existing': { | ||
for (let i = 0; i < n; i++) { | ||
fs.mkdirSync(tmpdir.resolve(`rmsync-bench-dir-${i}`), { | ||
recursive: true, | ||
}); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
fs.rmSync(tmpdir.resolve(`rmsync-bench-dir-${i}`), { | ||
recursive: true, | ||
maxRetries: 3, | ||
}); | ||
} | ||
bench.end(n); | ||
break; | ||
} | ||
case 'non-existing': { | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.rmSync(tmpdir.resolve(`.non-existent-folder-${i}`), { | ||
recursive: true, | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
maxRetries: 3, | ||
}); | ||
} catch (err) { | ||
assert.match(err.message, /ENOENT/); | ||
} | ||
} | ||
bench.end(n); | ||
break; | ||
} | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include "node_file.h" // NOLINT(build/include_inline) | ||
#include "node_file-inl.h" | ||
|
||
#include <optional> | ||
|
||
#if defined(_WIN32) | ||
#include<windows.h> // for windows | ||
#else | ||
#include<unistd.h> // for linux | ||
#endif | ||
|
||
namespace node { | ||
|
||
namespace fs { | ||
|
||
using v8::FunctionCallbackInfo; | ||
using v8::Int32; | ||
using v8::Isolate; | ||
using v8::Value; | ||
|
||
#define TRACE_NAME(name) "fs.sync." #name | ||
#define GET_TRACE_ENABLED \ | ||
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \ | ||
TRACING_CATEGORY_NODE2(fs, sync)) != 0) | ||
#define FS_SYNC_TRACE_BEGIN(syscall, ...) \ | ||
if (GET_TRACE_ENABLED) \ | ||
TRACE_EVENT_BEGIN( \ | ||
TRACING_CATEGORY_NODE2(fs, sync), TRACE_NAME(syscall), ##__VA_ARGS__); | ||
#define FS_SYNC_TRACE_END(syscall, ...) \ | ||
if (GET_TRACE_ENABLED) \ | ||
TRACE_EVENT_END( \ | ||
TRACING_CATEGORY_NODE2(fs, sync), TRACE_NAME(syscall), ##__VA_ARGS__); | ||
|
||
static void UnlinkSync(char* path, uint32_t max_retries, uint32_t retry_delay) { | ||
Isolate* isolate = Isolate::GetCurrent(); | ||
Environment* env = Environment::GetCurrent(isolate); | ||
|
||
THROW_IF_INSUFFICIENT_PERMISSIONS( | ||
env, permission::PermissionScope::kFileSystemWrite, path); | ||
|
||
const auto tries = max_retries + 1; | ||
constexpr std::array<int, 5> retryable_errors = { | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
EBUSY, EMFILE, ENFILE, ENOTEMPTY, EPERM}; | ||
|
||
uv_fs_t req; | ||
|
||
for (uint64_t i = 1; i <= tries; i++) { | ||
FS_SYNC_TRACE_BEGIN(unlink); | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto err = uv_fs_unlink(nullptr, &req, path, nullptr); | ||
FS_SYNC_TRACE_END(unlink); | ||
|
||
if(!is_uv_error(err)) { | ||
return; | ||
} | ||
|
||
if (i < tries && retry_delay > 0 && | ||
std::find(retryable_errors.begin(), retryable_errors.end(), err) != retryable_errors.end()) { | ||
sleep(i * retry_delay * 1e-3); | ||
} else if (err == UV_ENOENT) { | ||
return; | ||
} else if (i == tries) { | ||
env->ThrowUVException(err, nullptr, "unlink"); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
constexpr bool is_uv_error_enoent(int result) { | ||
return result == UV_ENOENT; | ||
} | ||
|
||
static void FixWINEPERMSync(char* path, uint32_t max_retries, uint32_t retry_delay) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning void makes it harder to keep track if this function errored or not. I recommend a boolean or std::optional if you need to pass a value to caller |
||
int chmod_result; | ||
uv_fs_t chmod_req; | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FS_SYNC_TRACE_BEGIN(chmod); | ||
chmod_result = uv_fs_chmod(nullptr, &chmod_req, path, 0666, nullptr); | ||
FS_SYNC_TRACE_END(chmod); | ||
|
||
if(is_uv_error(chmod_result)) { | ||
if (chmod_result == UV_ENOENT) { | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} else { | ||
// @TODO throw original error | ||
return; | ||
} | ||
} | ||
|
||
int stat_result; | ||
uv_fs_t stat_req; | ||
FS_SYNC_TRACE_BEGIN(stat); | ||
stat_result = uv_fs_stat(nullptr, &stat_req, path, nullptr); | ||
FS_SYNC_TRACE_END(stat); | ||
|
||
if(is_uv_error(stat_result)) { | ||
if(stat_result != UV_ENOENT){ | ||
// @TODO throw original error | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return; | ||
} | ||
|
||
if(S_ISDIR(stat_req.statbuf.st_mode)) { | ||
// @TODO call rmdirSync | ||
} else { | ||
return UnlinkSync(path, max_retries, retry_delay); | ||
} | ||
} | ||
|
||
|
||
} // end namespace fs | ||
|
||
} // end namespace node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.