define constants in central file

This commit is contained in:
joshuaboud 2022-05-25 16:50:53 -03:00
parent d3d16bf903
commit a0c179380d
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
3 changed files with 24 additions and 13 deletions

View File

@ -38,6 +38,7 @@ import { notificationsInjectionKey, settingsInjectionKey } from '../keys';
import DirectoryEntry from './DirectoryEntry.vue';
import getDirListing from '../functions/getDirListing';
import getDirEntryObjects from '../functions/getDirEntryObjects';
import { RECORD_SEPARATOR, UNIT_SEPARATOR } from '../constants';
export default {
name: 'DirectoryEntryList',
@ -155,18 +156,16 @@ export default {
}
selection.lastSelectedInd = null;
processingHandler.start();
const US = '\x1F';
const RS = '\x1E';
const processLinks = (linkTargets) => {
if (linkTargets.length === 0)
return null;
const callback = state => state.stdout
.trim()
.split('\n')
.split(RECORD_SEPARATOR)
.filter(record => record)
.map((record, index) => {
if (record.includes(US)) {
const [type, mode] = record.split(US);
if (record.includes(UNIT_SEPARATOR)) {
const [type, mode] = record.split(UNIT_SEPARATOR);
linkTargets[index].type = type;
linkTargets[index].mode = mode;
linkTargets[index].broken = false;
@ -175,7 +174,7 @@ export default {
}
});
return new Promise((resolve, reject) =>
useSpawn(['stat', `--printf=%F${US}%f\n`, ...linkTargets.map(target => target.path)], { superuser: 'try', err: 'out' }).promise()
useSpawn(['stat', `--printf=%F${UNIT_SEPARATOR}%f${RECORD_SEPARATOR}`, ...linkTargets.map(target => target.path)], { superuser: 'try', err: 'out' }).promise()
.then(callback)
.catch(callback)
.finally(resolve)

View File

@ -0,0 +1,13 @@
/**
* ASCII field delimiter
*/
const UNIT_SEPARATOR = '\x1F';
/**
* ASCII record delimiter
*/
const RECORD_SEPARATOR = '\x1E';
export {
UNIT_SEPARATOR,
RECORD_SEPARATOR,
}

View File

@ -1,4 +1,5 @@
import { useSpawn, errorString } from "@45drives/cockpit-helpers";
import { UNIT_SEPARATOR, RECORD_SEPARATOR } from "../constants";
/**
* Callback for handling errors during parsing of `dir` output lines
@ -47,8 +48,6 @@ import { useSpawn, errorString } from "@45drives/cockpit-helpers";
* @returns {Promise<DirectoryEntry[]>} Array of DirectoryEntry objects
*/
export default async function getDirEntryObjects(dirListing, cwd, failCallback, byteFormatter = cockpit.format_bytes) {
const US = '\x1F';
const RS = '\x1E';
const fields = [
'%n', // path
'%f', // mode (raw hex)
@ -67,7 +66,7 @@ export default async function getDirEntryObjects(dirListing, cwd, failCallback,
(
await useSpawn([
'stat',
`--printf=${fields.join(US)}${RS}`,
`--printf=${fields.join(UNIT_SEPARATOR)}${RECORD_SEPARATOR}`,
...dirListing
], { superuser: 'try', directory: cwd }
)
@ -87,13 +86,13 @@ export default async function getDirEntryObjects(dirListing, cwd, failCallback,
* @returns {DirectoryEntry[]}
*/
function parseRawEntryStats(raw, cwd, failCallback, byteFormatter = cockpit.format_bytes) {
const US = '\x1F';
const RS = '\x1E';
return raw.split(RS)
const UNIT_SEPARATOR = '\x1F'; // "Unit Separator" - ASCII field delimiter
const RECORD_SEPARATOR = '\x1E'; // "Record Separator" - ASCII record delimiter
return raw.split(RECORD_SEPARATOR)
.filter(record => record) // remove empty lines
.map(record => {
try {
let [name, mode, modeStr, size, owner, group, ctime, mtime, atime, type, symlinkStr] = record.split(US);
let [name, mode, modeStr, size, owner, group, ctime, mtime, atime, type, symlinkStr] = record.split(UNIT_SEPARATOR);
[size, ctime, mtime, atime] = [size, ctime, mtime, atime].map(num => parseInt(num));
[ctime, mtime, atime] = [ctime, mtime, atime].map(ts => ts ? new Date(ts * 1000) : null);
mode = parseInt(mode, 16);