-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Filesystem Guide
Emscripten allows you to set up a virtual filesystem that points to preloaded data or lazy-loaded URLs, as well as virtual devices that can read and write data. Note that all the configuration should be done before the main run()
method is executed.
Emscripten standard I/O works by going though the virtual /dev/stdin
, /dev/stdout
and /dev/stderr
devices. You can set them up using your own I/O functions by calling FS.init(input_callback, output_callback, error_callback)
(all arguments optional).
- The input callback will be called with no parameters whenever the program attempts to read from
stdin
. It should return an ASCII character code when data is available, ornull
when it isn't. - The output callback will be called with an ASCII character code whenever the program writes to
stdout
. It may also be called withnull
to flush the output. - The error callback is similar to the output one, except it is called when data is written to
stderr
.
If any of the callbacks throws an exception, it will be caught and handled as if the device malfunctioned.
By default:
-
stdin
will read from the terminal in command line engines and usewindow.prompt()
in browsers (in both cases, with line buffering). -
stdout
will use aprint
function if one such is defined, printing to the terminal in command line engines and to the browser console in browsers that have a console (again, both line-buffered). -
stderr
will use the same output function asstdout
.
The FS
object provides several methods to create files, folders, devices and symbolic links:
-
FS.createFolder(parent, name, canRead, canWrite)
: Creates a single empty folder and returns a reference to it.-
(string|object) parent
: The parent folder, either as a path (e.g.'/usr/lib'
) or an object previously returned from aFS.createFolder()
orFS.createPath()
call. -
string name
: The name of the new folder. -
bool canRead
: Whether this folder should have read permissions set from the program's point of view. -
bool canWrite
: Whether this folder should have write permissions set from the program's point of view.
Example:
var home = FS.createFolder('/', 'home', true, false); FS.createFolder(home, 'user', true, true); FS.createFolder('/home', 'other-user', false, false);
-
-
FS.createPath(parent, path, canRead, canWrite)
: Recursively creates a path and returns a reference to the innermost folder.-
(string|object) parent
: The parent folder, either as a path (e.g.'/usr/lib'
) or an object previously returned from aFS.createFolder()
orFS.createPath()
call. -
string path
: The path to the new folder. Any folders missing in this path will be created. -
bool canRead
: Whether the created folders should have read permissions set from the program's point of view. -
bool canWrite
: Whether the created folders should have write permissions set from the program's point of view.
Example:
FS.createPath('/', 'home/user1', true, false); FS.createPath('/', 'home/user2/Desktop', true, false);
-
-
FS.createDataFile(parent, name, data, canRead, canWrite)
: Creates a file containing given data and returns a reference to it.-
(string|object) parent
: The parent folder, either as a path (e.g.'/usr/lib'
) or an object previously returned from aFS.createFolder()
orFS.createPath()
call. -
string name
: The name of the new file. -
(string|array) data
: The data that the new file will contain, either as a string or an array of bytes (integers in the [-128, 255] range). -
bool canRead
: Whether the file should have read permissions set from the program's point of view. -
bool canWrite
: Whether the file should have write permissions set from the program's point of view.
Example:
FS.createDataFile('/', 'foo', 'abc', true, false); FS.createDataFile('/', 'bar', [1, 2, 3], true, true);
-
-
FS.createLazyFile(parent, name, url, canRead, canWrite)
: Creates a file that will be loaded lazily on first access from a given URL or local filesystem path, and returns a reference to it.-
(string|object) parent
: The parent folder, either as a path (e.g.'/usr/lib'
) or an object previously returned from aFS.createFolder()
orFS.createPath()
call. -
string name
: The name of the new file. -
string url
: In the browser, this is the URL whose contents will be returned when this file is accessed. In a command line engine, this will be the local (real) filesystem path from where the contents will be loaded. Note that writes to this file are virtual. -
bool canRead
: Whether the file should have read permissions set from the program's point of view. -
bool canWrite
: Whether the file should have write permissions set from the program's point of view.
Example:
FS.createLazyFile('/', 'foo', 'other/page.htm', true, false); FS.createLazyFile('/', 'bar', '/get_file.php?name=baz', true, true);
-
-
FS.createLink(parent, name, target, canRead, canWrite)
: Creates a symbolic link and returns a reference to it.-
(string|object) parent
: The parent folder, either as a path (e.g.'/usr/lib'
) or an object previously returned from aFS.createFolder()
orFS.createPath()
call. -
string name
: The name of the link. -
string target
: The target of the link, a relative or absolute path. The path does not need to exist at the time the link is created. -
bool canRead
: Whether the link should have read permissions set from the program's point of view. -
bool canWrite
: Whether the link should have write permissions set from the program's point of view.
Example:
FS.createLink('/bin', 'g++', 'gcc' true, true); FS.createLink('/home/jack', 'log', '/var/log/prog.log', true, true); FS.createLink('/home/jack/Desktop', 'test', '../dev/prog/run.sh', true, true);
-
-
FS.createDevice(parent, name, input, output)
: Creates a virtual device and returns a reference to it.-
(string|object) parent
: The parent folder, either as a path (e.g.'/usr/lib'
) or an object previously returned from aFS.createFolder()
orFS.createPath()
call. -
string name
: The name of the file representing the device. -
function input
: The function called when reading from the device. Should return a byte-sized integer if there's data, ornull
if there isn't. Ifoutput
is specified, this can benull
orundefined
. -
function output
: The function called when writing to the device. Will be called with a byte-sizes integer to write data, ornull
to flush it (if flushing makes sense for the device). Ifinput
is specified, this can benull
orundefined
.
Example:
FS.createDevice('/dev', 'random', function() { return Math.floor(Math.random() * 256); }); FS.createDevice('/dev/snd', 'pcm', null, playSound);
-
The whole filesystem can be safely serialized and restored at any time by saving the values of FS.root
and FS.nextInode
, and replacing them with previously saved values, respectively. Keep in mind, however, that devices use functions, which are discarded on a normal JSON.stringify()
call, so you might want to use a custom stringifier.
README.md ``