MCPcopy
hub / github.com/pyodide/pyodide / initializeNativeFS

Function initializeNativeFS

src/js/nativefs.ts:35–264  ·  view source on GitHub ↗
(module: PyodideModule)

Source from the content-addressed store, hash-verified

33 * @private
34 */
35export function initializeNativeFS(module: PyodideModule) {
36 const FS = module.FS;
37 const MEMFS = module.FS.filesystems.MEMFS;
38 const PATH = module.PATH;
39
40 const nativeFSAsync = {
41 // DIR_MODE: {{{ cDefine('S_IFDIR') }}} | 511 /* 0777 */,
42 // FILE_MODE: {{{ cDefine('S_IFREG') }}} | 511 /* 0777 */,
43 DIR_MODE: 16384 | 511,
44 FILE_MODE: 32768 | 511,
45 mount: function (mount: any) {
46 if (!mount.opts.fileSystemHandle) {
47 throw new Error("opts.fileSystemHandle is required");
48 }
49
50 // reuse all of the core MEMFS functionality
51 return MEMFS.mount.apply(null, arguments);
52 },
53 syncfs: async (mount: any, populate: Boolean, callback: Function) => {
54 try {
55 const local = nativeFSAsync.getLocalSet(mount);
56 const remote = await nativeFSAsync.getRemoteSet(mount);
57 const src = populate ? remote : local;
58 const dst = populate ? local : remote;
59 await nativeFSAsync.reconcile(mount, src, dst);
60 callback(null);
61 } catch (e) {
62 callback(e);
63 }
64 },
65 // Returns file set of emscripten's filesystem at the mountpoint.
66 getLocalSet: (mount: any) => {
67 let entries = Object.create(null);
68
69 function isRealDir(p: string) {
70 return p !== "." && p !== "..";
71 }
72
73 function toAbsolute(root: string) {
74 return (p: string) => {
75 return PATH.join2(root, p);
76 };
77 }
78
79 let check = FS.readdir(mount.mountpoint)
80 .filter(isRealDir)
81 .map(toAbsolute(mount.mountpoint));
82
83 while (check.length) {
84 let path = check.pop()!;
85 let stat = FS.stat(path);
86
87 if (FS.isDir(stat.mode)) {
88 check.push.apply(
89 check,
90 FS.readdir(path).filter(isRealDir).map(toAbsolute(path)),
91 );
92 }

Callers

nothing calls this directly

Calls 15

callbackFunction · 0.85
toAbsoluteFunction · 0.85
getFsHandlesFunction · 0.85
applyMethod · 0.80
mapMethod · 0.80
filterMethod · 0.80
arrayBufferMethod · 0.80
setMethod · 0.80
deleteMethod · 0.80
forEachMethod · 0.80
sortMethod · 0.80
writeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…