Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/common/libs/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 1 addition & 56 deletions base/cvd/cuttlefish/common/libs/utils/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -297,62 +298,6 @@ Result<bool> 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 {};
Expand Down
1 change: 0 additions & 1 deletion base/cvd/cuttlefish/common/libs/utils/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Result<void> ChangeGroup(const std::string& path,
const std::string& group_name);
bool CanAccess(const std::string& path, int mode);
Result<bool> IsDirectoryEmpty(const std::string& path);
bool Copy(const std::string& from, const std::string& to);
off_t FileSize(const std::string& path);
Result<void> RemoveFile(const std::string& file);
Result<std::string> RenameFile(const std::string& current_filepath,
Expand Down
11 changes: 11 additions & 0 deletions base/cvd/cuttlefish/files/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
89 changes: 89 additions & 0 deletions base/cvd/cuttlefish/files/copy.cc
Original file line number Diff line number Diff line change
@@ -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 <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>

#include <string>

#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
25 changes: 25 additions & 0 deletions base/cvd/cuttlefish/files/copy.h
Original file line number Diff line number Diff line change
@@ -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 <string>

namespace cuttlefish {

bool Copy(const std::string& from, const std::string& to);

} // namespace cuttlefish
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/commands/cvd/fetch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/libs/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/libs/config/data_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading