1 | /*␊ |
2 | * shell_process.cpp␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/2010.␊ |
5 | *␊ |
6 | */␊ |
7 | ␊ |
8 | #include "ShellProcess.h"␊ |
9 | #include <string.h>␊ |
10 | #include <sys/stat.h>␊ |
11 | #include <string_util.h>␊ |
12 | ␊ |
13 | ␊ |
14 | //----------------------------------------------------------------␊ |
15 | // portable open process:␊ |
16 | FILE * ShellProcess::open(const char *cmd, const char *mode) {␊ |
17 | ␉_fpt=::popen(cmd,mode);␊ |
18 | ␉return _fpt;␊ |
19 | }␊ |
20 | ␊ |
21 | //----------------------------------------------------------------␊ |
22 | int ShellProcess::close() {␊ |
23 | ␉int ret = ::pclose(_fpt);␊ |
24 | ␉_fpt=NULL;␊ |
25 | ␉return ret;␊ |
26 | }␊ |
27 | ␊ |
28 | //----------------------------------------------------------------␊ |
29 | void PartitionInfo::removeSpaces(std::string &str)␊ |
30 | {␊ |
31 | ␉if (!str.size()) return;␊ |
32 | ␉int posl=0,posr=0;␊ |
33 | ␉for (posl=0; posl<str.size()-1 && str[posl]==' '; posl++);␊ |
34 | ␉for (posr=str.size()-1; posr>0 && str[posr]==' '; posr--);␊ |
35 | ␉str = (posl>posr) ? "" : str.substr(posl, posr-posl+1);␊ |
36 | }␊ |
37 | //----------------------------------------------------------------␊ |
38 | /**␊ |
39 | * ␉return the image index corresponding to a fs:␊ |
40 | */␊ |
41 | int PartitionInfo::imageIndexFromFs() const␊ |
42 | {␊ |
43 | ␉if (_fsType.find("Apple")!=std::string::npos ||␊ |
44 | ␉␉_fsType.find("HFS")!=std::string::npos)␊ |
45 | ␉␉return 0; // Windows␊ |
46 | ␉else if (_fsType.find("Windows")!=std::string::npos ||␊ |
47 | ␉␉␉ _fsType.find("Microsoft")!=std::string::npos ||␊ |
48 | ␉␉␉ _fsType.find("NTFS")!=std::string::npos ||␊ |
49 | ␉␉␉ _fsType.find("FAT")!=std::string::npos)␊ |
50 | ␉␉return 1; // Windows␊ |
51 | ␉else if (_fsType.find("Linux")!=std::string::npos)␊ |
52 | ␉␉return 2; // Unknown␊ |
53 | ␉return 10; //Unknown␊ |
54 | ␉␊ |
55 | }␊ |
56 | //----------------------------------------------------------------␊ |
57 | bool PartitionInfo::fromPartitionHdString(const char * inHdStr)␊ |
58 | {␊ |
59 | ␉if (!inHdStr || !(*inHdStr) || ␊ |
60 | ␉␉strlen(inHdStr)<7 || !strstr(inHdStr,"hd(")) return false;␊ |
61 | ␉␊ |
62 | ␉// enhance me here: should not assume that we have less than 10 partitions per disk/ disks␊ |
63 | ␉_disk = inHdStr[3]-'0'; ␊ |
64 | ␉_part = inHdStr[5]-'0';␊ |
65 | ␉return true;␊ |
66 | }␊ |
67 | //----------------------------------------------------------------␊ |
68 | const char * PartitionExtractor::checkForRename(const char * label, const char *szHd)␊ |
69 | {␊ |
70 | ␉const int MAX_ALIAS_SIZE=31;␊ |
71 | ␉static char szAlias[MAX_ALIAS_SIZE+1];␊ |
72 | ␉char *q=szAlias;␊ |
73 | ␉const char* szAliases = _renamedParts.c_str();␊ |
74 | ␉␊ |
75 | ␉if (!szHd || !*szHd || !szAliases || !*szAliases) return label; // no renaming wanted␊ |
76 | ␉␊ |
77 | ␉const char * p = strstr(szAliases, szHd);␊ |
78 | ␉if(!p || !(*p)) return label; // this volume must not be renamed, or option is malformed␊ |
79 | ␉␊ |
80 | ␉p+= strlen(szHd); // skip the "hd(n,m) " field␊ |
81 | ␉// multiple aliases can be found separated by a semi-column␊ |
82 | ␉while(*p && *p != ';' && q<(szAlias+MAX_ALIAS_SIZE)) *q++=*p++;␊ |
83 | ␉*q='\0';␊ |
84 | ␉␊ |
85 | ␉return szAlias;␊ |
86 | ␉␊ |
87 | }␊ |
88 | ␊ |
89 | // uncomment the following line for debug␊ |
90 | #define DEBUG_DISKUTIL 0␊ |
91 | ␊ |
92 | //----------------------------------------------------------------␊ |
93 | const std::vector<PartitionInfo>& ␊ |
94 | PartitionExtractor::extractPartitions(const char* szHide, const char* szRenamed)␊ |
95 | {␊ |
96 | ␉const char * const diskTag = "/dev/disk";␊ |
97 | ␉const char * const nameTag = "NAME";␊ |
98 | ␉const char * const sizeTag = "SIZE";␊ |
99 | ␊ |
100 | ␉PartitionInfo partInfo;␊ |
101 | ␊ |
102 | ␉char line[1024]="";␊ |
103 | ␉char label[32]="", fsType[32]="" ;␊ |
104 | ␉size_t len=0;␊ |
105 | ␉int disk =0, part=0;␊ |
106 | ␉int label_pos=0, size_pos=0;␊ |
107 | ␉char * p=0,*q=0;␊ |
108 | ␉int skipwhite=0;␊ |
109 | ␉␊ |
110 | ␉_partList.clear();␊ |
111 | ␉if (szHide) hidePartitions(szHide);␊ |
112 | ␉if (szRenamed) renamedPartitions(szRenamed);␊ |
113 | ␊ |
114 | #if DEBUG_DISKUTIL␊ |
115 | ␉this->open("cat /diskutil.txt");␊ |
116 | #else␊ |
117 | ␉this->open("diskutil list");␊ |
118 | #endif␊ |
119 | ␉␊ |
120 | ␉while(get_line(line, sizeof(line)-1))␊ |
121 | ␉{␊ |
122 | ␉␉// printf("%s\n",line);␊ |
123 | ␉␉len = strlen(line);␊ |
124 | ␉␉for(skipwhite=0; line[skipwhite]==' ';skipwhite++);␊ |
125 | ␉␉␊ |
126 | ␉␉const char * sdisk = strstr(line, diskTag);␊ |
127 | ␉␉const char * sName = strstr(line, nameTag);␊ |
128 | ␉␉const char * sSize = strstr(line, sizeTag);␊ |
129 | ␉␉␊ |
130 | ␉␉if (sdisk)␊ |
131 | ␉␉{␊ |
132 | ␉␉␉// extract disk number␊ |
133 | ␉␉␉sscanf(&sdisk[strlen(diskTag)],"%d", &disk);␊ |
134 | ␉␉␉if (disk>=0 && disk <MAX_HD)␊ |
135 | ␉␉␉␉disk = _hdRedirTable[disk];␊ |
136 | ␉␉}␊ |
137 | ␉␉else if (len && line[skipwhite]=='#' && line[skipwhite+1]==':')␊ |
138 | ␉␉{␊ |
139 | ␉␉␉label_pos = sName ? (sName - line) : 0;␊ |
140 | ␉␉␉size_pos = sSize ? (sSize - line) : 0;␊ |
141 | ␉␉}␊ |
142 | ␉␉else if (len && line[skipwhite+1]==':' && isdigit(line[skipwhite]))␊ |
143 | ␉␉{␊ |
144 | ␉␉␉for (p=&line[skipwhite+2], q=fsType; *p && p < &line[label_pos-1]; p++,q++) *q=*p;␊ |
145 | ␉␉␉for (p=&line[label_pos], q=label; *p && p < &line[size_pos]; p++,q++) *q=*p;␊ |
146 | ␉␉␉␊ |
147 | ␉␉␉*q='\0';␊ |
148 | ␉␉␉part = line[skipwhite]-'0';␊ |
149 | ␉␉␉partInfo.fsType(fsType);␊ |
150 | ␉␉␉partInfo.disk(disk);␊ |
151 | ␉␉␉partInfo.partition(part);␊ |
152 | ␉␉␉partInfo.label(checkForRename(label, partInfo.toHdStr().c_str()));␊ |
153 | ␉␉␉size_t len = partInfo.label().length();␊ |
154 | ␉␉␉// filter useless partitions:␊ |
155 | ␉␉␉if ( len > 0 && ␊ |
156 | ␉␉␉␉partInfo.clabel()[len-1] !='*' && ␊ |
157 | ␉␉␉␉partInfo.fsType()!="EFI" &&␊ |
158 | ␉␉␉␉partInfo.fsType()!="Apple_partition_scheme" &&␊ |
159 | ␉␉␉␉partInfo.fsType()!="Apple_partition_map" &&␊ |
160 | ␉␉␉␉partInfo.fsType()!="Apple_Free" ␊ |
161 | ␉␉␉␉)␊ |
162 | ␉␉␉{␊ |
163 | ␉␉␉␉std::string diskLabel(trim(label)); ␊ |
164 | ␉␉␉␉bool found=false;␊ |
165 | ␉␉␉␉␊ |
166 | ␉␉␉␉// early bail out if we found what we need: DON't make any assumption ␊ |
167 | ␉␉␉␉// that Windows is the name of WIN32 bootable disk dir ...␊ |
168 | ␉␉␉␉if (␊ |
169 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/boot") || ␊ |
170 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/System/Library/Extensions") ||␊ |
171 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/ntldr") ||␊ |
172 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/bootmgr") ||␊ |
173 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/Boot") ||␊ |
174 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/pagefile.sys") ||␊ |
175 | ␉␉␉␉␉fileExists("/Volumes/" + diskLabel + "/hiberfil.sys")␊ |
176 | ␉␉␉␉␉)␊ |
177 | ␉␉␉␉␉found=true;␊ |
178 | ␉␉␉␉else if ((strstr(fsType,"Linux") ) && !strstr(fsType, "Linux_Swap"))␊ |
179 | ␉␉␉␉␉found=true; // Added Linux case␊ |
180 | ␉␉␉␉if (found)␊ |
181 | ␉␉␉␉{ //check if one of them exists␊ |
182 | ␉␉␉␉␉if (partInfo.label().size()==0) partInfo.label("Untitled");␊ |
183 | ␉␉␉␉␉if(_hiddenParts.length()==0 || ␊ |
184 | ␉␉␉␉␉ _hiddenParts.find(partInfo.toHdStr()) == std::string::npos)␊ |
185 | ␉␉␉␉␉␉_partList.push_back(partInfo);␊ |
186 | ␉␉␉␉}␊ |
187 | ␉␉␉}␊ |
188 | ␉␉}␊ |
189 | ␉}␊ |
190 | ␉close();␊ |
191 | ␉␊ |
192 | ␉sort(_partList.begin(), _partList.end(), isDiskIndexInf);␊ |
193 | ␉return _partList;␊ |
194 | }␊ |
195 | ␊ |
196 | /** ␊ |
197 | * Get the index in the internal partlist of the hd(n,m) partition specified by the input string␊ |
198 | * return -1 if no match, >=0 if a match happens␊ |
199 | */␊ |
200 | int PartitionExtractor::getIndexFromHdStringSpec(const char* inHDStr)␊ |
201 | {␉␊ |
202 | ␉PartitionInfo info;␊ |
203 | ␉␊ |
204 | ␉if (info.fromPartitionHdString(inHDStr)) // try decode the partition disk and part infos␊ |
205 | ␉{␊ |
206 | ␉␉for (int i=0; i<_partList.size(); i++)␊ |
207 | ␉␉{␊ |
208 | ␉␉␉if (_partList[i].disk() == info.disk() && _partList[i].partition() == info.partition())␊ |
209 | ␉␉␉␉return i;␊ |
210 | ␉␉}␊ |
211 | ␉}␊ |
212 | ␉␊ |
213 | ␉return -1;␊ |
214 | }␊ |
215 | |