-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathfilesystem_desktop_linux.cc
120 lines (97 loc) · 3.12 KB
/
filesystem_desktop_linux.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <dirent.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "app/src/filesystem.h"
#include "app/src/util.h"
namespace firebase {
namespace {
std::string HomeDir(std::string* out_error) {
const char* home_dir = getenv("HOME");
if (home_dir) return home_dir;
auto buffer_size = static_cast<size_t>(sysconf(_SC_GETPW_R_SIZE_MAX));
std::string buffer(buffer_size, '\0');
uid_t uid = getuid();
int rc;
passwd pwd;
passwd* result;
do {
rc = getpwuid_r(uid, &pwd, &buffer[0], buffer_size, &result);
} while (rc == EINTR);
if (rc != 0) {
if (out_error) {
*out_error = std::string(
"Failed to find the home directory for the current user "
"(errno: ") +
std::to_string(errno) + ")";
}
return "";
}
return pwd.pw_dir;
}
std::string XdgDataHomeDir(std::string* out_error) {
const char* data_home = getenv("XDG_DATA_HOME");
if (data_home) return data_home;
return "";
}
bool Mkdir(const std::string& path, std::string* out_error) {
int retval = mkdir(path.c_str(), 0700);
if (retval != 0 && errno != EEXIST) {
if (out_error) {
*out_error = std::string("Could not create directory '") + path +
"' (error code: " + std::to_string(errno) + ")";
}
return false;
}
return true;
}
bool PathExists(const std::string& path) {
struct stat sb;
return (stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode));
}
} // namespace
PathString AppDataDir(const char* app_name, bool should_create,
std::string* out_error) {
if (!app_name || strlen(app_name) == 0) {
if (out_error) {
*out_error = "AppDataDir failed: no app_name provided";
}
return "";
}
std::string app_dir = app_name;
// On Linux, use XDG_DATA_HOME, usually $HOME/.local/share/$app_name
std::string home = XdgDataHomeDir(out_error);
if (home.empty()) {
home = HomeDir(out_error);
if (home.empty()) return "";
app_dir = std::string(".local/share/") + app_name;
}
std::string full_path = home + '/' + app_dir;
if (should_create && !PathExists(full_path)) {
std::string current_path = full_path[0] == '/' ? "/" : "";
for (const std::string& nested_dir : SplitString(full_path, '/')) {
current_path += nested_dir;
if (!PathExists(current_path)) {
bool created = Mkdir(current_path, out_error);
if (!created) return "";
}
current_path += '/';
}
}
return home + "/" + app_dir;
}
} // namespace firebase