1 | /*␊ |
2 | * shell_process.h␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/2010.␊ |
5 | *␊ |
6 | */␊ |
7 | #include <Security/Authorization.h>␊ |
8 | #include <stdio.h>␊ |
9 | #include <stdlib.h>␊ |
10 | #include <errno.h>␊ |
11 | #include <sys/stat.h>␊ |
12 | #include <vector>␊ |
13 | #include <string>␊ |
14 | ␊ |
15 | //----------------------------------------------------------------␊ |
16 | const int MAX_HD = 10;␊ |
17 | ␊ |
18 | //----------------------------------------------------------------␊ |
19 | ␊ |
20 | /**␊ |
21 | * Check whether file exists:␊ |
22 | */␊ |
23 | inline bool fileExists(const char * str) ␊ |
24 | { ␊ |
25 | ␉struct stat stFileInfo; ␊ |
26 | ␉if((stat(str,&stFileInfo)) == 0) return true; ␊ |
27 | ␉else return false;␊ |
28 | }␊ |
29 | ␊ |
30 | inline bool fileExists(std::string str) {return fileExists(str.c_str());}␊ |
31 | ␊ |
32 | //----------------------------------------------------------------␊ |
33 | class ShellProcess ␊ |
34 | {␊ |
35 | public:␊ |
36 | ␉// construction / destruction␊ |
37 | ␉ShellProcess() {_fpt= NULL;}␊ |
38 | ␉~ShellProcess() {if (_fpt) close();}␊ |
39 | ␉␊ |
40 | ␉FILE * open␉(const char *cmd, const char *mode="r");␊ |
41 | ␉int close();␊ |
42 | ␉FILE * desc() const { return _fpt;} // non null if file is open␊ |
43 | ␉char * get_line(char * line, size_t s) const {return _fpt ? fgets(line, s, _fpt) : NULL;}␊ |
44 | ␉␊ |
45 | protected:␊ |
46 | ␉FILE * _fpt;␊ |
47 | };␊ |
48 | ␊ |
49 | //----------------------------------------------------------------␊ |
50 | class PartitionInfo␊ |
51 | {␊ |
52 | public:␊ |
53 | ␉PartitionInfo() : _disk(0), _part(0) {}␊ |
54 | ␉PartitionInfo(int disk, int part) {␉set(disk, part);}␊ |
55 | ␉␊ |
56 | ␉void disk(int disk) { _disk=disk;}␊ |
57 | ␉int disk() const { return _disk;}␊ |
58 | ␉␊ |
59 | ␉void partition(int part) { _part=part;}␊ |
60 | ␉int partition () const { return _part;}␊ |
61 | ␉␊ |
62 | ␉const char * clabel() const { return _label.c_str();}␊ |
63 | ␉const std::string& label() const { return _label;␉}␊ |
64 | ␉void label(const char * l) { ␊ |
65 | ␉␉if (l) _label = l; ␊ |
66 | ␉␉removeSpaces(_label);␊ |
67 | ␉}␊ |
68 | ␊ |
69 | ␉const char * cfsType() const { return _fsType.c_str();}␊ |
70 | ␉const std::string& fsType() const { return _fsType;}␊ |
71 | ␉void fsType(const char * fs) { ␊ |
72 | ␉␉if (fs) _fsType = fs; ␊ |
73 | ␉␉removeSpaces(_fsType);␊ |
74 | ␉}␊ |
75 | ␉␉␊ |
76 | ␉void set(int disk, int part) { _disk =disk; _part = part;}␊ |
77 | ␉bool fromPartitionHdString(const char * inHdStr);␊ |
78 | ␉std::string toHdStr() const ␊ |
79 | ␉{␊ |
80 | ␉␉std::string buf = "hd(n,m)";␊ |
81 | ␉␉buf[3]= '0'+disk();␊ |
82 | ␉␉buf[5]='0'+partition();␊ |
83 | ␉␉return buf;␊ |
84 | ␉}␊ |
85 | ␉int imageIndexFromFs() const;␊ |
86 | protected:␊ |
87 | ␉void removeSpaces(std::string & s);␊ |
88 | ␊ |
89 | private:␊ |
90 | ␉int _disk, _part;␊ |
91 | ␉std::string _fsType, _label;␊ |
92 | };␊ |
93 | ␊ |
94 | static inline bool isDiskIndexInf(PartitionInfo i, PartitionInfo j) ␊ |
95 | { ␊ |
96 | ␉return ((i.disk()*100+i.partition()) < (j.disk()*100 + j.partition()) ) ;␊ |
97 | }␊ |
98 | ␊ |
99 | //----------------------------------------------------------------␊ |
100 | class PartitionExtractor : public ShellProcess␊ |
101 | {␊ |
102 | public:␊ |
103 | ␉PartitionExtractor() :␉ShellProcess() ␉{ init(); }␊ |
104 | ␉␊ |
105 | ␉const std::vector<PartitionInfo>& extractPartitions(␊ |
106 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉const char* szHide=NULL,␊ |
107 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉const char* szRenamed=NULL);␊ |
108 | ␉int getListCount() const {return (int) _partList.size();}␊ |
109 | ␉␊ |
110 | ␉const std::vector<PartitionInfo>& partList() const {return _partList;}␊ |
111 | ␉std::vector<PartitionInfo>& editPartList() {return _partList;}␊ |
112 | ␉␊ |
113 | ␉// get the index in the internal partlist of the hd(n,m) partition specified by the input string␊ |
114 | ␉// return -1 if no match, >=0 if a match happens␊ |
115 | ␉int getIndexFromHdStringSpec(const char*); ␊ |
116 | ␉␊ |
117 | ␉void hidePartitions(const char* szParts){ _hiddenParts = (szParts ? szParts : "");}␊ |
118 | ␉void renamedPartitions(const char* szParts){ _renamedParts = (szParts ? szParts : "");}␊ |
119 | ␉void sortPartList() {␉sort(_partList.begin(), _partList.end(), isDiskIndexInf);␉}␊ |
120 | ␊ |
121 | ␉const char * checkForRename(const char * label, const char* szHd);␊ |
122 | ␊ |
123 | ␉void swapHD(int src, int dst)␊ |
124 | ␉{␊ |
125 | ␉␉if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;␊ |
126 | ␉␉_hdRedirTable[src]=dst;␊ |
127 | ␉␉_hdRedirTable[dst]=src;␊ |
128 | ␉}␊ |
129 | ␉␊ |
130 | ␉void resetSwapping() { init();}␊ |
131 | ␉␊ |
132 | protected:␊ |
133 | ␉void init() {␊ |
134 | ␉␉for (int i=0; i<MAX_HD; i++) _hdRedirTable[i]=i;␊ |
135 | ␉}␊ |
136 | ␉␊ |
137 | private:␊ |
138 | ␉std::vector<PartitionInfo> _partList;␊ |
139 | ␉int _hdRedirTable[MAX_HD];␊ |
140 | ␉std::string _hiddenParts;␊ |
141 | ␉std::string _renamedParts;␊ |
142 | ␉␊ |
143 | };␊ |
144 | |