Skip to content

Commit de71bbd

Browse files
authored
Report the host's reason when a new file cannot be created (#35)
1 parent 7786014 commit de71bbd

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

‎src/natives/io.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ export function openFile(t: JThread, path: string, write: boolean, truncate: boo
169169
let data: Uint8Array;
170170
if (!st) {
171171
if (!write) notFound(path);
172-
if (!fs.write(abs, new Uint8Array(0))) notFound(path, fs.writable(abs) ? "No such file or directory" : "Read-only file system");
172+
if (!fs.write(abs, new Uint8Array(0))) {
173+
notFound(path, ERRNO[fs.writeError(abs) ?? ""] ?? (fs.writable(abs) ? "No such file or directory" : "Read-only file system"));
174+
}
173175
data = new Uint8Array(0);
174176
} else {
175177
if (write && !fs.writable(abs)) notFound(path, "Read-only file system");
@@ -224,7 +226,8 @@ export function flush(t: JThread, f: OpenFile): boolean {
224226
const ERRNO: Record<string, string> = {
225227
ENOSPC: "No space left on device", EFBIG: "File too large", EDQUOT: "Disk quota exceeded",
226228
EACCES: "Permission denied", EPERM: "Operation not permitted", EROFS: "Read-only file system",
227-
ENOENT: "No such file or directory",
229+
ENOENT: "No such file or directory", ENAMETOOLONG: "File name too long", ENOTDIR: "Not a directory",
230+
EIO: "Input/output error", ELOOP: "Too many levels of symbolic links",
228231
};
229232

230233
// Where Java learns of a write that failed: HotSpot reports it from write(),

‎src/natives/nio.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ import type { Natives } from "./util.ts";
1212

1313
export const EPERM = 1, ENOENT = 2, EINTR = 4, EIO = 5, EBADF = 9, ENOMEM = 12, EACCES = 13,
1414
EEXIST = 17, EXDEV = 18, ENOTDIR = 20, EISDIR = 21, EINVAL = 22, ENOSPC = 28, EROFS = 30,
15-
ENOTEMPTY = 39, ENOSYS = 38, ELOOP = 40, ENOTSUP = 95, ESTALE = 116;
15+
ENOTEMPTY = 39, ENOSYS = 38, ENAMETOOLONG = 36, ELOOP = 40, ENOTSUP = 95, ESTALE = 116, EDQUOT = 122;
1616

1717
const MESSAGES: Record<number, string> = {
1818
[EPERM]: "Operation not permitted", [EINTR]: "Interrupted system call", [EIO]: "Input/output error",
1919
[ENOMEM]: "Cannot allocate memory", [ENOSPC]: "No space left on device",
2020
[ELOOP]: "Too many levels of symbolic links", [ENOTSUP]: "Operation not supported", [ESTALE]: "Stale file handle",
21+
[ENAMETOOLONG]: "File name too long", [EDQUOT]: "Disk quota exceeded",
2122
[ENOENT]: "No such file or directory", [EBADF]: "Bad file descriptor", [EACCES]: "Permission denied",
2223
[EEXIST]: "File exists", [EXDEV]: "Invalid cross-device link", [ENOTDIR]: "Not a directory",
2324
[EISDIR]: "Is a directory", [EINVAL]: "Invalid argument", [EROFS]: "Read-only file system",
2425
[ENOTEMPTY]: "Directory not empty", [ENOSYS]: "Function not implemented",
2526
};
2627

27-
const CHMOD_ERRNO: Record<string, number> = {
28+
// errno numbers of the host errors Node reports by name
29+
const HOST_ERRNO: Record<string, number> = {
2830
EPERM, ENOENT, EINTR, EIO, EBADF, ENOMEM, EACCES, ENOTDIR, EINVAL, ENOSPC, EROFS, ELOOP,
29-
ENOTSUP, EOPNOTSUPP: ENOTSUP, ESTALE,
31+
ENOTSUP, EOPNOTSUPP: ENOTSUP, ESTALE, ENAMETOOLONG, EDQUOT,
3032
};
3133

3234
// Linux open(2) flags
@@ -66,7 +68,7 @@ function chmod(t: JThread, p: string, mode: number) {
6668
if (!fs.stat(p)) unixError(ENOENT);
6769
unixError(EROFS);
6870
}
69-
unixError(CHMOD_ERRNO[fs.chmodError(p) ?? "EIO"] ?? EIO);
71+
unixError(HOST_ERRNO[fs.chmodError(p) ?? "EIO"] ?? EIO);
7072
}
7173

7274
function fillAttrs(t: JThread, attrs: JObject, p: string): number {
@@ -148,7 +150,11 @@ export const natives: Natives = {
148150
fileOf(t, fd).ino = fileInode(t, abs);
149151
return fd;
150152
} catch (e) {
151-
if (e instanceof PendingThrow) unixError(t.vm.fs.writable(abs) ? ENOENT : EROFS);
153+
if (e instanceof PendingThrow) {
154+
// a new file the host would not create: its reason
155+
const host = st ? undefined : HOST_ERRNO[t.vm.fs.writeError(abs) ?? ""];
156+
unixError(host ?? (t.vm.fs.writable(abs) ? ENOENT : EROFS));
157+
}
152158
throw e;
153159
}
154160
},

‎src/node.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ export class NodeBackend implements Backend {
8080
if (!this.writable) this.lists.set(p, names);
8181
return names;
8282
}
83-
private writeErrors = new Map<string, string>();
83+
// the host's reason for the last failed write, read right after it
84+
private writeFailure: [string, string] | null = null;
8485
write(p: string, data: Uint8Array) {
8586
try {
8687
fs.writeFileSync(this.host(p), data);
87-
this.writeErrors.delete(p);
88+
this.writeFailure = null;
8889
return true;
8990
} catch (e) {
90-
this.writeErrors.set(p, (e as NodeJS.ErrnoException).code ?? "EIO");
91+
this.writeFailure = [p, (e as NodeJS.ErrnoException).code ?? "EIO"];
9192
return false;
9293
}
9394
}
9495
writeError(p: string) {
95-
return this.writeErrors.get(p);
96+
return this.writeFailure?.[0] === p ? this.writeFailure[1] : undefined;
9697
}
9798
denied(p: string) {
9899
const host = this.host(p);
@@ -103,19 +104,19 @@ export class NodeBackend implements Backend {
103104
return (e as NodeJS.ErrnoException).code === "EACCES" || (e as NodeJS.ErrnoException).code === "EPERM";
104105
}
105106
}
106-
private chmodErrors = new Map<string, string>();
107+
private chmodFailure: [string, string] | null = null;
107108
chmod(p: string, f: (mode: number) => number) {
108109
try {
109110
fs.chmodSync(this.host(p), f(fs.statSync(this.host(p)).mode & 0o7777));
110-
this.chmodErrors.delete(p);
111+
this.chmodFailure = null;
111112
return true;
112113
} catch (e) {
113-
this.chmodErrors.set(p, (e as NodeJS.ErrnoException).code ?? "EIO");
114+
this.chmodFailure = [p, (e as NodeJS.ErrnoException).code ?? "EIO"];
114115
return false;
115116
}
116117
}
117118
chmodError(p: string) {
118-
return this.chmodErrors.get(p);
119+
return this.chmodFailure?.[0] === p ? this.chmodFailure[1] : undefined;
119120
}
120121
access(p: string, mode: number) {
121122
try {

‎test/java/CreateRefused.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.io.*;
2+
import java.nio.file.*;
3+
4+
// Creating a file the host refuses reports the host's reason, as HotSpot does.
5+
public class CreateRefused {
6+
public static void main(String[] args) throws Exception {
7+
String name = Path.of(System.getProperty("java.io.tmpdir"), "x".repeat(300)).toString();
8+
try (var out = new FileOutputStream(name)) {
9+
System.out.println("created");
10+
} catch (IOException e) {
11+
System.out.println(e.getClass().getName() + ": " + e.getMessage().replace(name, "<tmp>/<long>"));
12+
}
13+
try {
14+
Files.createFile(Path.of(name));
15+
System.out.println("created");
16+
} catch (IOException e) {
17+
System.out.println(e.getClass().getName() + ": " + e.getMessage().replace(name, "<tmp>/<long>"));
18+
}
19+
try {
20+
Files.newOutputStream(Path.of(name)).close();
21+
System.out.println("created");
22+
} catch (IOException e) {
23+
System.out.println(e.getClass().getName() + ": " + e.getMessage().replace(name, "<tmp>/<long>"));
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)