File: wasmfs_opfs_errors.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (154 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <assert.h>
#include <emscripten/console.h>
#include <emscripten/emscripten.h>
#include <emscripten/wasmfs.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
  wasmfs_create_directory("/opfs", 0777, wasmfs_create_opfs_backend());
  EM_ASM({ run_test(); });
  emscripten_exit_with_live_runtime();
}

const char* file = "/opfs/data";

// Each of these functions returns:
//   0: failure with EACCES (or other expected error code)
//   1: success
//   2: other error

static int try_open(int flags) {
  int fd = open(file, flags);
  if (fd >= 0) {
    int err = close(fd);
    assert(err == 0);
    return 1;
  }
  if (errno == EACCES) {
    return 0;
  }
  emscripten_console_error(strerror(errno));
  return 2;
}


EMSCRIPTEN_KEEPALIVE
int try_open_wronly(void) {
  return try_open(O_WRONLY);
}

EMSCRIPTEN_KEEPALIVE
int try_open_rdwr(void) {
  return try_open(O_RDWR);
}

EMSCRIPTEN_KEEPALIVE
int try_open_rdonly(void) {
  return try_open(O_RDONLY);
}

EMSCRIPTEN_KEEPALIVE
int try_truncate(void) {
  int err = truncate(file, 42);
  if (err == 0) {
    return 1;
  }
  if (errno == EIO) {
    return 0;
  }
  emscripten_console_error(strerror(errno));
  return 2;
}

EMSCRIPTEN_KEEPALIVE
int try_unlink(void) {
  int err = unlink(file);
  if (err == 0) {
    return 1;
  }
  if (errno == EIO) {
    return 0;
  }
  emscripten_console_error(strerror(errno));
  return 2;
}

EMSCRIPTEN_KEEPALIVE
int try_oob_read(void) {
  int fd = open(file, O_RDWR);
  if (fd < 0) {
    emscripten_outf("fd %d", fd);
    return 2;
  }
  char buf;
  int nread = pread(fd, &buf, 1, (off_t)-1ll);
  if (nread > 0) {
    close(fd);
    return 1;
  }
  if (errno == EINVAL) {
    close(fd);
    return 0;
  }
  emscripten_outf("errno %d", errno);
  close(fd);
  return 2;
}

EMSCRIPTEN_KEEPALIVE
int try_oob_write(void) {
  int fd = open(file, O_RDWR);
  if (fd < 0) {
    emscripten_console_error(strerror(errno));
    return 2;
  }
  char buf = 0;
  int nread = pwrite(fd, &buf, 1, (off_t)-1ll);
  if (nread > 0) {
    close(fd);
    return 1;
  }
  if (errno == EINVAL) {
    close(fd);
    return 0;
  }
  emscripten_console_error(strerror(errno));
  close(fd);
  return 2;
}

EMSCRIPTEN_KEEPALIVE
int try_rename_dir(void) {
  int err = mkdir("/opfs/dir1", 0666);
  if (err != 0) {
    return 2;
  }
  err = rename("/opfs/dir1", "/opfs/dir2");
  if (err == 0) {
    return 1;
  }
  if (errno == EBUSY) {
    rmdir("/opfs/dir1");
    return 0;
  }
  emscripten_console_error(strerror(errno));
  rmdir("/opfs/dir1");
  return 2;
}

EMSCRIPTEN_KEEPALIVE
void report_result(int result) {
  EM_ASM({ out(new Error().stack); });
#ifdef REPORT_RESULT
  REPORT_RESULT(result);
#else
  if (result != 0) {
    abort();
  }
#endif
}