Chameleon Applications

Chameleon Applications Svn Source Tree

Root/ChameleonPrefPane/Sources/process.cpp

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

Archive Download this file

Revision: 1