| /* |
| * Copyright (C) 2019 The Android Open Source Project |
| * |
| * 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 "path.h" |
| |
| #include <iterator> |
| |
| #include <limits.h> |
| #include <stdlib.h> |
| #include <sys/stat.h> |
| #include <unistd.h> |
| |
| namespace android::incfs::path { |
| |
| std::string normalize(std::string_view path) { |
| if (path.empty()) { |
| return {}; |
| } |
| char buffer[PATH_MAX]; |
| if (!::realpath(path.data(), buffer)) { |
| // need to return something |
| return std::string{path}; |
| } |
| return std::string(buffer); |
| } |
| |
| std::string fromFd(int fd) { |
| static constexpr char fdNameFormat[] = "/proc/self/fd/%d"; |
| char fdNameBuffer[std::size(fdNameFormat) + 11 + 1]; // max int length + '\0' |
| snprintf(fdNameBuffer, std::size(fdNameBuffer), fdNameFormat, fd); |
| |
| std::string res; |
| // lstat() is supposed to return us exactly the needed buffer size, but |
| // somehow it may also return a smaller (but still >0) st_size field. |
| // That's why let's only use it for the initial estimate. |
| struct stat st = {}; |
| if (::lstat(fdNameBuffer, &st) || st.st_size == 0) { |
| st.st_size = PATH_MAX; |
| } |
| auto bufSize = st.st_size; |
| for (;;) { |
| res.resize(bufSize + 1, '\0'); |
| auto size = ::readlink(fdNameBuffer, &res[0], res.size()); |
| if (size < 0) { |
| return {}; |
| } |
| if (size > bufSize) { |
| // File got renamed in between lstat() and readlink() calls? Retry. |
| bufSize *= 2; |
| continue; |
| } |
| res.resize(size); |
| return res; |
| } |
| } |
| |
| } // namespace android::incfs::path |