File: test_mount.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 (136 lines) | stat: -rw-r--r-- 3,713 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
/*
 * Copyright 2014 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <stdio.h>
#include <emscripten.h>

int main() {
  EM_ASM(
    var ex;
    var contents;

    // write a file that should be unaffected by this process
    FS.writeFile('/safe.txt', 'abc');

    // mount it the first time
    FS.mkdir('/working');
    FS.mount(MEMFS, {}, '/working');
    FS.writeFile('/working/waka.txt', 'az');

    // check the waka file
    contents = FS.readFile('/working/waka.txt', { encoding: 'utf8' });
    assert(contents === 'az');

#if !defined(WASMFS)
    // the legacy API requires a mount directory to exist, while WasmFS will create the directory.
    try {
      FS.mount(MEMFS, {}, '/missing');
    } catch (e) {
      ex = e;
    }
    assert(ex.name === 'ErrnoError' && ex.errno === 44); // ENOENT
    ex = null;
#endif

#if WASMFS
    // WasmFS will throw an error if a directory to mount to is not empty, while the legacy API will not.
    try {
      FS.mkdir("/test");
      FS.writeFile("/test/hi.txt", "abc");
      FS.mount(MEMFS, {}, '/test');
    } catch (e) {
      ex = e;
    }
    assert(ex.name === 'ErrnoError' && ex.errno === 55); // ENOTEMPTY
    ex = null;
#endif

    // mount to an existing mountpoint
    try {
      FS.mount(MEMFS, {}, '/working');
    } catch (e) {
      ex = e;
    }
#if WASMFS
    // WasmFS returns ENOTEMPTY instead of EBUSY in exchange for a simpler implementation.
    assert(ex.name === 'ErrnoError' && ex.errno === 55); // ENOTEMPTY
#else
    assert(ex.name === 'ErrnoError' && ex.errno === 10); // EBUSY
#endif
    ex = null;

    // attempt to unmount a non-mountpoint directory inside a mountpoint
    FS.mkdir('/working/unmountable');
    try {
      FS.unmount('/working/unmountable');
    } catch (e) {
      ex = e;
    }
    assert(ex.name === 'ErrnoError' && ex.errno === 28); // EINVAL
    ex = null;

    // unmount
    FS.unmount('/working');

    // unmount something that's not mounted
    try {
      FS.unmount('/working');
    } catch (e) {
      ex = e;
    }
#if WASMFS
    // WasmFS will remove a directory on unmount, regardless of if a directory existed before.
    assert(ex.name === 'ErrnoError' && ex.errno === 44); // ENOENT
#else
    assert(ex.name === 'ErrnoError' && ex.errno === 28); // EINVAL
#endif
    ex = null;

    // mount and unmount again
    FS.mount(MEMFS, {}, '/working');
    FS.unmount('/working');

    ex = null;
    // try to read the file from the old mount
    try {
      FS.readFile('/working/waka.txt', { encoding: 'utf8' });
    } catch (e) {
      ex = e;
    }
#if !defined(WASMFS)
      // WasmFS readFile aborts on failure, instead throwing an ErrnoError.
      assert(ex.name === 'ErrnoError' && ex.errno === 44); // ENOENT
#else
      assert(ex);
#endif
    ex = null;

    // check the safe file
    contents = FS.readFile('/safe.txt', { encoding: 'utf8' });
    assert(contents === 'abc');

#if WASMFS
    // test JS_FILE backend
    FS.mount(JSFILEFS, {}, "/jsfile");
    FS.writeFile("/jsfile/jsfile.txt", "a=1");
    assert(FS.readFile("/jsfile/jsfile.txt", { encoding: 'utf8' }) === 'a=1');
    FS.unmount("/jsfile");

    // test ICASE backend
    FS.mount(ICASEFS, { backend: MEMFS }, "/icase");
    FS.writeFile("/icase/IGNORE.txt", "a=1");
    assert(FS.readFile("/icase/Ignore.txt", { encoding: 'utf8' }) === 'a=1');
    assert(FS.readFile("/icase/ignore.TXT", { encoding: 'utf8' }) === 'a=1');
    FS.unmount("/icase");
#endif
  );

  puts("success");

  return 0;
}