Skip to content

Commit 24e674d

Browse files
authored
Merge pull request #326 from tschaub/uid-cleanup
chore: use 0 internally for uid/gid on Windows
2 parents 980447a + ec41697 commit 24e674d

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

lib/item.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const permissions = {
1919
};
2020

2121
function getUid() {
22-
// force NaN on windows.
23-
return process.getuid ? process.getuid() : NaN;
22+
// force 0 on windows.
23+
return process.getuid ? process.getuid() : 0;
2424
}
2525

2626
function getGid() {
27-
// force NaN on windows.
28-
return process.getgid ? process.getgid() : NaN;
27+
// force 0 on windows.
28+
return process.getgid ? process.getgid() : 0;
2929
}
3030

3131
/**
@@ -108,10 +108,9 @@ Item.prototype.canRead = function() {
108108
const uid = getUid();
109109
const gid = getGid();
110110
let can = false;
111-
if (uid === 0) {
111+
if (process.getuid && uid === 0) {
112112
can = true;
113-
} else if (uid === this._uid || uid !== uid) {
114-
// (uid !== uid) means uid is NaN, only for windows
113+
} else if (uid === this._uid) {
115114
can = (permissions.USER_READ & this._mode) === permissions.USER_READ;
116115
} else if (gid === this._gid) {
117116
can = (permissions.GROUP_READ & this._mode) === permissions.GROUP_READ;
@@ -129,10 +128,9 @@ Item.prototype.canWrite = function() {
129128
const uid = getUid();
130129
const gid = getGid();
131130
let can = false;
132-
if (uid === 0) {
131+
if (process.getuid && uid === 0) {
133132
can = true;
134-
} else if (uid === this._uid || uid !== uid) {
135-
// (uid !== uid) means uid is NaN, only for windows
133+
} else if (uid === this._uid) {
136134
can = (permissions.USER_WRITE & this._mode) === permissions.USER_WRITE;
137135
} else if (gid === this._gid) {
138136
can = (permissions.GROUP_WRITE & this._mode) === permissions.GROUP_WRITE;
@@ -150,10 +148,9 @@ Item.prototype.canExecute = function() {
150148
const uid = getUid();
151149
const gid = getGid();
152150
let can = false;
153-
if (uid === 0) {
151+
if (process.getuid && uid === 0) {
154152
can = true;
155-
} else if (uid === this._uid || isNaN(uid)) {
156-
// NaN occurs on windows
153+
} else if (uid === this._uid) {
157154
can = (permissions.USER_EXEC & this._mode) === permissions.USER_EXEC;
158155
} else if (gid === this._gid) {
159156
can = (permissions.GROUP_EXEC & this._mode) === permissions.GROUP_EXEC;
@@ -289,19 +286,8 @@ Item.prototype.getStats = function(bigint) {
289286
stats[0] = convert(8675309); // dev
290287
// [1] is mode
291288
stats[2] = convert(this.links); // nlink
292-
293-
const uid = this.getUid();
294-
if (!isNaN(uid)) {
295-
// uid is NaN on windows
296-
stats[3] = convert(uid); // uid
297-
}
298-
299-
const gid = this.getGid();
300-
if (!isNaN(gid)) {
301-
// gid is NaN on windows
302-
stats[4] = convert(gid); // gid
303-
}
304-
289+
stats[3] = convert(this.getUid()); // uid
290+
stats[4] = convert(this.getGid()); // gid
305291
stats[5] = convert(0); // rdev
306292
stats[6] = convert(4096); // blksize
307293
stats[7] = convert(this._id); // ino

0 commit comments

Comments
 (0)