Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 17 | #include <ftw.h> |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 18 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 20 | #include <pwd.h> |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/stat.h> |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 27 | #include <android-base/file.h> |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
| 30 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 33 | static void MakeTree(const char* root) { |
| 34 | char path[PATH_MAX]; |
| 35 | |
| 36 | snprintf(path, sizeof(path), "%s/dir", root); |
Elliott Hughes | 0ad256c | 2015-04-01 12:22:40 -0700 | [diff] [blame] | 37 | ASSERT_EQ(0, mkdir(path, 0755)) << path; |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 38 | snprintf(path, sizeof(path), "%s/dir/sub", root); |
Elliott Hughes | 0ad256c | 2015-04-01 12:22:40 -0700 | [diff] [blame] | 39 | ASSERT_EQ(0, mkdir(path, 0555)) << path; |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 40 | snprintf(path, sizeof(path), "%s/unreadable-dir", root); |
Elliott Hughes | 0ad256c | 2015-04-01 12:22:40 -0700 | [diff] [blame] | 41 | ASSERT_EQ(0, mkdir(path, 0000)) << path; |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 42 | |
| 43 | snprintf(path, sizeof(path), "%s/dangler", root); |
| 44 | ASSERT_EQ(0, symlink("/does-not-exist", path)); |
| 45 | snprintf(path, sizeof(path), "%s/symlink", root); |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 46 | ASSERT_EQ(0, symlink("dir/sub", path)); |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 47 | |
| 48 | int fd; |
| 49 | snprintf(path, sizeof(path), "%s/regular", root); |
| 50 | ASSERT_NE(-1, fd = open(path, O_CREAT|O_TRUNC, 0666)); |
| 51 | ASSERT_EQ(0, close(fd)); |
| 52 | } |
| 53 | |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 54 | void smoke_test_ftw(const char* fpath, const struct stat* sb, int tflag) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 55 | ASSERT_TRUE(fpath != nullptr); |
| 56 | ASSERT_TRUE(sb != nullptr); |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 57 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 58 | // Was it a case where the struct stat we're given is meaningless? |
| 59 | if (tflag == FTW_NS || tflag == FTW_SLN) { |
| 60 | // If so, double-check that we really can't stat. |
| 61 | struct stat sb; |
| 62 | EXPECT_EQ(-1, stat(fpath, &sb)); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Otherwise check that the struct stat matches the type flag. |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 67 | if (S_ISDIR(sb->st_mode)) { |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 68 | if (access(fpath, R_OK) == 0) { |
| 69 | EXPECT_TRUE(tflag == FTW_D || tflag == FTW_DP) << fpath << ' ' << tflag; |
| 70 | } else { |
| 71 | EXPECT_EQ(FTW_DNR, tflag) << fpath; |
| 72 | } |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 73 | } else if (S_ISLNK(sb->st_mode)) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 74 | EXPECT_EQ(FTW_SL, tflag) << fpath; |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 75 | } else { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 76 | EXPECT_EQ(FTW_F, tflag) << fpath; |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 77 | } |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 80 | void smoke_test_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) { |
| 81 | smoke_test_ftw(fpath, sb, tflag); |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 82 | ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath; |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int check_ftw(const char* fpath, const struct stat* sb, int tflag) { |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 86 | smoke_test_ftw(fpath, sb, tflag); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int check_ftw64(const char* fpath, const struct stat64* sb, int tflag) { |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 91 | smoke_test_ftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 95 | int check_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) { |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 96 | smoke_test_nftw(fpath, sb, tflag, ftwbuf); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 100 | int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, FTW* ftwbuf) { |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 101 | smoke_test_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | TEST(ftw, ftw) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 106 | TemporaryDir root; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 107 | MakeTree(root.path); |
| 108 | ASSERT_EQ(0, ftw(root.path, check_ftw, 128)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 111 | TEST(ftw, ftw64_smoke) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 112 | TemporaryDir root; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 113 | MakeTree(root.path); |
| 114 | ASSERT_EQ(0, ftw64(root.path, check_ftw64, 128)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(ftw, nftw) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 118 | TemporaryDir root; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 119 | MakeTree(root.path); |
| 120 | ASSERT_EQ(0, nftw(root.path, check_nftw, 128, 0)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 123 | TEST(ftw, nftw64_smoke) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 124 | TemporaryDir root; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 125 | MakeTree(root.path); |
| 126 | ASSERT_EQ(0, nftw64(root.path, check_nftw64, 128, 0)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 127 | } |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 128 | |
| 129 | template <typename StatT> |
| 130 | static int bug_28197840_ftw(const char* path, const StatT*, int flag) { |
| 131 | EXPECT_EQ(strstr(path, "unreadable") != nullptr ? FTW_DNR : FTW_D, flag) << path; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | template <typename StatT> |
| 136 | static int bug_28197840_nftw(const char* path, const StatT* sb, int flag, FTW*) { |
| 137 | return bug_28197840_ftw(path, sb, flag); |
| 138 | } |
| 139 | |
| 140 | TEST(ftw, bug_28197840) { |
| 141 | // Drop root for this test, because root can still read directories even if |
| 142 | // permissions would imply otherwise. |
| 143 | if (getuid() == 0) { |
| 144 | passwd* pwd = getpwnam("shell"); |
| 145 | ASSERT_EQ(0, setuid(pwd->pw_uid)); |
| 146 | } |
| 147 | |
| 148 | TemporaryDir root; |
| 149 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 150 | std::string path = android::base::StringPrintf("%s/unreadable-directory", root.path); |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 151 | ASSERT_EQ(0, mkdir(path.c_str(), 0000)) << path; |
| 152 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 153 | ASSERT_EQ(0, ftw(root.path, bug_28197840_ftw<struct stat>, 128)); |
| 154 | ASSERT_EQ(0, ftw64(root.path, bug_28197840_ftw<struct stat64>, 128)); |
| 155 | ASSERT_EQ(0, nftw(root.path, bug_28197840_nftw<struct stat>, 128, FTW_PHYS)); |
| 156 | ASSERT_EQ(0, nftw64(root.path, bug_28197840_nftw<struct stat64>, 128, FTW_PHYS)); |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame] | 157 | } |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 158 | |
| 159 | template <typename StatT> |
| 160 | static int null_ftw_callback(const char*, const StatT*, int) { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | template <typename StatT> |
| 165 | static int null_nftw_callback(const char*, const StatT*, int, FTW*) { |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | TEST(ftw, ftw_non_existent_ENOENT) { |
| 170 | errno = 0; |
| 171 | ASSERT_EQ(-1, ftw("/does/not/exist", null_ftw_callback<struct stat>, 128)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 172 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 173 | errno = 0; |
| 174 | ASSERT_EQ(-1, ftw64("/does/not/exist", null_ftw_callback<struct stat64>, 128)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 175 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST(ftw, nftw_non_existent_ENOENT) { |
| 179 | errno = 0; |
| 180 | ASSERT_EQ(-1, nftw("/does/not/exist", null_nftw_callback<struct stat>, 128, FTW_PHYS)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 181 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 182 | errno = 0; |
| 183 | ASSERT_EQ(-1, nftw64("/does/not/exist", null_nftw_callback<struct stat64>, 128, FTW_PHYS)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 184 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST(ftw, ftw_empty_ENOENT) { |
| 188 | errno = 0; |
| 189 | ASSERT_EQ(-1, ftw("", null_ftw_callback<struct stat>, 128)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 190 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 191 | errno = 0; |
| 192 | ASSERT_EQ(-1, ftw64("", null_ftw_callback<struct stat64>, 128)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 193 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | TEST(ftw, nftw_empty_ENOENT) { |
| 197 | errno = 0; |
| 198 | ASSERT_EQ(-1, nftw("", null_nftw_callback<struct stat>, 128, FTW_PHYS)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 199 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 200 | errno = 0; |
| 201 | ASSERT_EQ(-1, nftw64("", null_nftw_callback<struct stat64>, 128, FTW_PHYS)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 202 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 70a8f22 | 2018-05-07 16:44:13 -0700 | [diff] [blame] | 203 | } |