diff --git a/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel b/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel index f0fd3c402a9..28cb3d33b90 100644 --- a/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel +++ b/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel @@ -122,6 +122,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:contains", "//cuttlefish/common/libs/utils:in_sandbox", "//cuttlefish/common/libs/utils:users", + "//cuttlefish/files:copy", "//cuttlefish/posix:rename", "//cuttlefish/posix:strerror", "//cuttlefish/posix:symlink", diff --git a/base/cvd/cuttlefish/common/libs/utils/files.cpp b/base/cvd/cuttlefish/common/libs/utils/files.cpp index a528ceb784d..8818ae8de44 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files.cpp +++ b/base/cvd/cuttlefish/common/libs/utils/files.cpp @@ -61,6 +61,7 @@ #include "cuttlefish/common/libs/utils/environment.h" #include "cuttlefish/common/libs/utils/in_sandbox.h" #include "cuttlefish/common/libs/utils/users.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/posix/rename.h" #include "cuttlefish/posix/strerror.h" #include "cuttlefish/result/result.h" @@ -297,62 +298,6 @@ Result IsDirectoryEmpty(const std::string& path) { return true; } -bool Copy(const std::string& from, const std::string& to) { - SharedFD fd_from = SharedFD::Open(from, O_RDONLY); - SharedFD fd_to = SharedFD::Open(to, O_WRONLY | O_CREAT | O_TRUNC, 0644); - - if (!fd_from->IsOpen() || !fd_to->IsOpen()) { - return false; - } - - off_t farthest_seek = fd_from->LSeek(0, SEEK_END); - if (farthest_seek == -1) { - LOG(ERROR) << "Could not lseek in \"" << from - << "\": " << fd_from->StrError(); - return false; - } - if (fd_to->Truncate(farthest_seek) < 0) { - LOG(ERROR) << "Failed to truncate " << to << ": " << fd_to->StrError(); - } - off_t offset = 0; - while (offset < farthest_seek) { - off_t new_offset = fd_from->LSeek(offset, SEEK_HOLE); - if (new_offset == -1) { - if (fd_from->GetErrno() == ENXIO) { - return true; - } - LOG(ERROR) << "Could not lseek in \"" << from - << "\": " << fd_from->StrError(); - return false; - } - auto data_bytes = new_offset - offset; - if (fd_to->LSeek(offset, SEEK_SET) < 0) { - LOG(ERROR) << "lseek() on " << to << " failed: " << fd_to->StrError(); - return false; - } - if (!fd_to->SendFile(*fd_from, &offset, data_bytes)) { - LOG(ERROR) << "SendFile failed: " << fd_to->StrError(); - return false; - } - CHECK_EQ(offset, new_offset); - - if (offset >= farthest_seek) { - return true; - } - new_offset = fd_from->LSeek(offset, SEEK_DATA); - if (new_offset == -1) { - if (fd_from->GetErrno() == ENXIO) { - return true; - } - LOG(ERROR) << "Could not lseek in \"" << from - << "\": " << fd_from->StrError(); - return false; - } - offset = new_offset; - } - return true; -} - std::string AbsolutePath(std::string_view path) { if (path.empty()) { return {}; diff --git a/base/cvd/cuttlefish/common/libs/utils/files.h b/base/cvd/cuttlefish/common/libs/utils/files.h index 4541bf38507..afb8f8a46f6 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files.h +++ b/base/cvd/cuttlefish/common/libs/utils/files.h @@ -65,7 +65,6 @@ Result ChangeGroup(const std::string& path, const std::string& group_name); bool CanAccess(const std::string& path, int mode); Result IsDirectoryEmpty(const std::string& path); -bool Copy(const std::string& from, const std::string& to); off_t FileSize(const std::string& path); Result RemoveFile(const std::string& file); Result RenameFile(const std::string& current_filepath, diff --git a/base/cvd/cuttlefish/files/BUILD.bazel b/base/cvd/cuttlefish/files/BUILD.bazel index e18bec5b8ee..adcdbba0278 100644 --- a/base/cvd/cuttlefish/files/BUILD.bazel +++ b/base/cvd/cuttlefish/files/BUILD.bazel @@ -6,6 +6,17 @@ package( cf_build_test(name = "files_build_test") +cf_cc_library( + name = "copy", + srcs = ["copy.cc"], + hdrs = ["copy.h"], + deps = [ + "//cuttlefish/common/libs/fs", + "@abseil-cpp//absl/log", + "@abseil-cpp//absl/log:check", + ], +) + cf_cc_library( name = "recursively_remove_directory", srcs = ["recursively_remove_directory.cc"], diff --git a/base/cvd/cuttlefish/files/copy.cc b/base/cvd/cuttlefish/files/copy.cc new file mode 100644 index 00000000000..b6a3e5cdaf5 --- /dev/null +++ b/base/cvd/cuttlefish/files/copy.cc @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2017 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 "cuttlefish/files/copy.h" + +#include +#include +#include +#include + +#include + +#include "absl/log/check.h" +#include "absl/log/log.h" + +#include "cuttlefish/common/libs/fs/shared_fd.h" + +namespace cuttlefish { + +bool Copy(const std::string& from, const std::string& to) { + SharedFD fd_from = SharedFD::Open(from, O_RDONLY); + SharedFD fd_to = SharedFD::Open(to, O_WRONLY | O_CREAT | O_TRUNC, 0644); + + if (!fd_from->IsOpen() || !fd_to->IsOpen()) { + return false; + } + + off_t farthest_seek = fd_from->LSeek(0, SEEK_END); + if (farthest_seek == -1) { + LOG(ERROR) << "Could not lseek in \"" << from + << "\": " << fd_from->StrError(); + return false; + } + if (fd_to->Truncate(farthest_seek) < 0) { + LOG(ERROR) << "Failed to truncate " << to << ": " << fd_to->StrError(); + } + off_t offset = 0; + while (offset < farthest_seek) { + off_t new_offset = fd_from->LSeek(offset, SEEK_HOLE); + if (new_offset == -1) { + if (fd_from->GetErrno() == ENXIO) { + return true; + } + LOG(ERROR) << "Could not lseek in \"" << from + << "\": " << fd_from->StrError(); + return false; + } + auto data_bytes = new_offset - offset; + if (fd_to->LSeek(offset, SEEK_SET) < 0) { + LOG(ERROR) << "lseek() on " << to << " failed: " << fd_to->StrError(); + return false; + } + if (!fd_to->SendFile(*fd_from, &offset, data_bytes)) { + LOG(ERROR) << "SendFile failed: " << fd_to->StrError(); + return false; + } + CHECK_EQ(offset, new_offset); + + if (offset >= farthest_seek) { + return true; + } + new_offset = fd_from->LSeek(offset, SEEK_DATA); + if (new_offset == -1) { + if (fd_from->GetErrno() == ENXIO) { + return true; + } + LOG(ERROR) << "Could not lseek in \"" << from + << "\": " << fd_from->StrError(); + return false; + } + offset = new_offset; + } + return true; +} + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/files/copy.h b/base/cvd/cuttlefish/files/copy.h new file mode 100644 index 00000000000..71de26f2d02 --- /dev/null +++ b/base/cvd/cuttlefish/files/copy.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2017 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. + */ + +#pragma once + +#include + +namespace cuttlefish { + +bool Copy(const std::string& from, const std::string& to); + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/BUILD.bazel b/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/BUILD.bazel index f8ca11a5d04..b8cb2919e5e 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/BUILD.bazel @@ -135,6 +135,7 @@ cf_cc_library( hdrs = ["gem5_image_unpacker.h"], deps = [ "//cuttlefish/common/libs/utils:files", + "//cuttlefish/files:copy", "//cuttlefish/host/commands/assemble_cvd:boot_image_utils", "//cuttlefish/host/commands/assemble_cvd/flags:boot_image", "//cuttlefish/host/libs/config:cuttlefish_config", @@ -235,6 +236,7 @@ cf_cc_library( hdrs = ["kernel_ramdisk_repacker.h"], deps = [ "//cuttlefish/common/libs/utils:files", + "//cuttlefish/files:copy", "//cuttlefish/host/commands/assemble_cvd:boot_image_utils", "//cuttlefish/host/commands/assemble_cvd:vendor_dlkm_utils", "//cuttlefish/host/commands/assemble_cvd/disk:image_file", diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/gem5_image_unpacker.cpp b/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/gem5_image_unpacker.cpp index d8be906d498..11be1b23160 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/gem5_image_unpacker.cpp +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/gem5_image_unpacker.cpp @@ -20,6 +20,7 @@ #include "android-base/file.h" #include "cuttlefish/common/libs/utils/files.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/host/commands/assemble_cvd/boot_image_utils.h" #include "cuttlefish/host/commands/assemble_cvd/flags/boot_image.h" #include "cuttlefish/host/libs/config/cuttlefish_config.h" diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/kernel_ramdisk_repacker.cpp b/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/kernel_ramdisk_repacker.cpp index bf8d4eebafe..5b140c4a95a 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/kernel_ramdisk_repacker.cpp +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/disk/kernel_ramdisk_repacker.cpp @@ -21,6 +21,7 @@ #include "absl/log/log.h" #include "cuttlefish/common/libs/utils/files.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/host/commands/assemble_cvd/boot_image_utils.h" #include "cuttlefish/host/commands/assemble_cvd/flags/boot_image.h" #include "cuttlefish/host/commands/assemble_cvd/vendor_dlkm_utils.h" diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel b/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel index e05c387c62a..22a3a4f9a3f 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel @@ -121,6 +121,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:environment", "//cuttlefish/common/libs/utils:files", "//cuttlefish/common/libs/utils:users", + "//cuttlefish/files:copy", "//cuttlefish/files:recursively_remove_directory", "//cuttlefish/flag_parser", "//cuttlefish/host/commands/cvd/cli:command_request", diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp index 344f4d49f62..5bb440d3acb 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp @@ -43,6 +43,7 @@ #include "cuttlefish/common/libs/utils/environment.h" #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/common/libs/utils/users.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/files/recursively_remove_directory.h" #include "cuttlefish/flag_parser/flag.h" #include "cuttlefish/flag_parser/gflags_compat.h" diff --git a/base/cvd/cuttlefish/host/commands/cvd/fetch/BUILD.bazel b/base/cvd/cuttlefish/host/commands/cvd/fetch/BUILD.bazel index 32744b56ea4..ec0d196c503 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/fetch/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/cvd/fetch/BUILD.bazel @@ -180,6 +180,7 @@ cf_cc_library( deps = [ "//cuttlefish/common/libs/utils:archive", "//cuttlefish/common/libs/utils:files", + "//cuttlefish/files:copy", "//cuttlefish/host/commands/cvd/fetch:builds", "//cuttlefish/host/commands/cvd/fetch:de_android_sparse", "//cuttlefish/host/commands/cvd/fetch:fetch_tracer", diff --git a/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_context.cc b/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_context.cc index 36d46bc921a..9b71a3a7a54 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_context.cc +++ b/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_context.cc @@ -31,6 +31,7 @@ #include "fmt/ranges.h" #include "cuttlefish/common/libs/utils/files.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/host/commands/cvd/fetch/builds.h" #include "cuttlefish/host/commands/cvd/fetch/de_android_sparse.h" #include "cuttlefish/host/commands/cvd/fetch/fetch_tracer.h" diff --git a/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel b/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel index ae9013b1a26..68f3cb02ebb 100644 --- a/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel @@ -35,6 +35,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:environment", "//cuttlefish/common/libs/utils:files", "//cuttlefish/common/libs/utils:json", + "//cuttlefish/files:copy", "//cuttlefish/host/libs/config:config_instance_derived", "//cuttlefish/host/libs/config:cuttlefish_config", "//cuttlefish/posix:readlink", diff --git a/base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc b/base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc index 366056cff4b..908ea37b7b2 100644 --- a/base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc +++ b/base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc @@ -32,6 +32,7 @@ #include "cuttlefish/common/libs/utils/environment.h" #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/common/libs/utils/json.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/posix/readlink.h" #include "cuttlefish/posix/symlink.h" #include "cuttlefish/result/result.h" diff --git a/base/cvd/cuttlefish/host/libs/config/BUILD.bazel b/base/cvd/cuttlefish/host/libs/config/BUILD.bazel index 219dff81f20..4c7068bf022 100644 --- a/base/cvd/cuttlefish/host/libs/config/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/config/BUILD.bazel @@ -155,6 +155,7 @@ cf_cc_library( "//cuttlefish/common/libs/fs", "//cuttlefish/common/libs/utils:files", "//cuttlefish/common/libs/utils:host_info", + "//cuttlefish/files:copy", "//cuttlefish/host/libs/config:ap_boot_flow", "//cuttlefish/host/libs/config:boot_flow", "//cuttlefish/host/libs/config:config_utils", diff --git a/base/cvd/cuttlefish/host/libs/config/data_image.cpp b/base/cvd/cuttlefish/host/libs/config/data_image.cpp index 5df5e516770..cc6af158709 100644 --- a/base/cvd/cuttlefish/host/libs/config/data_image.cpp +++ b/base/cvd/cuttlefish/host/libs/config/data_image.cpp @@ -30,6 +30,7 @@ #include "cuttlefish/common/libs/fs/shared_fd.h" #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/common/libs/utils/host_info.h" +#include "cuttlefish/files/copy.h" #include "cuttlefish/host/libs/config/ap_boot_flow.h" #include "cuttlefish/host/libs/config/boot_flow.h" #include "cuttlefish/host/libs/config/config_utils.h"