source: src-qt4/pc-mounttray/devCheck.cpp @ f7d313c

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since f7d313c was f7d313c, checked in by Ken Moore <ken@…>, 11 months ago

Fix an issue where devices with a single child were still (incorrectly) listing the parent device as well.

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include <pcbsd-utils.h>
2#include "devCheck.h"
3
4
5DevCheck::DevCheck(){
6  //Initialize the lists of valid device types
7  validDevs << "da" << "ad" << "mmcsd" << "cd" << "acd" << "md";
8  validDevTypes << "USB" << "SATA" << "SD" << "CD9660" << "CD9660" << "ISO";
9  for(int i=0; i<validDevs.length(); i++){
10    devFilter << validDevs[i]+"*";
11  }
12  //Initialize lists of filesystems and detection strings
13  fsDetection.clear();
14  fsMatch.clear();
15  fsDetection << "FAT" << "NTFS" << "EXT" << "ISO 9660" << "Unix Fast File system" << "Reiser" << "XFS"; //string to match for a particular filesystem
16  fsMatch << "FAT" << "NTFS" << "EXT" << "CD9660" << "UFS" << "REISERFS" << "XFS"; //internal labels for the filesystems
17  fsFilter << "fat" << "ntfs" << "ext" << "cdrom" << "ufs" << "reiser" << "xfs"; //label categories in /dev/
18  //Initialize the device directory
19  devDir = QDir(DEVICEDIR);
20}
21
22DevCheck::~DevCheck(){
23}
24
25/*
26  PUBLIC FUNCTIONS
27*/
28
29bool DevCheck::isValid(QString node){
30  bool ok = FALSE;
31  for(int i=0; i<validDevs.length(); i++){
32    if(node.startsWith(validDevs[i]) && node!="mdctl"){ 
33        ok = TRUE; 
34        break; 
35    }
36  }
37  return ok;
38}
39
40QStringList DevCheck::devChildren(QString node){
41  devDir.cd(DEVICEDIR);
42  QStringList subdevs;
43  if(node.isEmpty()){
44    subdevs = devDir.entryList(devFilter, QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System, QDir::NoSort);
45  }else{
46    subdevs = devDir.entryList(QStringList(node+"*"), QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System, QDir::NoSort);
47  }
48  //Clean up the list of children
49  for(int i=0; i<subdevs.length(); i++){
50    if( (subdevs[i] == node) || subdevs[i].isEmpty() ){
51      subdevs.removeAt(i);
52      i--;
53    }
54  }
55  return subdevs;
56}
57
58QString DevCheck::devLabel(QString node, QString filesystem){
59  QString dlabel;
60  QStringList glout = pcbsd::Utils::runShellCommand("glabel list");
61  int index = glout.indexOf("Geom name: "+node);
62  while(index != -1){
63    for(int i=index; i<glout.length(); i++){ 
64      if(glout[i].contains("Name: ")){ index = i; break;} //should only be 1 or 2 lines to find this
65    }
66    if(index == -1){ break; } //quick check to make sure we don't error out
67    QString path = glout[index].section("Name:",1,10,QString::SectionSkipEmpty).simplified(); 
68    int fschk = fsMatch.indexOf(filesystem);
69    if(fschk != -1){
70      if(fsFilter[fschk] == path.section("/",0,0).simplified() ){
71        //good label
72        dlabel = path.section("/",-1).simplified();
73        break;
74      }
75    }
76    index = glout.indexOf("Geom name: "+node, index); //move to the next index if there is one
77  }
78  return dlabel;
79}
80
81bool DevCheck::devInfo(QString dev, QString* type, QString* label, QString* filesystem, QString* maxsize){
82  //OUTPUT: bool isGoodDevice
83  //INPUTS/OUTPUTS: type, label, filesystem, maxsize
84  //INPUT: dev = device node (da0, not /dev/da0)
85       
86  //Clear the output variables
87  type->clear(); label->clear(); filesystem->clear(); maxsize->clear();
88  //make sure to have both the full path and just node
89  QString node;
90  QString fullDev;
91  if(dev.startsWith(DEVICEDIR)){ fullDev = dev; node = dev.section("/",-1); }
92  else{ node = dev; fullDev = DEVICEDIR + dev; }
93  //Do not allow sym-links
94  if(!QFile::symLinkTarget(fullDev).isEmpty()){ return FALSE; }
95 
96  //Double check for valid device types (just in case)
97  QString detType;
98  for(int i=0; i<validDevs.length(); i++){
99    if(node.startsWith(validDevs[i]) && node != "mdctl"){ 
100        detType = validDevTypes[i]; 
101        break; 
102    }
103  }
104  if(detType == "USB"){
105    //make sure that it is not a SCSI device
106   
107  }
108 
109  //Make sure we quit before running commands on any invalid device nodes
110  if(detType.isEmpty() || !QFile::exists(fullDev) ){return FALSE;}
111  else{type->append(detType);}
112  bool isCD=FALSE;
113  if(detType == "CD9660" || detType == "ISO"){ isCD=TRUE; }
114 
115  //Read the Device Info using "file -s <device>"
116  QString cmd = "file -s "+fullDev;
117  QString output = pcbsd::Utils::runShellCommand(cmd).join(" ");
118  //if(isCD){ qDebug() << "File -s output:" << output; }
119 
120  // ** Get the max storage size **
121  int kb = 0;
122  bool hasPartitions = FALSE; 
123  bool isMounted = FALSE;
124  if( !isCD ){
125    QStringList tmp = output.split(",");
126    if( !tmp.filter("partition ").isEmpty() ){
127      //Check for actual sub-devices (*s[#][a/b/c/....])
128      if( devChildren(node).length() > 0 ){ hasPartitions = TRUE; } //the main device will always make it length 1
129    }
130    if( !tmp.filter("last mounted on /").isEmpty() && (detType == "SATA")){
131      isMounted = TRUE;
132    }
133    //Now try to get the size of the disk
134    if( !tmp.filter("number of data blocks").isEmpty() ){
135      tmp = tmp.filter("number of data blocks");
136      kb = tmp[0].remove("number of data blocks").simplified().toInt();
137    }else if( !tmp.filter("number of blocks").isEmpty() ){
138      tmp = tmp.filter("number of blocks");
139      kb = tmp[0].remove("number of blocks").simplified().toInt();
140    }else if( !tmp.filter("hidden sectors").isEmpty()){
141      tmp = tmp.filter("hidden sectors");
142      kb = tmp[0].remove("hidden sectors").simplified().toInt();
143    }else if( !tmp.filter("sectors").isEmpty()){
144      tmp = tmp.filter("sectors");
145      //qDebug() << "Det Sectors line:"<<tmp;
146      int num=0;
147      for(int i=0; i<tmp.length(); i++){
148        int n = tmp[i].remove("sectors").section("(",0,0).simplified().toInt();
149        if(n > num){ num = n; }
150      }
151      kb = num;
152    }else{ kb = -1; }
153  } //end non-CD size and isMounted detection
154  bool oksize = (kb > 0);
155  //First try to get the device label using the "file -s" output
156  QString dlabel;
157  if(isCD){
158    if( !output.contains("ERROR:") ){
159      dlabel = output.section("'",-2).remove("'").simplified();
160      if(dlabel.contains("(")){ dlabel = dlabel.left(dlabel.indexOf("(")+1).trimmed();}
161    }
162  }else{
163   dlabel = output.section("label: \"",1,1).section("\"",0,0).simplified(); //device name
164  }
165  // - trim the label out of the output line for filesystem type detection
166  QString devFSsec, devFSsec2;
167  if(!isCD){
168    devFSsec = output.section("label:",0,0);
169    devFSsec2 = output.section("label:",1,3).section(",",1,1,QString::SectionSkipEmpty);
170  }else{
171    devFSsec = output.simplified();       
172  }
173  // ** Find the filesystem type for the device **
174  QString filesys;
175  for(int i=0; i<fsDetection.length(); i++){
176    if(devFSsec.contains(fsDetection[i]) || devFSsec2.contains(fsDetection[i]) ){
177      filesys = fsMatch[i]; 
178    }
179  }
180  //If the filesystem could not be detected or is not supported
181  bool hasFS = TRUE;
182  if(filesys.isEmpty()){ filesys = "UNKNOWN"; hasFS=FALSE; }
183 
184  //Now get the device label (if there is one) using glabel
185  bool hasLabel = FALSE;
186  QString glabel;
187  if(!isCD){ glabel = devLabel(node, filesys); }
188  //Check to see if we have a label, otherwise assign one
189  if( !glabel.isEmpty() ){ dlabel = glabel; hasLabel = TRUE; } //glabel
190  else if(!dlabel.isEmpty()){ hasLabel = TRUE; } //file -s label
191  else{
192    //Assign a device label
193    if(isCD){
194      if(detType == "ISO"){
195        dlabel = "ISO_File";
196      }else{
197        dlabel = "Optical_Disk";
198      }
199    }else{
200      dlabel = detType+"-Device"; //this is not a "detected" label
201    }
202  }
203  //make sure that a device label does not contain "(" or ")"
204  if(dlabel.contains("(")){ dlabel.remove("(").simplified(); }
205  if(dlabel.contains(")")){ dlabel.remove(")").simplified(); }
206  dlabel = dlabel.simplified();
207  //Now perform the final checks to see if it is a good device
208  bool good = FALSE;
209  if( isMounted ){}//Always ignore this device (local FreeBSD installation that is being used)
210  else if( hasPartitions ){} //Ignore devices that have partitions accessible as seperate devices
211  else if( hasFS && isCD ){ good = TRUE; } //CD/DVD data disks don't have as much info
212  //Allow devices that match 2 of the 3 criteria
213  else if( hasFS && oksize ){ good = TRUE; } //This will catch most good devices
214  else if( hasLabel && oksize ){ good = TRUE; } //allow unknown filesystems if there is a good size reading
215  else if( hasFS && hasLabel ){ good = TRUE; } // allow device if it has a known label and filesystem
216 
217  //Now setup the outputs as appropriate
218  maxsize->append( QString::number(kb) );
219  label->append(dlabel);
220  filesystem->append(filesys);
221 
222  if(!good){
223    qDebug() << "Invalid Device:" << node << detType << dlabel << filesys << kb; 
224    if(DEBUG_MODE){qDebug() << " -Detected Flags:" << isMounted << hasPartitions << hasLabel << hasFS << oksize;} 
225  }
226  return good;
227}
228
229/*
230  PRIVATE FUNCTIONS
231*/
232
233
Note: See TracBrowser for help on using the repository browser.