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
 
32
bool TValidateFile::isValidFile(const string& file)
33
{
71 andreas 34
    DECL_TRACER("TValidateFile::isValidFile(const string& file)");
3 andreas 35
 
36
    struct stat buffer;
37
 
38
    if (stat (file.c_str(), &buffer) != 0)
88 andreas 39
    {
40
        MSG_WARNING("File access error: " << strerror(errno));
3 andreas 41
        return false;
88 andreas 42
    }
3 andreas 43
 
88 andreas 44
    if ((buffer.st_mode & S_IFREG) > 0)
71 andreas 45
        return true;
46
 
88 andreas 47
    return false;
3 andreas 48
}
49
 
71 andreas 50
bool TValidateFile::isValidDir(const string& path)
51
{
52
    DECL_TRACER("TValidateFile::isValidDir(const string& path)");
53
 
54
    struct stat buffer;
55
 
56
    if (stat (path.c_str(), &buffer) != 0)
57
        return false;
58
 
88 andreas 59
    if ((buffer.st_mode & S_IFDIR) > 0)
71 andreas 60
        return true;
61
 
88 andreas 62
    return false;
71 andreas 63
}
64
 
3 andreas 65
std::string &TValidateFile::makeFileName(const std::string& path, const std::string& name)
66
{
67
    DECL_TRACER("TValidateFile::makeFileName(const std::string& path, const std::string& name)");
68
 
69
    if (name.empty())
70
    {
88 andreas 71
        MSG_DEBUG("No file name given!");
3 andreas 72
        mFile.clear();
73
        return mFile;
74
    }
75
 
76
    if (path.empty())
77
        mFile = "./";
78
    else
79
        mFile = path + "/";
80
 
81
    mFile.append(name);
82
    return mFile;
83
}
84
 
22 andreas 85
bool TValidateFile::createPath(const string &path)
86
{
87
    DECL_TRACER("TValidateFile::createPath(const string &path)");
88
 
89
    if (path.length() == 0 || path == ".")
90
        return true;
91
 
92
    vector<string> parts = StrSplit(path, "/");
93
    vector<string>::iterator iter;
94
    bool absolut = false;
95
    string pPart;
96
 
97
    if (path[0] == '/')
98
        absolut = true;
99
 
100
    for (iter = parts.begin(); iter != parts.end(); ++iter)
101
    {
102
        if ((pPart.empty() && absolut) || !pPart.empty())
103
            pPart += "/" + *iter;
104
        else
105
            pPart += *iter;
106
 
107
        // Test for existence of the path part and whether it is a file or a directory.
108
        struct stat buffer;
109
 
110
        if (stat (pPart.data(), &buffer) == 0)      // If there exists something test what it is.
111
        {
112
            if (!S_ISDIR(buffer.st_mode) && !S_ISLNK(buffer.st_mode))
113
            {
114
                MSG_WARNING(pPart << " is not a directory!");
115
                return false;
116
            }
117
        }
118
        else    // We try to create it
119
        {
120
            if (mkdir(pPart.c_str(), S_IRWXU | S_IRWXG | S_IXOTH | S_IROTH) == -1)
121
            {
122
                MSG_ERROR("Error creating directory " << pPart << ": " << strerror(errno));
123
                return false;
124
            }
125
        }
126
    }
127
 
128
    return true;
129
 
130
}