Skip to content

Commit d30432f

Browse files
committed
Report nonexistent directories from IsDirectoryEmpty
#1762 (comment) Bug: b/457549895
1 parent eaad16d commit d30432f

File tree

5 files changed

+8
-13
lines changed

5 files changed

+8
-13
lines changed

base/cvd/cuttlefish/common/libs/utils/files.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,14 @@ bool CanAccess(const std::string& path, const int mode) {
284284
return access(path.c_str(), mode) == 0;
285285
}
286286

287-
bool IsDirectoryEmpty(const std::string& path) {
287+
Result<bool> IsDirectoryEmpty(const std::string& path) {
288288
std::unique_ptr<DIR, int (*)(DIR*)> direc(opendir(path.c_str()), closedir);
289-
if (!direc) {
290-
LOG(ERROR) << "IsDirectoryEmpty test failed with " << path
291-
<< " as it failed to be open" << std::endl;
292-
return false;
293-
}
289+
CF_EXPECTF(direc.get(), "opendir('{}') failed: {}", path, StrError(errno));
294290

295-
int cnt {0};
291+
int cnt = 0;
296292
while (::readdir(direc.get())) {
297293
cnt++;
298294
if (cnt > 2) {
299-
LOG(ERROR) << "IsDirectoryEmpty test failed with " << path
300-
<< " as it exists but not empty" << std::endl;
301295
return false;
302296
}
303297
}

base/cvd/cuttlefish/common/libs/utils/files.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Result<void> EnsureDirectoryExists(const std::string& directory_path,
6262
Result<void> ChangeGroup(const std::string& path,
6363
const std::string& group_name);
6464
bool CanAccess(const std::string& path, int mode);
65-
bool IsDirectoryEmpty(const std::string& path);
65+
Result<bool> IsDirectoryEmpty(const std::string& path);
6666
Result<void> RecursivelyRemoveDirectory(const std::string& path);
6767
bool Copy(const std::string& from, const std::string& to);
6868
off_t FileSize(const std::string& path);

base/cvd/cuttlefish/common/libs/utils/files_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ TEST_F(FilesTests, MoveDirectoryContentsFailsIfSourceIsNotADirectory) {
9292
TEST_F(FilesTests, MoveDirectoryContents) {
9393
Result<void> result = MoveDirectoryContents(src_dir_, dst_dir_);
9494
EXPECT_THAT(result, IsOk());
95-
EXPECT_THAT(IsDirectoryEmpty(src_dir_), IsTrue());
95+
EXPECT_THAT(IsDirectoryEmpty(src_dir_), IsOkAndValue(true));
9696
EXPECT_THAT(FileExists(dst_dir_ + "/file1.txt"), IsTrue());
9797
EXPECT_THAT(FileExists(dst_dir_ + "/sub_dir/file2.txt"), IsTrue());
9898
}

base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ Result<const CuttlefishConfig*> InitFilesystemAndCreateConfig(
453453
std::string vsock_dir = fmt::format("{}/vsock_{}_{}", TempDir(),
454454
instance.vsock_guest_cid(), getuid());
455455
if (DirectoryExists(vsock_dir, /* follow_symlinks */ false) &&
456-
!IsDirectoryEmpty(vsock_dir)) {
456+
!CF_EXPECT(IsDirectoryEmpty(vsock_dir))) {
457457
CF_EXPECT(RecursivelyRemoveDirectory(vsock_dir));
458458
}
459459
CF_EXPECT(EnsureDirectoryExists(vsock_dir, default_mode, default_group));

base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,8 @@ Result<void> SetDefaultFlagsForCrosvm(
13591359
bool default_enable_sandbox =
13601360
supported_archs.find(HostArch()) != supported_archs.end() &&
13611361
EnsureDirectoryExists(kCrosvmVarEmptyDir).ok() &&
1362-
IsDirectoryEmpty(kCrosvmVarEmptyDir) && !IsRunningInContainer();
1362+
CF_EXPECT(IsDirectoryEmpty(kCrosvmVarEmptyDir)) &&
1363+
!IsRunningInContainer();
13631364

13641365
std::string default_enable_sandbox_str = "";
13651366
for (int instance_index = 0; instance_index < instance_nums.size();

0 commit comments

Comments
 (0)