|
1 | 1 | #include "wsjcpp_core.h"
|
2 | 2 | #include <dirent.h>
|
3 | 3 | #include <sys/stat.h>
|
| 4 | +#include <sys/types.h> |
4 | 5 | #include <iostream>
|
5 | 6 | #include <sstream>
|
6 | 7 | #include <fstream>
|
|
16 | 17 | #include <cstdint>
|
17 | 18 | #include <unistd.h>
|
18 | 19 | #include <streambuf>
|
19 |
| -#include <sys/types.h> |
20 |
| -#include <sys/socket.h> |
| 20 | +// #include <sys/socket.h> |
21 | 21 | #include <arpa/inet.h>
|
22 | 22 | #include <random>
|
23 | 23 |
|
| 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 | + |
24 | 251 | // ---------------------------------------------------------------------
|
25 | 252 | // WsjcppCore
|
26 | 253 |
|
@@ -723,8 +950,6 @@ bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::s
|
723 | 950 | return true;
|
724 | 951 | }
|
725 | 952 |
|
726 |
| - |
727 |
| - |
728 | 953 | // ---------------------------------------------------------------------
|
729 | 954 |
|
730 | 955 | bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
|
@@ -755,6 +980,71 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
|
755 | 980 | return true;
|
756 | 981 | }
|
757 | 982 |
|
| 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 | + |
758 | 1048 | // ---------------------------------------------------------------------
|
759 | 1049 | // WsjcppLog
|
760 | 1050 |
|
|
0 commit comments