Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/dedicated-disks.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6This makes dedicated disks conveniently available from the $HOME directory – a kucalc-only functionality.7*/89import { ROOT, HOME_PREFIX } from "@cocalc/util/consts/dedicated";10import { constants as fs_constants, promises as fs } from "fs";11import { isArray } from "lodash";12import { homedir } from "os";13import { join } from "path";14import { getLogger } from "./logger";15import { getProjectConfig } from "./project-setup";1617const { F_OK, W_OK, R_OK } = fs_constants;1819const { info, warn } = getLogger("dedicated-disks");2021async function ensureSymlink(name: string): Promise<boolean> {22const disk = join(ROOT, name);23const link = join(homedir(), HOME_PREFIX);24try {25await fs.access(disk, F_OK | R_OK | W_OK);26} catch {27warn(`disk directory ${disk} not writeable -- abort`);28return false;29}30// create a symlink to the /local directory31// don't disturb what's already in $HOME32try {33await fs.access(link, F_OK);34info(`'${link}' already exists`);35} catch {36// link does not exist, hence we create it37try {38await fs.symlink(ROOT, link);39info(`successfully symlinked ${link} → ${disk}`);40} catch (err) {41warn(`problem symlinking ${link} → ${disk} -- ${err}`);42}43}44// even if there is a problem, it makes no sense to try again45return true;46}4748export async function init() {49info("initializing");50const conf = getProjectConfig();51// we're a bit extra careful, because there could be anything in the DB52if (conf?.quota?.dedicated_disks == null) return;53if (!isArray(conf.quota.dedicated_disks)) return;54for (const disk of conf.quota.dedicated_disks) {55if (typeof disk.name === "string") {56// if there is a disk, a symlink is made to point to the directory where it is57// hence it is enough to link to it once58if (await ensureSymlink(disk.name)) {59return;60}61}62}63}646566