|
| 1 | +// Copyright 2017 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <dirent.h> |
| 16 | +#include <pwd.h> |
| 17 | +#include <sys/stat.h> |
| 18 | +#include <sys/types.h> |
| 19 | +#include <unistd.h> |
| 20 | + |
| 21 | +#include "database/src/desktop/util_desktop.h" |
| 22 | + |
| 23 | +namespace firebase { |
| 24 | +namespace database { |
| 25 | +namespace internal { |
| 26 | + |
| 27 | +std::string GetAppDataPath(const char* app_name, bool should_create) { |
| 28 | + // On Linux, use XDG data home, usually $HOME/.local/share/$app_name, or user |
| 29 | + // home directory otherwise. |
| 30 | + |
| 31 | + const char* xdg_data_home = getenv("XDG_DATA_HOME"); |
| 32 | + if (xdg_data_home) { |
| 33 | + // Assume that this directory already exists. |
| 34 | + return std::string(xdg_data_home); |
| 35 | + } |
| 36 | + std::string home_directory = ""; |
| 37 | + |
| 38 | + const char* home = getenv("HOME"); |
| 39 | + if (home) { |
| 40 | + home_directory = std::string(home); |
| 41 | + } else { |
| 42 | + // Get home directory from user info. |
| 43 | + passwd pwd; |
| 44 | + passwd* result; |
| 45 | + auto buffer_size = static_cast<size_t>(sysconf(_SC_GETPW_R_SIZE_MAX)); |
| 46 | + std::string buffer(buffer_size, '\0'); |
| 47 | + uid_t uid = getuid(); |
| 48 | + int rc; |
| 49 | + do { |
| 50 | + rc = getpwuid_r(uid, &pwd, &buffer[0], buffer_size, &result); |
| 51 | + } while (rc == EINTR); |
| 52 | + if (rc == 0) { |
| 53 | + home_directory = std::string(pwd.pw_dir); |
| 54 | + } |
| 55 | + } |
| 56 | + if (home_directory.empty()) return ""; |
| 57 | + |
| 58 | + // Make sure home/.local/share exists, ignore "already exists" errors making |
| 59 | + // these directories. |
| 60 | + if (should_create) { |
| 61 | + int retval; |
| 62 | + retval = mkdir((home_directory + "/.local").c_str(), 0700); |
| 63 | + if (retval != 0 && errno != EEXIST) return ""; |
| 64 | + retval = mkdir((home_directory + "/.local/share").c_str(), 0700); |
| 65 | + if (retval != 0 && errno != EEXIST) return ""; |
| 66 | + retval = mkdir((home_directory + "/.local/share" + app_name).c_str(), 0700); |
| 67 | + if (retval != 0 && errno != EEXIST) return ""; |
| 68 | + } |
| 69 | + return (home_directory + "/.local/share" + app_name); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace internal |
| 73 | +} // namespace database |
| 74 | +} // namespace firebase |
0 commit comments