summaryrefslogtreecommitdiff
path: root/includes/external/chvfs/index.js
blob: 0dfd9580546560a828df32c8a3ece19c042b1caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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();
});