Mercurial > minori
annotate src/core/filesystem.cc @ 158:80d6b28eb29f
dep/animia: fix most X11 stuff
it looks like _NET_WM_PID isn't supported by MOST clients, or my code is wrong...
core/filesystem: fix Linux config path handling on *nix
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 17 Nov 2023 02:07:33 -0500 |
parents | 28842a8d0c6b |
children | 121c2d5b321f |
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 |
10 #ifdef WIN32 | |
15 | 11 # define DELIM "\\" |
11 | 12 #else |
15 | 13 # define DELIM "/" |
14 # include <errno.h> | |
15 # include <unistd.h> | |
18
28d8f4c0ae12
*nix: add missing header file for stat
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
16 # include <sys/stat.h> |
11 | 17 #endif |
18 | |
36 | 19 #include "core/filesystem.h" |
15 | 20 #include "core/config.h" |
62 | 21 #include "core/strings.h" |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
22 #include <filesystem> |
2 | 23 #include <limits.h> |
24 | |
11 | 25 namespace Filesystem { |
26 | |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
27 /* 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
|
28 PARENT directory. */ |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
29 void CreateDirectories(const std::filesystem::path& path) { |
158
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
30 if (path.empty()) |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
31 return; |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
32 |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
33 const auto& parent = path.parent_path(); |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
34 if (!std::filesystem::exists(parent)) |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
35 std::filesystem::create_directories(parent); |
11 | 36 } |
37 | |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
38 std::filesystem::path GetDotPath() { |
9 | 39 #ifdef WIN32 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
40 std::filesystem::path path; |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
41 wchar_t* buf; |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
42 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
|
43 path = buf; |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
44 else |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
45 return std::filesystem::path(); |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
46 CoTaskMemFree(buf); |
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) |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
49 return std::filesystem::path(osx::GetApplicationSupportDirectory()) / CONFIG_DIR; |
2 | 50 #else // just assume POSIX |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
136
diff
changeset
|
51 std::filesystem::path path; |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
52 const char* home = getenv("HOME"); |
15 | 53 # ifdef __linux__ |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
136
diff
changeset
|
54 if (!home) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
136
diff
changeset
|
55 home = getpwuid(getuid())->pw_dir; |
15 | 56 # endif // __linux__ |
158
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
57 if (home) |
80d6b28eb29f
dep/animia: fix most X11 stuff
Paper <mrpapersonic@gmail.com>
parents:
138
diff
changeset
|
58 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
|
59 else |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
60 return std::filesystem::path(); |
15 | 61 #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
|
62 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
63 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
64 std::filesystem::path GetConfigPath() { |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
65 return GetDotPath() / CONFIG_NAME; |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
66 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
67 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
68 std::filesystem::path GetPlayersPath() { |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
69 return GetDotPath() / "recognition" / "players.json"; |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
70 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
71 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
72 std::filesystem::path GetExtensionsPath() { |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
73 return GetDotPath() / "recognition" / "extensions.json"; |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
74 } |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
75 |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
76 std::filesystem::path GetAnimeDBPath() { |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
134
diff
changeset
|
77 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
|
78 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
79 |
15 | 80 } // namespace Filesystem |