GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/fs/paths.cpp Lines: 30 77 39.0 %
Date: 2018-07-14 Branches: 14 98 14.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2018  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
1
    std::string mPackageDir;
74
}  // namespace
75
76
488
std::string getRealPath(const std::string &str)
77
{
78
#if defined(__GLIBC__)
79
488
    if (str.find("(unreachable)") != std::string::npos)
80
        return "";
81
#endif  // defined(__GLIBC__)
82
#if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__native_client__)
83
    char *realPath = reinterpret_cast<char*>(calloc(PATH_MAX, sizeof(char)));
84
    if (!realPath)
85
        return "";
86
    realpath(str.c_str(), realPath);
87
#else  // defined(__OpenBSD__) || defined(__ANDROID__) ||
88
       // defined(__native_client__)
89
90
976
    char *realPath = realpath(str.c_str(), nullptr);
91
488
    if (realPath == nullptr)
92
        return "";
93
#endif  // defined(__OpenBSD__) || defined(__ANDROID__) ||
94
        // defined(__native_client__)
95
96
1464
    std::string path = realPath;
97
488
    free(realPath);
98
488
    return path;
99
}
100
101
bool isRealPath(const std::string &str)
102
{
103
    return str == getRealPath(str);
104
}
105
106
10741
bool checkPath(const std::string &path)
107
{
108
10741
    if (path.empty())
109
        return true;
110
10451
    return path.find("../") == std::string::npos
111
10449
        && path.find("..\\") == std::string::npos
112
10449
        && path.find("/..") == std::string::npos
113
10449
        && path.find("\\..") == std::string::npos
114

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

480
    if (findLast(dir, std::string(dirSeparator)) == false)
263
120
        dir += dirSeparator;
264
120
    return dir;
265
#endif  // WIN32
266
#endif  // defined(UNITTESTS) && defined(UNITESTSDIR)
267
}
268
269
#ifdef ANDROID
270
std::string getSdStoragePath()
271
{
272
    return getenv("DATADIR2");
273
}
274
#endif  // ANDROID
275
276
2
std::string getPackageDir()
277
{
278
2
    return mPackageDir;
279
}
280
281
void setPackageDir(const std::string &dir)
282
{
283
    mPackageDir = dir;
284
2
}