Thursday, September 13, 2018

NodeJS Stat

The Basics

If you're not familiar with stat I will give you a quick rundown. Stat is used in UNIX to view information about a file. In the fs module of NodeJs most stat related functions return a Stats object. This is what is looks like in the documentation.
Stats {
  dev: 2114,
  ino: 48064969,
  mode: 33188,
  nlink: 1,
  uid: 85,
  gid: 100,
  rdev: 0,
  size: 527,
  blksize: 4096,
  blocks: 8,
  atimeMs: 1318289051000.1,
  mtimeMs: 1318289051000.1,
  ctimeMs: 1318289051000.1,
  birthtimeMs: 1318289051000.1,
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  birthtime: Mon, 10 Oct 2011 23:24:11 GMT } 

This object holds a lot of information that seems to be irrelevant to us, but in reality it is all useful! I made a handy reference sheet so that the file statistics represent
 
  dev: Device ID
  ino: Inode number
  mode: File type and mode
  nlink: Number of links
  uid: User ID of owner
  gid: Group ID of owner
  rdev: Device ID (if special file)
  size: Total size in bytes
  blksize: Block size of file system I/O
  blocks: Number of 512 byte blocks allocated
  atimeMs: Time of last access in milliseconds
  mtimeMs: Time of last modification in milliseconds
  ctimeMs: Time of last status change in milliseconds
  birthtimeMs: Time of creation in milliseconds
  atime: Time of last access 
  mtime: Time of last modification
  ctime: Time of last status
  birthtime: Time of creation 

Lets put our new knowledge to use.
const fs = require("fs");
let filepath = "test1.txt";
fs.stat(filepath, (err, stats) => {
    if (err) {
        console.log(err);
    } else {
        console.log("Date Created: ");
        console.log(stats.birthtime);
        console.log("Date Accessed: ");
        console.log(stats.atime);
        console.log("Date Modified: ");
        console.log(stats.mtime);
        console.log("Date Changed: ");
        console.log(stats.ctime);
    }
});

Output:

Date Created: 
2018-09-13T22:04:58.180Z
Date Accessed: 
2018-09-13T22:04:58.180Z
Date Modified: 
2018-09-13T22:04:58.180Z
Date Changed: 
2018-09-13T22:04:58.180Z

We can also do the same thing using fsPromises
const fsPromises = require("fs").promises;
let filepath = "test1.txt";
let statPromise = fsPromises.stat(filepath);
statPromise.then((stats) => {
        console.log("Date Created: ");
        console.log(stats.birthtime);
        console.log("Date Accessed: ");
        console.log(stats.atime);
        console.log("Date Modified: ");
        console.log(stats.mtime);
        console.log("Date Changed: ");
        console.log(stats.ctime);
    }).catch((err) => {
        console.log(err); 
});

lstat works similarly to stat except if the filepath is a symbolic link the link in statted instead of the file. fstat takes a file descriptor instead of a filepath. To use fstat open a file using fs then using the file descrpitor from the opfunction and pass it into fstat
It would look something like this:
const fs = require("fs");
let filepath = "test1.txt";
fs.open(filepath, 'r', (err, fd) =>{
      if (err) console.log(err);
      console.log(fd);
      fs.fstat(fd, (err, stats) => {
          if (err) {
              console.log(err);
          } else {
              console.log(stats);
          }
      });
      // always close the file descriptor!
      fs.close(fd, (err) => {
          if (err) throw err;
      });
 });


No comments:

Post a Comment

Creating Releases