-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.js
151 lines (136 loc) · 3.42 KB
/
stats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
const fs = require('fs');
module.exports = Stats;
/**
* fs.Stats constructor with improved signature and defaults.
* Also works as a copy constructor.
*
* @param {Object} [init] provide initial state
* @param {Number} [init.dev=0]
* @param {Number} [init.mode=0]
* @param {Number} [init.nlink=0]
* @param {Number} [init.uid] (Default: `process.getuid()`)
* @param {Number} [init.gid] (Default: `process.getgid()`)
* @param {Number} [init.rdev=0]
* @param {Number} [init.blksize=0]
* @param {Number} [init.ino=0]
* @param {Number} [init.size=0]
* @param {Number} [init.blocks=0]
* @param {Number} [init.atim_msec] (Default: `Date.now()`)
* @param {Number} [init.mtim_msec] (Default: `Date.now()`)
* @param {Number} [init.ctim_msec] (Default: `Date.now()`)
* @param {Number} [init.birthtim_msec] (Default: `Date.now()`)
*/
function Stats(init) {
const _now = Date.now();
fs.Stats.call(this,
0, // dev
0, // mode
0, // nlink
null, // uid
null, // gid
0, // rdev
0, // blksize
0, // ino
0, // size
0, // blocks
_now, // atim_msec
_now, // mtim_msec
_now, // ctim_msec
_now // birthtim_msec
);
// make these properties non-enumerable because they compete with
// 'atim_msec', 'mtim_msec', 'ctim_msec', & 'birthtim_msec' defined later
nonEnumerable(this, 'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs');
if (typeof init === 'object' && init !== null) {
for (const key in init) {
// disallow custom properties
if (key in this) {
this[key] = init[key];
}
}
}
// setting uid last to minimize getuid() calls
if (this.uid === null) {
this.uid = process.getuid();
}
// setting uid last to minimize getgid() calls
if (this.gid === null) {
this.gid = process.getgid();
}
}
Stats.super_ = fs.Stats;
Stats.prototype = Object.create(fs.Stats.prototype, {
constructor: {
configurable: true,
enumerable: false,
value: Stats,
writable: true
}
});
// getTime/setTime using the constructor names
Object.defineProperties(Stats.prototype, {
atim_msec: {
configurable: true,
enumerable: false,
get: function() {
return this.atime.getTime();
},
set: function(value) {
setIfOwnProperty(this, 'atimeMs', value);
this.atime.setTime(value);
}
},
mtim_msec: {
configurable: true,
enumerable: false,
get: function() {
return this.mtime.getTime();
},
set: function(value) {
setIfOwnProperty(this, 'mtimeMs', value);
this.mtime.setTime(value);
}
},
ctim_msec: {
configurable: true,
enumerable: false,
get: function() {
return this.ctime.getTime();
},
set: function(value) {
setIfOwnProperty(this, 'ctimeMs', value);
this.ctime.setTime(value);
}
},
birthtim_msec: {
configurable: true,
enumerable: false,
get: function() {
return this.birthtime.getTime();
},
set: function(value) {
setIfOwnProperty(this, 'birthtimeMs', value);
this.birthtime.setTime(value);
}
}
});
// TODO drop node v4.x support
function nonEnumerable(self/* , ...names */) {
for (let i = 1; i < arguments.length; i++) {
const key = arguments[i];
if (self.hasOwnProperty(key)) {
const descriptor = Object.getOwnPropertyDescriptor(self, key);
if (descriptor.enumerable && descriptor.configurable) {
descriptor.enumerable = false;
Object.defineProperty(self, key, descriptor);
}
}
}
return self;
}
function setIfOwnProperty(self, key, value) {
if (self.hasOwnProperty(key)) {
return self[key] = value;
}
}