(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); } })()