1// SuperTux
2// Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
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, see <http://www.gnu.org/licenses/>.
16
17#include "physfs/util.hpp"
18
19#include <physfs.h>
20
21#include "util/file_system.hpp"
22
23namespace physfsutil {
24
25std::string realpath(const std::string& path)
26{
27 std::string result = FileSystem::normalize(path);
28 if (result.empty()) {
29 return "/";
30 } else if (result[0] != '/') {
31 return '/' + result;
32 } else {
33 return result;
34 }
35}
36
37bool is_directory(const std::string& path)
38{
39 PHYSFS_Stat statbuf;
40 if (!PHYSFS_stat(path.c_str(), &statbuf)) {
41 return false;
42 } else {
43 if (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK) {
44 // PHYSFS_stat() doesn't follow symlinks, so we do it manually
45 const char* realdir = PHYSFS_getRealDir(path.c_str());
46 if (realdir == nullptr) {
47 return false;
48 } else {
49 const std::string realfname = FileSystem::join(realdir, path);
50 return FileSystem::is_directory(realfname);
51 }
52 } else {
53 return statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY;
54 }
55 }
56}
57
58bool remove(const std::string& filename)
59{
60 return PHYSFS_delete(filename.c_str()) == 0;
61}
62
63} // namespace physfsutil
64
65/* EOF */
66