mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-04-16 23:38:52 +00:00
Make readFromBoot behave more like Node readFile
The `readFromBoot` function replaced calls to readFile in the codebase when the target location is under `/mnt/boot` in order to use `fatrw` to check for corruption. Unfortunately this function behaved a little differently with regards to errors. While `readFile` returns a code `ENOENT` when the file is not found, `readFromBoot` would just throw an exception with whatever code was given by fatrw. This would cause confusion for some calls that were behaving differently with a `ENOENT`, causing some issues. Closes: #2071 Change-type: patch
This commit is contained in:
parent
cb72b8337b
commit
0605e996c9
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user