@@ -139,13 +139,47 @@ MlNodeDevice.prototype.open = function (name, f, raise_unix) {
139
139
}
140
140
} ;
141
141
142
- MlNodeDevice . prototype . rename = function ( o , n , raise_unix ) {
143
- try {
144
- this . fs . renameSync ( this . nm ( o ) , this . nm ( n ) ) ;
145
- } catch ( err ) {
146
- this . raise_nodejs_error ( err , raise_unix ) ;
147
- }
148
- } ;
142
+ if ( globalThis . process ?. platform === "win32" ) {
143
+ MlNodeDevice . prototype . rename = function ( o , n , raise_unix ) {
144
+ try {
145
+ var target = this . nm ( n ) ;
146
+ var source = this . nm ( o ) ;
147
+ var target_stats = this . fs . existsSync ( target )
148
+ ? this . fs . statSync ( target )
149
+ : null ;
150
+ var source_stats = this . fs . existsSync ( source )
151
+ ? this . fs . statSync ( source )
152
+ : null ;
153
+ if (
154
+ source_stats &&
155
+ ! source_stats . isDirectory ( ) &&
156
+ target_stats &&
157
+ target_stats . isDirectory ( )
158
+ ) {
159
+ var err = new Error ( "rename" ) ;
160
+ err . code = 26 ;
161
+ err . path = o ;
162
+ this . raise_nodejs_error ( err , raise_unix ) ;
163
+ }
164
+ if ( target_stats && target_stats . isDirectory ( ) )
165
+ try {
166
+ this . fs . rmdirSync ( target ) ;
167
+ } catch { }
168
+ this . fs . renameSync ( this . nm ( o ) , this . nm ( n ) ) ;
169
+ } catch ( err ) {
170
+ this . raise_nodejs_error ( err , raise_unix ) ;
171
+ }
172
+ } ;
173
+ } else {
174
+ MlNodeDevice . prototype . rename = function ( o , n , raise_unix ) {
175
+ try {
176
+ this . fs . renameSync ( this . nm ( o ) , this . nm ( n ) ) ;
177
+ } catch ( err ) {
178
+ this . raise_nodejs_error ( err , raise_unix ) ;
179
+ }
180
+ } ;
181
+ }
182
+
149
183
MlNodeDevice . prototype . stat = function ( name , raise_unix ) {
150
184
try {
151
185
var js_stats = this . fs . statSync ( this . nm ( name ) ) ;
0 commit comments