446 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
|
|
|
3 |
*
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
|
6 |
* the Free Software Foundation; either version 3 of the License, or
|
|
|
7 |
* (at your option) any later version.
|
|
|
8 |
*
|
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
* GNU General Public License for more details.
|
|
|
13 |
*
|
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
|
15 |
* along with this program; if not, write to the Free Software Foundation,
|
|
|
16 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
#include <string>
|
|
|
20 |
#include <vector>
|
|
|
21 |
|
|
|
22 |
#include <sys/types.h>
|
|
|
23 |
#include <sys/stat.h>
|
|
|
24 |
|
|
|
25 |
#include "tvalidatefile.h"
|
|
|
26 |
#include "terror.h"
|
|
|
27 |
#include "tresources.h"
|
|
|
28 |
|
|
|
29 |
using std::string;
|
|
|
30 |
using std::vector;
|
|
|
31 |
|
|
|
32 |
#if __cplusplus < 201402L
|
|
|
33 |
# error "This module requires at least C++14 standard!"
|
|
|
34 |
#else
|
|
|
35 |
# if __cplusplus < 201703L
|
|
|
36 |
# include <experimental/filesystem>
|
|
|
37 |
namespace fs = std::experimental::filesystem;
|
|
|
38 |
# warning "Support for C++14 and experimental filesystem will be removed in a future version!"
|
|
|
39 |
# else
|
|
|
40 |
# include <filesystem>
|
|
|
41 |
# ifdef __ANDROID__
|
|
|
42 |
namespace fs = std::__fs::filesystem;
|
|
|
43 |
# else
|
|
|
44 |
namespace fs = std::filesystem;
|
|
|
45 |
# endif
|
|
|
46 |
# endif
|
|
|
47 |
#endif
|
|
|
48 |
|
|
|
49 |
bool TValidateFile::isValidFile(const string& file)
|
|
|
50 |
{
|
|
|
51 |
DECL_TRACER("TValidateFile::isValidFile(const string& file)");
|
|
|
52 |
|
|
|
53 |
struct stat buffer;
|
|
|
54 |
|
|
|
55 |
if (stat (file.c_str(), &buffer) != 0)
|
|
|
56 |
return false;
|
|
|
57 |
|
|
|
58 |
if ((buffer.st_mode & S_IFREG) > 0)
|
|
|
59 |
return true;
|
|
|
60 |
|
|
|
61 |
return false;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
bool TValidateFile::isValidDir(const string& path)
|
|
|
65 |
{
|
|
|
66 |
DECL_TRACER("TValidateFile::isValidDir(const string& path)");
|
|
|
67 |
|
|
|
68 |
struct stat buffer;
|
|
|
69 |
|
|
|
70 |
if (stat (path.c_str(), &buffer) != 0)
|
|
|
71 |
return false;
|
|
|
72 |
|
|
|
73 |
if ((buffer.st_mode & S_IFDIR) > 0)
|
|
|
74 |
return true;
|
|
|
75 |
|
|
|
76 |
return false;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
std::string &TValidateFile::makeFileName(const std::string& path, const std::string& name)
|
|
|
80 |
{
|
|
|
81 |
DECL_TRACER("TValidateFile::makeFileName(const std::string& path, const std::string& name)");
|
|
|
82 |
|
|
|
83 |
if (name.empty())
|
|
|
84 |
{
|
|
|
85 |
MSG_DEBUG("No file name given!");
|
|
|
86 |
mFile.clear();
|
|
|
87 |
return mFile;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if (path.empty())
|
|
|
91 |
mFile = "./";
|
|
|
92 |
else
|
|
|
93 |
mFile = path + "/";
|
|
|
94 |
|
|
|
95 |
mFile.append(name);
|
|
|
96 |
return mFile;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
bool TValidateFile::createPath(const string &path)
|
|
|
100 |
{
|
|
|
101 |
DECL_TRACER("TValidateFile::createPath(const string &path)");
|
|
|
102 |
|
|
|
103 |
if (path.length() == 0 || path == ".")
|
|
|
104 |
return true;
|
|
|
105 |
|
|
|
106 |
vector<string> parts = StrSplit(path, "/");
|
|
|
107 |
vector<string>::iterator iter;
|
|
|
108 |
bool absolut = false;
|
|
|
109 |
string pPart;
|
|
|
110 |
|
|
|
111 |
if (path[0] == '/')
|
|
|
112 |
absolut = true;
|
|
|
113 |
|
|
|
114 |
for (iter = parts.begin(); iter != parts.end(); ++iter)
|
|
|
115 |
{
|
|
|
116 |
if ((pPart.empty() && absolut) || !pPart.empty())
|
|
|
117 |
pPart += "/" + *iter;
|
|
|
118 |
else
|
|
|
119 |
pPart += *iter;
|
|
|
120 |
|
|
|
121 |
// Test for existence of the path part and whether it is a file or a directory.
|
|
|
122 |
struct stat buffer;
|
|
|
123 |
|
|
|
124 |
if (stat (pPart.data(), &buffer) == 0) // If there exists something test what it is.
|
|
|
125 |
{
|
|
|
126 |
if (!S_ISDIR(buffer.st_mode) && !S_ISLNK(buffer.st_mode))
|
|
|
127 |
{
|
|
|
128 |
MSG_WARNING(pPart << " is not a directory!");
|
|
|
129 |
return false;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
else // We try to create it
|
|
|
133 |
{
|
|
|
134 |
if (mkdir(pPart.c_str(), S_IRWXU | S_IRWXG | S_IXOTH | S_IROTH) == -1)
|
|
|
135 |
{
|
|
|
136 |
MSG_ERROR("Error creating directory " << pPart << ": " << strerror(errno));
|
|
|
137 |
return false;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return true;
|
|
|
143 |
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
string TValidateFile::getPermissions(const string& path)
|
|
|
147 |
{
|
|
|
148 |
DECL_TRACER("TValidateFile::getPermissions(const string& path)");
|
|
|
149 |
|
|
|
150 |
if (!isValidFile(path) && !isValidDir(path))
|
|
|
151 |
return "---------";
|
|
|
152 |
|
|
|
153 |
fs::perms p = fs::status(path).permissions();
|
|
|
154 |
std::stringstream str;
|
|
|
155 |
|
|
|
156 |
str << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
|
|
|
157 |
<< ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
|
|
|
158 |
<< ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
|
|
|
159 |
<< ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
|
|
|
160 |
<< ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
|
|
|
161 |
<< ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
|
|
|
162 |
<< ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
|
|
|
163 |
<< ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
|
|
|
164 |
<< ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-");
|
|
|
165 |
|
|
|
166 |
return str.str();
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
string TValidateFile::getPermissions()
|
|
|
170 |
{
|
|
|
171 |
DECL_TRACER("TValidateFile::getPermissions()");
|
|
|
172 |
|
|
|
173 |
if (mFile.empty())
|
|
|
174 |
return mFile;
|
|
|
175 |
|
|
|
176 |
return getPermissions(mFile);
|
|
|
177 |
}
|