diff --git a/main.js b/main.js index 5c10cf6..a4c6117 100644 --- a/main.js +++ b/main.js @@ -8,6 +8,7 @@ let md5Previous = md5(fs.readFileSync(env)); /** * Watch the .env file for changes and restart the scanner with new options if there is any change + * Time Complexity: O(1): Async event listener, static time * @param {string} env - Path to the .env file * @param {event} event - A filesystem event * @param {string} filename - Name of the file for the filesystem event diff --git a/scripts/config.js b/scripts/config.js index f3cff8d..68eebfb 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -9,6 +9,7 @@ const quarantinePath = "/usr/VSterilizer/infected/"; /** * Find the line for the given key and splice it to replace with the new value + * Time Complexity: O(n): Depends on which option is being changed * @param {string} key - Key for the given env variable * @param {string} newValue - New value for the given key * @param {string} env - path to the environment file. Defaults to the project supplied one @@ -29,6 +30,7 @@ async function changeParam(key, newValue, env = "./.env") { * if the given new option is the same with the old one since the watcher function * which restarts the scanner for changes checks the MD5 hash of the file to ensure * that there is an actual change in the file + * Time Complexity: O(1): Static time complexity, input doesn't change the operations only changes the outcome * @param {request} req - The option change request * @param {response} res - Passed to the function to enable sending responses in the handler function itself */ @@ -46,11 +48,14 @@ async function optionHandler(req, res) { /** * Parse incoming requests body as JSON + * Time Complexity: O(n): Depends on the size of the body of the incoming request * @param {function} - JSON parser */ app.use(bodyParser.json()); /** + * Listen for incoming post requests to the given endpoint + * Time Complexity: O(1): Async event listener, static time * @param {string} '/options' - API endpoint * @param {request} req - Incoming post request * @param {response} res - Response to be sent @@ -61,12 +66,14 @@ app.post('/options', (req, res) => { /** * Launch the server instance + * Time Complexity: O(1): The target port doesn't change the time, only the outcome * @param {integer} port - Port to launch the server on. Default: 8080 */ const server = app.listen(port, () => console.log(`Listening on port ${port}!`)) /** * Close the server when an exit event is emited + * Time Complexity: O(1): No variable input * @param {string} "exit" - Event name */ app.on("exit", () => server.close()) diff --git a/scripts/scan.js b/scripts/scan.js index e165608..2c65430 100644 --- a/scripts/scan.js +++ b/scripts/scan.js @@ -38,6 +38,8 @@ options = /** * Send the results of a scan to the API endpoint in realtime + * Time Complexity: O(1): When there is no infected files + * Time Complexity: O(n): When there are infected files * @param {array} badFileList - List of infected files, defaults to null */ async function sendResults(badFileList = null) { @@ -56,7 +58,8 @@ async function sendResults(badFileList = null) { /** * Sends status information to the API endpoint - * This includes data with information level severity such as a new USB being detected, a new scan running etc. + * This includes data with information level severity such as a new USB being detected, a new scan running etc. + * Time Complexity: O(n): Depends on the size of the message * @param {string} status - Status message to send */ async function sendStatus(status) { @@ -69,6 +72,7 @@ async function sendStatus(status) { /** * Scan the given directory path for infected files * Call the result sending function to send the results to the frontend API + * Time Complexity: O(n): Scanner needs to check every file seperately. * @param {string} path - Path for the directory to be scanned */ async function scanDirectory(path) { @@ -100,6 +104,7 @@ function sleep(ms) { /** * Parses the scanner log file to get detailed information about infected files * Takes a keyword parameter which has the path of a given infected file + * Time Complexity: O(n): Depends on the number of files scanned. * @param {string} keyword - path of the file we are intersted in */ async function parseLog(keyword, logfile = options.scanLog) { @@ -123,6 +128,7 @@ async function parseLog(keyword, logfile = options.scanLog) { /** * Get the mounting point for a given serial number + * Time Complexity: O(n): Depends on number of phyisal ports the computer has * @param {string} serialNumber - Serial number of the USB device, can include chars and integers */ async function getMountPoint(serialNumber) { @@ -135,6 +141,7 @@ async function getMountPoint(serialNumber) { /** * Mount the given device to a auto-generated dir under the * products own mounting directory + * Time Complexity: O(1): Directly calls system functions, static time * @param {string} source - physical port of the device to be mounted. ex: /dev/sda0 */ function mount(source) { @@ -148,6 +155,7 @@ function mount(source) { /** * Enable watcher to detect newly plugged USB devices * Calls the mounting point getter function when a new device is detected + * Time Complexity: O(1): Async event listener, static time * @param {string} 'add' - Adds a new USB event listener */ USBWatch.on('add', function (device) { @@ -157,6 +165,7 @@ USBWatch.on('add', function (device) { /** * Starting point of the scanner script * Handles clean-up and starts the USB monitoring + * Time Complexity: O(1): No variable input, static time */ function start() { fs.writeFileSync(options.scanLog, ''); // Clear the logs @@ -167,6 +176,7 @@ function start() { /** * Helper to end the USB monitoring * Utilized while stopping or restarting the service + * Time Complexity: O(1): No variable input, static time. Near instant */ function endWatch() { USBWatch.stopMonitoring();