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