1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifdef _WIN32
18 #include <android-base/utf8.h>
19 #include <direct.h>
20 #include <shlobj.h>
21 #else
22 #include <pwd.h>
23 #endif
24 
25 #include <android-base/logging.h>
26 #include <android-base/parseint.h>
27 #include <sys/file.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 
31 #include <vector>
32 
33 #include "filesystem.h"
34 
35 namespace {
36 
LockFile(int fd)37 int LockFile(int fd) {
38 #ifdef _WIN32
39     HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
40     OVERLAPPED overlapped = {};
41     const BOOL locked =
42             LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, MAXDWORD, MAXDWORD, &overlapped);
43     return locked ? 0 : -1;
44 #else
45     return flock(fd, LOCK_EX);
46 #endif
47 }
48 
49 }  // namespace
50 
51 // inspired by adb implementation:
52 // cs.android.com/android/platform/superproject/+/master:packages/modules/adb/adb_utils.cpp;l=275
GetHomeDirPath()53 std::string GetHomeDirPath() {
54 #ifdef _WIN32
55     WCHAR path[MAX_PATH];
56     const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
57     if (FAILED(hr)) {
58         return {};
59     }
60     std::string home_str;
61     if (!android::base::WideToUTF8(path, &home_str)) {
62         return {};
63     }
64     return home_str;
65 #else
66     if (const char* const home = getenv("HOME")) {
67         return home;
68     }
69 
70     struct passwd pwent;
71     struct passwd* result;
72     int pwent_max = sysconf(_SC_GETPW_R_SIZE_MAX);
73     if (pwent_max == -1) {
74         pwent_max = 16384;
75     }
76     std::vector<char> buf(pwent_max);
77     int rc = getpwuid_r(getuid(), &pwent, buf.data(), buf.size(), &result);
78     if (rc == 0 && result) {
79         return result->pw_dir;
80     }
81 #endif
82 
83     return {};
84 }
85 
FileExists(const std::string & path)86 bool FileExists(const std::string& path) {
87     return access(path.c_str(), F_OK) == 0;
88 }
89 
EnsureDirectoryExists(const std::string & directory_path)90 bool EnsureDirectoryExists(const std::string& directory_path) {
91     const int result =
92 #ifdef _WIN32
93             _mkdir(directory_path.c_str());
94 #else
95             mkdir(directory_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
96 #endif
97 
98     return result == 0 || errno == EEXIST;
99 }
100 
FileLock(const std::string & path)101 FileLock::FileLock(const std::string& path) : fd_(open(path.c_str(), O_CREAT | O_WRONLY, 0644)) {
102     if (LockFile(fd_.get()) != 0) {
103         LOG(FATAL) << "Failed to acquire a lock on " << path;
104     }
105 }