commit d6c9cc471a360d78f2dc865aec7f6cba757e0eec
parent 7da272c6bf097d76d996fbe75e587749f7f9a70e
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date: Thu, 20 Jan 2022 03:18:51 -0800
Move permission string to function
Diffstat:
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/server.js b/server.js
@@ -30,6 +30,12 @@ function lsTime(timeMS) {
return `${getMonthByNumber(fileDate.getMonth())} ${fileDate.getDate()} ${fileDate.getFullYear()}`;
}
+function permissionToString(i) {
+ // Unix file permission array. The mode is the index in the array.
+ const permStrings = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx'];
+ return (i in permStrings) ? permStrings[i] : null;
+}
+
function ls(thePath) {
let result = {};
@@ -41,11 +47,9 @@ function ls(thePath) {
}
// Convert mode to string.
let unixFilePermissions = (stats.mode & parseInt('777', 8)).toString(8);
- // Unix file permission array. The mode is the index in the array.
- const permStrings = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx'];
- let permsResult = permStrings[parseInt(unixFilePermissions[0])];
- permsResult += permStrings[parseInt(unixFilePermissions[1])];
- permsResult += permStrings[parseInt(unixFilePermissions[2])];
+ let permsResult = permissionToString(parseInt(unixFilePermissions[0]));
+ permsResult += permissionToString(parseInt(unixFilePermissions[1]));
+ permsResult += permissionToString(parseInt(unixFilePermissions[2]));
let prefixChar = '-';
if(stats.isDirectory()) {