File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66#include " node_file.h"
77#include " req_wrap-inl.h"
88
9+ #include < algorithm>
10+
911namespace node {
1012namespace fs {
1113
@@ -27,6 +29,15 @@ void FSContinuationData::MaybeSetFirstPath(const std::string& path) {
2729 }
2830}
2931
32+ bool FSContinuationData::ShouldRetryENOENT (const std::string& path) {
33+ if (std::find (enoent_paths_.begin (), enoent_paths_.end (), path) !=
34+ enoent_paths_.end ()) {
35+ return false ;
36+ }
37+ enoent_paths_.push_back (path);
38+ return true ;
39+ }
40+
3041std::string FSContinuationData::PopPath () {
3142 CHECK (!paths_.empty ());
3243 std::string path = std::move (paths_.back ());
Original file line number Diff line number Diff line change @@ -219,6 +219,7 @@ typedef void (*uv_fs_callback_t)(uv_fs_t*);
219219
220220void FSContinuationData::MemoryInfo (MemoryTracker* tracker) const {
221221 tracker->TrackField (" paths" , paths_);
222+ tracker->TrackField (" enoent_paths" , enoent_paths_);
222223}
223224
224225FileHandleReadWrap::~FileHandleReadWrap () = default ;
@@ -1966,6 +1967,9 @@ int MKDirpSync(uv_loop_t* loop,
19661967 std::string dirname =
19671968 next_path.substr (0 , next_path.find_last_of (kPathSeparator ));
19681969 if (dirname != next_path) {
1970+ if (!req_wrap->continuation_data ()->ShouldRetryENOENT (next_path)) {
1971+ return err;
1972+ }
19691973 req_wrap->continuation_data ()->PushPath (std::move (next_path));
19701974 req_wrap->continuation_data ()->PushPath (std::move (dirname));
19711975 } else if (req_wrap->continuation_data ()->paths ().empty ()) {
@@ -2047,6 +2051,10 @@ int MKDirpAsync(
20472051 std::string dirname =
20482052 path.substr (0 , path.find_last_of (kPathSeparator ));
20492053 if (dirname != path) {
2054+ if (!req_wrap->continuation_data ()->ShouldRetryENOENT (path)) {
2055+ req_wrap->continuation_data ()->Done (err);
2056+ break ;
2057+ }
20502058 req_wrap->continuation_data ()->PushPath (path);
20512059 req_wrap->continuation_data ()->PushPath (std::move (dirname));
20522060 } else if (req_wrap->continuation_data ()->paths ().empty ()) {
Original file line number Diff line number Diff line change @@ -114,6 +114,9 @@ class FSContinuationData : public MemoryRetainer {
114114 inline std::string PopPath ();
115115 // Used by mkdirp to track the first path created:
116116 inline void MaybeSetFirstPath (const std::string& path);
117+ // Used by mkdirp to retry a path that failed with ENOENT only once: its
118+ // parent has been created or checked by then, so retrying again cannot help.
119+ inline bool ShouldRetryENOENT (const std::string& path);
117120 inline void Done (int result);
118121
119122 int mode () const { return mode_; }
@@ -129,6 +132,7 @@ class FSContinuationData : public MemoryRetainer {
129132 uv_fs_t * req_;
130133 int mode_;
131134 std::vector<std::string> paths_;
135+ std::vector<std::string> enoent_paths_;
132136 std::string first_path_;
133137};
134138
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ // Refs: https://github.com/nodejs/node/issues/66268
3+ // On procfs, mkdir fails with ENOENT although the parent exists. A recursive
4+ // mkdir must report the error instead of retrying forever.
5+ const common = require ( '../common' ) ;
6+ const fs = require ( 'fs' ) ;
7+
8+ if ( ! common . isLinux ) common . skip ( 'procfs is Linux only' ) ;
9+
10+ const assert = require ( 'assert' ) ;
11+ const dir = `/proc/node-test-${ process . pid } ` ;
12+ const expected = { code : 'ENOENT' , syscall : 'mkdir' } ;
13+
14+ assert . throws ( ( ) => fs . mkdirSync ( dir , { recursive : true } ) , expected ) ;
15+
16+ fs . mkdir ( dir , { recursive : true } , common . mustCall ( ( err ) => {
17+ assert . strictEqual ( err . code , expected . code ) ;
18+ assert . strictEqual ( err . syscall , expected . syscall ) ;
19+ } ) ) ;
20+
21+ assert . rejects ( fs . promises . mkdir ( dir , { recursive : true } ) , expected )
22+ . then ( common . mustCall ( ) ) ;
You can’t perform that action at this time.
0 commit comments