aboutsummaryrefslogtreecommitdiff
path: root/index.js
blob: e12e60834c71ae7f3b87e7498b1b2a8555ac194b (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(async () => {
    global.diskStatus = TempDisk.LOADING;
    global.waiting = false;
    global.lockDate = null;
    global.fileWatchers = {};
    global.fileWatchersPurgeable = [];
    updateMenu();

    const fs = require('fs');
    const child_process = require('child_process');

    function waitForDisk() {
        return new Promise((res, _rej) => {
            let me = setInterval(() => {
                updateMenu();

                if (fs.existsSync(directory)) {
                    clearInterval(me);
                    res();
                }
            }, 1000)
        })
    }

    function waitForAccess() {
        return new Promise((res, _rej) => {
            let hasAccess = false;
            let me = setInterval(() => {
                updateMenu();

                try {
                    fs.accessSync(directory, fs.constants.R_OK);
                    hasAccess = true;
                } catch (e) {
                    hasAccess = false;
                }

                if (hasAccess) {
                    clearInterval(me);
                    res();
                }
            }, 1000)
        })
    }

    async function wait() {
        global.waiting = true;
        global.lockDate = new Date(global.config.lastLockTime ?? new Date().getTime());

        console.log("Waiting for disk to be mounted...");
        global.diskStatus = TempDisk.LOCKED;
        updateMenu();
        await waitForDisk();
        console.log("Disk found!");
        global.lockDate = null;

        console.log("Waiting for permission to read the disk...");
        await waitForAccess();
        console.log("Permission granted!");
        global.waiting = false;

        global.filesList = [];
        global.trackingFailures = [];
        getFilesList(directory);

        for (let file of filesList) {
            if (!Object.keys(fileWatchers).includes(file)) watchFile(file);
        }

        global.lastAccess = new Date(global.config.lastAccessTime ?? new Date().getTime());
        global.diskStatus = TempDisk.UNLOCKED;

        global.notifications.unlock();

        updateMenu();
    }

    global.lastAccess = null;
    await wait();

    setInterval(() => {
        for (let item of fileWatchersPurgeable) {
            if (Object.keys(fileWatchers).includes(item)) {
                delete fileWatchers[item];
            }
        }

        global.fileWatchersPurgeable = [];
    }, 60000);

    setInterval(async () => {
        if (!fs.existsSync(directory)) {
            process.stdout.cursorTo(0);
            process.stdout.clearLine(null);
            if (!global.waiting) await wait();
            return;
        }

        let seconds = Math.round((new Date().getTime() - global.lastAccess.getTime()) / 1000);

        if (global.processes.length > 0) {
            global.left = Infinity;
            global.lastAccess = new Date();
        } else {
            global.left = time - seconds;
        }

        updateMenu();

        process.stdout.cursorTo(0);
        process.stdout.clearLine(null);
        process.stdout.write("Unmounting in " + left + " seconds");

        if (left <= 0) {
            console.log("Unmounting...");
            global.commands.lock();
            global.notifications.lock("inactivity");
        }

        for (let file of Object.keys(fileWatchers)) {
            try {
                if (!fs.existsSync(file) && !fileWatchersPurgeable.includes(file)) fileWatchersPurgeable.push(file);
            } catch (e) {}
        }
    }, 1000)

    global.filesList = [];
    global.trackingFailures = [];
    function getFilesList(currentDirectory) {
        for (let file of fs.readdirSync(currentDirectory)) {
            try {
                fs.accessSync(currentDirectory + "/" + file, fs.constants.R_OK);
            } catch (e) {
                process.stdout.clearLine(null);
                process.stdout.cursorTo(0);
                console.log("Failed to track file " + currentDirectory + "/" + file + " (" + e.code + ")")
                trackingFailures.push(currentDirectory + "/" + file);
                continue;
            }

            filesList.push(currentDirectory + "/" + file);

            if (fs.lstatSync(currentDirectory + "/" + file).isDirectory()) {
                getFilesList(currentDirectory + "/" + file);
            }
        }
    }

    getFilesList(directory);

    const dirWatcher = fs.watch(directory, { recursive: !global.linux }, (event, filename) => {
        process.stdout.clearLine(null);
        process.stdout.cursorTo(0);
        console.log("File " + directory + "/" + filename + " was updated (dir_watcher)");
        global.lastAccess = new Date();

        if (filename && !filesList.includes(directory + "/" + filename)) {
            filesList.push(directory + "/" + filename);
            watchFile(directory + "/" + filename);
        }
    });
    console.log("Watching directory: " + directory);

    function watchFile(file) {
        try {
            fs.accessSync(file);
        } catch (e) {
            process.stdout.clearLine(null);
            process.stdout.cursorTo(0);
            console.log("Failed to track file " + file + " (" + e.code + ")")
            trackingFailures.push(file);
            return;
        }

        process.stdout.clearLine(null);
        process.stdout.cursorTo(0);
        console.log("Watching file: " + file);
        fileWatchers[file] = fs.watchFile(file, (_current, _previous) => {
            process.stdout.clearLine(null);
            process.stdout.cursorTo(0);
            console.log("File " + file + " was updated (file_watcher)");
            global.lastAccess = new Date();
        });
    }

    for (let file of filesList) {
        if (!Object.keys(fileWatchers).includes(file)) watchFile(file);
    }
})()