Skip to content

Commit

Permalink
Add time complexity analysis for the relevant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
denizariyan committed May 5, 2022
1 parent b3f5436 commit 198447e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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())
Expand Down
12 changes: 11 additions & 1 deletion scripts/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} badFileList - List of infected files, defaults to null
*/
async function sendResults(badFileList = null) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 198447e

Please sign in to comment.