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