This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
devCheck.cpp
238 lines (220 loc) · 8.81 KB
/
devCheck.cpp
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
#include <pcbsd-utils.h>
#include "devCheck.h"
DevCheck::DevCheck(){
//Initialize the lists of valid device types
validDevs << "da" << "ad" << "mmcsd" << "cd" << "acd" << "md" << "da";
validDevTypes << "USB" << "SATA" << "SD" << "CD9660" << "CD9660" << "ISO" << "SCSI";
for(int i=0; i<validDevs.length(); i++){
devFilter << validDevs[i]+"*";
}
//Initialize lists of filesystems and detection strings
fsDetection.clear();
fsMatch.clear();
fsDetection << "FAT" << "NTFS" << "EXT" << "ISO 9660" << "Unix Fast File system" << "Reiser" << "XFS"; //string to match for a particular filesystem
fsMatch << "FAT" << "NTFS" << "EXT" << "CD9660" << "UFS" << "REISERFS" << "XFS"; //internal labels for the filesystems
fsFilter << "fat" << "ntfs" << "ext" << "cdrom" << "ufs" << "reiser" << "xfs"; //label categories in /dev/
//Initialize the device directory
devDir = QDir(DEVICEDIR);
}
DevCheck::~DevCheck(){
}
/*
PUBLIC FUNCTIONS
*/
bool DevCheck::isValid(QString node){
bool ok = FALSE;
for(int i=0; i<validDevs.length(); i++){
if(node.startsWith(validDevs[i]) && node!="mdctl"){
ok = TRUE;
break;
}
}
return ok;
}
QStringList DevCheck::devChildren(QString node){
devDir.cd(DEVICEDIR);
QStringList subdevs;
if(node.isEmpty()){
subdevs = devDir.entryList(devFilter, QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System, QDir::NoSort);
}else{
subdevs = devDir.entryList(QStringList(node+"*"), QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System, QDir::NoSort);
}
//Clean up the list of children
for(int i=0; i<subdevs.length(); i++){
if( (subdevs[i] == node) || subdevs[i].isEmpty() ){
subdevs.removeAt(i);
i--;
}
}
return subdevs;
}
QString DevCheck::devLabel(QString node, QString filesystem){
QString dlabel;
QStringList glout = pcbsd::Utils::runShellCommand("glabel list");
int index = glout.indexOf("Geom name: "+node);
while(index != -1){
for(int i=index; i<glout.length(); i++){
if(glout[i].contains("Name: ")){ index = i; break;} //should only be 1 or 2 lines to find this
}
if(index == -1){ break; } //quick check to make sure we don't error out
QString path = glout[index].section("Name:",1,10,QString::SectionSkipEmpty).simplified();
int fschk = fsMatch.indexOf(filesystem);
if(fschk != -1){
if(fsFilter[fschk] == path.section("/",0,0).simplified() ){
//good label
dlabel = path.section("/",-1).simplified();
break;
}
}
index = glout.indexOf("Geom name: "+node, index); //move to the next index if there is one
}
return dlabel;
}
bool DevCheck::devInfo(QString dev, QString* type, QString* label, QString* filesystem, QString* maxsize){
//OUTPUT: bool isGoodDevice
//INPUTS/OUTPUTS: type, label, filesystem, maxsize
//INPUT: dev = device node (da0, not /dev/da0)
//Clear the output variables
type->clear(); label->clear(); filesystem->clear(); maxsize->clear();
//make sure to have both the full path and just node
QString node;
QString fullDev;
if(dev.startsWith(DEVICEDIR)){ fullDev = dev; node = dev.section("/",-1); }
else{ node = dev; fullDev = DEVICEDIR + dev; }
//Do not allow sym-links
if(!QFile::symLinkTarget(fullDev).isEmpty()){ return FALSE; }
//Double check for valid device types (just in case)
QString detType;
for(int i=0; i<validDevs.length(); i++){
if(node.startsWith(validDevs[i]) && node != "mdctl"){
detType = validDevTypes[i];
break;
}
}
QString camctl;
if(detType == "USB" && QFile::exists(fullDev)){
//make sure that it is not a SCSI device
camctl = pcbsd::Utils::runShellCommand( QString("camcontrol inquiry ")+node ).join(" ");
if(camctl.contains(" Fixed Direct Access SCSI")){ detType = "SCSI"; } //USB devices do not have any output
if(camctl.contains("camcontrol")){ camctl.clear(); } //error or invalid device type
}
//Make sure we quit before running commands on any invalid device nodes
if(detType.isEmpty() || !QFile::exists(fullDev) ){return FALSE;}
else{type->append(detType);}
bool isCD=FALSE;
if(detType == "CD9660" || detType == "ISO"){ isCD=TRUE; }
//Read the Device Info using "file -s <device>"
QString cmd = "file -s "+fullDev;
QString output = pcbsd::Utils::runShellCommand(cmd).join(" ");
//if(isCD){ qDebug() << "File -s output:" << output; }
// ** Get the max storage size **
int kb = 0;
bool hasPartitions = FALSE;
bool isMounted = FALSE;
if( !isCD ){
QStringList tmp = output.split(",");
if( !tmp.filter("partition ").isEmpty() ){
//Check for actual sub-devices (*s[#][a/b/c/....])
if( devChildren(node).length() > 0 ){ hasPartitions = TRUE; }
}
if( !tmp.filter("last mounted on /").isEmpty() && (detType == "SATA")){
isMounted = TRUE;
}
//Now try to get the size of the disk
if( !tmp.filter("number of data blocks").isEmpty() ){
tmp = tmp.filter("number of data blocks");
kb = tmp[0].remove("number of data blocks").simplified().toInt();
}else if( !tmp.filter("number of blocks").isEmpty() ){
tmp = tmp.filter("number of blocks");
kb = tmp[0].remove("number of blocks").simplified().toInt();
}else if( !tmp.filter("hidden sectors").isEmpty()){
tmp = tmp.filter("hidden sectors");
kb = tmp[0].remove("hidden sectors").simplified().toInt();
}else if( !tmp.filter("sectors").isEmpty()){
tmp = tmp.filter("sectors");
//qDebug() << "Det Sectors line:"<<tmp;
int num=0;
for(int i=0; i<tmp.length(); i++){
int n = tmp[i].remove("sectors").section("(",0,0).simplified().toInt();
if(n > num){ num = n; }
}
kb = num;
}else{ kb = -1; }
} //end non-CD size and isMounted detection
bool oksize = (kb > 0);
//First try to get the device label using the "file -s" output
QString dlabel;
if(isCD){
if( !output.contains("ERROR:") ){
dlabel = output.section("'",-2).remove("'").simplified();
if(dlabel.contains("(")){ dlabel = dlabel.left(dlabel.indexOf("(")+1).trimmed();}
}
}else{
dlabel = output.section("label: \"",1,1).section("\"",0,0).simplified(); //device name
}
// - trim the label out of the output line for filesystem type detection
QString devFSsec, devFSsec2;
if(!isCD){
devFSsec = output.section("label:",0,0);
devFSsec2 = output.section("label:",1,3).section(",",1,1,QString::SectionSkipEmpty);
}else{
devFSsec = output.simplified();
}
// ** Find the filesystem type for the device **
QString filesys;
for(int i=0; i<fsDetection.length(); i++){
if(devFSsec.contains(fsDetection[i]) || devFSsec2.contains(fsDetection[i]) ){
filesys = fsMatch[i];
}
}
//If the filesystem could not be detected or is not supported
bool hasFS = TRUE;
if(filesys.isEmpty()){ filesys = "UNKNOWN"; hasFS=FALSE; }
//Now get the device label (if there is one) using glabel
bool hasLabel = FALSE;
QString glabel;
if(!isCD){ glabel = devLabel(node, filesys); }
//Check to see if we have a label, otherwise assign one
if( !glabel.isEmpty() ){ dlabel = glabel; hasLabel = TRUE; } //glabel
else if(!dlabel.isEmpty()){ hasLabel = TRUE; } //file -s label
else if( !camctl.isEmpty() ){
//not necessarily a "detected" label, but just an alternate fallback name
dlabel = camctl.section(">",0,0).section("<",-1).section(" ",0,0).simplified();
}else{
//Assign a device label
if(isCD){
if(detType == "ISO"){
dlabel = "ISO_File";
}else{
dlabel = "Optical_Disk";
}
}else{
dlabel = detType+"-Device"; //this is not a "detected" label
}
}
//make sure that a device label does not contain "(" or ")"
if(dlabel.contains("(")){ dlabel = dlabel.remove("(").simplified(); }
if(dlabel.contains(")")){ dlabel = dlabel.remove(")").simplified(); }
dlabel = dlabel.simplified();
//Now perform the final checks to see if it is a good device
bool good = FALSE;
if( isMounted ){}//Always ignore this device (local FreeBSD installation that is being used)
else if( hasPartitions ){} //Ignore devices that have partitions accessible as seperate devices
else if( hasFS && isCD ){ good = TRUE; } //CD/DVD data disks don't have as much info
//Allow devices that match 2 of the 3 criteria
else if( hasFS && oksize ){ good = TRUE; } //This will catch most good devices
else if( hasLabel && oksize ){ good = TRUE; } //allow unknown filesystems if there is a good size reading
else if( hasFS && hasLabel ){ good = TRUE; } // allow device if it has a known label and filesystem
//Now setup the outputs as appropriate
maxsize->append( QString::number(kb) );
label->append(dlabel);
filesystem->append(filesys);
if(!good){
qDebug() << "Invalid Device:" << node << detType << dlabel << filesys << kb;
if(DEBUG_MODE){qDebug() << " -Detected Flags:" << isMounted << hasPartitions << hasLabel << hasFS << oksize;}
}
return good;
}
/*
PRIVATE FUNCTIONS
*/