GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/fs/paths.cpp Lines: 28 73 38.4 %
Date: 2017-11-29 Branches: 12 94 12.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifdef _MSC_VER
22
# include "msvc/config.h"
23
#elif defined(HAVE_CONFIG_H)
24
#include "config.h"
25
#endif  // _MSC_VER
26
27
#include "fs/paths.h"
28
29
#include "fs/virtfs/fs.h"
30
31
#include "utils/checkutils.h"
32
#include "utils/stringutils.h"
33
34
#ifdef USE_X11
35
#include "fs/files.h"
36
37
#include "utils/foreach.h"
38
#endif  // USE_X11
39
40
#ifdef __native_client__
41
#include <limits.h>
42
#define realpath(N, R) strcpy(R, N)
43
#endif  // __native_client__
44
45
#ifdef WIN32
46
#include "fs/specialfolder.h"
47
#define realpath(N, R) _fullpath((R), (N), _MAX_PATH)
48
#elif defined __OpenBSD__
49
#include <limits>
50
#elif defined __native_client__
51
#include <limits.h>
52
#endif  // WIN32
53
54
#ifndef WIN32
55
#include <unistd.h>
56
#include <sys/types.h>
57
#include <pwd.h>
58
#endif  // WIN32
59
60
PRAGMA48(GCC diagnostic push)
61
PRAGMA48(GCC diagnostic ignored "-Wshadow")
62
#ifdef ANDROID
63
#ifdef USE_SDL2
64
#include <SDL_system.h>
65
#endif  // USE_SDL2
66
#endif  // ANDROID
67
PRAGMA48(GCC diagnostic pop)
68
69
#include "debug.h"
70
71
namespace
72
{
73
2
    std::string mPackageDir;
74
}  // namespace
75
76
930
std::string getRealPath(const std::string &str)
77
{
78
#if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__native_client__)
79
    char *realPath = reinterpret_cast<char*>(calloc(PATH_MAX, sizeof(char)));
80
    if (!realPath)
81
        return "";
82
    realpath(str.c_str(), realPath);
83
#else  // defined(__OpenBSD__) || defined(__ANDROID__) ||
84
       // defined(__native_client__)
85
86
1860
    char *realPath = realpath(str.c_str(), nullptr);
87
930
    if (realPath == nullptr)
88
        return "";
89
#endif  // defined(__OpenBSD__) || defined(__ANDROID__) ||
90
        // defined(__native_client__)
91
92
2790
    std::string path = realPath;
93
930
    free(realPath);
94
930
    return path;
95
}
96
97
bool isRealPath(const std::string &str)
98
{
99
    return str == getRealPath(str);
100
}
101
102
20192
bool checkPath(const std::string &path)
103
{
104
20192
    if (path.empty())
105
        return true;
106
19656
    return path.find("../") == std::string::npos
107
19652
        && path.find("..\\") == std::string::npos
108
19652
        && path.find("/..") == std::string::npos
109

39308
        && path.find("\\..") == std::string::npos;
110
}
111
112
25998
void prepareFsPath(std::string &path)
113
{
114
#ifdef DEBUGFS
115
    std::string path2 = path;
116
#endif
117
25998
    sanitizePath(path);
118
// can be enabled for debugging
119
#ifdef DEBUGFS
120
    if (path != path2)
121
    {
122
        reportAlways("Path can be improved: '%s' -> '%s'",
123
            path2.c_str(),
124
            path.c_str());
125
    }
126
#endif
127
25998
}
128
129
1492
std::string &fixDirSeparators(std::string &str)
130
{
131
#ifdef WIN32
132
    return replaceAll(str, "/", "\\");
133
#else
134
1492
    return str;
135
#endif
136
}
137
138
std::string removeLast(const std::string &str)
139
{
140
    size_t pos2 = str.rfind('/');
141
    const size_t pos3 = str.rfind('\\');
142
    if (pos3 != std::string::npos)
143
    {
144
        if (pos2 == std::string::npos || pos3 > pos2)
145
            pos2 = pos3;
146
    }
147
    if (pos2 != std::string::npos)
148
        return str.substr(0, pos2);
149
    return str;
150
}
151
152
#ifdef WIN32
153
std::string getSelfName()
154
{
155
    return "manaplus.exe";
156
}
157
158
#elif defined(__APPLE__)
159
std::string getSelfName()
160
{
161
    return "manaplus.exe";
162
}
163
164
#elif defined __linux__ || defined __linux
165
166
std::string getSelfName()
167
{
168
    char buf[257];
169
    const ssize_t sz = readlink("/proc/self/exe", buf, 256);
170
    if (sz > 0 && sz < 256)
171
    {
172
        buf[sz] = 0;
173
        return buf;
174
    }
175
    return "";
176
}
177
178
#else  // WIN32
179
180
std::string getSelfName()
181
{
182
    return "";
183
}
184
185
#endif  // WIN32
186
187
std::string getPicturesDir()
188
{
189
#ifdef WIN32
190
    std::string dir = getSpecialFolderLocation(CSIDL_MYPICTURES);
191
    if (dir.empty())
192
        dir = getSpecialFolderLocation(CSIDL_DESKTOP);
193
    return dir;
194
#elif defined USE_X11
195
    char *xdg = getenv("XDG_CONFIG_HOME");
196
    std::string file;
197
    if (!xdg)
198
    {
199
        file = pathJoin(VirtFs::getUserDir(),
200
            ".config/user-dirs.dirs");
201
    }
202
    else
203
    {
204
        file = pathJoin(xdg, "user-dirs.dirs");
205
    }
206
207
    if (Files::existsLocal(file))
208
    {
209
        StringVect arr;
210
        Files::loadTextFileLocal(file, arr);
211
        FOR_EACH (StringVectCIter, it, arr)
212
        {
213
            std::string str = *it;
214
            if (findCutFirst(str, "XDG_PICTURES_DIR=\""))
215
            {
216
                str = str.substr(0, str.size() - 1);
217
                // use hack to replace $HOME var.
218
                // if in string other vars, fallback to default path
219
                replaceAll(str, "$HOME/", VirtFs::getUserDir());
220
                str = getRealPath(str);
221
                if (str.empty())
222
                    str = pathJoin(VirtFs::getUserDir(), "Desktop");
223
                return str;
224
            }
225
        }
226
    }
227
#endif  // WIN32
228
229
    return pathJoin(VirtFs::getUserDir(), "Desktop");
230
}
231
232
240
std::string getHomePath()
233
{
234
#if defined(UNITTESTS) && defined(UNITESTSDIR)
235
    std::string dir = UNITESTSDIR;
236
    if (findLast(dir, std::string(dirSeparator)) == false)
237
        dir += dirSeparator;
238
    return dir;
239
#else  // defined(UNITTESTS) && defined(UNITESTSDIR)
240
#ifdef WIN32
241
    return getSpecialFolderLocation(CSIDL_LOCAL_APPDATA);
242
#else
243
240
    const char *path = getenv("HOME");
244
240
    if (path == nullptr)
245
    {
246
        const uid_t uid = getuid();
247
        const struct passwd *const pw = getpwuid(uid);
248
        if (pw != nullptr &&
249
            pw->pw_dir != nullptr)
250
        {
251
            path = pw->pw_dir;
252
        }
253
        if (path == nullptr)
254
            return dirSeparator;
255
    }
256
720
    std::string dir = path;
257

960
    if (findLast(dir, std::string(dirSeparator)) == false)
258
240
        dir += dirSeparator;
259
240
    return dir;
260
#endif  // WIN32
261
#endif  // defined(UNITTESTS) && defined(UNITESTSDIR)
262
}
263
264
#ifdef ANDROID
265
std::string getSdStoragePath()
266
{
267
    return getenv("DATADIR2");
268
}
269
#endif  // ANDROID
270
271
6
std::string getPackageDir()
272
{
273
6
    return mPackageDir;
274
}
275
276
void setPackageDir(const std::string &dir)
277
{
278
    mPackageDir = dir;
279
4
}