From 64eef9169c97c46f4dbfce823c66d57bb8980f21 Mon Sep 17 00:00:00 2001 From: George Stagg Date: Fri, 30 Aug 2024 11:42:19 +0100 Subject: [PATCH] Fix creating vfs-file subdirectory structure --- _extensions/live/templates/pyodide-setup.ojs | 33 ++++++++++++++------ _extensions/live/templates/webr-setup.ojs | 13 +++++--- docs/other/data/subdir/data.csv | 4 +++ 3 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 docs/other/data/subdir/data.csv diff --git a/_extensions/live/templates/pyodide-setup.ojs b/_extensions/live/templates/pyodide-setup.ojs index defa91f..8d66675 100644 --- a/_extensions/live/templates/pyodide-setup.ojs +++ b/_extensions/live/templates/pyodide-setup.ojs @@ -60,21 +60,34 @@ pyodideOjs = { file = name; } - // Create directory tree, collapsing higher directory structure - let path = file = collapsePath(file); - try { - while (path = path.substr(0, path.lastIndexOf('/'))) { + // Collapse higher directory structure + file = collapsePath(file); + + // Create directory tree, ignoring "directory exists" VFS errors + const parts = file.split('/').slice(0, -1); + let path = ''; + while (parts.length > 0) { + path += parts.shift() + '/'; + try { await pyodide.FS.mkdir(path); + } catch (e) { + if (e.name !== "ErrnoError") throw e; + if (e.errno !== 20) { + const errorTextPtr = await pyodide._module._strerror(e.errno); + const errorText = await pyodide._module.UTF8ToString(errorTextPtr); + throw new Error(`Filesystem Error ${e.errno} "${errorText}".`); + } } + } + + // Write this file to the VFS + try { return await pyodide.FS.writeFile(file, new Uint8Array(data)); } catch (e) { if (e.name !== "ErrnoError") throw e; - // Ignore "dir exists" filesystem error - if (e.errno !== 20) { - const errorTextPtr = await pyodide._module._strerror(e.errno); - const errorText = await pyodide._module.UTF8ToString(errorTextPtr); - throw new Error(`Filesystem Error ${e.errno} "${errorText}".`); - } + const errorTextPtr = await pyodide._module._strerror(e.errno); + const errorText = await pyodide._module.UTF8ToString(errorTextPtr); + throw new Error(`Filesystem Error ${e.errno} "${errorText}".`); } }).reduce((cur, next) => cur.then(next), Promise.resolve()); diff --git a/_extensions/live/templates/webr-setup.ojs b/_extensions/live/templates/webr-setup.ojs index 5af4df8..ad5ef81 100644 --- a/_extensions/live/templates/webr-setup.ojs +++ b/_extensions/live/templates/webr-setup.ojs @@ -66,19 +66,24 @@ webROjs = { file = name; } - // Create directory tree, collapsing higher directory structure - let path = file = collapsePath(file); - while (path = path.substr(0, path.lastIndexOf('/'))) { + // Collapse higher directory structure + file = collapsePath(file); + + // Create directory tree, ignoring "directory exists" VFS errors + const parts = file.split('/').slice(0, -1); + let path = ''; + while (parts.length > 0) { + path += parts.shift() + '/'; try { await webR.FS.mkdir(path); } catch (e) { - // Ignore "dir exists" filesystem error if (!e.message.includes("FS error")) { throw e; } } } + // Write this file to the VFS return await webR.FS.writeFile(file, new Uint8Array(data)); }).reduce((cur, next) => cur.then(next), Promise.resolve()); diff --git a/docs/other/data/subdir/data.csv b/docs/other/data/subdir/data.csv new file mode 100644 index 0000000..0c8063f --- /dev/null +++ b/docs/other/data/subdir/data.csv @@ -0,0 +1,4 @@ +x,y +1,2 +3,4 +5,6