GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/fs/files.cpp Lines: 35 66 53.0 %
Date: 2018-06-18 21:15:35 Branches: 18 80 22.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-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
#include "fs/files.h"
22
23
#include "fs/mkdir.h"
24
#if defined(ANDROID) || defined(__native_client__)
25
#include "fs/paths.h"
26
27
#include "fs/virtfs/fs.h"
28
#include "fs/virtfs/tools.h"
29
#include "fs/virtfs/list.h"
30
#endif  // defined(ANDROID) || defined(__native_client__)
31
32
#if defined(ANDROID) || defined(__native_client__)
33
#include "utils/foreach.h"
34
#endif  // defined(ANDROID) || defined(__native_client__)
35
36
#include "utils/checkutils.h"
37
#include "utils/stringutils.h"
38
39
#include <dirent.h>
40
#include <fstream>
41
#include <sys/stat.h>
42
43
#include "debug.h"
44
45
extern const char *dirSeparator;
46
47
#ifdef ANDROID
48
void Files::extractLocale()
49
{
50
    // in future need also remove all locales in local dir
51
52
    const std::string fileName2 = pathJoin(getenv("APPDIR"), "locale.zip");
53
    VirtFs::mountZip(fileName2, Append_false);
54
55
    const std::string localDir = std::string(getenv("APPDIR"));
56
    VirtFs::List *const rootDirs = VirtFs::enumerateFiles("locale");
57
    FOR_EACH (StringVectCIter, i, rootDirs->names)
58
    {
59
        const std::string dir = pathJoin("locale", *i);
60
        if (VirtFs::isDirectory(dir))
61
        {
62
            const std::string moFile = dir + "/LC_MESSAGES/manaplus.mo";
63
            if (VirtFs::exists((moFile)))
64
            {
65
                const std::string localFile = pathJoin(localDir, moFile);
66
                const std::string localDir2 = pathJoin(localDir,
67
                    dir,
68
                    "LC_MESSAGES");
69
                mkdir_r(localDir2.c_str());
70
                copyVirtFsFile(moFile, localFile);
71
            }
72
        }
73
    }
74
    VirtFs::freeList(rootDirs);
75
    VirtFs::unmountZip(fileName2);
76
    remove(fileName2.c_str());
77
}
78
#endif  // ANDROID
79
80
#if defined(ANDROID) || defined(__native_client__)
81
82
namespace
83
{
84
#ifdef ANDROID
85
    int mFilesCount = 0;
86
#endif  // ANDROID
87
88
    Files::CopyFileCallbackPtr mCallbackPtr = nullptr;
89
}  // namespace
90
91
void Files::setCopyCallBack(Files::CopyFileCallbackPtr callback)
92
{
93
    mCallbackPtr = callback;
94
}
95
96
void Files::copyVirtFsFile(const std::string &restrict inFile,
97
                           const std::string &restrict outFile)
98
{
99
    int size = 0;
100
    const char *const buf = VirtFs::loadFile(inFile, size);
101
    FILE *const file = fopen(outFile.c_str(), "w");
102
    fwrite(buf, 1, size, file);
103
    fclose(file);
104
    delete [] buf;
105
#ifdef ANDROID
106
    if (mCallbackPtr)
107
    {
108
        mCallbackPtr(mFilesCount);
109
        mFilesCount ++;
110
    }
111
#endif  // ANDROID
112
}
113
114
void Files::copyVirtFsDir(const std::string &restrict inDir,
115
                          const std::string &restrict outDir)
116
{
117
    mkdir_r(outDir.c_str());
118
    VirtFs::List *const files = VirtFs::enumerateFiles(inDir);
119
    FOR_EACH (StringVectCIter, i, files->names)
120
    {
121
        const std::string file = pathJoin(inDir, *i);
122
        const std::string outDir2 = pathJoin(outDir, *i);
123
        if (VirtFs::isDirectory(file))
124
            copyVirtFsDir(file, outDir2);
125
        else
126
            copyVirtFsFile(file, outDir2);
127
    }
128
    VirtFs::freeList(files);
129
}
130
131
#endif  // ANDROID __native_client__
132
133
2
int Files::renameFile(const std::string &restrict srcName,
134
                      const std::string &restrict dstName)
135
{
136
#if defined __native_client__
137
    FILE *srcFile = fopen(srcName.c_str(), "rb");
138
    if (srcFile == nullptr)
139
        return -1;
140
    FILE *dstFile = fopen(dstName.c_str(), "w+b");
141
    if (dstFile == nullptr)
142
    {
143
        fclose(srcFile);
144
        return -1;
145
    }
146
147
    const int chunkSize = 500000;
148
    char *buf = new char[chunkSize];
149
    size_t sz = 0;
150
    while ((sz = fread(buf, 1, chunkSize, srcFile)))
151
    {
152
        if (fwrite(buf, 1, sz, dstFile) != sz)
153
        {
154
            delete [] buf;
155
            fclose(srcFile);
156
            fclose(dstFile);
157
            ::remove(dstName.c_str());
158
            return -1;
159
        }
160
    }
161
162
    delete [] buf;
163
    fclose(srcFile);
164
    fclose(dstFile);
165
    if (!::remove(srcName.c_str()))
166
        return 0;
167
168
    return -1;
169
#else  // defined __native_client__
170
171
4
    return ::rename(srcName.c_str(), dstName.c_str());
172
#endif  // defined __native_client__
173
}
174
175
6
int Files::copyFile(const std::string &restrict srcName,
176
                    const std::string &restrict dstName)
177
{
178
6
    FILE *srcFile = fopen(srcName.c_str(), "rb");
179
6
    if (srcFile == nullptr)
180
        return -1;
181
4
    FILE *dstFile = fopen(dstName.c_str(), "w+b");
182
4
    if (dstFile == nullptr)
183
    {
184
2
        fclose(srcFile);
185
2
        return -1;
186
    }
187
188
2
    const int chunkSize = 512000;
189
2
    char *buf = new char[chunkSize];
190
2
    size_t sz = 0;
191
4
    while ((sz = fread(buf, 1, chunkSize, srcFile)) != 0u)
192
    {
193
2
        if (fwrite(buf, 1, sz, dstFile) != sz)
194
        {
195
            delete [] buf;
196
            fclose(srcFile);
197
            fclose(dstFile);
198
            return -1;
199
        }
200
    }
201
202
2
    delete [] buf;
203
2
    fclose(srcFile);
204
2
    fclose(dstFile);
205
2
    return 0;
206
}
207
208
36475
bool Files::existsLocal(const std::string &path)
209
{
210
    struct stat statbuf;
211
#ifdef WIN32
212
    // in windows path\file.ext\ by default detected as exists
213
    // if file.ext is not directory, need return false
214
    const bool res = (stat(path.c_str(), &statbuf) == 0);
215
    if (res == false)
216
        return false;
217
    if ((findLast(path, "/") == true || findLast(path, "\\") == true) &&
218
        S_ISDIR(statbuf.st_mode) == 0)
219
    {
220
        return false;
221
    }
222
    return true;
223
#else  // WIN32
224
72950
    return stat(path.c_str(), &statbuf) == 0;
225
#endif  // WIN32
226
}
227
228
2
bool Files::loadTextFileLocal(const std::string &fileName,
229
                              StringVect &lines)
230
{
231
4
    std::ifstream file;
232
    char line[501];
233
234
2
    file.open(fileName.c_str(), std::ios::in);
235
236
2
    if (!file.is_open())
237
    {
238
        reportAlways("Couldn't load text file: %s",
239
            fileName.c_str());
240
        return false;
241
    }
242
243

16
    while (file.getline(line, 500))
244
20
        lines.push_back(line);
245
246
    return true;
247
}
248
249
4
void Files::saveTextFile(const std::string &path,
250
                         const std::string &restrict name,
251
                         const std::string &restrict text)
252
{
253
4
    if (mkdir_r(path.c_str()) == 0)
254
    {
255
8
        std::ofstream file;
256
12
        std::string fileName = pathJoin(path, name);
257
4
        file.open(fileName.c_str(), std::ios::out);
258
4
        if (file.is_open())
259
        {
260
8
            file << text << std::endl;
261
        }
262
        else
263
        {
264
            reportAlways("Error opening file for writing: %s",
265
                fileName.c_str());
266
        }
267
4
        file.close();
268
    }
269
4
}
270
271
void Files::deleteFilesInDirectory(std::string path)
272
{
273
    path = pathJoin(path, dirSeparator);
274
    const struct dirent *next_file = nullptr;
275
    DIR *const dir = opendir(path.c_str());
276
277
    if (dir != nullptr)
278
    {
279
        while ((next_file = readdir(dir)) != nullptr)
280
        {
281
            const std::string file = next_file->d_name;
282
            if (file != "." && file != "..")
283
                remove((path + file).c_str());
284
        }
285
        closedir(dir);
286
    }
287
}
288
289
void Files::enumFiles(StringVect &files,
290
                      std::string path,
291
                      const bool skipSymlinks A_WIN_UNUSED)
292
{
293
    if (findLast(path, dirSeparator) == false)
294
        path += dirSeparator;
295
    DIR *const dir = opendir(path.c_str());
296
297
    if (dir != nullptr)
298
    {
299
        const struct dirent *next_file = nullptr;
300
        while ((next_file = readdir(dir)) != nullptr)
301
        {
302
            const std::string file = next_file->d_name;
303
            if (file == "." || file == "..")
304
                continue;
305
#ifndef WIN32
306
            if (skipSymlinks == true)
307
            {
308
                struct stat statbuf;
309
                if (lstat(path.c_str(), &statbuf) == 0 &&
310
                    S_ISLNK(statbuf.st_mode) != 0)
311
                {
312
                    continue;
313
                }
314
            }
315
#endif  // WIN32
316
            files.push_back(file);
317
        }
318
        closedir(dir);
319
    }
320
}