Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 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 | #include "FileUtils.h" |
| 18 | |
| 19 | #ifdef BINDER_NO_LIBBASE |
| 20 | |
| 21 | #include <sys/stat.h> |
| 22 | #include <filesystem> |
| 23 | |
| 24 | #if defined(__APPLE__) |
| 25 | #include <mach-o/dyld.h> |
| 26 | #endif |
| 27 | #if defined(_WIN32) |
| 28 | #include <direct.h> |
| 29 | #include <windows.h> |
| 30 | #endif |
| 31 | |
| 32 | namespace android::binder { |
| 33 | |
| 34 | bool ReadFdToString(borrowed_fd fd, std::string* content) { |
| 35 | content->clear(); |
| 36 | |
| 37 | // Although original we had small files in mind, this code gets used for |
| 38 | // very large files too, where the std::string growth heuristics might not |
| 39 | // be suitable. https://code.google.com/p/android/issues/detail?id=258500. |
| 40 | struct stat sb; |
| 41 | if (fstat(fd.get(), &sb) != -1 && sb.st_size > 0) { |
| 42 | content->reserve(sb.st_size); |
| 43 | } |
| 44 | |
| 45 | char buf[4096] __attribute__((__uninitialized__)); |
| 46 | ssize_t n; |
| 47 | while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) { |
| 48 | content->append(buf, n); |
| 49 | } |
| 50 | return (n == 0) ? true : false; |
| 51 | } |
| 52 | |
| 53 | bool WriteStringToFd(std::string_view content, borrowed_fd fd) { |
| 54 | const char* p = content.data(); |
| 55 | size_t left = content.size(); |
| 56 | while (left > 0) { |
| 57 | ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, left)); |
| 58 | if (n == -1) { |
| 59 | return false; |
| 60 | } |
| 61 | p += n; |
| 62 | left -= n; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | static std::filesystem::path GetExecutablePath2() { |
| 68 | #if defined(__linux__) |
| 69 | return std::filesystem::read_symlink("/proc/self/exe"); |
| 70 | #elif defined(__APPLE__) |
| 71 | char path[PATH_MAX + 1]; |
| 72 | uint32_t path_len = sizeof(path); |
| 73 | int rc = _NSGetExecutablePath(path, &path_len); |
| 74 | if (rc < 0) { |
| 75 | std::unique_ptr<char> path_buf(new char[path_len]); |
| 76 | _NSGetExecutablePath(path_buf.get(), &path_len); |
| 77 | return path_buf.get(); |
| 78 | } |
| 79 | return path; |
| 80 | #elif defined(_WIN32) |
| 81 | char path[PATH_MAX + 1]; |
| 82 | DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 83 | if (result == 0 || result == sizeof(path) - 1) return ""; |
| 84 | path[PATH_MAX - 1] = 0; |
| 85 | return path; |
| 86 | #elif defined(__EMSCRIPTEN__) |
| 87 | abort(); |
| 88 | #else |
| 89 | #error unknown OS |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | std::string GetExecutableDirectory() { |
| 94 | return GetExecutablePath2().parent_path(); |
| 95 | } |
| 96 | |
| 97 | } // namespace android::binder |
| 98 | |
| 99 | #endif // BINDER_NO_LIBBASE |