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

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 0cfe72f was 0cfe72f, checked in by Kris Moore <kris@…>, 19 months ago

Add all the changes for related using of split pcbsd-utils libs

  • Property mode set to 100644
File size: 7.9 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";
8  validDevTypes << "USB" << "SATA" << "SD" << "CD9660" << "CD9660";
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])){ 
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])){ 
100        detType = validDevTypes[i]; 
101        break; 
102    }
103  }
104 
105  //Make sure we quit before running commands on any invalid device nodes
106  if(detType.isEmpty() || !QFile::exists(fullDev) ){return FALSE;}
107  else{type->append(detType);}
108  bool isCD=FALSE;
109  if(detType == "CD9660"){ isCD=TRUE; }
110 
111  //Read the Device Info using "file -s <device>"
112  QString cmd = "file -s "+fullDev;
113  QString output = pcbsd::Utils::runShellCommand(cmd).join(" ");
114  //if(isCD){ qDebug() << "File -s output:" << output; }
115 
116  // ** Get the max storage size **
117  int kb = 0;
118  bool hasPartitions = FALSE; 
119  bool isMounted = FALSE;
120  if( !isCD ){
121    QStringList tmp = output.split(",");
122    if( !tmp.filter("partition ").isEmpty() ){
123      //Check for actual sub-devices (*s[#][a/b/c/....])
124      if( devChildren(node).length() > 1 ){ hasPartitions = TRUE; } //the main device will always make it length 1
125    }
126    if( !tmp.filter("last mounted on /").isEmpty() && (detType == "SATA")){
127      isMounted = TRUE;
128    }
129    //Now try to get the size of the disk
130    if( !tmp.filter("number of data blocks").isEmpty() ){
131      tmp = tmp.filter("number of data blocks");
132      kb = tmp[0].remove("number of data blocks").simplified().toInt();
133    }else if( !tmp.filter("number of blocks").isEmpty() ){
134      tmp = tmp.filter("number of blocks");
135      kb = tmp[0].remove("number of blocks").simplified().toInt();
136    }else if( !tmp.filter("hidden sectors").isEmpty()){
137      tmp = tmp.filter("hidden sectors");
138      kb = tmp[0].remove("hidden sectors").simplified().toInt();
139    }else if( !tmp.filter("sectors").isEmpty()){
140      tmp = tmp.filter("sectors");
141      //qDebug() << "Det Sectors line:"<<tmp;
142      int num=0;
143      for(int i=0; i<tmp.length(); i++){
144        int n = tmp[i].remove("sectors").section("(",0,0).simplified().toInt();
145        if(n > num){ num = n; }
146      }
147      kb = num;
148    }else{ kb = -1; }
149  } //end non-CD size and isMounted detection
150  bool oksize = (kb > 0);
151  //First try to get the device label using the "file -s" output
152  QString dlabel;
153  if(isCD){
154    if( !output.contains("ERROR:") ){
155      dlabel = output.section("'",-2).remove("'").simplified();
156      //qDebug() << dlabel;
157      //while( dlabel.endsWith(" ") || dlabel.endsWith("\n") ){ dlabel.chop(1); }
158      //qDebug() << dlabel;
159    }
160  }else{
161   dlabel = output.section("label: \"",1,1).section("\"",0,0).simplified(); //device name
162  }
163  // - trim the label out of the output line for filesystem type detection
164  QString devFSsec, devFSsec2;
165  if(!isCD){
166    devFSsec = output.section("label:",0,0);
167    devFSsec2 = output.section("label:",1,3).section(",",1,1,QString::SectionSkipEmpty);
168  }else{
169    devFSsec = output.simplified();       
170  }
171  // ** Find the filesystem type for the device **
172  QString filesys;
173  for(int i=0; i<fsDetection.length(); i++){
174    if(devFSsec.contains(fsDetection[i]) || devFSsec2.contains(fsDetection[i]) ){
175      filesys = fsMatch[i]; 
176    }
177  }
178  //If the filesystem could not be detected or is not supported
179  bool hasFS = TRUE;
180  if(filesys.isEmpty()){ filesys = "UNKNOWN"; hasFS=FALSE; }
181 
182  //Now get the device label (if there is one) using glabel
183  bool hasLabel = FALSE;
184  QString glabel;
185  if(!isCD){ glabel = devLabel(node, filesys); }
186  //Check to see if we have a label, otherwise assign one
187  if( !glabel.isEmpty() ){ dlabel = glabel; hasLabel = TRUE; } //glabel
188  else if(!dlabel.isEmpty()){ hasLabel = TRUE; } //file -s label
189  else{
190    //Assign a device label
191    if(isCD){
192      dlabel = "Optical_Disk";
193    }else{
194      dlabel = detType+"-Device"; //this is not a "detected" label
195    }
196  }
197 
198  //Now perform the final checks to see if it is a good device
199  bool good = FALSE;
200  if( isMounted ){}//Always ignore this device (local FreeBSD installation that is being used)
201  else if( hasPartitions ){} //Ignore devices that have partitions accessible as seperate devices
202  else if( hasFS && isCD ){ good = TRUE; } //CD/DVD data disks don't have as much info
203  //Allow devices that match 2 of the 3 criteria
204  else if( hasFS && oksize ){ good = TRUE; } //This will catch most good devices
205  else if( hasLabel && oksize ){ good = TRUE; } //allow unknown filesystems if there is a good size reading
206  else if( hasFS && hasLabel ){ good = TRUE; } // allow device if it has a known label and filesystem
207 
208  //Now setup the outputs as appropriate
209  maxsize->append( QString::number(kb) );
210  label->append(dlabel);
211  filesystem->append(filesys);
212 
213  if(!good){
214    qDebug() << "Invalid Device:" << node << detType << dlabel << filesys << kb; 
215    if(DEBUG_MODE){qDebug() << " -Detected Flags:" << isMounted << hasPartitions << hasLabel << hasFS << oksize;} 
216  }
217  return good;
218}
219
220/*
221  PRIVATE FUNCTIONS
222*/
223
224
Note: See TracBrowser for help on using the repository browser.