2022-02-24 21:01:41 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-11-14 01:20:36 +00:00
|
|
|
import { createHash } from 'crypto';
|
2022-02-24 21:01:41 +00:00
|
|
|
import { createReadStream } from 'fs';
|
2024-11-14 01:20:36 +00:00
|
|
|
import { rename, rm } from 'fs/promises';
|
2022-03-03 22:34:51 +00:00
|
|
|
import { pipeline } from 'stream/promises';
|
2022-02-24 21:01:41 +00:00
|
|
|
|
2022-03-28 19:05:44 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2024-11-14 01:20:36 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-03-28 19:05:44 +00:00
|
|
|
import * as durations from '../util/durations';
|
|
|
|
import { sleep } from '../util/sleep';
|
2024-11-14 01:20:36 +00:00
|
|
|
import { isOlderThan } from '../util/timestamp';
|
2023-03-02 17:57:36 +00:00
|
|
|
|
2022-02-24 21:01:41 +00:00
|
|
|
export type CheckIntegrityResultType = Readonly<
|
|
|
|
| {
|
|
|
|
ok: true;
|
|
|
|
error?: void;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
ok: false;
|
|
|
|
error: string;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export async function checkIntegrity(
|
|
|
|
fileName: string,
|
|
|
|
sha512: string
|
|
|
|
): Promise<CheckIntegrityResultType> {
|
|
|
|
try {
|
|
|
|
const hash = createHash('sha512');
|
2022-03-03 22:34:51 +00:00
|
|
|
await pipeline(createReadStream(fileName), hash);
|
2022-02-24 21:01:41 +00:00
|
|
|
|
|
|
|
const actualSHA512 = hash.digest('base64');
|
|
|
|
if (sha512 === actualSHA512) {
|
|
|
|
return { ok: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
ok: false,
|
|
|
|
error: `Integrity check failure: expected ${sha512}, got ${actualSHA512}`,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
ok: false,
|
|
|
|
error: Errors.toLogFormat(error),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 19:05:44 +00:00
|
|
|
|
2023-03-02 17:57:36 +00:00
|
|
|
async function doGracefulFSOperation<Args extends ReadonlyArray<unknown>>({
|
|
|
|
name,
|
|
|
|
operation,
|
|
|
|
args,
|
2022-03-28 19:05:44 +00:00
|
|
|
logger,
|
|
|
|
startedAt,
|
|
|
|
retryCount,
|
|
|
|
retryAfter = 5 * durations.SECOND,
|
|
|
|
timeout = 5 * durations.MINUTE,
|
|
|
|
}: {
|
2023-03-02 17:57:36 +00:00
|
|
|
name: string;
|
|
|
|
operation: (...args: Args) => Promise<void>;
|
|
|
|
args: Args;
|
2022-03-28 19:05:44 +00:00
|
|
|
logger: LoggerType;
|
|
|
|
startedAt: number;
|
|
|
|
retryCount: number;
|
|
|
|
retryAfter?: number;
|
|
|
|
timeout?: number;
|
|
|
|
}): Promise<void> {
|
2023-03-02 17:57:36 +00:00
|
|
|
const logId = `gracefulFS(${name})`;
|
2022-03-28 19:05:44 +00:00
|
|
|
try {
|
2023-03-02 17:57:36 +00:00
|
|
|
await operation(...args);
|
2022-03-28 19:05:44 +00:00
|
|
|
|
|
|
|
if (retryCount !== 0) {
|
|
|
|
logger.info(
|
2023-03-02 17:57:36 +00:00
|
|
|
`${logId}: succeeded after ${retryCount} retries, ${args.join(', ')}`
|
2022-03-28 19:05:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-12-12 04:09:30 +00:00
|
|
|
if (error.code !== 'EACCES' && error.code !== 'EPERM') {
|
2022-03-28 19:05:44 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOlderThan(startedAt, timeout)) {
|
2023-03-02 17:57:36 +00:00
|
|
|
logger.warn(`${logId}: timed out, ${args.join(', ')}`);
|
2022-03-28 19:05:44 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn(
|
2023-03-02 17:57:36 +00:00
|
|
|
`${logId}: got ${error.code} when running on ${args.join(', ')}; ` +
|
|
|
|
`retrying in one second. (retryCount=${retryCount})`
|
2022-03-28 19:05:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await sleep(retryAfter);
|
|
|
|
|
2023-03-02 17:57:36 +00:00
|
|
|
return doGracefulFSOperation({
|
|
|
|
name,
|
|
|
|
operation,
|
|
|
|
args,
|
2022-03-28 19:05:44 +00:00
|
|
|
logger,
|
|
|
|
startedAt,
|
|
|
|
retryCount: retryCount + 1,
|
|
|
|
retryAfter,
|
|
|
|
timeout,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function gracefulRename(
|
|
|
|
logger: LoggerType,
|
|
|
|
fromPath: string,
|
|
|
|
toPath: string
|
|
|
|
): Promise<void> {
|
2023-03-02 17:57:36 +00:00
|
|
|
return doGracefulFSOperation({
|
|
|
|
name: 'rename',
|
|
|
|
operation: rename,
|
|
|
|
args: [fromPath, toPath],
|
|
|
|
logger,
|
|
|
|
startedAt: Date.now(),
|
|
|
|
retryCount: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-14 01:20:36 +00:00
|
|
|
function rmRecursive(path: string): Promise<void> {
|
|
|
|
return rm(path, { recursive: true, force: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function gracefulRmRecursive(
|
2023-03-02 17:57:36 +00:00
|
|
|
logger: LoggerType,
|
|
|
|
path: string
|
|
|
|
): Promise<void> {
|
|
|
|
return doGracefulFSOperation({
|
2024-11-14 01:20:36 +00:00
|
|
|
name: 'rmRecursive',
|
|
|
|
operation: rmRecursive,
|
2023-03-02 17:57:36 +00:00
|
|
|
args: [path],
|
2022-03-28 19:05:44 +00:00
|
|
|
logger,
|
|
|
|
startedAt: Date.now(),
|
|
|
|
retryCount: 0,
|
|
|
|
});
|
|
|
|
}
|