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/browser-websocket/compute-filesystem-cache.ts
Views: 687
/*1This is used by src/compute/compute/lib/filesystem-cache.2*/34import { join } from "path";5import { open, rm, stat } from "fs/promises";6import { exists } from "@cocalc/backend/misc/async-utils-node";7import type { ComputeFilesystemOptions } from "@cocalc/comm/websocket/types";89export default async function computeFilesystemCache(10opts: ComputeFilesystemOptions,11) {12const { func } = opts;13switch (func) {14case "filesToDelete":15return await filesToDelete(opts.allComputeFiles);16case "deleteWhiteouts":17return await deleteWhiteouts(opts.whiteouts);18default:19throw Error(`unknown command ${func}`);20}21}2223// Given a path to a file that contains a list (one per line)24// of the files and on the compute server that are sync'd,25// return the ones that should be deleted from the compute server,26// because they were deleted locally.27async function filesToDelete(allComputeFiles: string) {28if (typeof allComputeFiles != "string") {29throw Error(30"filesToDelete takes the path to list of all files in the compute server as input",31);32}33if (!process.env.HOME) {34throw Error("HOME must be defined");35}36const file = await open(join(process.env.HOME, allComputeFiles));37const toDelete: string[] = [];38for await (const path of file.readLines()) {39const abspath = join(process.env.HOME, path);40if (!(await exists(abspath))) {41toDelete.push(path);42}43}44await file.close();45return toDelete;46}4748async function deleteWhiteouts(whiteouts: { [path: string]: number }) {49if (!process.env.HOME) {50throw Error("HOME must be defined");51}52for (const path in whiteouts) {53try {54const abspath = join(process.env.HOME, path);55// if file already gone, this throws, which is fine56const { ctimeMs } = await stat(abspath);57if (ctimeMs >= whiteouts[path]) {58// file changed in the project *after* the delete, so don't delete.59continue;60}61await rm(join(process.env.HOME, path), { recursive: true });62} catch (_) {}63}64}656667