CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/dedicated-disks.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
This makes dedicated disks conveniently available from the $HOME directory – a kucalc-only functionality.
8
*/
9
10
import { ROOT, HOME_PREFIX } from "@cocalc/util/consts/dedicated";
11
import { constants as fs_constants, promises as fs } from "fs";
12
import { isArray } from "lodash";
13
import { homedir } from "os";
14
import { join } from "path";
15
import { getLogger } from "./logger";
16
import { getProjectConfig } from "./project-setup";
17
18
const { F_OK, W_OK, R_OK } = fs_constants;
19
20
const { info, warn } = getLogger("dedicated-disks");
21
22
async function ensureSymlink(name: string): Promise<boolean> {
23
const disk = join(ROOT, name);
24
const link = join(homedir(), HOME_PREFIX);
25
try {
26
await fs.access(disk, F_OK | R_OK | W_OK);
27
} catch {
28
warn(`disk directory ${disk} not writeable -- abort`);
29
return false;
30
}
31
// create a symlink to the /local directory
32
// don't disturb what's already in $HOME
33
try {
34
await fs.access(link, F_OK);
35
info(`'${link}' already exists`);
36
} catch {
37
// link does not exist, hence we create it
38
try {
39
await fs.symlink(ROOT, link);
40
info(`successfully symlinked ${link} → ${disk}`);
41
} catch (err) {
42
warn(`problem symlinking ${link} → ${disk} -- ${err}`);
43
}
44
}
45
// even if there is a problem, it makes no sense to try again
46
return true;
47
}
48
49
export async function init() {
50
info("initializing");
51
const conf = getProjectConfig();
52
// we're a bit extra careful, because there could be anything in the DB
53
if (conf?.quota?.dedicated_disks == null) return;
54
if (!isArray(conf.quota.dedicated_disks)) return;
55
for (const disk of conf.quota.dedicated_disks) {
56
if (typeof disk.name === "string") {
57
// if there is a disk, a symlink is made to point to the directory where it is
58
// hence it is enough to link to it once
59
if (await ensureSymlink(disk.name)) {
60
return;
61
}
62
}
63
}
64
}
65
66