Jorge E. Moreira | 02f464c | 2018-07-06 11:30:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "common/libs/utils/files.h" |
| 18 | |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 19 | #include <glog/logging.h> |
| 20 | |
| 21 | #include <array> |
| 22 | #include <climits> |
| 23 | #include <cstdlib> |
Jorge E. Moreira | 02f464c | 2018-07-06 11:30:34 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | namespace cvd { |
| 29 | |
| 30 | bool FileHasContent(const std::string& path) { |
Cody Schuffelen | 2402ee5 | 2018-12-05 17:51:36 -0800 | [diff] [blame^] | 31 | return FileSize(path) > 0; |
Jorge E. Moreira | 02f464c | 2018-07-06 11:30:34 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | bool DirectoryExists(const std::string& path) { |
| 35 | struct stat st; |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 36 | if (stat(path.c_str(), &st) == -1) { |
| 37 | return false; |
| 38 | } |
| 39 | if ((st.st_mode & S_IFMT) != S_IFDIR) { |
| 40 | return false; |
| 41 | } |
Jorge E. Moreira | 02f464c | 2018-07-06 11:30:34 -0700 | [diff] [blame] | 42 | return true; |
| 43 | } |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 44 | |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 45 | std::string AbsolutePath(const std::string& path) { |
| 46 | if (path.empty()) { |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 47 | return {}; |
| 48 | } |
Ryan Haining | d3f185d | 2018-07-19 12:11:47 -0700 | [diff] [blame] | 49 | if (path[0] == '/') { |
| 50 | return path; |
| 51 | } |
| 52 | |
| 53 | std::array<char, PATH_MAX> buffer{}; |
| 54 | if (!realpath(".", buffer.data())) { |
| 55 | LOG(WARNING) << "Could not get real path for current directory \".\"" |
| 56 | << ": " << strerror(errno); |
| 57 | return {}; |
| 58 | } |
| 59 | return std::string{buffer.data()} + "/" + path; |
Ryan Haining | 10e4231 | 2018-07-17 12:11:52 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Cody Schuffelen | 2402ee5 | 2018-12-05 17:51:36 -0800 | [diff] [blame^] | 62 | off_t FileSize(const std::string& path) { |
| 63 | struct stat st; |
| 64 | if (stat(path.c_str(), &st) == -1) { |
| 65 | return 0; |
| 66 | } |
| 67 | return st.st_size; |
| 68 | } |
| 69 | |
Jorge E. Moreira | 02f464c | 2018-07-06 11:30:34 -0700 | [diff] [blame] | 70 | } // namespace cvd |