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