Skip to content

Commit

Permalink
Implement ls long with fixed chmod for missing role
Browse files Browse the repository at this point in the history
  • Loading branch information
jason2020 committed Jan 6, 2023
1 parent 1ede3cb commit 19a48db
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
31 changes: 21 additions & 10 deletions src/components/shared/TerminalCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,23 @@ function executeFind(
return result;
}

function setAllPermissions(
permissionMap: Map<string, string>,
action: string,
role: string,
file: FileSystemObject
): void {
const p = permissionMap.get(action);
const val = role === '+';
if (p === 'read' || p === 'write' || p === 'execute') {
Object.keys(file.permissions).forEach((r) => {
if (r === 'user' || r === 'group' || r === 'other') {
file.permissions[r][p] = val;
}
});
}
}

function executeChmod(
fileSystem: Directory,
cwd: Directory,
Expand Down Expand Up @@ -779,18 +796,12 @@ function executeChmod(
if (permission.length == 2 && (role === '+' || role === '-')) {
if (path === '.') {
file.children?.forEach((child) => {
const p = permissionMap.get(action);
const val = role === '+';
if (p === 'read' || p === 'write' || p === 'execute') {
Object.keys(child.permissions).forEach((r) => {
if (r === 'user' || r === 'group' || r === 'other') {
child.permissions[r][p] = val;
}
});
}
setAllPermissions(permissionMap, action, role, child);
});
continue;
} else {
setAllPermissions(permissionMap, action, role, file);
}
continue;
}

// Minor error handling
Expand Down
32 changes: 28 additions & 4 deletions src/components/shared/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,34 @@ export class Directory extends FileSystemObject {
}

getChildrenNames(showHidden = false, longFormat = false): Array<string> {
if (longFormat) {
// TODO: implement long format
}
return this.getChildren(showHidden).map((child) => child.name);
return this.getChildren(showHidden).map((child) => {
if (longFormat) {
let permissionString = '';
for (const permission of [
child.permissions.user,
child.permissions.group,
child.permissions.other,
]) {
permissionString += ' ';
permissionString += `${permission.read ? 'r' : '-'}`;
permissionString += `${permission.write ? 'w' : '-'}`;
permissionString += `${permission.execute ? 'x' : '-'}`;
}
const options = {
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false,
};
// The user is tux, so we just hardcode that
return `${permissionString} tux ${new Date().toLocaleString(
'en-us',
options
)} ${child.name}`;
}
return child.name;
});
}

getChild(name: string): Directory | File | undefined {
Expand Down

0 comments on commit 19a48db

Please sign in to comment.