Root/
Source at commit 7 created 14 years 4 months ago. By rekursor, Added a version file in the bin directory, refreshed the binaries for 10.5 and 10.6 | |
---|---|
1 | /*␊ |
2 | * shell_process.cpp␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/2010.␊ |
5 | *␊ |
6 | */␊ |
7 | ␊ |
8 | #include "process.h"␊ |
9 | #include <string.h>␊ |
10 | ␊ |
11 | //----------------------------------------------------------------␊ |
12 | // portable open process:␊ |
13 | FILE * ShellProcess::open(const char *cmd, const char *mode) {␊ |
14 | ␉_fpt=::popen(cmd,mode);␊ |
15 | ␉return _fpt;␊ |
16 | }␊ |
17 | ␊ |
18 | //----------------------------------------------------------------␊ |
19 | int ShellProcess::close() {␊ |
20 | ␉int ret = ::pclose(_fpt);␊ |
21 | ␉_fpt=NULL;␊ |
22 | ␉return ret;␊ |
23 | }␊ |
24 | ␊ |
25 | //----------------------------------------------------------------␊ |
26 | void PartitionInfo::removeSpaces(std::string &str)␊ |
27 | {␊ |
28 | ␉if (!str.size()) return;␊ |
29 | ␉int posl=0,posr=0;␊ |
30 | ␉for (posl=0; posl<str.size()-1 && str[posl]==' '; posl++);␊ |
31 | ␉for (posr=str.size()-1; posr>0 && str[posr]==' '; posr--);␊ |
32 | ␉str = (posl>posr) ? "" : str.substr(posl, posr-posl+1);␊ |
33 | }␊ |
34 | //----------------------------------------------------------------␊ |
35 | /**␊ |
36 | * ␉return the image index corresponding to a fs:␊ |
37 | */␊ |
38 | int PartitionInfo::indexFromFs() const␊ |
39 | {␊ |
40 | ␉if (_fsType.find("Apple")!=std::string::npos ||␊ |
41 | ␉␉_fsType.find("HFS")!=std::string::npos)␊ |
42 | ␉␉return 0; // Windows␊ |
43 | ␉else if (_fsType.find("Windows")!=std::string::npos ||␊ |
44 | ␉␉␉ _fsType.find("NTFS")!=std::string::npos ||␊ |
45 | ␉␉␉ _fsType.find("FAT")!=std::string::npos)␊ |
46 | ␉␉return 1; // Windows␊ |
47 | ␉else if (_fsType.find("Linux")!=std::string::npos)␊ |
48 | ␉␉return 2; // Unknown␊ |
49 | ␉return 10; //Unknown␊ |
50 | ␉␊ |
51 | }␊ |
52 | //----------------------------------------------------------------␊ |
53 | bool PartitionInfo::fromPartitionHdString(const char * inHdStr)␊ |
54 | {␊ |
55 | ␉if (!inHdStr || !(*inHdStr) || ␊ |
56 | ␉␉strlen(inHdStr)<7 || !strstr(inHdStr,"hd(")) return false;␊ |
57 | ␉␊ |
58 | ␉// enhance me here: should not assume that we have less than 10 partitions per disk/ disks␊ |
59 | ␉_disk = inHdStr[3]-'0'; ␊ |
60 | ␉_part = inHdStr[5]-'0';␊ |
61 | ␉return true;␊ |
62 | }␊ |
63 | //----------------------------------------------------------------␊ |
64 | const char * PartitionExtractor::checkForRename(const char * label, const char *szHd)␊ |
65 | {␊ |
66 | ␉const int MAX_ALIAS_SIZE=31;␊ |
67 | ␉static char szAlias[MAX_ALIAS_SIZE+1];␊ |
68 | ␉char *q=szAlias;␊ |
69 | ␉const char* szAliases = _renamedParts.c_str();␊ |
70 | ␉␊ |
71 | ␉if (!szHd || !*szHd || !szAliases || !*szAliases) return label; // no renaming wanted␊ |
72 | ␉␊ |
73 | ␉const char * p = strstr(szAliases, szHd);␊ |
74 | ␉if(!p || !(*p)) return label; // this volume must not be renamed, or option is malformed␊ |
75 | ␉␊ |
76 | ␉p+= strlen(szHd); // skip the "hd(n,m) " field␊ |
77 | ␉// multiple aliases can be found separated by a semi-column␊ |
78 | ␉while(*p && *p != ';' && q<(szAlias+MAX_ALIAS_SIZE)) *q++=*p++;␊ |
79 | ␉*q='\0';␊ |
80 | ␉␊ |
81 | ␉return szAlias;␊ |
82 | ␉␊ |
83 | }␊ |
84 | //----------------------------------------------------------------␊ |
85 | const std::vector<PartitionInfo>& ␊ |
86 | PartitionExtractor::extractPartitions(const char* szHide, const char* szRenamed)␊ |
87 | {␊ |
88 | ␉const char * const diskTag = "/dev/disk";␊ |
89 | ␉const char * const nameTag = "NAME";␊ |
90 | ␉const char * const sizeTag = "SIZE";␊ |
91 | ␊ |
92 | ␉PartitionInfo partInfo;␊ |
93 | ␊ |
94 | ␉char line[1024]="";␊ |
95 | ␉char label[32]="", fsType[32]="" ;␊ |
96 | ␉size_t len=0;␊ |
97 | ␉int disk =0, part=0;␊ |
98 | ␉int label_pos=0, size_pos=0;␊ |
99 | ␉char * p=0,*q=0;␊ |
100 | ␉int skipwhite=0;␊ |
101 | ␉␊ |
102 | ␉_partList.clear();␊ |
103 | ␉if (szHide) hidePartitions(szHide);␊ |
104 | ␉if (szRenamed) renamedPartitions(szRenamed);␊ |
105 | ␉this->open("diskutil list");␊ |
106 | ␉␊ |
107 | ␉while(get_line(line, sizeof(line)-1))␊ |
108 | ␉{␊ |
109 | ␉␉// printf("%s\n",line);␊ |
110 | ␉␉len = strlen(line);␊ |
111 | ␉␉for(skipwhite=0; line[skipwhite]==' ';skipwhite++);␊ |
112 | ␉␉␊ |
113 | ␉␉const char * sdisk = strstr(line, diskTag);␊ |
114 | ␉␉const char * sName = strstr(line, nameTag);␊ |
115 | ␉␉const char * sSize = strstr(line, sizeTag);␊ |
116 | ␉␉␊ |
117 | ␉␉if (sdisk)␊ |
118 | ␉␉{␊ |
119 | ␉␉␉// extract disk number␊ |
120 | ␉␉␉disk= sdisk[strlen(diskTag)]-'0';␊ |
121 | ␉␉␉if (disk>=0 && disk <MAX_HD)␊ |
122 | ␉␉␉␉disk = _hdRedirTable[disk];␊ |
123 | ␉␉}␊ |
124 | ␉␉else if (len && line[skipwhite]=='#' && line[skipwhite+1]==':')␊ |
125 | ␉␉{␊ |
126 | ␉␉␉label_pos = sName ? (sName - line) : 0;␊ |
127 | ␉␉␉size_pos = sSize ? (sSize - line) : 0;␊ |
128 | ␉␉}␊ |
129 | ␉␉else if (len && line[skipwhite+1]==':' && isdigit(line[skipwhite]))␊ |
130 | ␉␉{␊ |
131 | ␉␉␉for (p=&line[skipwhite+2], q=fsType; *p && p < &line[label_pos-1]; p++,q++) *q=*p;␊ |
132 | ␉␉␉for (p=&line[label_pos], q=label; *p && p < &line[size_pos]; p++,q++) *q=*p;␊ |
133 | ␉␉␉␊ |
134 | ␉␉␉*q='\0';␊ |
135 | ␉␉␉part = line[skipwhite]-'0';␊ |
136 | ␉␉␉partInfo.fsType(fsType);␊ |
137 | ␉␉␉partInfo.disk(disk);␊ |
138 | ␉␉␉partInfo.partition(part);␊ |
139 | ␉␉␉partInfo.label(checkForRename(label, partInfo.toHdStr().c_str()));␊ |
140 | ␉␉␉// filter useless partitions:␊ |
141 | ␉␉␉if (partInfo.label()!="*" && ␊ |
142 | ␉␉␉␉partInfo.fsType()!="EFI" &&␊ |
143 | ␉␉␉␉partInfo.fsType()!="Apple_partition_scheme" &&␊ |
144 | ␉␉␉␉partInfo.fsType()!="Apple_partition_map" &&␊ |
145 | ␉␉␉␉partInfo.fsType()!="Apple_Free" ␊ |
146 | ␉␉␉␉)␊ |
147 | ␉␉␉{␊ |
148 | ␉␉␉␉if (partInfo.label().size()==0) partInfo.label("Untitled");␊ |
149 | ␉␉␉␉if(_hiddenParts.length()==0 || _hiddenParts.find(partInfo.toHdStr()) == std::string::npos)␊ |
150 | ␉␉␉␉␉_partList.push_back(partInfo);␊ |
151 | ␉␉␉}␊ |
152 | ␉␉}␊ |
153 | ␉}␊ |
154 | ␉close();␊ |
155 | ␉␊ |
156 | ␉sort(_partList.begin(), _partList.end(), isDiskIndexInf);␊ |
157 | ␉return _partList;␊ |
158 | }␊ |
159 | ␊ |
160 | /** ␊ |
161 | * Get the index in the internal partlist of the hd(n,m) partition specified by the input string␊ |
162 | * return -1 if no match, >=0 if a match happens␊ |
163 | */␊ |
164 | int PartitionExtractor::getIndexFromHdStringSpec(const char* inHDStr)␊ |
165 | {␉␊ |
166 | ␉PartitionInfo info;␊ |
167 | ␉␊ |
168 | ␉if (info.fromPartitionHdString(inHDStr)) // try decode the partition disk and part infos␊ |
169 | ␉{␊ |
170 | ␉␉for (int i=0; i<_partList.size(); i++)␊ |
171 | ␉␉{␊ |
172 | ␉␉␉if (_partList[i].disk() == info.disk() && _partList[i].partition() == info.partition())␊ |
173 | ␉␉␉␉return i;␊ |
174 | ␉␉}␊ |
175 | ␉}␊ |
176 | ␉␊ |
177 | ␉return -1;␊ |
178 | }␊ |
179 |