fattr.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const fs = require("./fileSystem").require();
  2. const pth = require("path");
  3. fs.existsSync = fs.existsSync || pth.existsSync;
  4. module.exports = function (/*String*/ path) {
  5. var _path = path || "",
  6. _obj = newAttr(),
  7. _stat = null;
  8. function newAttr() {
  9. return {
  10. directory: false,
  11. readonly: false,
  12. hidden: false,
  13. executable: false,
  14. mtime: 0,
  15. atime: 0
  16. };
  17. }
  18. if (_path && fs.existsSync(_path)) {
  19. _stat = fs.statSync(_path);
  20. _obj.directory = _stat.isDirectory();
  21. _obj.mtime = _stat.mtime;
  22. _obj.atime = _stat.atime;
  23. _obj.executable = (0o111 & _stat.mode) !== 0; // file is executable who ever har right not just owner
  24. _obj.readonly = (0o200 & _stat.mode) === 0; // readonly if owner has no write right
  25. _obj.hidden = pth.basename(_path)[0] === ".";
  26. } else {
  27. console.warn("Invalid path: " + _path);
  28. }
  29. return {
  30. get directory() {
  31. return _obj.directory;
  32. },
  33. get readOnly() {
  34. return _obj.readonly;
  35. },
  36. get hidden() {
  37. return _obj.hidden;
  38. },
  39. get mtime() {
  40. return _obj.mtime;
  41. },
  42. get atime() {
  43. return _obj.atime;
  44. },
  45. get executable() {
  46. return _obj.executable;
  47. },
  48. decodeAttributes: function () {},
  49. encodeAttributes: function () {},
  50. toJSON: function () {
  51. return {
  52. path: _path,
  53. isDirectory: _obj.directory,
  54. isReadOnly: _obj.readonly,
  55. isHidden: _obj.hidden,
  56. isExecutable: _obj.executable,
  57. mTime: _obj.mtime,
  58. aTime: _obj.atime
  59. };
  60. },
  61. toString: function () {
  62. return JSON.stringify(this.toJSON(), null, "\t");
  63. }
  64. };
  65. };