File.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.File = void 0;
  4. const constants_1 = require("../constants");
  5. const { O_APPEND } = constants_1.constants;
  6. /**
  7. * Represents an open file (file descriptor) that points to a `Link` (Hard-link) and a `Node`.
  8. *
  9. * @todo Rename to `OpenFile`.
  10. */
  11. class File {
  12. /**
  13. * Open a Link-Node pair. `node` is provided separately as that might be a different node
  14. * rather the one `link` points to, because it might be a symlink.
  15. * @param link
  16. * @param node
  17. * @param flags
  18. * @param fd
  19. */
  20. constructor(link, node, flags, fd) {
  21. this.link = link;
  22. this.node = node;
  23. this.flags = flags;
  24. this.fd = fd;
  25. this.position = 0;
  26. if (this.flags & O_APPEND)
  27. this.position = this.getSize();
  28. }
  29. getString(encoding = 'utf8') {
  30. return this.node.getString();
  31. }
  32. setString(str) {
  33. this.node.setString(str);
  34. }
  35. getBuffer() {
  36. return this.node.getBuffer();
  37. }
  38. setBuffer(buf) {
  39. this.node.setBuffer(buf);
  40. }
  41. getSize() {
  42. return this.node.getSize();
  43. }
  44. truncate(len) {
  45. this.node.truncate(len);
  46. }
  47. seekTo(position) {
  48. this.position = position;
  49. }
  50. write(buf, offset = 0, length = buf.length, position) {
  51. if (typeof position !== 'number')
  52. position = this.position;
  53. const bytes = this.node.write(buf, offset, length, position);
  54. this.position = position + bytes;
  55. return bytes;
  56. }
  57. read(buf, offset = 0, length = buf.byteLength, position) {
  58. if (typeof position !== 'number')
  59. position = this.position;
  60. const bytes = this.node.read(buf, offset, length, position);
  61. this.position = position + bytes;
  62. return bytes;
  63. }
  64. chmod(perm) {
  65. this.node.chmod(perm);
  66. }
  67. chown(uid, gid) {
  68. this.node.chown(uid, gid);
  69. }
  70. }
  71. exports.File = File;
  72. //# sourceMappingURL=File.js.map