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