123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Node = void 0;
- const process_1 = require("../process");
- const buffer_1 = require("../internal/buffer");
- const constants_1 = require("../constants");
- const events_1 = require("events");
- const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, S_IFCHR } = constants_1.constants;
- const getuid = () => { var _a, _b; return (_b = (_a = process_1.default.getuid) === null || _a === void 0 ? void 0 : _a.call(process_1.default)) !== null && _b !== void 0 ? _b : 0; };
- const getgid = () => { var _a, _b; return (_b = (_a = process_1.default.getgid) === null || _a === void 0 ? void 0 : _a.call(process_1.default)) !== null && _b !== void 0 ? _b : 0; };
- /**
- * Node in a file system (like i-node, v-node).
- */
- class Node extends events_1.EventEmitter {
- constructor(ino, mode = 0o666) {
- super();
- // User ID and group ID.
- this._uid = getuid();
- this._gid = getgid();
- this._atime = new Date();
- this._mtime = new Date();
- this._ctime = new Date();
- this.rdev = 0;
- // Number of hard links pointing at this Node.
- this._nlink = 1;
- this.mode = mode;
- this.ino = ino;
- }
- set ctime(ctime) {
- this._ctime = ctime;
- }
- get ctime() {
- return this._ctime;
- }
- set uid(uid) {
- this._uid = uid;
- this.ctime = new Date();
- }
- get uid() {
- return this._uid;
- }
- set gid(gid) {
- this._gid = gid;
- this.ctime = new Date();
- }
- get gid() {
- return this._gid;
- }
- set atime(atime) {
- this._atime = atime;
- this.ctime = new Date();
- }
- get atime() {
- return this._atime;
- }
- set mtime(mtime) {
- this._mtime = mtime;
- this.ctime = new Date();
- }
- get mtime() {
- return this._mtime;
- }
- get perm() {
- return this.mode & ~S_IFMT;
- }
- set perm(perm) {
- this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
- this.ctime = new Date();
- }
- set nlink(nlink) {
- this._nlink = nlink;
- this.ctime = new Date();
- }
- get nlink() {
- return this._nlink;
- }
- getString(encoding = 'utf8') {
- this.atime = new Date();
- return this.getBuffer().toString(encoding);
- }
- setString(str) {
- // this.setBuffer(bufferFrom(str, 'utf8'));
- this.buf = (0, buffer_1.bufferFrom)(str, 'utf8');
- this.touch();
- }
- getBuffer() {
- this.atime = new Date();
- if (!this.buf)
- this.setBuffer((0, buffer_1.bufferAllocUnsafe)(0));
- return (0, buffer_1.bufferFrom)(this.buf); // Return a copy.
- }
- setBuffer(buf) {
- this.buf = (0, buffer_1.bufferFrom)(buf); // Creates a copy of data.
- this.touch();
- }
- getSize() {
- return this.buf ? this.buf.length : 0;
- }
- setModeProperty(property) {
- this.mode = property;
- }
- isFile() {
- return (this.mode & S_IFMT) === S_IFREG;
- }
- isDirectory() {
- return (this.mode & S_IFMT) === S_IFDIR;
- }
- isSymlink() {
- // return !!this.symlink;
- return (this.mode & S_IFMT) === S_IFLNK;
- }
- isCharacterDevice() {
- return (this.mode & S_IFMT) === S_IFCHR;
- }
- makeSymlink(symlink) {
- this.mode = S_IFLNK | 0o666;
- this.symlink = symlink;
- }
- write(buf, off = 0, len = buf.length, pos = 0) {
- if (!this.buf)
- this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
- if (pos + len > this.buf.length) {
- const newBuf = (0, buffer_1.bufferAllocUnsafe)(pos + len);
- this.buf.copy(newBuf, 0, 0, this.buf.length);
- this.buf = newBuf;
- }
- buf.copy(this.buf, pos, off, off + len);
- this.touch();
- return len;
- }
- // Returns the number of bytes read.
- read(buf, off = 0, len = buf.byteLength, pos = 0) {
- this.atime = new Date();
- if (!this.buf)
- this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
- if (pos >= this.buf.length)
- return 0;
- let actualLen = len;
- if (actualLen > buf.byteLength) {
- actualLen = buf.byteLength;
- }
- if (actualLen + pos > this.buf.length) {
- actualLen = this.buf.length - pos;
- }
- const buf2 = buf instanceof buffer_1.Buffer ? buf : buffer_1.Buffer.from(buf.buffer);
- this.buf.copy(buf2, off, pos, pos + actualLen);
- return actualLen;
- }
- truncate(len = 0) {
- if (!len)
- this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
- else {
- if (!this.buf)
- this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
- if (len <= this.buf.length) {
- this.buf = this.buf.slice(0, len);
- }
- else {
- const buf = (0, buffer_1.bufferAllocUnsafe)(len);
- this.buf.copy(buf);
- buf.fill(0, this.buf.length);
- this.buf = buf;
- }
- }
- this.touch();
- }
- chmod(perm) {
- this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
- this.touch();
- }
- chown(uid, gid) {
- this.uid = uid;
- this.gid = gid;
- this.touch();
- }
- touch() {
- this.mtime = new Date();
- this.emit('change', this);
- }
- canRead(uid = getuid(), gid = getgid()) {
- if (this.perm & 4 /* S.IROTH */) {
- return true;
- }
- if (gid === this.gid) {
- if (this.perm & 32 /* S.IRGRP */) {
- return true;
- }
- }
- if (uid === this.uid) {
- if (this.perm & 256 /* S.IRUSR */) {
- return true;
- }
- }
- return false;
- }
- canWrite(uid = getuid(), gid = getgid()) {
- if (this.perm & 2 /* S.IWOTH */) {
- return true;
- }
- if (gid === this.gid) {
- if (this.perm & 16 /* S.IWGRP */) {
- return true;
- }
- }
- if (uid === this.uid) {
- if (this.perm & 128 /* S.IWUSR */) {
- return true;
- }
- }
- return false;
- }
- canExecute(uid = getuid(), gid = getgid()) {
- if (this.perm & 1 /* S.IXOTH */) {
- return true;
- }
- if (gid === this.gid) {
- if (this.perm & 8 /* S.IXGRP */) {
- return true;
- }
- }
- if (uid === this.uid) {
- if (this.perm & 64 /* S.IXUSR */) {
- return true;
- }
- }
- return false;
- }
- del() {
- this.emit('delete', this);
- }
- toJSON() {
- return {
- ino: this.ino,
- uid: this.uid,
- gid: this.gid,
- atime: this.atime.getTime(),
- mtime: this.mtime.getTime(),
- ctime: this.ctime.getTime(),
- perm: this.perm,
- mode: this.mode,
- nlink: this.nlink,
- symlink: this.symlink,
- data: this.getString(),
- };
- }
- }
- exports.Node = Node;
- //# sourceMappingURL=Node.js.map
|