@@ -1614,105 +1614,6 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
16141614 }
16151615}
16161616
1617- static void RmSync (const FunctionCallbackInfo<Value>& args) {
1618- Environment* env = Environment::GetCurrent (args);
1619- Isolate* isolate = env->isolate ();
1620-
1621- CHECK_EQ (args.Length (), 4 ); // path, maxRetries, recursive, retryDelay
1622-
1623- BufferValue path (isolate, args[0 ]);
1624- CHECK_NOT_NULL (*path);
1625- ToNamespacedPath (env, &path);
1626- THROW_IF_INSUFFICIENT_PERMISSIONS (
1627- env, permission::PermissionScope::kFileSystemWrite , path.ToStringView ());
1628- auto file_path = std::filesystem::path (path.ToStringView ());
1629- std::error_code error;
1630- auto file_status = std::filesystem::status (file_path, error);
1631-
1632- if (file_status.type () == std::filesystem::file_type::not_found) {
1633- return ;
1634- }
1635-
1636- int maxRetries = args[1 ].As <Int32>()->Value ();
1637- int recursive = args[2 ]->IsTrue ();
1638- int retryDelay = args[3 ].As <Int32>()->Value ();
1639-
1640- // File is a directory and recursive is false
1641- if (file_status.type () == std::filesystem::file_type::directory &&
1642- !recursive) {
1643- return THROW_ERR_FS_EISDIR (
1644- isolate, " Path is a directory: %s" , file_path.c_str ());
1645- }
1646-
1647- // Allowed errors are:
1648- // - EBUSY: std::errc::device_or_resource_busy
1649- // - EMFILE: std::errc::too_many_files_open
1650- // - ENFILE: std::errc::too_many_files_open_in_system
1651- // - ENOTEMPTY: std::errc::directory_not_empty
1652- // - EPERM: std::errc::operation_not_permitted
1653- auto can_omit_error = [](std::error_code error) -> bool {
1654- return (error == std::errc::device_or_resource_busy ||
1655- error == std::errc::too_many_files_open ||
1656- error == std::errc::too_many_files_open_in_system ||
1657- error == std::errc::directory_not_empty ||
1658- error == std::errc::operation_not_permitted);
1659- };
1660-
1661- int i = 1 ;
1662-
1663- while (maxRetries >= 0 ) {
1664- if (recursive) {
1665- std::filesystem::remove_all (file_path, error);
1666- } else {
1667- std::filesystem::remove (file_path, error);
1668- }
1669-
1670- if (!error || error == std::errc::no_such_file_or_directory) {
1671- return ;
1672- } else if (!can_omit_error (error)) {
1673- break ;
1674- }
1675-
1676- if (retryDelay > 0 ) {
1677- #ifdef _WIN32
1678- Sleep (i * retryDelay / 1000 );
1679- #else
1680- sleep (i * retryDelay / 1000 );
1681- #endif
1682- }
1683- maxRetries--;
1684- i++;
1685- }
1686-
1687- // On Windows path::c_str() returns wide char, convert to std::string first.
1688- std::string file_path_str = file_path.string ();
1689- const char * path_c_str = file_path_str.c_str ();
1690- #ifdef _WIN32
1691- int permission_denied_error = EPERM;
1692- #else
1693- int permission_denied_error = EACCES;
1694- #endif // !_WIN32
1695-
1696- if (error == std::errc::operation_not_permitted) {
1697- std::string message = " Operation not permitted: " + file_path_str;
1698- return env->ThrowErrnoException (EPERM, " rm" , message.c_str (), path_c_str);
1699- } else if (error == std::errc::directory_not_empty) {
1700- std::string message = " Directory not empty: " + file_path_str;
1701- return env->ThrowErrnoException (EACCES, " rm" , message.c_str (), path_c_str);
1702- } else if (error == std::errc::not_a_directory) {
1703- std::string message = " Not a directory: " + file_path_str;
1704- return env->ThrowErrnoException (ENOTDIR, " rm" , message.c_str (), path_c_str);
1705- } else if (error == std::errc::permission_denied) {
1706- std::string message = " Permission denied: " + file_path_str;
1707- return env->ThrowErrnoException (
1708- permission_denied_error, " rm" , message.c_str (), path_c_str);
1709- }
1710-
1711- std::string message = " Unknown error: " + error.message ();
1712- return env->ThrowErrnoException (
1713- UV_UNKNOWN, " rm" , message.c_str (), path_c_str);
1714- }
1715-
17161617int MKDirpSync (uv_loop_t * loop,
17171618 uv_fs_t * req,
17181619 const std::string& path,
@@ -3181,6 +3082,106 @@ std::string ConvertWideToUTF8(const std::wstring& wstr) {
31813082
31823083#endif // _WIN32
31833084
3085+ static void RmSync (const FunctionCallbackInfo<Value>& args) {
3086+ Environment* env = Environment::GetCurrent (args);
3087+ Isolate* isolate = env->isolate ();
3088+
3089+ CHECK_EQ (args.Length (), 4 ); // path, maxRetries, recursive, retryDelay
3090+
3091+ BufferValue path (isolate, args[0 ]);
3092+ CHECK_NOT_NULL (*path);
3093+ ToNamespacedPath (env, &path);
3094+ THROW_IF_INSUFFICIENT_PERMISSIONS (
3095+ env, permission::PermissionScope::kFileSystemWrite , path.ToStringView ());
3096+ auto file_path = BufferValueToPath (path);
3097+ std::error_code error;
3098+ auto file_status = std::filesystem::status (file_path, error);
3099+
3100+ if (file_status.type () == std::filesystem::file_type::not_found) {
3101+ return ;
3102+ }
3103+
3104+ int maxRetries = args[1 ].As <Int32>()->Value ();
3105+ int recursive = args[2 ]->IsTrue ();
3106+ int retryDelay = args[3 ].As <Int32>()->Value ();
3107+
3108+ // File is a directory and recursive is false
3109+ if (file_status.type () == std::filesystem::file_type::directory &&
3110+ !recursive) {
3111+ auto file_path_as_str = PathToString (file_path);
3112+ return THROW_ERR_FS_EISDIR (
3113+ isolate, " Path is a directory: %s" , file_path_as_str);
3114+ }
3115+
3116+ // Allowed errors are:
3117+ // - EBUSY: std::errc::device_or_resource_busy
3118+ // - EMFILE: std::errc::too_many_files_open
3119+ // - ENFILE: std::errc::too_many_files_open_in_system
3120+ // - ENOTEMPTY: std::errc::directory_not_empty
3121+ // - EPERM: std::errc::operation_not_permitted
3122+ auto can_omit_error = [](std::error_code error) -> bool {
3123+ return (error == std::errc::device_or_resource_busy ||
3124+ error == std::errc::too_many_files_open ||
3125+ error == std::errc::too_many_files_open_in_system ||
3126+ error == std::errc::directory_not_empty ||
3127+ error == std::errc::operation_not_permitted);
3128+ };
3129+
3130+ int i = 1 ;
3131+
3132+ while (maxRetries >= 0 ) {
3133+ if (recursive) {
3134+ std::filesystem::remove_all (file_path, error);
3135+ } else {
3136+ std::filesystem::remove (file_path, error);
3137+ }
3138+
3139+ if (!error || error == std::errc::no_such_file_or_directory) {
3140+ return ;
3141+ } else if (!can_omit_error (error)) {
3142+ break ;
3143+ }
3144+
3145+ if (retryDelay > 0 ) {
3146+ #ifdef _WIN32
3147+ Sleep (i * retryDelay / 1000 );
3148+ #else
3149+ sleep (i * retryDelay / 1000 );
3150+ #endif
3151+ }
3152+ maxRetries--;
3153+ i++;
3154+ }
3155+
3156+ // On Windows path::c_str() returns wide char, convert to std::string first.
3157+ std::string file_path_str = PathToString (file_path);
3158+ const char * path_c_str = file_path_str.c_str ();
3159+ #ifdef _WIN32
3160+ int permission_denied_error = EPERM;
3161+ #else
3162+ int permission_denied_error = EACCES;
3163+ #endif // !_WIN32
3164+
3165+ if (error == std::errc::operation_not_permitted) {
3166+ std::string message = " Operation not permitted: " ;
3167+ return env->ThrowErrnoException (EPERM, " rm" , message.c_str (), path_c_str);
3168+ } else if (error == std::errc::directory_not_empty) {
3169+ std::string message = " Directory not empty: " ;
3170+ return env->ThrowErrnoException (EACCES, " rm" , message.c_str (), path_c_str);
3171+ } else if (error == std::errc::not_a_directory) {
3172+ std::string message = " Not a directory: " ;
3173+ return env->ThrowErrnoException (ENOTDIR, " rm" , message.c_str (), path_c_str);
3174+ } else if (error == std::errc::permission_denied) {
3175+ std::string message = " Permission denied: " ;
3176+ return env->ThrowErrnoException (
3177+ permission_denied_error, " rm" , message.c_str (), path_c_str);
3178+ }
3179+
3180+ std::string message = " Unknown error: " + error.message ();
3181+ return env->ThrowErrnoException (
3182+ UV_UNKNOWN, " rm" , message.c_str (), path_c_str);
3183+ }
3184+
31843185static void CpSyncCheckPaths (const FunctionCallbackInfo<Value>& args) {
31853186 Environment* env = Environment::GetCurrent (args);
31863187 Isolate* isolate = env->isolate ();
0 commit comments