Skip to content

Commit

Permalink
Fix creating vfs-file subdirectory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
georgestagg committed Aug 30, 2024
1 parent 1c2e7a5 commit 64eef91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
33 changes: 23 additions & 10 deletions _extensions/live/templates/pyodide-setup.ojs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
13 changes: 9 additions & 4 deletions _extensions/live/templates/webr-setup.ojs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
4 changes: 4 additions & 0 deletions docs/other/data/subdir/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x,y
1,2
3,4
5,6

0 comments on commit 64eef91

Please sign in to comment.