Skip to content

Commit 5d43678

Browse files
committed
Fixed #17 added setFilePermissions / getFilePermissions
1 parent 69f5434 commit 5d43678

File tree

10 files changed

+598
-5
lines changed

10 files changed

+598
-5
lines changed

src/wsjcpp_core.cpp

Lines changed: 294 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "wsjcpp_core.h"
22
#include <dirent.h>
33
#include <sys/stat.h>
4+
#include <sys/types.h>
45
#include <iostream>
56
#include <sstream>
67
#include <fstream>
@@ -16,11 +17,237 @@
1617
#include <cstdint>
1718
#include <unistd.h>
1819
#include <streambuf>
19-
#include <sys/types.h>
20-
#include <sys/socket.h>
20+
// #include <sys/socket.h>
2121
#include <arpa/inet.h>
2222
#include <random>
2323

24+
// ---------------------------------------------------------------------
25+
// WsjcppFilePermissions
26+
27+
WsjcppFilePermissions::WsjcppFilePermissions() {
28+
// default permissions
29+
m_bOwnerReadFlag = true;
30+
m_bOwnerWriteFlag = true;
31+
m_bOwnerExecuteFlag = false;
32+
m_bGroupReadFlag = false;
33+
m_bGroupWriteFlag = false;
34+
m_bGroupExecuteFlag = false;
35+
m_bOtherReadFlag = true;
36+
m_bOtherWriteFlag = false;
37+
m_bOtherExecuteFlag = false;
38+
}
39+
40+
WsjcppFilePermissions::WsjcppFilePermissions(
41+
bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag,
42+
bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag,
43+
bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag
44+
) {
45+
m_bOwnerReadFlag = bOwnerReadFlag;
46+
m_bOwnerWriteFlag = bOwnerWriteFlag;
47+
m_bOwnerExecuteFlag = bOwnerExecuteFlag;
48+
m_bGroupReadFlag = bGroupReadFlag;
49+
m_bGroupWriteFlag = bGroupWriteFlag;
50+
m_bGroupExecuteFlag = bGroupExecuteFlag;
51+
m_bOtherReadFlag = bOtherReadFlag;
52+
m_bOtherWriteFlag = bOtherWriteFlag;
53+
m_bOtherExecuteFlag = bOtherExecuteFlag;
54+
}
55+
56+
WsjcppFilePermissions::WsjcppFilePermissions(uint16_t nFilePermission) {
57+
58+
// owner
59+
m_bOwnerReadFlag = nFilePermission & 0x0400 != 0x0;
60+
m_bOwnerWriteFlag = nFilePermission & 0x0200 != 0x0;
61+
m_bOwnerExecuteFlag = nFilePermission & 0x0100 != 0x0;
62+
63+
// group
64+
m_bGroupReadFlag = nFilePermission & 0x0040 != 0x0;
65+
m_bGroupWriteFlag = nFilePermission & 0x0020 != 0x0;
66+
m_bGroupExecuteFlag = nFilePermission & 0x0010 != 0x0;
67+
68+
// for other
69+
m_bOtherReadFlag = nFilePermission & 0x0004 != 0x0;
70+
m_bOtherWriteFlag = nFilePermission & 0x0002 != 0x0;
71+
m_bOtherExecuteFlag = nFilePermission & 0x0001 != 0x0;
72+
}
73+
74+
// ---------------------------------------------------------------------
75+
76+
void WsjcppFilePermissions::setOwnerReadFlag(bool bOwnerReadFlag) {
77+
m_bOwnerReadFlag = bOwnerReadFlag;
78+
}
79+
80+
// ---------------------------------------------------------------------
81+
82+
bool WsjcppFilePermissions::getOwnerReadFlag() const {
83+
return m_bOwnerReadFlag;
84+
}
85+
86+
// ---------------------------------------------------------------------
87+
88+
void WsjcppFilePermissions::setOwnerWriteFlag(bool bOwnerWriteFlag) {
89+
m_bOwnerWriteFlag = bOwnerWriteFlag;
90+
}
91+
92+
// ---------------------------------------------------------------------
93+
94+
bool WsjcppFilePermissions::getOwnerWriteFlag() const {
95+
return m_bOwnerWriteFlag;
96+
}
97+
98+
// ---------------------------------------------------------------------
99+
100+
void WsjcppFilePermissions::setOwnerExecuteFlag(bool bOwnerExecuteFlag) {
101+
m_bOwnerExecuteFlag = bOwnerExecuteFlag;
102+
}
103+
104+
// ---------------------------------------------------------------------
105+
106+
bool WsjcppFilePermissions::getOwnerExecuteFlag() const {
107+
return m_bOwnerExecuteFlag;
108+
}
109+
110+
// ---------------------------------------------------------------------
111+
112+
void WsjcppFilePermissions::setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag) {
113+
m_bOwnerReadFlag = bOwnerReadFlag;
114+
m_bOwnerWriteFlag = bOwnerWriteFlag;
115+
m_bOwnerExecuteFlag = bOwnerExecuteFlag;
116+
}
117+
118+
// ---------------------------------------------------------------------
119+
120+
void WsjcppFilePermissions::setGroupReadFlag(bool bGroupReadFlag) {
121+
m_bGroupReadFlag = bGroupReadFlag;
122+
}
123+
124+
// ---------------------------------------------------------------------
125+
126+
bool WsjcppFilePermissions::getGroupReadFlag() const {
127+
return m_bGroupReadFlag;
128+
}
129+
130+
// ---------------------------------------------------------------------
131+
132+
void WsjcppFilePermissions::setGroupWriteFlag(bool bGroupWriteFlag) {
133+
m_bGroupWriteFlag = bGroupWriteFlag;
134+
}
135+
136+
// ---------------------------------------------------------------------
137+
138+
bool WsjcppFilePermissions::getGroupWriteFlag() const {
139+
return m_bGroupWriteFlag;
140+
}
141+
142+
// ---------------------------------------------------------------------
143+
144+
void WsjcppFilePermissions::setGroupExecuteFlag(bool bGroupExecuteFlag) {
145+
m_bGroupExecuteFlag = bGroupExecuteFlag;
146+
}
147+
148+
// ---------------------------------------------------------------------
149+
150+
bool WsjcppFilePermissions::getGroupExecuteFlag() const {
151+
return m_bGroupExecuteFlag;
152+
}
153+
154+
// ---------------------------------------------------------------------
155+
156+
void WsjcppFilePermissions::setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag) {
157+
m_bGroupReadFlag = bGroupReadFlag;
158+
m_bGroupWriteFlag = bGroupWriteFlag;
159+
m_bGroupExecuteFlag = bGroupExecuteFlag;
160+
}
161+
162+
// ---------------------------------------------------------------------
163+
164+
void WsjcppFilePermissions::setOtherReadFlag(bool bOtherReadFlag) {
165+
m_bOtherReadFlag = bOtherReadFlag;
166+
}
167+
168+
// ---------------------------------------------------------------------
169+
170+
bool WsjcppFilePermissions::getOtherReadFlag() const {
171+
return m_bOtherReadFlag;
172+
}
173+
174+
// ---------------------------------------------------------------------
175+
176+
void WsjcppFilePermissions::setOtherWriteFlag(bool bOtherWriteFlag) {
177+
m_bOtherWriteFlag = bOtherWriteFlag;
178+
}
179+
180+
// ---------------------------------------------------------------------
181+
182+
bool WsjcppFilePermissions::getOtherWriteFlag() const {
183+
return m_bOtherWriteFlag;
184+
}
185+
186+
// ---------------------------------------------------------------------
187+
188+
void WsjcppFilePermissions::setOtherExecuteFlag(bool bOtherExecuteFlag) {
189+
m_bOtherExecuteFlag = bOtherExecuteFlag;
190+
}
191+
192+
// ---------------------------------------------------------------------
193+
194+
bool WsjcppFilePermissions::getOtherExecuteFlag() const {
195+
return m_bOtherExecuteFlag;
196+
}
197+
198+
// ---------------------------------------------------------------------
199+
200+
void WsjcppFilePermissions::setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag) {
201+
m_bOtherReadFlag = bOtherReadFlag;
202+
m_bOtherWriteFlag = bOtherWriteFlag;
203+
m_bOtherExecuteFlag = bOtherExecuteFlag;
204+
}
205+
206+
// ---------------------------------------------------------------------
207+
208+
std::string WsjcppFilePermissions::toString() const {
209+
std::string sRet = "-";
210+
211+
// owner
212+
sRet += m_bOwnerReadFlag ? "r" : "-";
213+
sRet += m_bOwnerWriteFlag ? "w" : "-";
214+
sRet += m_bOwnerExecuteFlag ? "x" : "-";
215+
216+
// group
217+
sRet += m_bGroupReadFlag ? "r" : "-";
218+
sRet += m_bGroupWriteFlag ? "w" : "-";
219+
sRet += m_bGroupExecuteFlag ? "x" : "-";
220+
221+
// for other
222+
sRet += m_bOtherReadFlag ? "r" : "-";
223+
sRet += m_bOtherWriteFlag ? "w" : "-";
224+
sRet += m_bOtherExecuteFlag ? "x" : "-";
225+
226+
return sRet;
227+
}
228+
229+
// ---------------------------------------------------------------------
230+
231+
uint16_t WsjcppFilePermissions::toUInt16() const {
232+
uint16_t nRet = 0x0;
233+
// owner
234+
nRet |= m_bOwnerReadFlag ? 0x0400 : 0x0;
235+
nRet |= m_bOwnerWriteFlag ? 0x0200 : 0x0;
236+
nRet |= m_bOwnerExecuteFlag ? 0x0100 : 0x0;
237+
238+
// group
239+
nRet += m_bGroupReadFlag ? 0x0040 : 0x0;
240+
nRet += m_bGroupWriteFlag ? 0x0020 : 0x0;
241+
nRet += m_bGroupExecuteFlag ? 0x0010 : 0x0;
242+
243+
// for other
244+
nRet += m_bOtherReadFlag ? 0x0004 : 0x0;
245+
nRet += m_bOtherWriteFlag ? 0x0002 : 0x0;
246+
nRet += m_bOtherExecuteFlag ? 0x0001 : 0x0;
247+
return nRet;
248+
}
249+
250+
24251
// ---------------------------------------------------------------------
25252
// WsjcppCore
26253

@@ -723,8 +950,6 @@ bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::s
723950
return true;
724951
}
725952

726-
727-
728953
// ---------------------------------------------------------------------
729954

730955
bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
@@ -755,6 +980,71 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
755980
return true;
756981
}
757982

983+
// ---------------------------------------------------------------------
984+
985+
bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError) {
986+
987+
mode_t m;
988+
989+
// owner
990+
m |= filePermissions.getOwnerReadFlag() ? S_IRUSR : 0x0;
991+
m |= filePermissions.getOwnerWriteFlag() & S_IWUSR != 0x0;
992+
m |= filePermissions.getOwnerExecuteFlag() & S_IXUSR != 0x0;
993+
994+
// group
995+
m |= filePermissions.getGroupReadFlag() ? S_IRGRP : 0x0;
996+
m |= filePermissions.getGroupWriteFlag() ? S_IWGRP : 0x0;
997+
m |= filePermissions.getGroupExecuteFlag() ? S_IXGRP : 0x0;
998+
999+
// for other
1000+
m |= filePermissions.getOtherReadFlag() ? S_IROTH : 0x0;
1001+
m |= filePermissions.getOtherWriteFlag() ? S_IWOTH : 0x0;
1002+
m |= filePermissions.getOtherExecuteFlag() ? S_IXOTH : 0x0;
1003+
1004+
if (chmod(sFilePath.c_str(), m) != 0) {
1005+
sError = "Could not change permissions for: '" + sFilePath + "'";
1006+
return false;
1007+
}
1008+
return true;
1009+
}
1010+
1011+
// ---------------------------------------------------------------------
1012+
1013+
bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError) {
1014+
if (!WsjcppCore::fileExists(sFilePath)) {
1015+
sError = "File '" + sFilePath + "' - not found";
1016+
return false;
1017+
}
1018+
1019+
struct stat fileStat;
1020+
if (stat(sFilePath.c_str(), &fileStat) < 0) {
1021+
sError = "Could not get info about file '" + sFilePath + "'.";
1022+
return false;
1023+
}
1024+
1025+
mode_t m = fileStat.st_mode;
1026+
1027+
// S_ISDIR(fileStat.st_mode)) ? "d" : "-"
1028+
1029+
// owner
1030+
filePermissions.setOwnerReadFlag(m & S_IRUSR);
1031+
filePermissions.setOwnerWriteFlag(m & S_IWUSR);
1032+
filePermissions.setOwnerExecuteFlag(m & S_IXUSR);
1033+
1034+
1035+
// group
1036+
filePermissions.setGroupReadFlag(m & S_IRGRP);
1037+
filePermissions.setGroupWriteFlag(m & S_IWGRP);
1038+
filePermissions.setGroupExecuteFlag(m & S_IXGRP);
1039+
1040+
// for other
1041+
filePermissions.setOtherReadFlag(m & S_IROTH);
1042+
filePermissions.setOtherWriteFlag(m & S_IWOTH);
1043+
filePermissions.setOtherExecuteFlag(m & S_IXOTH);
1044+
1045+
return true;
1046+
}
1047+
7581048
// ---------------------------------------------------------------------
7591049
// WsjcppLog
7601050

src/wsjcpp_core.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,58 @@
88
#include <deque>
99
#include <iostream>
1010

11+
class WsjcppFilePermissions {
12+
public:
13+
WsjcppFilePermissions();
14+
WsjcppFilePermissions(
15+
bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag,
16+
bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag,
17+
bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag
18+
);
19+
WsjcppFilePermissions(uint16_t nFilePermission);
20+
21+
// owner flags
22+
void setOwnerReadFlag(bool bOwnerReadFlag);
23+
bool getOwnerReadFlag() const;
24+
void setOwnerWriteFlag(bool bOwnerWriteFlag);
25+
bool getOwnerWriteFlag() const;
26+
void setOwnerExecuteFlag(bool bOwnerExecuteFlag);
27+
bool getOwnerExecuteFlag() const;
28+
void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag);
29+
30+
// group flags
31+
void setGroupReadFlag(bool bGroupReadFlag);
32+
bool getGroupReadFlag() const;
33+
void setGroupWriteFlag(bool bGroupWriteFlag);
34+
bool getGroupWriteFlag() const;
35+
void setGroupExecuteFlag(bool bGroupExecuteFlag);
36+
bool getGroupExecuteFlag() const;
37+
void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag);
38+
39+
// for other flags
40+
void setOtherReadFlag(bool bOtherReadFlag);
41+
bool getOtherReadFlag() const;
42+
void setOtherWriteFlag(bool bOtherWriteFlag);
43+
bool getOtherWriteFlag() const;
44+
void setOtherExecuteFlag(bool bOtherExecuteFlag);
45+
bool getOtherExecuteFlag() const;
46+
void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag);
47+
48+
std::string toString() const;
49+
uint16_t toUInt16() const;
50+
51+
private:
52+
bool m_bOwnerReadFlag;
53+
bool m_bOwnerWriteFlag;
54+
bool m_bOwnerExecuteFlag;
55+
bool m_bGroupReadFlag;
56+
bool m_bGroupWriteFlag;
57+
bool m_bGroupExecuteFlag;
58+
bool m_bOtherReadFlag;
59+
bool m_bOtherWriteFlag;
60+
bool m_bOtherExecuteFlag;
61+
};
62+
1163
class WsjcppCore {
1264
public:
1365
static bool init(
@@ -71,6 +123,10 @@ class WsjcppCore {
71123

72124
static bool recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir);
73125
static bool recoursiveRemoveDir(const std::string& sDir);
126+
127+
static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError);
128+
static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError);
129+
74130
};
75131

76132

0 commit comments

Comments
 (0)