1 | stat-mode
|
2 | =========
|
3 | ### Offers convenient getters and setters for the stat `mode`
|
4 | [![Build Status](https://github.com/TooTallNate/stat-mode/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/stat-mode/actions?workflow=Node+CI)
|
5 |
|
6 | You know that `mode` property on the `fs.Stat` object that you probably
|
7 | usually just ignore? Well there's acutally a lot of information packed
|
8 | into that number.
|
9 |
|
10 | The specific information includes:
|
11 |
|
12 | * What the ["file type"](http://en.wikipedia.org/wiki/Unix_file_types) of file it is
|
13 | * Whether or not the [`setuid` and `setgid` bits](http://en.wikipedia.org/wiki/Setuid) are set
|
14 | * Whether or not the [`sticky` bit](http://en.wikipedia.org/wiki/Sticky_bit) is set
|
15 | * The [_read_, _write_, and _execute_ permissions for the _owner_, _group_ and _others_](http://en.wikipedia.org/wiki/File_system_permissions)
|
16 |
|
17 | This module helps you extract that information.
|
18 |
|
19 | All the getters are also setters, which change the `mode` property
|
20 | appropriately. This is useful for when you have to build up your
|
21 | own `fs.Stat` object for whatever reason (like when implementing a
|
22 | FUSE filesystem.
|
23 |
|
24 |
|
25 | Installation
|
26 | ------------
|
27 |
|
28 | ``` bash
|
29 | $ npm install stat-mode
|
30 | ```
|
31 |
|
32 |
|
33 | Example
|
34 | -------
|
35 |
|
36 | So given some arbitrary file (let's say `/bin/echo`):
|
37 |
|
38 | ``` bash
|
39 | $ ls -l /bin/echo
|
40 | -rwxr-xr-x 1 root wheel 14128 Aug 11 2013 /bin/echo
|
41 | ```
|
42 |
|
43 | We can inspect it using the `fs.stat()` call and creating a `Mode` instance
|
44 | on top of it.
|
45 |
|
46 | ``` javascript
|
47 | var fs = require('fs');
|
48 | var Mode = require('stat-mode');
|
49 |
|
50 | fs.stat('/bin/echo', function (err, stat) {
|
51 | if (err) throw err;
|
52 |
|
53 | // create a "Mode" instance on top of the `stat` object
|
54 | var mode = new Mode(stat);
|
55 |
|
56 | // you can check what kind of file it is:
|
57 | mode.isDirectory();
|
58 | // false
|
59 |
|
60 | mode.isFIFO();
|
61 | // false
|
62 |
|
63 | mode.isFile();
|
64 | // true
|
65 |
|
66 |
|
67 | // and you can also check individual owner, group and others permissions
|
68 | mode.owner.read;
|
69 | // true
|
70 |
|
71 | mode.owner.write;
|
72 | // true
|
73 |
|
74 | mode.owner.execute;
|
75 | // true
|
76 |
|
77 | mode.group.read;
|
78 | // true
|
79 |
|
80 | mode.group.write;
|
81 | // false
|
82 |
|
83 | mode.group.execute;
|
84 | // true
|
85 |
|
86 | mode.others.read;
|
87 | // true
|
88 |
|
89 | mode.others.write;
|
90 | // false
|
91 |
|
92 | mode.others.execute;
|
93 | // true
|
94 |
|
95 |
|
96 | // the `toString()` output resembes the `ls -l` output:
|
97 | mode.toString();
|
98 | // '-rwxr-xr-x'
|
99 | });
|
100 | ```
|
101 |
|
102 |
|
103 | API
|
104 | ---
|
105 |
|
106 | ### new Mode(Object stat) → Mode
|
107 |
|
108 | You must pass in "stat" object to the `Mode` constructor. The "stat"
|
109 | object can be a real `fs.Stat` instance, or really any Object with a
|
110 | `mode` property.
|
111 |
|
112 | #### mode.isDirectory([Boolean set]) → Boolean
|
113 |
|
114 | Returns `true` if the mode's file type is "directory", `false` otherwise.
|
115 | If you pass `true` to the function, then the mode will be set to "directory".
|
116 |
|
117 | #### mode.isFile([Boolean set]) → Boolean
|
118 |
|
119 | Returns `true` if the mode's file type is "file", `false` otherwise.
|
120 | If you pass `true` to the function, then the mode will be set to "file".
|
121 |
|
122 | #### mode.isBlockDevice([Boolean set]) → Boolean
|
123 |
|
124 | Returns `true` if the mode's file type is "block device", `false` otherwise.
|
125 | If you pass `true` to the function, then the mode will be set to "block device".
|
126 |
|
127 | #### mode.isCharacterDevice([Boolean set]) → Boolean
|
128 |
|
129 | Returns `true` if the mode's file type is "character device", `false` otherwise.
|
130 | If you pass `true` to the function, then the mode will be set to "character
|
131 | device".
|
132 |
|
133 | #### mode.isSymbolicLink([Boolean set]) → Boolean
|
134 |
|
135 | Returns `true` if the mode's file type is "symbolic link", `false` otherwise.
|
136 | If you pass `true` to the function, then the mode will be set to "symbolic link".
|
137 |
|
138 | #### mode.isFIFO([Boolean set]) → Boolean
|
139 |
|
140 | Returns `true` if the mode's file type is "FIFO", `false` otherwise.
|
141 | If you pass `true` to the function, then the mode will be set to "FIFO".
|
142 |
|
143 | #### mode.isSocket([Boolean set]) → Boolean
|
144 |
|
145 | Returns `true` if the mode's file type is "socket", `false` otherwise.
|
146 | If you pass `true` to the function, then the mode will be set to "socket".
|
147 |
|
148 | #### mode.owner.read → Boolean [Getter/Setter]
|
149 |
|
150 | `true` if the mode is "owner read" rights, `false` otherwise.
|
151 |
|
152 | #### mode.owner.write → Boolean [Getter/Setter]
|
153 |
|
154 | `true` if the mode is "owner write" rights, `false` otherwise.
|
155 |
|
156 | #### mode.owner.execute → Boolean [Getter/Setter]
|
157 |
|
158 | `true` if the mode is "owner execute" rights, `false` otherwise.
|
159 |
|
160 | #### mode.group.read → Boolean [Getter/Setter]
|
161 |
|
162 | `true` if the mode is "group read" rights, `false` otherwise.
|
163 |
|
164 | #### mode.group.write → Boolean [Getter/Setter]
|
165 |
|
166 | `true` if the mode is "group write" rights, `false` otherwise.
|
167 |
|
168 | #### mode.group.execute → Boolean [Getter/Setter]
|
169 |
|
170 | `true` if the mode is "group execute" rights, `false` otherwise.
|
171 |
|
172 | #### mode.others.read → Boolean [Getter/Setter]
|
173 |
|
174 | `true` if the mode is "others read" rights, `false` otherwise.
|
175 |
|
176 | #### mode.others.write → Boolean [Getter/Setter]
|
177 |
|
178 | `true` if the mode is "others write" rights, `false` otherwise.
|
179 |
|
180 | #### mode.others.execute → Boolean [Getter/Setter]
|
181 |
|
182 | `true` if the mode is "others execute" rights, `false` otherwise.
|