Merge pull request #2073 from balena-os/host-config

Make readFromBoot behave more like Node readFile
This commit is contained in:
Christina Wang 2022-12-08 10:38:27 -08:00 committed by GitHub
commit 577fec7ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 8 deletions

View File

@ -117,7 +117,8 @@ function generateRedsocksConfEntries(conf: ProxyConfig): string {
let v = conf[field];
if (v != null) {
if (isAuthField(field)) {
v = `"${v}"`;
// Escape any quotes in the field value
v = `"${v.toString().replace(/"/g, '\\"')}"`;
}
val += `\t${field} = ${v};\n`;
}

View File

@ -1,7 +1,7 @@
import { spawn } from 'child_process';
import * as path from 'path';
import * as constants from './constants';
import { exec } from './fs-utils';
import { exec, exists } from './fs-utils';
// Returns an absolute path starting from the hostOS root partition
// This path is accessible from within the Supervisor container
@ -16,7 +16,7 @@ export function pathOnBoot(relPath: string) {
}
class CodedError extends Error {
constructor(msg: string, readonly code: number) {
constructor(msg: string, readonly code: number | string) {
super(msg);
}
}
@ -33,11 +33,22 @@ export async function readFromBoot(
fileName: string,
encoding?: 'utf8' | 'utf-8',
): Promise<string | Buffer> {
if (!(await exists(fileName))) {
// Mimic the behavior of Node readFile
throw new CodedError(`Failed to read file ${fileName}`, 'ENOENT');
}
const cmd = ['fatrw', 'read', fileName].join(' ');
const { stdout } = await exec(cmd, {
encoding,
});
return stdout;
try {
const { stdout } = await exec(cmd, {
encoding,
});
return stdout;
} catch (e: any) {
const { code, stderr } = e;
throw new CodedError(stderr ?? e.message, code);
}
}
// Receives an absolute path for a file (assumed to be under the boot partition, e.g. `/mnt/root/mnt/boot/config.txt`)
@ -59,7 +70,7 @@ export async function writeToBoot(fileName: string, data: string | Buffer) {
fatrw.on('close', resolve);
});
if (exitCode) {
if (exitCode !== 0) {
throw new CodedError(`Write failed with error: ${error}`, exitCode);
}
}