浏览代码

Check the permissions on the history dir on start

Fixes https://github.com/lovasoa/whitebophir/issues/164
dev_h
ophir 4 年前
父节点
当前提交
8b161e9db5
共有 4 个文件被更改,包括 75 次插入4 次删除
  1. 1
    1
      package-lock.json
  2. 1
    1
      package.json
  3. 68
    0
      server/check_output_directory.js
  4. 5
    2
      server/server.js

+ 1
- 1
package-lock.json 查看文件

@@ -1,6 +1,6 @@
1 1
 {
2 2
    "name": "whitebophir",
3
-   "version": "1.6.5",
3
+   "version": "1.7.1",
4 4
    "lockfileVersion": 1,
5 5
    "requires": true,
6 6
    "dependencies": {

+ 1
- 1
package.json 查看文件

@@ -1,7 +1,7 @@
1 1
 {
2 2
   "name": "whitebophir",
3 3
   "description": "Online collaborative whiteboard",
4
-  "version": "1.7.0",
4
+  "version": "1.7.1",
5 5
   "keywords": [
6 6
     "collaborative",
7 7
     "whiteboard"

+ 68
- 0
server/check_output_directory.js 查看文件

@@ -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

+ 5
- 2
server/server.js 查看文件

@@ -9,7 +9,8 @@ var app = require('http').createServer(handler)
9 9
 	, createSVG = require("./createSVG.js")
10 10
 	, templating = require("./templating.js")
11 11
 	, config = require("./configuration.js")
12
-	, polyfillLibrary = require('polyfill-library');
12
+	, polyfillLibrary = require('polyfill-library')
13
+	, check_output_directory = require("./check_output_directory.js");
13 14
 
14 15
 
15 16
 var MIN_NODE_VERSION = 8.0;
@@ -20,7 +21,9 @@ if (parseFloat(process.versions.node) < MIN_NODE_VERSION) {
20 21
 		", wbo requires at least " + MIN_NODE_VERSION + " !!!");
21 22
 }
22 23
 
23
-var io = sockets.start(app);
24
+check_output_directory(config.HISTORY_DIR);
25
+
26
+sockets.start(app);
24 27
 
25 28
 app.listen(config.PORT, config.HOST);
26 29
 log("server started", { port: config.PORT });

正在加载...
取消
保存