Skip to content

Commit ae7cba1

Browse files
committed
WIP
1 parent 37bcffe commit ae7cba1

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

runtime/js/fs_node.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,47 @@ MlNodeDevice.prototype.open = function (name, f, raise_unix) {
139139
}
140140
};
141141

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+
149183
MlNodeDevice.prototype.stat = function (name, raise_unix) {
150184
try {
151185
var js_stats = this.fs.statSync(this.nm(name));

0 commit comments

Comments
 (0)