您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

check_output_directory.js 2.3KB

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