test - inolen/emscripten GitHub Wiki

Setting up files and folders

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 a FS.createFolder() or FS.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 a FS.createFolder() or FS.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. A convenient way to set up a file for you is to call emcc with --preload-file. That will do everything necessary, including calling this function.

    • (string|object) parent: The parent folder, either as a path (e.g. '/usr/lib') or an object previously returned from a FS.createFolder() or FS.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. WARNING: Firefox and Chrome have recently disabled synchronous binary XHRs, which means this cannot work for Javascript in regular HTML pages (but it works within WebWorkers). Instead, use createDataFile.

    • (string|object) parent: The parent folder, either as a path (e.g. '/usr/lib') or an object previously returned from a FS.createFolder() or FS.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.createPreloadedFile(parent, name, url, canRead, canWrite): Preloads a file asychronously. You should call this in preRun, and then run() will be delayed until all preloaded files are ready. This is how --preload-file works in emcc.

  • 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 a FS.createFolder() or FS.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 a FS.createFolder() or FS.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, or null if there isn't. If output is specified, this can be null or undefined.
    • function output: The function called when writing to the device. Will be called with a byte-sizes integer to write data, or null to flush it (if flushing makes sense for the device). If input is specified, this can be null or undefined.

    Example:

      FS.createDevice('/dev', 'random', function() { return Math.floor(Math.random() * 256); });
      FS.createDevice('/dev/snd', 'pcm', null, playSound);
    

Deleting Files

    FS.writeFile('/foobar.txt', 'Hello, world');
    FS.unlink('/foobar.txt');