Skip to content

Commit

Permalink
chore: use 0 internally for uid/gid on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp authored and tschaub committed May 10, 2021
1 parent 980447a commit ae38cfe
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions lib/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const permissions = {
};

function getUid() {
// force NaN on windows.
return process.getuid ? process.getuid() : NaN;
// force 0 on windows.
return process.getuid ? process.getuid() : 0;
}

function getGid() {
// force NaN on windows.
return process.getgid ? process.getgid() : NaN;
// force 0 on windows.
return process.getgid ? process.getgid() : 0;
}

/**
Expand Down Expand Up @@ -110,8 +110,7 @@ Item.prototype.canRead = function() {
let can = false;
if (uid === 0) {
can = true;
} else if (uid === this._uid || uid !== uid) {
// (uid !== uid) means uid is NaN, only for windows
} else if (uid === this._uid) {
can = (permissions.USER_READ & this._mode) === permissions.USER_READ;
} else if (gid === this._gid) {
can = (permissions.GROUP_READ & this._mode) === permissions.GROUP_READ;
Expand All @@ -131,8 +130,7 @@ Item.prototype.canWrite = function() {
let can = false;
if (uid === 0) {
can = true;
} else if (uid === this._uid || uid !== uid) {
// (uid !== uid) means uid is NaN, only for windows
} else if (uid === this._uid) {
can = (permissions.USER_WRITE & this._mode) === permissions.USER_WRITE;
} else if (gid === this._gid) {
can = (permissions.GROUP_WRITE & this._mode) === permissions.GROUP_WRITE;
Expand All @@ -152,8 +150,7 @@ Item.prototype.canExecute = function() {
let can = false;
if (uid === 0) {
can = true;
} else if (uid === this._uid || isNaN(uid)) {
// NaN occurs on windows
} else if (uid === this._uid) {
can = (permissions.USER_EXEC & this._mode) === permissions.USER_EXEC;
} else if (gid === this._gid) {
can = (permissions.GROUP_EXEC & this._mode) === permissions.GROUP_EXEC;
Expand Down Expand Up @@ -289,19 +286,8 @@ Item.prototype.getStats = function(bigint) {
stats[0] = convert(8675309); // dev
// [1] is mode
stats[2] = convert(this.links); // nlink

const uid = this.getUid();
if (!isNaN(uid)) {
// uid is NaN on windows
stats[3] = convert(uid); // uid
}

const gid = this.getGid();
if (!isNaN(gid)) {
// gid is NaN on windows
stats[4] = convert(gid); // gid
}

stats[3] = convert(this.getUid()); // uid
stats[4] = convert(this.getGid()); // gid
stats[5] = convert(0); // rdev
stats[6] = convert(4096); // blksize
stats[7] = convert(this._id); // ino
Expand Down

0 comments on commit ae38cfe

Please sign in to comment.