Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/file-server/btrfs/util.ts
1539 views
1
import {
2
type ExecuteCodeOptions,
3
type ExecuteCodeOutput,
4
} from "@cocalc/util/types/execute-code";
5
import { executeCode } from "@cocalc/backend/execute-code";
6
import getLogger from "@cocalc/backend/logger";
7
import { stat } from "node:fs/promises";
8
9
const logger = getLogger("file-server:storage:util");
10
11
const DEFAULT_EXEC_TIMEOUT_MS = 60 * 1000;
12
13
export async function mkdirp(paths: string[]) {
14
if (paths.length == 0) return;
15
await sudo({ command: "mkdir", args: ["-p", ...paths] });
16
}
17
18
export async function sudo(
19
opts: ExecuteCodeOptions & { desc?: string },
20
): Promise<ExecuteCodeOutput> {
21
if (opts.verbose !== false && opts.desc) {
22
logger.debug("exec", opts.desc);
23
}
24
let command, args;
25
if (opts.bash) {
26
command = `sudo ${opts.command}`;
27
args = undefined;
28
} else {
29
command = "sudo";
30
args = [opts.command, ...(opts.args ?? [])];
31
}
32
return await executeCode({
33
verbose: true,
34
timeout: DEFAULT_EXEC_TIMEOUT_MS / 1000,
35
...opts,
36
command,
37
args,
38
});
39
}
40
41
export async function btrfs(
42
opts: Partial<ExecuteCodeOptions & { desc?: string }>,
43
) {
44
return await sudo({ ...opts, command: "btrfs" });
45
}
46
47
export async function isdir(path: string) {
48
return (await stat(path)).isDirectory();
49
}
50
51
export function parseBupTime(s: string): Date {
52
const [year, month, day, time] = s.split("-");
53
const hours = time.slice(0, 2);
54
const minutes = time.slice(2, 4);
55
const seconds = time.slice(4, 6);
56
57
return new Date(
58
Number(year),
59
Number(month) - 1, // JS months are 0-based
60
Number(day),
61
Number(hours),
62
Number(minutes),
63
Number(seconds),
64
);
65
}
66
67