Mercurial > minori
annotate src/core/filesystem.cc @ 248:cf47a8f687c0
cmake: remove cmake
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 24 Jan 2024 20:18:14 -0500 |
parents | 2f5a9247e501 |
children | c130f47f6f48 |
rev | line source |
---|---|
9 | 1 #ifdef WIN32 |
15 | 2 # include <shlobj.h> |
5 | 3 #elif defined(MACOSX) |
15 | 4 # include "sys/osx/filesystem.h" |
7 | 5 #elif defined(__linux__) |
15 | 6 # include <pwd.h> |
7 # include <sys/types.h> | |
2 | 8 #endif |
11 | 9 |
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
194
diff
changeset
|
10 #ifndef WIN32 |
15 | 11 # include <errno.h> |
12 # include <unistd.h> | |
18
28d8f4c0ae12
*nix: add missing header file for stat
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
13 # include <sys/stat.h> |
11 | 14 #endif |
15 | |
36 | 16 #include "core/filesystem.h" |
15 | 17 #include "core/config.h" |
62 | 18 #include "core/strings.h" |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
19 #include <filesystem> |
2 | 20 #include <limits.h> |
21 | |
11 | 22 namespace Filesystem { |
23 | |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
24 /* this runs fs::create_directories() on the |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
25 PARENT directory. */ |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
26 void CreateDirectories(const std::filesystem::path& path) { |
158
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
27 if (path.empty()) |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
28 return; |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
29 |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
30 const auto& parent = path.parent_path(); |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
31 if (!std::filesystem::exists(parent)) |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
32 std::filesystem::create_directories(parent); |
11 | 33 } |
34 | |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
35 std::filesystem::path GetDotPath() { |
9 | 36 #ifdef WIN32 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
37 std::filesystem::path path; |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
38 wchar_t* buf; |
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
158
diff
changeset
|
39 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
40 if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &buf) == S_OK) |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
41 path = buf; |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
42 else |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
43 return std::filesystem::path(); |
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
158
diff
changeset
|
44 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
45 CoTaskMemFree(buf); |
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
158
diff
changeset
|
46 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
47 return path / CONFIG_DIR; |
2 | 48 #elif defined(MACOSX) |
194
8548dc425697
sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
49 std::string appsupport; |
8548dc425697
sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
50 if (!osx::GetApplicationSupportDirectory(appsupport)) |
8548dc425697
sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
51 return ""; |
8548dc425697
sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
52 |
8548dc425697
sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
53 return std::filesystem::path(appsupport) / CONFIG_DIR; |
2 | 54 #else // just assume POSIX |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
136
diff
changeset
|
55 std::filesystem::path path; |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
56 const char* home = getenv("HOME"); |
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
158
diff
changeset
|
57 |
15 | 58 # ifdef __linux__ |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
136
diff
changeset
|
59 if (!home) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
136
diff
changeset
|
60 home = getpwuid(getuid())->pw_dir; |
15 | 61 # endif // __linux__ |
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
158
diff
changeset
|
62 |
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
158
diff
changeset
|
63 /* only do this if the home directory was really found */ |
158
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
64 if (home) |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
65 return std::filesystem::path(home) / ".config" / CONFIG_DIR; |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
66 else |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
67 return std::filesystem::path(); |
15 | 68 #endif // !WIN32 && !MACOSX |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
69 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
70 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
71 std::filesystem::path GetConfigPath() { |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
72 return GetDotPath() / CONFIG_NAME; |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
73 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
74 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
75 std::filesystem::path GetAnimeDBPath() { |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
76 return GetDotPath() / "anime" / "db.json"; |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
77 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
78 |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
79 std::filesystem::path GetTorrentsPath() { |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
80 return GetDotPath() / "torrents"; |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
81 } |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
82 |
15 | 83 } // namespace Filesystem |