webassembly - WASM IDBFS 不持久

标签 webassembly indexeddb

我有一个加载 wasm 模块的 HTML 页面。 我将此 JS 代码添加到 html 中:

// Create a directory in IDBFS to store the file
FS.mkdir('/persistent');

// Mount IDBFS as the file system
FS.mount(IDBFS, {}, '/persistent');

// Define a function to write a string to a file in the persistent directory
function writeStringToFile(path, string) {
  var fd = FS.open(path, 'w+');
  FS.write(fd, string, 0, string.length, 0);
  FS.close(fd);
}

// Define a function to read a file from the persistent directory
function readStringFromFile(path) {
  var fd = FS.open(path, 'r');
  var buffer = new Uint8Array(FS.stat(path).size);
  FS.read(fd, buffer, 0, buffer.length, 0);
  var decoder = new TextDecoder('utf8');
  var data = decoder.decode(buffer);
  FS.close(fd);
  return data;
}

在此代码之后,我在浏览器控制台中执行此代码:

// Sync the file system to IDBFS with persistence enabled
FS.syncfs(true, (err) => {
  if (err) {
    console.log('Error syncing file system:', err);
  } else {
    console.log('File system synced successfully with persistence enabled!');
  }
});

// Write a string to a file in the persistent directory
writeStringToFile('/persistent/myfile.txt', 'Hello, world!');
FS.readdir('/persistent') //Check File exists -> OK

然后我重新加载 broser (F5) 并在浏览器控制台中执行此操作:

// Read the string back from the file after reloading the page
FS.syncfs(false, (err) => {
  if (err) {
    console.log('Error syncing file system:', err);
  } else {
    FS.readdir('/persistent') //Check File exists -> File doesn't exist.
    console.log('File system synced successfully with persistence disabled!');
    var data = readStringFromFile('/persistent/myfile.txt');
    console.log('Contents of file:', data);
  }
});

我看到在浏览器的 IndexDB 中创建了一个条目:$url/persistent/FILE_DATA

但我没有看到 myfile.txt。

我不明白这是怎么回事?这是 wasm FS 实现中的错误吗?

这是我的 emscripten 版本:

emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.32 (eab98adf462c39f3c31d348331c4830bcaa36949)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

构建 wasm 是一个由 2 个方法组成的小程序。我在此示例中没有使用它们。

在构建中,我设置了以下参数:-std=c++11 --bind -lidbfs.js -s FORCE_FILESYSTEM=1

最佳答案

文档说:

populate (bool) – true to initialize Emscripten’s file system data with the data from the file system’s persistent source, and false to save Emscripten`s file system data to the file system’s persistent source.

所以你必须做相反的事情:

FS.mkdir('/workdir');
FS.mount(FS.filesystems.IDBFS, {}, '/workdir')

FS.syncfs(true, (err) => {
  console.log(FS.readdir('/workdir'))
  const s = FS.analyzePath('/workdir/test')
  if (s.exists) {
    console.log(FS.readFile('/workdir/test', {encoding: 'utf8'}).toString())
  } else {
    FS.writeFile('/workdir/test', 'hello world')
    FS.syncfs(false, (err) => {
      console.log('saved to idbfs', FS.readdir('/workdir'))
    })
  }
})

刷新页面时将输出hello world。另请注意,syncfs 参数默认为 false,它将 FS 同步到 IDBFS。

关于webassembly - WASM IDBFS 不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75543945/

相关文章:

c++ - 如何将 C++ 文件编译为 WebAssembly?

javascript - 如何在 IndexedDB 中按属性查询对象?

javascript - 如何加载整个 AngularJs 应用程序使用的数据?

javascript - 从 Indexeddb 获取特定的 ID

webassembly - 转换为 WebAssembly 时使用什么语言有关系吗?

webassembly - Wasm 访问 DOM

node.js - 如何使用 Create-React-App Node 服务器定义 MIME 类型?

javascript - 优化批量( block )将对象上传到 IndexedDB

javascript - 防止IndexedDB请求错误取消事务

c++ - 减少 Wasm 文件大小(libc、优化、emscripten)