1
1
'use strict' ;
2
2
3
- const { Reflect } = primordials ;
3
+ const { Object , Reflect } = primordials ;
4
4
5
5
const { Buffer, kMaxLength } = require ( 'buffer' ) ;
6
6
const {
@@ -17,7 +17,8 @@ const {
17
17
const {
18
18
isArrayBufferView,
19
19
isUint8Array,
20
- isDate
20
+ isDate,
21
+ isBigUint64Array
21
22
} = require ( 'internal/util/types' ) ;
22
23
const { once } = require ( 'internal/util' ) ;
23
24
const { toPathIfFileURL } = require ( 'internal/url' ) ;
@@ -231,27 +232,9 @@ function preprocessSymlinkDestination(path, type, linkPath) {
231
232
}
232
233
}
233
234
234
- function dateFromNumeric ( num ) {
235
- return new Date ( Number ( num ) + 0.5 ) ;
236
- }
237
-
238
235
// Constructor for file stats.
239
- function Stats (
240
- dev ,
241
- mode ,
242
- nlink ,
243
- uid ,
244
- gid ,
245
- rdev ,
246
- blksize ,
247
- ino ,
248
- size ,
249
- blocks ,
250
- atim_msec ,
251
- mtim_msec ,
252
- ctim_msec ,
253
- birthtim_msec
254
- ) {
236
+ function StatsBase ( dev , mode , nlink , uid , gid , rdev , blksize ,
237
+ ino , size , blocks ) {
255
238
this . dev = dev ;
256
239
this . mode = mode ;
257
240
this . nlink = nlink ;
@@ -262,63 +245,132 @@ function Stats(
262
245
this . ino = ino ;
263
246
this . size = size ;
264
247
this . blocks = blocks ;
265
- this . atimeMs = atim_msec ;
266
- this . mtimeMs = mtim_msec ;
267
- this . ctimeMs = ctim_msec ;
268
- this . birthtimeMs = birthtim_msec ;
269
- this . atime = dateFromNumeric ( atim_msec ) ;
270
- this . mtime = dateFromNumeric ( mtim_msec ) ;
271
- this . ctime = dateFromNumeric ( ctim_msec ) ;
272
- this . birthtime = dateFromNumeric ( birthtim_msec ) ;
273
248
}
274
249
275
- Stats . prototype . _checkModeProperty = function ( property ) {
276
- if ( isWindows && ( property === S_IFIFO || property === S_IFBLK ||
277
- property === S_IFSOCK ) ) {
278
- return false ; // Some types are not available on Windows
279
- }
280
- if ( typeof this . mode === 'bigint' ) {
281
- return ( this . mode & BigInt ( S_IFMT ) ) === BigInt ( property ) ;
282
- }
283
- return ( this . mode & S_IFMT ) === property ;
284
- } ;
285
-
286
- Stats . prototype . isDirectory = function ( ) {
250
+ StatsBase . prototype . isDirectory = function ( ) {
287
251
return this . _checkModeProperty ( S_IFDIR ) ;
288
252
} ;
289
253
290
- Stats . prototype . isFile = function ( ) {
254
+ StatsBase . prototype . isFile = function ( ) {
291
255
return this . _checkModeProperty ( S_IFREG ) ;
292
256
} ;
293
257
294
- Stats . prototype . isBlockDevice = function ( ) {
258
+ StatsBase . prototype . isBlockDevice = function ( ) {
295
259
return this . _checkModeProperty ( S_IFBLK ) ;
296
260
} ;
297
261
298
- Stats . prototype . isCharacterDevice = function ( ) {
262
+ StatsBase . prototype . isCharacterDevice = function ( ) {
299
263
return this . _checkModeProperty ( S_IFCHR ) ;
300
264
} ;
301
265
302
- Stats . prototype . isSymbolicLink = function ( ) {
266
+ StatsBase . prototype . isSymbolicLink = function ( ) {
303
267
return this . _checkModeProperty ( S_IFLNK ) ;
304
268
} ;
305
269
306
- Stats . prototype . isFIFO = function ( ) {
270
+ StatsBase . prototype . isFIFO = function ( ) {
307
271
return this . _checkModeProperty ( S_IFIFO ) ;
308
272
} ;
309
273
310
- Stats . prototype . isSocket = function ( ) {
274
+ StatsBase . prototype . isSocket = function ( ) {
311
275
return this . _checkModeProperty ( S_IFSOCK ) ;
312
276
} ;
313
277
278
+ const kNsPerMsBigInt = 10n ** 6n ;
279
+ const kNsPerSecBigInt = 10n ** 9n ;
280
+ const kMsPerSec = 10 ** 3 ;
281
+ const kNsPerMs = 10 ** 6 ;
282
+ function msFromTimeSpec ( sec , nsec ) {
283
+ return sec * kMsPerSec + nsec / kNsPerMs ;
284
+ }
285
+
286
+ function nsFromTimeSpecBigInt ( sec , nsec ) {
287
+ return sec * kNsPerSecBigInt + nsec ;
288
+ }
289
+
290
+ function dateFromMs ( ms ) {
291
+ return new Date ( Number ( ms ) + 0.5 ) ;
292
+ }
293
+
294
+ function BigIntStats ( dev , mode , nlink , uid , gid , rdev , blksize ,
295
+ ino , size , blocks ,
296
+ atimeNs , mtimeNs , ctimeNs , birthtimeNs ) {
297
+ StatsBase . call ( this , dev , mode , nlink , uid , gid , rdev , blksize ,
298
+ ino , size , blocks ) ;
299
+
300
+ this . atimeMs = atimeNs / kNsPerMsBigInt ;
301
+ this . mtimeMs = mtimeNs / kNsPerMsBigInt ;
302
+ this . ctimeMs = ctimeNs / kNsPerMsBigInt ;
303
+ this . birthtimeMs = birthtimeNs / kNsPerMsBigInt ;
304
+ this . atimeNs = atimeNs ;
305
+ this . mtimeNs = mtimeNs ;
306
+ this . ctimeNs = ctimeNs ;
307
+ this . birthtimeNs = birthtimeNs ;
308
+ this . atime = dateFromMs ( this . atimeMs ) ;
309
+ this . mtime = dateFromMs ( this . mtimeMs ) ;
310
+ this . ctime = dateFromMs ( this . ctimeMs ) ;
311
+ this . birthtime = dateFromMs ( this . birthtimeMs ) ;
312
+ }
313
+
314
+ Object . setPrototypeOf ( BigIntStats . prototype , StatsBase . prototype ) ;
315
+ Object . setPrototypeOf ( BigIntStats , StatsBase ) ;
316
+
317
+ BigIntStats . prototype . _checkModeProperty = function ( property ) {
318
+ if ( isWindows && ( property === S_IFIFO || property === S_IFBLK ||
319
+ property === S_IFSOCK ) ) {
320
+ return false ; // Some types are not available on Windows
321
+ }
322
+ return ( this . mode & BigInt ( S_IFMT ) ) === BigInt ( property ) ;
323
+ } ;
324
+
325
+ function Stats ( dev , mode , nlink , uid , gid , rdev , blksize ,
326
+ ino , size , blocks ,
327
+ atimeMs , mtimeMs , ctimeMs , birthtimeMs ) {
328
+ StatsBase . call ( this , dev , mode , nlink , uid , gid , rdev , blksize ,
329
+ ino , size , blocks ) ;
330
+ this . atimeMs = atimeMs ;
331
+ this . mtimeMs = mtimeMs ;
332
+ this . ctimeMs = ctimeMs ;
333
+ this . birthtimeMs = birthtimeMs ;
334
+ this . atime = dateFromMs ( atimeMs ) ;
335
+ this . mtime = dateFromMs ( mtimeMs ) ;
336
+ this . ctime = dateFromMs ( ctimeMs ) ;
337
+ this . birthtime = dateFromMs ( birthtimeMs ) ;
338
+ }
339
+
340
+ Object . setPrototypeOf ( Stats . prototype , StatsBase . prototype ) ;
341
+ Object . setPrototypeOf ( Stats , StatsBase ) ;
342
+
343
+ Stats . prototype . _checkModeProperty = function ( property ) {
344
+ if ( isWindows && ( property === S_IFIFO || property === S_IFBLK ||
345
+ property === S_IFSOCK ) ) {
346
+ return false ; // Some types are not available on Windows
347
+ }
348
+ return ( this . mode & S_IFMT ) === property ;
349
+ } ;
350
+
314
351
function getStatsFromBinding ( stats , offset = 0 ) {
315
- return new Stats ( stats [ 0 + offset ] , stats [ 1 + offset ] , stats [ 2 + offset ] ,
316
- stats [ 3 + offset ] , stats [ 4 + offset ] , stats [ 5 + offset ] ,
317
- stats [ 6 + offset ] , // blksize
318
- stats [ 7 + offset ] , stats [ 8 + offset ] ,
319
- stats [ 9 + offset ] , // blocks
320
- stats [ 10 + offset ] , stats [ 11 + offset ] ,
321
- stats [ 12 + offset ] , stats [ 13 + offset ] ) ;
352
+ if ( isBigUint64Array ( stats ) ) {
353
+ return new BigIntStats (
354
+ stats [ 0 + offset ] , stats [ 1 + offset ] , stats [ 2 + offset ] ,
355
+ stats [ 3 + offset ] , stats [ 4 + offset ] , stats [ 5 + offset ] ,
356
+ stats [ 6 + offset ] , stats [ 7 + offset ] , stats [ 8 + offset ] ,
357
+ stats [ 9 + offset ] ,
358
+ nsFromTimeSpecBigInt ( stats [ 10 + offset ] , stats [ 11 + offset ] ) ,
359
+ nsFromTimeSpecBigInt ( stats [ 12 + offset ] , stats [ 13 + offset ] ) ,
360
+ nsFromTimeSpecBigInt ( stats [ 14 + offset ] , stats [ 15 + offset ] ) ,
361
+ nsFromTimeSpecBigInt ( stats [ 16 + offset ] , stats [ 17 + offset ] )
362
+ ) ;
363
+ }
364
+ return new Stats (
365
+ stats [ 0 + offset ] , stats [ 1 + offset ] , stats [ 2 + offset ] ,
366
+ stats [ 3 + offset ] , stats [ 4 + offset ] , stats [ 5 + offset ] ,
367
+ stats [ 6 + offset ] , stats [ 7 + offset ] , stats [ 8 + offset ] ,
368
+ stats [ 9 + offset ] ,
369
+ msFromTimeSpec ( stats [ 10 + offset ] , stats [ 11 + offset ] ) ,
370
+ msFromTimeSpec ( stats [ 12 + offset ] , stats [ 13 + offset ] ) ,
371
+ msFromTimeSpec ( stats [ 14 + offset ] , stats [ 15 + offset ] ) ,
372
+ msFromTimeSpec ( stats [ 16 + offset ] , stats [ 17 + offset ] )
373
+ ) ;
322
374
}
323
375
324
376
function stringToFlags ( flags ) {
@@ -466,6 +518,7 @@ function warnOnNonPortableTemplate(template) {
466
518
467
519
module . exports = {
468
520
assertEncoding,
521
+ BigIntStats, // for testing
469
522
copyObject,
470
523
Dirent,
471
524
getDirents,
0 commit comments