forked from 418sec/freediskspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreediskspace.js
131 lines (111 loc) · 2.64 KB
/
freediskspace.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
'use strict';
var cp = require('child_process');
var os = require('os');
var fd = require('freedisk');
var Q = require('q');
exports.drivelist = driveList; // for backward compatibility
exports.driveList = driveList;
exports.detail = detail;
function detail(drive, callback) {
if (arguments.length === 1) {
return detailPromise(drive)
} else {
detailCallback(drive, callback)
}
}
function detailPromise(drive) {
var deferred = Q.defer();
detailCallback(drive, function (error, details) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(details);
}
});
return deferred.promise;
}
function detailCallback(drive, callback) {
if (os.platform() == "win32") {
detailWindows(drive, callback);
}
else {
detailPosix(drive, callback);
}
}
var windowsDetailsPattern = /\s(\d+)\s*(\w:)\s*(\d+)\s/;
function detailWindows(drive, callback) {
cp.exec("wmic logicaldisk where \"DeviceID='" + drive + "'\" get FreeSpace, Name, Size", function (error, stdout) {
if (error) {
callback(error);
return;
}
var match = windowsDetailsPattern.exec(stdout);
if (match) {
var detail = {
drive: match[2],
total: match[3],
used: match[3] - match[1],
free: match[1]
};
callback(error, detail);
}
else {
callback(new Error('Parsing error: ' + stdout));
}
}
)
}
var windowsDriveListPattern = /\r\n(\w:)/g;
function detailPosix(drive, callback) {
fd.detail(drive, function (error, total, used, free) {
var detail = {
drive: drive,
total: total,
used: used,
free: free
};
callback(error, detail);
})
}
function driveList(callback) {
if (arguments.length === 0){
return driveListPromise();
}
else{
driveListCallback(callback);
}
}
function driveListPromise(){
var deferred = Q.defer();
driveListCallback(function (error, drivelist) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(drivelist);
}
});
return deferred.promise;
}
function driveListCallback(callback){
if (os.platform() == "win32") {
driveListWindows(callback);
}
else {
driveListPosix(callback);
}
}
function driveListWindows(callback) {
cp.exec("wmic logicaldisk get caption,providername,drivetype,volumename", function (error, stdout) {
if (error) {
callback(error);
return;
}
var drives = stdout.match(windowsDriveListPattern).map(function (x) {
return x.substr(2, 2);
});
callback(undefined, drives);
})
}
function driveListPosix(callback) {
fd.drivelist(callback);
}