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/backend/files/delete-files.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { getHome } from "./util";
7
import { deleted_file_variations } from "@cocalc/util/delete-files";
8
import { rimraf } from "rimraf";
9
import { join } from "path";
10
import getLogger from "@cocalc/backend/logger";
11
12
const log = getLogger("delete-files");
13
14
// Delete the files/directories in the given project with the given list of paths.
15
export async function delete_files(
16
paths: string[],
17
home?: string,
18
): Promise<void> {
19
const HOME = getHome(home);
20
log.debug({ paths, HOME });
21
paths = paths.map((x) => (x.startsWith("/") ? x : join(HOME, x)));
22
// Add in all the hidden variants.
23
let extra: string[] = [];
24
for (const path of paths) {
25
for (const variation of deleted_file_variations(path)) {
26
extra.push(variation);
27
}
28
}
29
// Actually delete the files and directories and any hidden variants.
30
// This is just simply deleting the files from disk. It will get noticed
31
// by browser clients, etc. We could do stuff that is way more clever here
32
// involving the listings table... but:
33
// - that results in weird race conditions that can make files immediately
34
// reappear after deletion
35
// - we MUST make deletion detection fully work based entirely on what happens
36
// on the file system, e.g., due to git checkout and people using the terminal
37
log.debug("extra = ", extra);
38
await rimraf(paths.concat(extra), { maxRetries: 2 });
39
}
40
41