-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmountinfoparser.js
260 lines (217 loc) · 6.43 KB
/
mountinfoparser.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// requires the user be part of group disk.
const { throws } = require('assert');
const { retryable } = require('async');
const { exec, execSync } = require('child_process');
const fs = require('fs');
const { startsWith, replace, kebabCase } = require('lodash');
const lodash = require('lodash');
const path = require('path');
const { dirname } = require('path');
/***
* Class that contains information about a single mounted filesystem.
* Also contains static methods for gathering information on mounted filesystems.
*/
class MountInfo
{
/**
* Static property initialized with a list of current mounts on call to ParseMounts()
*/
static Mounts = []
/**
* Populates the Mounts static array of MountInfo
*/
static ParseMounts()
{
MountInfo.Mounts = FSMounts();
var retval = execSync("lsblk -fJ").toString();
var j = JSON.parse(retval);
var parts = []
// flatten the list to the partition devices
while (j.blockdevices.length > 0)
{
var bdev = j.blockdevices.pop();
if (bdev.children)
{
parts = parts.concat(bdev.children) ;
}
else
{
parts.push(bdev);
}
}
// pull the information out of the output.
while (parts.length > 0)
{
var bdev =parts.pop();
for (var i in MountInfo.Mounts)
{
var mt = MountInfo.Mounts[i];
// check ALL of them because of subvolumes
// in types like btrfs
if (mt.Device.replaceAll('/dev/','') == bdev.name)
{
mt.LABEL = bdev.label;
mt.UUID = bdev.uuid;
mt.Size = bdev.fsavail
//mt.MountPoints = bdev.mountpoints;
}
}
}
}
static ByUUID(UUID,subvol=null)
{
for (var i in MountInfo.Mounts)
{
if (MountInfo.Mounts[i].UUID == UUID)
{
// just return the first one if no subvolume indicated
if (!subvol)
{
return MountInfo.Mounts[i];
}
else
{
if (subvol == MountInfo.Mounts[i].SUBVOLUMEID)
{
return MountInfo.Mounts[i];
}
}
}
}
return null;
}
static ExpandPath(subpath)
{
return path.join(process.cwd(),subpath);
}
static ExpandBTRFSPath(UUID,subpath,volid=null)
{
// this will likely only be used to find existing files.
var m = this.ByUUID(UUID,volid);
if (m==null)
{
return null;
}
return path.join(m.MountPoint,subpath);
}
/**
* Once Mounts is populated, returns the MountInfo object representing where the file is located under
* @param {*} file the file to find the mountpoint for
* @returns an array with the MountInfo object the supplied file is under as first item and the relative path as the second
*/
static WhichDevice(file)
{
if (!fs.existsSync(file))
{
throw `File: ${file} not found!`
}
var pathname = dirname(path.resolve(file));
// basically keeps testing the string until the currently mounted filesystems
// are all tested and the one with the longest path is where the file will be under
// then return the object.
var plen = 0
var themnt = null
for (var i in MountInfo.Mounts)
{
var m = MountInfo.Mounts[i];
if (startsWith(pathname, m.MountPoint))
{
if (plen < m.MountPoint.length)
{
plen = m.MountPoint.length;
themnt = m;
}
}
}
return [themnt, themnt!=null ? path.relative(themnt.MountPoint,pathname):null]
}
constructor(entry)
{
/**
* The device name that would be supplied in the mount command
*/
this.Device = entry[0];
/**
* The point to which this filesystem is mounted
*/
this.MountPoint = entry[1];
/**
* Filesystem type.
*/
this.FSType = entry[2];
/**
* Mount options string
*/
this.Options = entry[3] ? entry[3].split(",") : [];
this.Options = this.Options.map( function (v)
{
var b = {};
if (v.indexOf("=") == -1 )
{
b.Key = v.trim();
b.Value = null;
return b;
}
else
{
var vals = v.split("=");
b.Key = vals[0].trim();
b.Value = vals[1].trim();
return b;
}
});
/**
* Dump option setting.
*/
this.DumpOption = entry[4];
/**
* Order, if any, in which this filesystem would be mounted
*/
this.MountOrder= entry[5];
/**
* If a mounted block or loop device, the unique identifer of the partition
*/
this.UUID ="";
/**
* If available, the volume label.
*/
this.LABEL="";
this.SUBVOLUMEID = -1;
this.SUBVOLUME = "";
for (var i in this.Options)
{
var op = this.Options[i];
if (op.Key== "subvol")
{
this.SUBVOLUME = op.Value;
}
else if (op.Key == "subvolid")
{
this.SUBVOLUMEID = 1 * op.Value;
}
}
}
}
/**
* Reads current mountpoints, initializes and returns an array of MountInfo objects
*/
function FSMounts() {
var f = fs.readFileSync('/proc/self/mounts').toString();
var p = f.split('\n');
// so what if there is a goddamn space in the mountpoint name ?
var mounts = [];
for (var i in p) {
var toentry = [];
var entry = p[i].split(' ');
for (var r in entry) {
// replace space characters.
toentry.push(entry[r].replaceAll('\\040', ' '));
}
var mp = new MountInfo(toentry)
mounts.push(mp);
}
return mounts;
}
module.exports = {
MountInfo: MountInfo
}