Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/process.cpp

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::imageIndexFromFs() 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("Microsoft")!=std::string::npos ||
45 _fsType.find("NTFS")!=std::string::npos ||
46 _fsType.find("FAT")!=std::string::npos)
47return 1; // Windows
48else if (_fsType.find("Linux")!=std::string::npos)
49return 2; // Unknown
50return 10; //Unknown
51
52}
53//----------------------------------------------------------------
54bool PartitionInfo::fromPartitionHdString(const char * inHdStr)
55{
56if (!inHdStr || !(*inHdStr) ||
57strlen(inHdStr)<7 || !strstr(inHdStr,"hd(")) return false;
58
59// enhance me here: should not assume that we have less than 10 partitions per disk/ disks
60_disk = inHdStr[3]-'0';
61_part = inHdStr[5]-'0';
62return true;
63}
64//----------------------------------------------------------------
65const char * PartitionExtractor::checkForRename(const char * label, const char *szHd)
66{
67const int MAX_ALIAS_SIZE=31;
68static char szAlias[MAX_ALIAS_SIZE+1];
69char *q=szAlias;
70const char* szAliases = _renamedParts.c_str();
71
72if (!szHd || !*szHd || !szAliases || !*szAliases) return label; // no renaming wanted
73
74const char * p = strstr(szAliases, szHd);
75if(!p || !(*p)) return label; // this volume must not be renamed, or option is malformed
76
77p+= strlen(szHd); // skip the "hd(n,m) " field
78// multiple aliases can be found separated by a semi-column
79while(*p && *p != ';' && q<(szAlias+MAX_ALIAS_SIZE)) *q++=*p++;
80*q='\0';
81
82return szAlias;
83
84}
85//----------------------------------------------------------------
86const std::vector<PartitionInfo>&
87PartitionExtractor::extractPartitions(const char* szHide, const char* szRenamed)
88{
89const char * const diskTag = "/dev/disk";
90const char * const nameTag = "NAME";
91const char * const sizeTag = "SIZE";
92
93PartitionInfo partInfo;
94
95char line[1024]="";
96char label[32]="", fsType[32]="" ;
97size_t len=0;
98int disk =0, part=0;
99int label_pos=0, size_pos=0;
100char * p=0,*q=0;
101int skipwhite=0;
102
103_partList.clear();
104if (szHide) hidePartitions(szHide);
105if (szRenamed) renamedPartitions(szRenamed);
106this->open("diskutil list");
107
108while(get_line(line, sizeof(line)-1))
109{
110// printf("%s\n",line);
111len = strlen(line);
112for(skipwhite=0; line[skipwhite]==' ';skipwhite++);
113
114const char * sdisk = strstr(line, diskTag);
115const char * sName = strstr(line, nameTag);
116const char * sSize = strstr(line, sizeTag);
117
118if (sdisk)
119{
120// extract disk number
121disk= sdisk[strlen(diskTag)]-'0';
122if (disk>=0 && disk <MAX_HD)
123disk = _hdRedirTable[disk];
124}
125else if (len && line[skipwhite]=='#' && line[skipwhite+1]==':')
126{
127label_pos = sName ? (sName - line) : 0;
128size_pos = sSize ? (sSize - line) : 0;
129}
130else if (len && line[skipwhite+1]==':' && isdigit(line[skipwhite]))
131{
132for (p=&line[skipwhite+2], q=fsType; *p && p < &line[label_pos-1]; p++,q++) *q=*p;
133for (p=&line[label_pos], q=label; *p && p < &line[size_pos]; p++,q++) *q=*p;
134
135*q='\0';
136part = line[skipwhite]-'0';
137partInfo.fsType(fsType);
138partInfo.disk(disk);
139partInfo.partition(part);
140partInfo.label(checkForRename(label, partInfo.toHdStr().c_str()));
141// filter useless partitions:
142if (partInfo.label()!="*" &&
143partInfo.fsType()!="EFI" &&
144partInfo.fsType()!="Apple_partition_scheme" &&
145partInfo.fsType()!="Apple_partition_map" &&
146partInfo.fsType()!="Apple_Free"
147)
148{
149if (partInfo.label().size()==0) partInfo.label("Untitled");
150if(_hiddenParts.length()==0 || _hiddenParts.find(partInfo.toHdStr()) == std::string::npos)
151_partList.push_back(partInfo);
152}
153}
154}
155close();
156
157sort(_partList.begin(), _partList.end(), isDiskIndexInf);
158return _partList;
159}
160
161/**
162 * Get the index in the internal partlist of the hd(n,m) partition specified by the input string
163 * return -1 if no match, >=0 if a match happens
164 */
165int PartitionExtractor::getIndexFromHdStringSpec(const char* inHDStr)
166{
167PartitionInfo info;
168
169if (info.fromPartitionHdString(inHDStr)) // try decode the partition disk and part infos
170{
171for (int i=0; i<_partList.size(); i++)
172{
173if (_partList[i].disk() == info.disk() && _partList[i].partition() == info.partition())
174return i;
175}
176}
177
178return -1;
179}
180

Archive Download this file

Revision: 14