|
@@ -0,0 +1,68 @@
|
|
1
|
+const fs = require("./fs_promises");
|
|
2
|
+const path = require('path');
|
|
3
|
+let os = require('os');
|
|
4
|
+
|
|
5
|
+const { R_OK, W_OK } = fs.constants;
|
|
6
|
+
|
|
7
|
+/**
|
|
8
|
+ * Checks that the output directory is writeable
|
|
9
|
+ * @param {string} directory
|
|
10
|
+ * @returns {string?}
|
|
11
|
+ */
|
|
12
|
+async function get_error(directory) {
|
|
13
|
+ if (!fs.existsSync(directory)) {
|
|
14
|
+ return "does not exist";
|
|
15
|
+ }
|
|
16
|
+ if (!fs.statSync(directory).isDirectory()) {
|
|
17
|
+ error = "exists, but is not a directory";
|
|
18
|
+ }
|
|
19
|
+ const { uid, gid } = os.userInfo();
|
|
20
|
+ const tmpfile = path.join(directory, Math.random() + ".json");
|
|
21
|
+ try {
|
|
22
|
+ fs.writeFileSync(tmpfile, "{}");
|
|
23
|
+ fs.unlinkSync(tmpfile);
|
|
24
|
+ } catch (e) {
|
|
25
|
+ return "does not allow file creation and deletion. " +
|
|
26
|
+ "Check the permissions of the directory, and if needed change them so that " +
|
|
27
|
+ `user with UID ${uid} has access to them. This can be achieved by running the command: chown ${uid}:${gid} on the directory`;
|
|
28
|
+ }
|
|
29
|
+ const fileChecks = [];
|
|
30
|
+ const dir = fs.opendirSync(directory);
|
|
31
|
+ while (true) {
|
|
32
|
+ const elem = await dir.read();
|
|
33
|
+ if (!elem) break;
|
|
34
|
+ if (/^board-(.*)\.json$/.test(elem.name)) {
|
|
35
|
+ const elemPath = path.join(directory, elem.name);
|
|
36
|
+ if (!elem.isFile()) return `contains a board file named "${elemPath}" which is not a normal file`
|
|
37
|
+ fileChecks.push(fs.promises.access(elemPath, R_OK | W_OK)
|
|
38
|
+ .catch(function () { return elemPath }))
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+ dir.closeSync();
|
|
42
|
+ const errs = (await Promise.all(fileChecks)).filter(function (x) { return x });
|
|
43
|
+ if (errs.length > 0) {
|
|
44
|
+ return `contains the following board files that are not readable and writable by the current user: "` +
|
|
45
|
+ errs.join('", "') +
|
|
46
|
+ `". Please make all board files accessible with chown 1000:1000.`
|
|
47
|
+ }
|
|
48
|
+}
|
|
49
|
+
|
|
50
|
+/**
|
|
51
|
+ * Checks that the output directory is writeable,
|
|
52
|
+ * ans exits the current process with an error otherwise.
|
|
53
|
+ * @param {string} directory
|
|
54
|
+ */
|
|
55
|
+function check_output_directory(directory) {
|
|
56
|
+ get_error(directory).then(function (error) {
|
|
57
|
+ if (error) {
|
|
58
|
+ console.error(
|
|
59
|
+ `The configured history directory in which boards are stored ${error}.` +
|
|
60
|
+ `\nThe history directory can be configured with the environment variable HISTORY_DIR. ` +
|
|
61
|
+ `It is currently set to "${directory}".`
|
|
62
|
+ );
|
|
63
|
+ process.exit(1);
|
|
64
|
+ }
|
|
65
|
+ })
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+module.exports = check_output_directory
|