diff options
Diffstat (limited to 'includes/external/chvfs/index.js')
-rw-r--r-- | includes/external/chvfs/index.js | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/includes/external/chvfs/index.js b/includes/external/chvfs/index.js new file mode 100644 index 0000000..0dfd958 --- /dev/null +++ b/includes/external/chvfs/index.js @@ -0,0 +1,104 @@ +let active = true; +let queue = []; + +const watch = require('node-watch'); +const child_process = require('child_process'); +const fs = require('fs').promises; +const fss = require('fs'); + +process.on('uncaughtException', (e) => { + console.error(e); +}); + +function start() { + console.log("Mounting chvfs..."); + child_process.execSync("mount -t tmpfs -o size=1G chvfs /_ch"); + console.log("Mounted chvfs to /_ch"); + + console.log("Preparing filesystem..."); + child_process.execSync("cp -r /opt/peh_save/* /_ch"); + child_process.execSync("chmod -Rf 777 /_ch"); + console.log("Filesystem is ready"); + + console.log("Watching for changes..."); + watch("/_ch", { recursive: true }, (event, filename) => { + try { + if (!active || !filename) return; + + if (event === "update") { + console.log(filename + " was created or affected"); + + queue.push({ + file: filename, + remove: false + }); + } else if (event === "remove") { + console.log(filename + " was dropped"); + + queue.push({ + file: filename, + remove: true + }); + } + } catch (e) { + console.error(e); + } + }) +} + +function stop() { + active = false; + + console.log("Unmounting chvfs..."); + child_process.execSync("umount -l /_ch"); + console.log("Unmounted chvfs"); +} + +start(); +setInterval(async () => { + for (let item of queue) { + try { + if (item.remove) { + console.log("Dropping " + item.file); + await fs.unlink("/opt/peh_save/" + item.file.substring(5)); + } else { + console.log("Copying " + item.file); + await fs.copyFile(item.file, "/opt/peh_save/" + item.file.substring(5)); + } + } catch (e) { + console.error(e); + } + + queue.splice(0, 1); + } +}); + +process.on('exit', () => { + process.stdout.write("\n"); + + for (let item of queue) { + try { + if (item.remove) { + console.log("Dropping " + item.file); + fss.unlinkSync("/opt/peh_save/" + item.file.substring(5)); + } else { + console.log("Copying " + item.file); + fss.copyFileSync(item.file, "/opt/peh_save/" + item.file.substring(5)); + } + } catch (e) { + console.error(e); + } + + queue.splice(0, 1); + } + + stop(); +}); + +process.on('SIGINT', () => { + process.exit(); +}); + +process.on('SIGTERM', () => { + process.exit(); +});
\ No newline at end of file |