Skip to content

Commit 82ac6ee

Browse files
Dan Gohmanjohn-sharratt
authored andcommitted
Use ENOENT rather than ENOTCAPABLE for missing preopens. (WebAssembly#370)
When a user calls `open` with a path that does not have a corresponding preopen, set errno to `ENOENT` rather than `ENOTCAPABLE`. This conceptually represents an attempt to open a path which has not been provided within the sandbox, so it's more accurately represented as "not present" rather than "insufficient capabilities".
1 parent fb31bcc commit 82ac6ee

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

libc-bottom-half/sources/posix.c

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ int __wasilibc_open_nomode(const char *path, int oflag) {
5858
char *relative_path;
5959
int dirfd = find_relpath(path, &relative_path);
6060

61-
// If we can't find a preopen for it, indicate that we lack capabilities.
61+
// If we can't find a preopen for it, fail as if we can't find the path.
6262
if (dirfd == -1) {
63-
errno = ENOTCAPABLE;
63+
errno = ENOENT;
6464
return -1;
6565
}
6666

@@ -71,9 +71,9 @@ int access(const char *path, int amode) {
7171
char *relative_path;
7272
int dirfd = find_relpath(path, &relative_path);
7373

74-
// If we can't find a preopen for it, indicate that we lack capabilities.
74+
// If we can't find a preopen for it, fail as if we can't find the path.
7575
if (dirfd == -1) {
76-
errno = ENOTCAPABLE;
76+
errno = ENOENT;
7777
return -1;
7878
}
7979

@@ -88,9 +88,9 @@ ssize_t readlink(
8888
char *relative_path;
8989
int dirfd = find_relpath(path, &relative_path);
9090

91-
// If we can't find a preopen for it, indicate that we lack capabilities.
91+
// If we can't find a preopen for it, fail as if we can't find the path.
9292
if (dirfd == -1) {
93-
errno = ENOTCAPABLE;
93+
errno = ENOENT;
9494
return -1;
9595
}
9696

@@ -101,9 +101,9 @@ int stat(const char *restrict path, struct stat *restrict buf) {
101101
char *relative_path;
102102
int dirfd = find_relpath(path, &relative_path);
103103

104-
// If we can't find a preopen for it, indicate that we lack capabilities.
104+
// If we can't find a preopen for it, fail as if we can't find the path.
105105
if (dirfd == -1) {
106-
errno = ENOTCAPABLE;
106+
errno = ENOENT;
107107
return -1;
108108
}
109109

@@ -114,9 +114,9 @@ int lstat(const char *restrict path, struct stat *restrict buf) {
114114
char *relative_path;
115115
int dirfd = find_relpath(path, &relative_path);
116116

117-
// If we can't find a preopen for it, indicate that we lack capabilities.
117+
// If we can't find a preopen for it, fail as if we can't find the path.
118118
if (dirfd == -1) {
119-
errno = ENOTCAPABLE;
119+
errno = ENOENT;
120120
return -1;
121121
}
122122

@@ -127,9 +127,9 @@ int utime(const char *path, const struct utimbuf *times) {
127127
char *relative_path;
128128
int dirfd = find_relpath(path, &relative_path);
129129

130-
// If we can't find a preopen for it, indicate that we lack capabilities.
130+
// If we can't find a preopen for it, fail as if we can't find the path.
131131
if (dirfd == -1) {
132-
errno = ENOTCAPABLE;
132+
errno = ENOENT;
133133
return -1;
134134
}
135135

@@ -147,9 +147,9 @@ int utimes(const char *path, const struct timeval times[2]) {
147147
char *relative_path;
148148
int dirfd = find_relpath(path, &relative_path);
149149

150-
// If we can't find a preopen for it, indicate that we lack capabilities.
150+
// If we can't find a preopen for it, fail as if we can't find the path.
151151
if (dirfd == -1) {
152-
errno = ENOTCAPABLE;
152+
errno = ENOENT;
153153
return -1;
154154
}
155155

@@ -169,9 +169,9 @@ int unlink(const char *path) {
169169
char *relative_path;
170170
int dirfd = find_relpath(path, &relative_path);
171171

172-
// If we can't find a preopen for it, indicate that we lack capabilities.
172+
// If we can't find a preopen for it, fail as if we can't find the path.
173173
if (dirfd == -1) {
174-
errno = ENOTCAPABLE;
174+
errno = ENOENT;
175175
return -1;
176176
}
177177

@@ -185,9 +185,9 @@ int rmdir(const char *path) {
185185
char *relative_path;
186186
int dirfd = find_relpath(path, &relative_path);
187187

188-
// If we can't find a preopen for it, indicate that we lack capabilities.
188+
// If we can't find a preopen for it, fail as if we can't find the path.
189189
if (dirfd == -1) {
190-
errno = ENOTCAPABLE;
190+
errno = ENOENT;
191191
return -1;
192192
}
193193

@@ -198,21 +198,21 @@ int remove(const char *path) {
198198
char *relative_path;
199199
int dirfd = find_relpath(path, &relative_path);
200200

201-
// If we can't find a preopen for it, indicate that we lack capabilities.
201+
// If we can't find a preopen for it, fail as if we can't find the path.
202202
if (dirfd == -1) {
203-
errno = ENOTCAPABLE;
203+
errno = ENOENT;
204204
return -1;
205205
}
206206

207207
// First try to remove it as a file.
208208
int r = __wasilibc_nocwd___wasilibc_unlinkat(dirfd, relative_path);
209-
if (r != 0 && (errno == EISDIR || errno == ENOTCAPABLE)) {
209+
if (r != 0 && (errno == EISDIR || errno == ENOENT)) {
210210
// That failed, but it might be a directory.
211211
r = __wasilibc_nocwd___wasilibc_rmdirat(dirfd, relative_path);
212212

213213
// If it isn't a directory, we lack capabilities to remove it as a file.
214214
if (errno == ENOTDIR)
215-
errno = ENOTCAPABLE;
215+
errno = ENOENT;
216216
}
217217
return r;
218218
}
@@ -221,9 +221,9 @@ int mkdir(const char *path, mode_t mode) {
221221
char *relative_path;
222222
int dirfd = find_relpath(path, &relative_path);
223223

224-
// If we can't find a preopen for it, indicate that we lack capabilities.
224+
// If we can't find a preopen for it, fail as if we can't find the path.
225225
if (dirfd == -1) {
226-
errno = ENOTCAPABLE;
226+
errno = ENOENT;
227227
return -1;
228228
}
229229

@@ -254,9 +254,9 @@ DIR *opendir(const char *dirname) {
254254
char *relative_path;
255255
int dirfd = find_relpath(dirname, &relative_path);
256256

257-
// If we can't find a preopen for it, indicate that we lack capabilities.
257+
// If we can't find a preopen for it, fail as if we can't find the path.
258258
if (dirfd == -1) {
259-
errno = ENOTCAPABLE;
259+
errno = ENOENT;
260260
return NULL;
261261
}
262262

@@ -272,9 +272,9 @@ int scandir(
272272
char *relative_path;
273273
int dirfd = find_relpath(dir, &relative_path);
274274

275-
// If we can't find a preopen for it, indicate that we lack capabilities.
275+
// If we can't find a preopen for it, fail as if we can't find the path.
276276
if (dirfd == -1) {
277-
errno = ENOTCAPABLE;
277+
errno = ENOENT;
278278
return -1;
279279
}
280280

@@ -285,9 +285,9 @@ int symlink(const char *target, const char *linkpath) {
285285
char *relative_path;
286286
int dirfd = find_relpath(linkpath, &relative_path);
287287

288-
// If we can't find a preopen for it, indicate that we lack capabilities.
288+
// If we can't find a preopen for it, fail as if we can't find the path.
289289
if (dirfd == -1) {
290-
errno = ENOTCAPABLE;
290+
errno = ENOENT;
291291
return -1;
292292
}
293293

@@ -307,8 +307,8 @@ int link(const char *old, const char *new) {
307307
new_dirfd, new_relative_path, 0);
308308
}
309309

310-
// We couldn't find a preopen for it; indicate that we lack capabilities.
311-
errno = ENOTCAPABLE;
310+
// We couldn't find a preopen for it; fail as if we can't find the path.
311+
errno = ENOENT;
312312
return -1;
313313
}
314314

@@ -325,8 +325,8 @@ int rename(const char *old, const char *new) {
325325
new_dirfd, new_relative_path);
326326
}
327327

328-
// We couldn't find a preopen for it; indicate that we lack capabilities.
329-
errno = ENOTCAPABLE;
328+
// We couldn't find a preopen for it; fail as if we can't find the path.
329+
errno = ENOENT;
330330
return -1;
331331
}
332332

@@ -337,9 +337,9 @@ __wasilibc_access(const char *path, int mode, int flags)
337337
char *relative_path;
338338
int dirfd = find_relpath(path, &relative_path);
339339

340-
// If we can't find a preopen for it, indicate that we lack capabilities.
340+
// If we can't find a preopen for it, fail as if we can't find the path.
341341
if (dirfd == -1) {
342-
errno = ENOTCAPABLE;
342+
errno = ENOENT;
343343
return -1;
344344
}
345345

@@ -354,9 +354,9 @@ __wasilibc_utimens(const char *path, const struct timespec times[2], int flags)
354354
char *relative_path;
355355
int dirfd = find_relpath(path, &relative_path);
356356

357-
// If we can't find a preopen for it, indicate that we lack capabilities.
357+
// If we can't find a preopen for it, fail as if we can't find the path.
358358
if (dirfd == -1) {
359-
errno = ENOTCAPABLE;
359+
errno = ENOENT;
360360
return -1;
361361
}
362362

@@ -371,9 +371,9 @@ __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, int fla
371371
char *relative_path;
372372
int dirfd = find_relpath(path, &relative_path);
373373

374-
// If we can't find a preopen for it, indicate that we lack capabilities.
374+
// If we can't find a preopen for it, fail as if we can't find the path.
375375
if (dirfd == -1) {
376-
errno = ENOTCAPABLE;
376+
errno = ENOENT;
377377
return -1;
378378
}
379379

@@ -389,9 +389,9 @@ __wasilibc_link(const char *oldpath, const char *newpath, int flags)
389389
int old_dirfd = find_relpath(oldpath, &old_relative_path);
390390
int new_dirfd = find_relpath(newpath, &new_relative_path);
391391

392-
// If we can't find a preopen for it, indicate that we lack capabilities.
392+
// If we can't find a preopen for it, fail as if we can't find the path.
393393
if (old_dirfd == -1 || new_dirfd == -1) {
394-
errno = ENOTCAPABLE;
394+
errno = ENOENT;
395395
return -1;
396396
}
397397

@@ -407,9 +407,9 @@ __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, in
407407
char *new_relative_path;
408408
int new_dirfd = find_relpath(newpath, &new_relative_path);
409409

410-
// If we can't find a preopen for it, indicate that we lack capabilities.
410+
// If we can't find a preopen for it, fail as if we can't find the path.
411411
if (new_dirfd == -1) {
412-
errno = ENOTCAPABLE;
412+
errno = ENOENT;
413413
return -1;
414414
}
415415

@@ -425,9 +425,9 @@ __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, in
425425
char *old_relative_path;
426426
int old_dirfd = find_relpath(oldpath, &old_relative_path);
427427

428-
// If we can't find a preopen for it, indicate that we lack capabilities.
428+
// If we can't find a preopen for it, fail as if we can't find the path.
429429
if (old_dirfd == -1) {
430-
errno = ENOTCAPABLE;
430+
errno = ENOENT;
431431
return -1;
432432
}
433433

@@ -443,9 +443,9 @@ __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to)
443443
char *to_relative_path;
444444
int to_dirfd = find_relpath(to, &to_relative_path);
445445

446-
// If we can't find a preopen for it, indicate that we lack capabilities.
446+
// If we can't find a preopen for it, fail as if we can't find the path.
447447
if (to_dirfd == -1) {
448-
errno = ENOTCAPABLE;
448+
errno = ENOENT;
449449
return -1;
450450
}
451451

@@ -459,9 +459,9 @@ __wasilibc_rename_newat(const char *from, int todirfd, const char *to)
459459
char *from_relative_path;
460460
int from_dirfd = find_relpath(from, &from_relative_path);
461461

462-
// If we can't find a preopen for it, indicate that we lack capabilities.
462+
// If we can't find a preopen for it, fail as if we can't find the path.
463463
if (from_dirfd == -1) {
464-
errno = ENOTCAPABLE;
464+
errno = ENOENT;
465465
return -1;
466466
}
467467

0 commit comments

Comments
 (0)