Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/file-server/btrfs/subvolume-fs.ts
1539 views
1
import {
2
appendFile,
3
chmod,
4
cp,
5
copyFile,
6
link,
7
readFile,
8
realpath,
9
rename,
10
rm,
11
rmdir,
12
mkdir,
13
stat,
14
symlink,
15
truncate,
16
writeFile,
17
unlink,
18
utimes,
19
watch,
20
} from "node:fs/promises";
21
import { exists } from "@cocalc/backend/misc/async-utils-node";
22
import { type DirectoryListingEntry } from "@cocalc/util/types";
23
import getListing from "@cocalc/backend/get-listing";
24
import { type Subvolume } from "./subvolume";
25
import { isdir, sudo } from "./util";
26
27
export class SubvolumeFilesystem {
28
constructor(private subvolume: Subvolume) {}
29
30
private normalize = this.subvolume.normalize;
31
32
ls = async (
33
path: string,
34
{ hidden, limit }: { hidden?: boolean; limit?: number } = {},
35
): Promise<DirectoryListingEntry[]> => {
36
return await getListing(this.normalize(path), hidden, {
37
limit,
38
home: "/",
39
});
40
};
41
42
readFile = async (path: string, encoding?: any): Promise<string | Buffer> => {
43
return await readFile(this.normalize(path), encoding);
44
};
45
46
writeFile = async (path: string, data: string | Buffer) => {
47
return await writeFile(this.normalize(path), data);
48
};
49
50
appendFile = async (path: string, data: string | Buffer, encoding?) => {
51
return await appendFile(this.normalize(path), data, encoding);
52
};
53
54
unlink = async (path: string) => {
55
await unlink(this.normalize(path));
56
};
57
58
stat = async (path: string) => {
59
return await stat(this.normalize(path));
60
};
61
62
exists = async (path: string) => {
63
return await exists(this.normalize(path));
64
};
65
66
// hard link
67
link = async (existingPath: string, newPath: string) => {
68
return await link(this.normalize(existingPath), this.normalize(newPath));
69
};
70
71
symlink = async (target: string, path: string) => {
72
return await symlink(this.normalize(target), this.normalize(path));
73
};
74
75
realpath = async (path: string) => {
76
const x = await realpath(this.normalize(path));
77
return x.slice(this.subvolume.path.length + 1);
78
};
79
80
rename = async (oldPath: string, newPath: string) => {
81
await rename(this.normalize(oldPath), this.normalize(newPath));
82
};
83
84
utimes = async (
85
path: string,
86
atime: number | string | Date,
87
mtime: number | string | Date,
88
) => {
89
await utimes(this.normalize(path), atime, mtime);
90
};
91
92
watch = (filename: string, options?) => {
93
return watch(this.normalize(filename), options);
94
};
95
96
truncate = async (path: string, len?: number) => {
97
await truncate(this.normalize(path), len);
98
};
99
100
copyFile = async (src: string, dest: string) => {
101
await copyFile(this.normalize(src), this.normalize(dest));
102
};
103
104
cp = async (src: string, dest: string, options?) => {
105
await cp(this.normalize(src), this.normalize(dest), options);
106
};
107
108
chmod = async (path: string, mode: string | number) => {
109
await chmod(this.normalize(path), mode);
110
};
111
112
mkdir = async (path: string, options?) => {
113
await mkdir(this.normalize(path), options);
114
};
115
116
rsync = async ({
117
src,
118
target,
119
args = ["-axH"],
120
timeout = 5 * 60 * 1000,
121
}: {
122
src: string;
123
target: string;
124
args?: string[];
125
timeout?: number;
126
}): Promise<{ stdout: string; stderr: string; exit_code: number }> => {
127
let srcPath = this.normalize(src);
128
let targetPath = this.normalize(target);
129
if (!srcPath.endsWith("/") && (await isdir(srcPath))) {
130
srcPath += "/";
131
if (!targetPath.endsWith("/")) {
132
targetPath += "/";
133
}
134
}
135
return await sudo({
136
command: "rsync",
137
args: [...args, srcPath, targetPath],
138
err_on_exit: false,
139
timeout: timeout / 1000,
140
});
141
};
142
143
rmdir = async (path: string, options?) => {
144
await rmdir(this.normalize(path), options);
145
};
146
147
rm = async (path: string, options?) => {
148
await rm(this.normalize(path), options);
149
};
150
}
151
152