Node.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Node = void 0;
  4. const fanout_1 = require("thingies/lib/fanout");
  5. const process_1 = require("./process");
  6. const buffer_1 = require("@jsonjoy.com/fs-node-builtins/lib/internal/buffer");
  7. const fs_node_utils_1 = require("@jsonjoy.com/fs-node-utils");
  8. const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, S_IFCHR } = fs_node_utils_1.constants;
  9. const getuid = () => process_1.default.getuid?.() ?? 0;
  10. const getgid = () => process_1.default.getgid?.() ?? 0;
  11. const EMPTY_BUFFER = (0, buffer_1.bufferAllocUnsafe)(0);
  12. /**
  13. * Node in a file system (like i-node, v-node).
  14. */
  15. class Node {
  16. constructor(ino, mode = 0o666) {
  17. this.changes = new fanout_1.FanOut();
  18. // User ID and group ID.
  19. this._uid = getuid();
  20. this._gid = getgid();
  21. this._atime = new Date();
  22. this._mtime = new Date();
  23. this._ctime = new Date();
  24. this.buf = EMPTY_BUFFER;
  25. /** Total allocated memory capacity for this node. */
  26. this.capacity = 0;
  27. /** Actually used bytes to store content. */
  28. this.size = 0;
  29. this.rdev = 0;
  30. // Number of hard links pointing at this Node.
  31. this._nlink = 1;
  32. this.mode = mode;
  33. this.ino = ino;
  34. }
  35. set ctime(ctime) {
  36. this._ctime = ctime;
  37. }
  38. get ctime() {
  39. return this._ctime;
  40. }
  41. set uid(uid) {
  42. this._uid = uid;
  43. this.ctime = new Date();
  44. }
  45. get uid() {
  46. return this._uid;
  47. }
  48. set gid(gid) {
  49. this._gid = gid;
  50. this.ctime = new Date();
  51. }
  52. get gid() {
  53. return this._gid;
  54. }
  55. set atime(atime) {
  56. this._atime = atime;
  57. }
  58. get atime() {
  59. return this._atime;
  60. }
  61. set mtime(mtime) {
  62. this._mtime = mtime;
  63. this.ctime = new Date();
  64. }
  65. get mtime() {
  66. return this._mtime;
  67. }
  68. get perm() {
  69. return this.mode & ~S_IFMT;
  70. }
  71. set perm(perm) {
  72. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  73. this.ctime = new Date();
  74. }
  75. set nlink(nlink) {
  76. this._nlink = nlink;
  77. this.ctime = new Date();
  78. }
  79. get nlink() {
  80. return this._nlink;
  81. }
  82. getString(encoding = 'utf8') {
  83. this.atime = new Date();
  84. return this.getBuffer().toString(encoding);
  85. }
  86. setString(str) {
  87. this._setBuf((0, buffer_1.bufferFrom)(str, 'utf8'));
  88. }
  89. getBuffer() {
  90. this.atime = new Date();
  91. if (!this.buf)
  92. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  93. return (0, buffer_1.bufferFrom)(this.buf.subarray(0, this.size)); // Return a copy of used portion.
  94. }
  95. setBuffer(buf) {
  96. const copy = (0, buffer_1.bufferFrom)(buf); // Creates a copy of data.
  97. this._setBuf(copy);
  98. }
  99. _setBuf(buf) {
  100. const size = buf.length;
  101. this.buf = buf;
  102. this.capacity = size;
  103. this.size = size;
  104. this.touch();
  105. }
  106. getSize() {
  107. return this.size;
  108. }
  109. setModeProperty(property) {
  110. this.mode = property;
  111. }
  112. isFile() {
  113. return (this.mode & S_IFMT) === S_IFREG;
  114. }
  115. isDirectory() {
  116. return (this.mode & S_IFMT) === S_IFDIR;
  117. }
  118. isSymlink() {
  119. // return !!this.symlink;
  120. return (this.mode & S_IFMT) === S_IFLNK;
  121. }
  122. isCharacterDevice() {
  123. return (this.mode & S_IFMT) === S_IFCHR;
  124. }
  125. makeSymlink(symlink) {
  126. this.mode = S_IFLNK | 0o666;
  127. this.symlink = symlink;
  128. }
  129. write(buf, off = 0, len = buf.length, pos = 0) {
  130. const bufLength = buf.length;
  131. if (off + len > bufLength)
  132. len = bufLength - off;
  133. if (len <= 0)
  134. return 0;
  135. const requiredSize = pos + len;
  136. if (requiredSize > this.capacity) {
  137. let newCapacity = Math.max(this.capacity * 2, 64);
  138. while (newCapacity < requiredSize)
  139. newCapacity *= 2;
  140. const newBuf = (0, buffer_1.bufferAllocUnsafe)(newCapacity);
  141. if (this.size > 0)
  142. this.buf.copy(newBuf, 0, 0, this.size);
  143. this.buf = newBuf;
  144. this.capacity = newCapacity;
  145. }
  146. if (pos > this.size)
  147. this.buf.fill(0, this.size, pos);
  148. buf.copy(this.buf, pos, off, off + len);
  149. if (requiredSize > this.size)
  150. this.size = requiredSize;
  151. this.touch();
  152. return len;
  153. }
  154. /**
  155. * Read data from the file.
  156. *
  157. * @param buf Buffer to read data into.
  158. * @param off Offset int the `buf` where to start writing data.
  159. * @param len How many bytes to read. Equals to `buf.byteLength` by default.
  160. * @param pos Position offset in file where to start reading. Defaults to `0`.
  161. * @returns Returns the number of bytes read.
  162. */
  163. read(buf, off = 0, len = buf.byteLength, pos = 0) {
  164. this.atime = new Date();
  165. if (pos >= this.size)
  166. return 0;
  167. let actualLen = len;
  168. if (actualLen > buf.byteLength)
  169. actualLen = buf.byteLength;
  170. if (actualLen + pos > this.size)
  171. actualLen = this.size - pos;
  172. if (actualLen <= 0)
  173. return 0;
  174. const buf2 = buf instanceof buffer_1.Buffer ? buf : buffer_1.Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  175. this.buf.copy(buf2, off, pos, pos + actualLen);
  176. return actualLen;
  177. }
  178. truncate(len = 0) {
  179. if (!len) {
  180. this.buf = EMPTY_BUFFER;
  181. this.capacity = 0;
  182. this.size = 0;
  183. this.touch();
  184. return;
  185. }
  186. if (len <= this.size)
  187. this.size = len;
  188. else {
  189. if (len > this.capacity) {
  190. let newCapacity = Math.max(this.capacity * 2, 64);
  191. while (newCapacity < len)
  192. newCapacity *= 2;
  193. const buf = (0, buffer_1.bufferAllocUnsafe)(newCapacity);
  194. if (this.size > 0)
  195. this.buf.copy(buf, 0, 0, this.size);
  196. buf.fill(0, this.size, len);
  197. this.buf = buf;
  198. this.capacity = newCapacity;
  199. }
  200. else
  201. this.buf.fill(0, this.size, len);
  202. this.size = len;
  203. }
  204. this.touch();
  205. }
  206. chmod(perm) {
  207. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  208. this.touch();
  209. }
  210. chown(uid, gid) {
  211. this.uid = uid;
  212. this.gid = gid;
  213. this.touch();
  214. }
  215. touch() {
  216. this.mtime = new Date();
  217. this.changes.emit(['modify']);
  218. }
  219. canRead(uid = getuid(), gid = getgid()) {
  220. if (this.perm & 4 /* S.IROTH */) {
  221. return true;
  222. }
  223. if (gid === this.gid) {
  224. if (this.perm & 32 /* S.IRGRP */) {
  225. return true;
  226. }
  227. }
  228. if (uid === this.uid) {
  229. if (this.perm & 256 /* S.IRUSR */) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. canWrite(uid = getuid(), gid = getgid()) {
  236. if (this.perm & 2 /* S.IWOTH */) {
  237. return true;
  238. }
  239. if (gid === this.gid) {
  240. if (this.perm & 16 /* S.IWGRP */) {
  241. return true;
  242. }
  243. }
  244. if (uid === this.uid) {
  245. if (this.perm & 128 /* S.IWUSR */) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. canExecute(uid = getuid(), gid = getgid()) {
  252. if (this.perm & 1 /* S.IXOTH */) {
  253. return true;
  254. }
  255. if (gid === this.gid) {
  256. if (this.perm & 8 /* S.IXGRP */) {
  257. return true;
  258. }
  259. }
  260. if (uid === this.uid) {
  261. if (this.perm & 64 /* S.IXUSR */) {
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. del() {
  268. this.changes.emit(['delete']);
  269. }
  270. toJSON() {
  271. return {
  272. ino: this.ino,
  273. uid: this.uid,
  274. gid: this.gid,
  275. atime: this.atime.getTime(),
  276. mtime: this.mtime.getTime(),
  277. ctime: this.ctime.getTime(),
  278. perm: this.perm,
  279. mode: this.mode,
  280. nlink: this.nlink,
  281. symlink: this.symlink,
  282. data: this.getString(),
  283. };
  284. }
  285. }
  286. exports.Node = Node;
  287. //# sourceMappingURL=Node.js.map