Skip to content

Commit f70ea04

Browse files
committed
Use rename(2) wrapper
Bug: b/491555334
1 parent febecad commit f70ea04

9 files changed

Lines changed: 19 additions & 24 deletions

File tree

base/cvd/cuttlefish/common/libs/utils/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ cf_cc_library(
104104
"//cuttlefish/common/libs/utils:contains",
105105
"//cuttlefish/common/libs/utils:in_sandbox",
106106
"//cuttlefish/common/libs/utils:users",
107+
"//cuttlefish/posix:rename",
107108
"//cuttlefish/posix:strerror",
108109
"//cuttlefish/posix:symlink",
109110
"//cuttlefish/result",

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "cuttlefish/common/libs/utils/contains.h"
6969
#include "cuttlefish/common/libs/utils/in_sandbox.h"
7070
#include "cuttlefish/common/libs/utils/users.h"
71+
#include "cuttlefish/posix/rename.h"
7172
#include "cuttlefish/posix/strerror.h"
7273
#include "cuttlefish/result/result.h"
7374

@@ -187,9 +188,7 @@ Result<void> MoveDirectoryContents(const std::string& source,
187188
std::string src_filepath = source + "/" + filepath;
188189
std::string dst_filepath = destination + "/" + filepath;
189190
if (should_rename) {
190-
CF_EXPECT(rename(src_filepath.c_str(), dst_filepath.c_str()) == 0,
191-
"rename " << src_filepath << " to " << dst_filepath
192-
<< " failed: " << strerror(errno));
191+
CF_EXPECT(Rename(src_filepath, dst_filepath));
193192
} else {
194193
CF_EXPECT(
195194
Copy(src_filepath, dst_filepath),
@@ -490,9 +489,7 @@ Result<std::chrono::system_clock::time_point> FileModificationTime(
490489
Result<std::string> RenameFile(const std::string& current_filepath,
491490
const std::string& target_filepath) {
492491
if (current_filepath != target_filepath) {
493-
CF_EXPECT(rename(current_filepath.c_str(), target_filepath.c_str()) == 0,
494-
"rename " << current_filepath << " to " << target_filepath
495-
<< " failed: " << strerror(errno));
492+
CF_EXPECT(Rename(current_filepath, target_filepath));
496493
}
497494
return target_filepath;
498495
}

base/cvd/cuttlefish/host/commands/cvd/fetch/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ cf_cc_library(
7979
hdrs = ["de_android_sparse.h"],
8080
deps = [
8181
"//cuttlefish/host/libs/image_aggregator:sparse_image",
82+
"//cuttlefish/posix:rename",
8283
"//cuttlefish/result",
8384
"//libbase",
8485
"@abseil-cpp//absl/log",
86+
"@abseil-cpp//absl/log:check",
8587
"@android_system_core//:libsparse",
8688
],
8789
)

base/cvd/cuttlefish/host/commands/cvd/fetch/de_android_sparse.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
#include "cuttlefish/host/commands/cvd/fetch/de_android_sparse.h"
1717

1818
#include <cstddef>
19-
#include <iostream>
2019
#include <string>
2120
#include <vector>
2221

23-
#include <android-base/file.h>
24-
#include <sparse/sparse.h>
22+
#include "absl/log/check.h"
2523
#include "absl/log/log.h"
24+
#include "android-base/file.h"
25+
#include "sparse/sparse.h"
2626

2727
#include "cuttlefish/host/libs/image_aggregator/sparse_image.h"
28+
#include "cuttlefish/posix/rename.h"
2829
#include "cuttlefish/result/result.h"
2930

3031
namespace cuttlefish {
@@ -71,11 +72,8 @@ bool ConvertToRawImageNoBinary(const std::string& image_path) {
7172
PLOG(FATAL) << "Unable to delete original sparse image";
7273
}
7374

74-
int success = rename(tmp_raw_image_path.c_str(), image_path.c_str());
75-
if (success != 0) {
76-
LOG(FATAL) << "Unable to rename raw image " << success;
77-
return false;
78-
}
75+
Result<void> result = Rename(tmp_raw_image_path, image_path.c_str());
76+
CHECK(result.ok()) << "Unable to rename raw image: " << result.error();
7977

8078
return true;
8179
}

base/cvd/cuttlefish/host/libs/directories/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cf_cc_library(
1717
"//cuttlefish/common/libs/utils:environment",
1818
"//cuttlefish/common/libs/utils:files",
1919
"//cuttlefish/common/libs/utils:users",
20+
"//cuttlefish/posix:rename",
2021
"//cuttlefish/posix:strerror",
2122
"//cuttlefish/result",
2223
"//libbase",

base/cvd/cuttlefish/host/libs/directories/xdg.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
#include "cuttlefish/common/libs/fs/shared_buf.h"
2929
#include "cuttlefish/common/libs/fs/shared_fd.h"
30-
#include "cuttlefish/common/libs/fs/shared_fd.h"
3130
#include "cuttlefish/common/libs/utils/environment.h"
3231
#include "cuttlefish/common/libs/utils/files.h"
3332
#include "cuttlefish/common/libs/utils/users.h"
33+
#include "cuttlefish/posix/rename.h"
3434
#include "cuttlefish/posix/strerror.h"
3535
#include "cuttlefish/result/result.h"
3636

@@ -175,9 +175,7 @@ Result<void> WriteCvdDataFile(std::string_view path, std::string contents) {
175175
CF_EXPECT_EQ(WriteAll(file_fd, contents), contents.size(),
176176
file_fd->StrError());
177177

178-
CF_EXPECTF(rename(full_path_template.data(), full_path.data()) == 0,
179-
"Failed to rename '{}' to '{}': '{}'", full_path_template,
180-
full_path, StrError(errno));
178+
CF_EXPECT(Rename(full_path_template, full_path));
181179

182180
return {};
183181
}

base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ cf_cc_library(
132132
"//cuttlefish/common/libs/utils:subprocess",
133133
"//cuttlefish/host/libs/config:known_paths",
134134
"//cuttlefish/host/libs/image_aggregator:disk_image",
135-
"//cuttlefish/posix:strerror",
135+
"//cuttlefish/posix:rename",
136136
"//cuttlefish/result",
137137
"//libbase",
138138
"@android_system_core//:libsparse",

base/cvd/cuttlefish/host/libs/image_aggregator/sparse_image.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "cuttlefish/common/libs/fs/shared_fd.h"
3030
#include "cuttlefish/common/libs/utils/subprocess.h"
3131
#include "cuttlefish/host/libs/config/known_paths.h"
32-
#include "cuttlefish/posix/strerror.h"
32+
#include "cuttlefish/posix/rename.h"
3333
#include "cuttlefish/result/result.h"
3434

3535
namespace cuttlefish {
@@ -91,9 +91,7 @@ Result<void> ForceRawImage(const std::string& image_path) {
9191
// `rename` can fail if these are on different mounts, but they are files
9292
// within the same directory so they can only be in different mounts if one
9393
// is a bind mount, in which case `rename` won't work anyway.
94-
CF_EXPECTF(rename(tmp_raw_image_path.c_str(), image_path.c_str()) == 0,
95-
"rename('{}','{}') failed: {}", tmp_raw_image_path, image_path,
96-
StrError(errno));
94+
CF_EXPECT(Rename(tmp_raw_image_path, image_path));
9795

9896
return {};
9997
}

base/cvd/cuttlefish/posix/rename.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2025 The Android Open Source Project
2+
* Copyright (C) 2026 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)