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 std::string& label() const { return _label;␉}␊ |
49 | ␉void label(const char * l) { ␊ |
50 | ␉␉if (l) _label = l; ␊ |
51 | ␉␉removeSpaces(_label);␊ |
52 | ␉}␊ |
53 | ␊ |
54 | ␉const std::string& fsType() const { return _fsType;}␊ |
55 | ␉void fsType(const char * fs) { ␊ |
56 | ␉␉if (fs) _fsType = fs; ␊ |
57 | ␉␉removeSpaces(_fsType);␊ |
58 | ␉}␊ |
59 | ␉␉␊ |
60 | ␉void set(int disk, int part) { _disk =disk; _part = part;}␊ |
61 | ␉bool fromPartitionHdString(const char * inHdStr);␊ |
62 | ␉std::string toHdStr() const ␊ |
63 | ␉{␊ |
64 | ␉␉std::string buf = "hd(n,m)";␊ |
65 | ␉␉buf[3]= '0'+disk();␊ |
66 | ␉␉buf[5]='0'+partition();␊ |
67 | ␉␉return buf;␊ |
68 | ␉}␊ |
69 | ␉␊ |
70 | ␉int imageIndexFromFs() const;␊ |
71 | protected:␊ |
72 | ␉void removeSpaces(std::string & s);␊ |
73 | ␊ |
74 | private:␊ |
75 | ␉int _disk, _part;␊ |
76 | ␉std::string _fsType, _label;␊ |
77 | };␊ |
78 | ␊ |
79 | static inline bool isDiskIndexInf(PartitionInfo i, PartitionInfo j) ␊ |
80 | { ␊ |
81 | ␉return ((i.disk()*100+i.partition()) < (j.disk()*100 + j.partition()) ) ;␊ |
82 | }␊ |
83 | ␊ |
84 | //----------------------------------------------------------------␊ |
85 | class PartitionExtractor : public ShellProcess␊ |
86 | {␊ |
87 | public:␊ |
88 | ␉PartitionExtractor() :␉ShellProcess() ␉{ init(); }␊ |
89 | ␉␊ |
90 | ␉const std::vector<PartitionInfo>& extractPartitions(␊ |
91 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉const char* szHide=NULL,␊ |
92 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉const char* szRenamed=NULL);␊ |
93 | ␉int getListCount() const {return (int) _partList.size();}␊ |
94 | ␉␊ |
95 | ␉const std::vector<PartitionInfo>& partList() const {return _partList;}␊ |
96 | ␉std::vector<PartitionInfo>& editPartList() {return _partList;}␊ |
97 | ␉␊ |
98 | ␉// get the index in the internal partlist of the hd(n,m) partition specified by the input string␊ |
99 | ␉// return -1 if no match, >=0 if a match happens␊ |
100 | ␉int getIndexFromHdStringSpec(const char*); ␊ |
101 | ␉␊ |
102 | ␉void hidePartitions(const char* szParts){ _hiddenParts = (szParts ? szParts : "");}␊ |
103 | ␉void renamedPartitions(const char* szParts){ _renamedParts = (szParts ? szParts : "");}␊ |
104 | ␉const char * checkForRename(const char * label, const char* szHd);␊ |
105 | ␊ |
106 | ␉void swapHD(int src, int dst)␊ |
107 | ␉{␊ |
108 | ␉␉if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;␊ |
109 | ␉␉_hdRedirTable[src]=dst;␊ |
110 | ␉␉_hdRedirTable[dst]=src;␊ |
111 | ␉}␊ |
112 | ␉␊ |
113 | ␉void resetSwapping() { init();}␊ |
114 | ␉␊ |
115 | protected:␊ |
116 | ␉void init() {␊ |
117 | ␉␉for (int i=0; i<MAX_HD; i++) _hdRedirTable[i]=i;␊ |
118 | ␉}␊ |
119 | ␉␊ |
120 | private:␊ |
121 | ␉std::vector<PartitionInfo> _partList;␊ |
122 | ␉int _hdRedirTable[MAX_HD];␊ |
123 | ␉std::string _hiddenParts;␊ |
124 | ␉std::string _renamedParts;␊ |
125 | ␉␊ |
126 | };␊ |
127 | |