Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
3 andreas 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
 
22 andreas 19
#include <string>
20
#include <vector>
21
 
3 andreas 22
#include <sys/types.h>
23
#include <sys/stat.h>
22 andreas 24
 
25
#include "tvalidatefile.h"
3 andreas 26
#include "terror.h"
22 andreas 27
#include "tresources.h"
3 andreas 28
 
29
using std::string;
22 andreas 30
using std::vector;
3 andreas 31
 
156 andreas 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
 
3 andreas 49
bool TValidateFile::isValidFile(const string& file)
50
{
71 andreas 51
    DECL_TRACER("TValidateFile::isValidFile(const string& file)");
3 andreas 52
 
53
    struct stat buffer;
54
 
55
    if (stat (file.c_str(), &buffer) != 0)
88 andreas 56
    {
116 andreas 57
        MSG_WARNING("File access error (" << file << "): " << strerror(errno));
3 andreas 58
        return false;
88 andreas 59
    }
3 andreas 60
 
88 andreas 61
    if ((buffer.st_mode & S_IFREG) > 0)
71 andreas 62
        return true;
63
 
88 andreas 64
    return false;
3 andreas 65
}
66
 
71 andreas 67
bool TValidateFile::isValidDir(const string& path)
68
{
69
    DECL_TRACER("TValidateFile::isValidDir(const string& path)");
70
 
71
    struct stat buffer;
72
 
73
    if (stat (path.c_str(), &buffer) != 0)
74
        return false;
75
 
88 andreas 76
    if ((buffer.st_mode & S_IFDIR) > 0)
71 andreas 77
        return true;
78
 
88 andreas 79
    return false;
71 andreas 80
}
81
 
3 andreas 82
std::string &TValidateFile::makeFileName(const std::string& path, const std::string& name)
83
{
84
    DECL_TRACER("TValidateFile::makeFileName(const std::string& path, const std::string& name)");
85
 
86
    if (name.empty())
87
    {
88 andreas 88
        MSG_DEBUG("No file name given!");
3 andreas 89
        mFile.clear();
90
        return mFile;
91
    }
92
 
93
    if (path.empty())
94
        mFile = "./";
95
    else
96
        mFile = path + "/";
97
 
98
    mFile.append(name);
99
    return mFile;
100
}
101
 
22 andreas 102
bool TValidateFile::createPath(const string &path)
103
{
104
    DECL_TRACER("TValidateFile::createPath(const string &path)");
105
 
106
    if (path.length() == 0 || path == ".")
107
        return true;
108
 
109
    vector<string> parts = StrSplit(path, "/");
110
    vector<string>::iterator iter;
111
    bool absolut = false;
112
    string pPart;
113
 
114
    if (path[0] == '/')
115
        absolut = true;
116
 
117
    for (iter = parts.begin(); iter != parts.end(); ++iter)
118
    {
119
        if ((pPart.empty() && absolut) || !pPart.empty())
120
            pPart += "/" + *iter;
121
        else
122
            pPart += *iter;
123
 
124
        // Test for existence of the path part and whether it is a file or a directory.
125
        struct stat buffer;
126
 
127
        if (stat (pPart.data(), &buffer) == 0)      // If there exists something test what it is.
128
        {
129
            if (!S_ISDIR(buffer.st_mode) && !S_ISLNK(buffer.st_mode))
130
            {
131
                MSG_WARNING(pPart << " is not a directory!");
132
                return false;
133
            }
134
        }
135
        else    // We try to create it
136
        {
137
            if (mkdir(pPart.c_str(), S_IRWXU | S_IRWXG | S_IXOTH | S_IROTH) == -1)
138
            {
139
                MSG_ERROR("Error creating directory " << pPart << ": " << strerror(errno));
140
                return false;
141
            }
142
        }
143
    }
144
 
145
    return true;
146
 
147
}
156 andreas 148
 
149
string TValidateFile::getPermissions(const string& path)
150
{
151
    DECL_TRACER("TValidateFile::getPermissions(const string& path)");
152
 
153
    if (!isValidFile(path) && !isValidDir(path))
154
        return "---------";
155
 
156
    fs::perms p = fs::status(path).permissions();
157
    std::stringstream str;
158
 
159
    str << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
160
        << ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
161
        << ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
162
        << ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
163
        << ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
164
        << ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
165
        << ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
166
        << ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
167
        << ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-");
168
 
169
    return str.str();
170
}
171
 
172
string TValidateFile::getPermissions()
173
{
174
    DECL_TRACER("TValidateFile::getPermissions()");
175
 
176
    if (mFile.empty())
177
        return mFile;
178
 
179
    return getPermissions(mFile);
180
}