UNPKG

4.93 kBMarkdownView Raw
1stat-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
6You know that `mode` property on the `fs.Stat` object that you probably
7usually just ignore? Well there's acutally a lot of information packed
8into that number.
9
10The 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
17This module helps you extract that information.
18
19All the getters are also setters, which change the `mode` property
20appropriately. This is useful for when you have to build up your
21own `fs.Stat` object for whatever reason (like when implementing a
22FUSE filesystem.
23
24
25Installation
26------------
27
28``` bash
29$ npm install stat-mode
30```
31
32
33Example
34-------
35
36So 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
43We can inspect it using the `fs.stat()` call and creating a `Mode` instance
44on top of it.
45
46``` javascript
47var fs = require('fs');
48var Mode = require('stat-mode');
49
50fs.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
103API
104---
105
106### new Mode(Object stat) → Mode
107
108You must pass in "stat" object to the `Mode` constructor. The "stat"
109object can be a real `fs.Stat` instance, or really any Object with a
110`mode` property.
111
112#### mode.isDirectory([Boolean set]) → Boolean
113
114Returns `true` if the mode's file type is "directory", `false` otherwise.
115If you pass `true` to the function, then the mode will be set to "directory".
116
117#### mode.isFile([Boolean set]) → Boolean
118
119Returns `true` if the mode's file type is "file", `false` otherwise.
120If you pass `true` to the function, then the mode will be set to "file".
121
122#### mode.isBlockDevice([Boolean set]) → Boolean
123
124Returns `true` if the mode's file type is "block device", `false` otherwise.
125If you pass `true` to the function, then the mode will be set to "block device".
126
127#### mode.isCharacterDevice([Boolean set]) → Boolean
128
129Returns `true` if the mode's file type is "character device", `false` otherwise.
130If you pass `true` to the function, then the mode will be set to "character
131device".
132
133#### mode.isSymbolicLink([Boolean set]) → Boolean
134
135Returns `true` if the mode's file type is "symbolic link", `false` otherwise.
136If you pass `true` to the function, then the mode will be set to "symbolic link".
137
138#### mode.isFIFO([Boolean set]) → Boolean
139
140Returns `true` if the mode's file type is "FIFO", `false` otherwise.
141If you pass `true` to the function, then the mode will be set to "FIFO".
142
143#### mode.isSocket([Boolean set]) → Boolean
144
145Returns `true` if the mode's file type is "socket", `false` otherwise.
146If 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.