Node.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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, uid = getuid(), gid = getgid()) {
  17. /**
  18. * @deprecated Subscribe to the volume-level `Superblock.changes` bus or use
  19. * `CoreWatcher` instead.
  20. */
  21. this.changes = new fanout_1.FanOut();
  22. // User ID and group ID.
  23. this._uid = getuid();
  24. this._gid = getgid();
  25. this._atime = new Date();
  26. this._mtime = new Date();
  27. this._ctime = new Date();
  28. this._btime = new Date();
  29. this.buf = EMPTY_BUFFER;
  30. /** Total allocated memory capacity for this node. */
  31. this.capacity = 0;
  32. /** Actually used bytes to store content. */
  33. this.size = 0;
  34. this.rdev = 0;
  35. // Number of hard links pointing at this Node.
  36. this._nlink = 1;
  37. this.mode = mode;
  38. this.ino = ino;
  39. this._uid = uid;
  40. this._gid = gid;
  41. }
  42. set ctime(ctime) {
  43. this._ctime = ctime;
  44. }
  45. get ctime() {
  46. return this._ctime;
  47. }
  48. /**
  49. * Birth (creation) time of this node. Unlike `ctime`, it is set once when the
  50. * node is created and is never advanced by writes or metadata changes. The
  51. * setter exists only for restore paths, like snapshot deserialization.
  52. */
  53. set btime(btime) {
  54. this._btime = btime;
  55. }
  56. get btime() {
  57. return this._btime;
  58. }
  59. set uid(uid) {
  60. this._uid = uid;
  61. this.ctime = new Date();
  62. }
  63. get uid() {
  64. return this._uid;
  65. }
  66. set gid(gid) {
  67. this._gid = gid;
  68. this.ctime = new Date();
  69. }
  70. get gid() {
  71. return this._gid;
  72. }
  73. set atime(atime) {
  74. this._atime = atime;
  75. }
  76. get atime() {
  77. return this._atime;
  78. }
  79. set mtime(mtime) {
  80. this._mtime = mtime;
  81. this.ctime = new Date();
  82. }
  83. get mtime() {
  84. return this._mtime;
  85. }
  86. get perm() {
  87. return this.mode & ~S_IFMT;
  88. }
  89. set perm(perm) {
  90. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  91. this.ctime = new Date();
  92. }
  93. set nlink(nlink) {
  94. this._nlink = nlink;
  95. this.ctime = new Date();
  96. }
  97. get nlink() {
  98. return this._nlink;
  99. }
  100. getString(encoding = 'utf8') {
  101. this.atime = new Date();
  102. return this.getBuffer().toString(encoding);
  103. }
  104. setString(str) {
  105. this._setBuf((0, buffer_1.bufferFrom)(str, 'utf8'));
  106. }
  107. getBuffer() {
  108. this.atime = new Date();
  109. if (!this.buf)
  110. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  111. return (0, buffer_1.bufferFrom)(this.buf.subarray(0, this.size)); // Return a copy of used portion.
  112. }
  113. setBuffer(buf) {
  114. const copy = (0, buffer_1.bufferFrom)(buf); // Creates a copy of data.
  115. this._setBuf(copy);
  116. }
  117. _setBuf(buf) {
  118. const size = buf.length;
  119. this.buf = buf;
  120. this.capacity = size;
  121. this.size = size;
  122. this.touch();
  123. }
  124. getSize() {
  125. return this.size;
  126. }
  127. setModeProperty(property) {
  128. this.mode = property;
  129. }
  130. isFile() {
  131. return (this.mode & S_IFMT) === S_IFREG;
  132. }
  133. isDirectory() {
  134. return (this.mode & S_IFMT) === S_IFDIR;
  135. }
  136. isSymlink() {
  137. // return !!this.symlink;
  138. return (this.mode & S_IFMT) === S_IFLNK;
  139. }
  140. isCharacterDevice() {
  141. return (this.mode & S_IFMT) === S_IFCHR;
  142. }
  143. makeSymlink(symlink) {
  144. this.mode = S_IFLNK | 0o666;
  145. this.symlink = symlink;
  146. }
  147. write(buf, off = 0, len = buf.length, pos = 0) {
  148. const bufLength = buf.length;
  149. if (off + len > bufLength)
  150. len = bufLength - off;
  151. if (len <= 0)
  152. return 0;
  153. const requiredSize = pos + len;
  154. if (requiredSize > this.capacity) {
  155. let newCapacity = Math.max(this.capacity * 2, 64);
  156. while (newCapacity < requiredSize)
  157. newCapacity *= 2;
  158. const newBuf = (0, buffer_1.bufferAllocUnsafe)(newCapacity);
  159. if (this.size > 0)
  160. this.buf.copy(newBuf, 0, 0, this.size);
  161. this.buf = newBuf;
  162. this.capacity = newCapacity;
  163. }
  164. if (pos > this.size)
  165. this.buf.fill(0, this.size, pos);
  166. buf.copy(this.buf, pos, off, off + len);
  167. if (requiredSize > this.size)
  168. this.size = requiredSize;
  169. this.touch();
  170. return len;
  171. }
  172. /**
  173. * Read data from the file.
  174. *
  175. * @param buf Buffer to read data into.
  176. * @param off Offset int the `buf` where to start writing data.
  177. * @param len How many bytes to read. Equals to `buf.byteLength` by default.
  178. * @param pos Position offset in file where to start reading. Defaults to `0`.
  179. * @returns Returns the number of bytes read.
  180. */
  181. read(buf, off = 0, len = buf.byteLength, pos = 0) {
  182. this.atime = new Date();
  183. if (pos >= this.size)
  184. return 0;
  185. let actualLen = len;
  186. if (actualLen > buf.byteLength)
  187. actualLen = buf.byteLength;
  188. if (actualLen + pos > this.size)
  189. actualLen = this.size - pos;
  190. if (actualLen <= 0)
  191. return 0;
  192. const buf2 = buf instanceof buffer_1.Buffer ? buf : buffer_1.Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  193. this.buf.copy(buf2, off, pos, pos + actualLen);
  194. return actualLen;
  195. }
  196. truncate(len = 0) {
  197. // A negative length is clamped to zero, matching Node.js `fs.truncate`,
  198. // which truncates to an empty file rather than producing a negative size.
  199. // Without this, `this.size` becomes negative and `getBuffer()`'s
  200. // `subarray(0, this.size)` reads past the used region, exposing
  201. // uninitialized heap memory from the unsafe-allocated backing buffer.
  202. if (len <= 0) {
  203. this.buf = EMPTY_BUFFER;
  204. this.capacity = 0;
  205. this.size = 0;
  206. this.touch();
  207. return;
  208. }
  209. if (len <= this.size)
  210. this.size = len;
  211. else {
  212. if (len > this.capacity) {
  213. let newCapacity = Math.max(this.capacity * 2, 64);
  214. while (newCapacity < len)
  215. newCapacity *= 2;
  216. const buf = (0, buffer_1.bufferAllocUnsafe)(newCapacity);
  217. if (this.size > 0)
  218. this.buf.copy(buf, 0, 0, this.size);
  219. buf.fill(0, this.size, len);
  220. this.buf = buf;
  221. this.capacity = newCapacity;
  222. }
  223. else
  224. this.buf.fill(0, this.size, len);
  225. this.size = len;
  226. }
  227. this.touch();
  228. }
  229. chmod(perm) {
  230. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  231. this.touch();
  232. }
  233. chown(uid, gid) {
  234. this.uid = uid;
  235. this.gid = gid;
  236. this.touch();
  237. }
  238. touch() {
  239. this.mtime = new Date();
  240. this.changes.emit(['modify']);
  241. }
  242. canRead(uid = getuid(), gid = getgid()) {
  243. if (this.perm & 4 /* S.IROTH */) {
  244. return true;
  245. }
  246. if (gid === this.gid) {
  247. if (this.perm & 32 /* S.IRGRP */) {
  248. return true;
  249. }
  250. }
  251. if (uid === this.uid) {
  252. if (this.perm & 256 /* S.IRUSR */) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. canWrite(uid = getuid(), gid = getgid()) {
  259. if (this.perm & 2 /* S.IWOTH */) {
  260. return true;
  261. }
  262. if (gid === this.gid) {
  263. if (this.perm & 16 /* S.IWGRP */) {
  264. return true;
  265. }
  266. }
  267. if (uid === this.uid) {
  268. if (this.perm & 128 /* S.IWUSR */) {
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. canExecute(uid = getuid(), gid = getgid()) {
  275. if (this.perm & 1 /* S.IXOTH */) {
  276. return true;
  277. }
  278. if (gid === this.gid) {
  279. if (this.perm & 8 /* S.IXGRP */) {
  280. return true;
  281. }
  282. }
  283. if (uid === this.uid) {
  284. if (this.perm & 64 /* S.IXUSR */) {
  285. return true;
  286. }
  287. }
  288. return false;
  289. }
  290. del() {
  291. this.changes.emit(['delete']);
  292. }
  293. toJSON() {
  294. return {
  295. ino: this.ino,
  296. uid: this.uid,
  297. gid: this.gid,
  298. atime: this.atime.getTime(),
  299. mtime: this.mtime.getTime(),
  300. ctime: this.ctime.getTime(),
  301. btime: this.btime.getTime(),
  302. perm: this.perm,
  303. mode: this.mode,
  304. nlink: this.nlink,
  305. symlink: this.symlink,
  306. data: this.getString(),
  307. };
  308. }
  309. }
  310. exports.Node = Node;
  311. //# sourceMappingURL=Node.js.map