From ccec5044160d650aa40990000aeef22a6d6cd3b7 Mon Sep 17 00:00:00 2001 From: "A. Cody Schuffelen" Date: Wed, 29 Jul 2026 18:24:49 -0700 Subject: [PATCH] Extract RealPath from utils:files Bug: b/542760101 --- .../cuttlefish/common/libs/utils/BUILD.bazel | 1 + .../cuttlefish/common/libs/utils/files.cpp | 14 +----- base/cvd/cuttlefish/common/libs/utils/files.h | 2 - .../host/commands/cvd/cli/BUILD.bazel | 1 + .../host/commands/cvd/cli/commands/create.cpp | 1 + .../host/commands/cvd/instances/BUILD.bazel | 1 + .../commands/cvd/instances/config_path.cpp | 3 ++ .../host/libs/command_util/BUILD.bazel | 1 + .../host/libs/command_util/snapshot_utils.cc | 1 + .../cuttlefish/host/libs/config/BUILD.bazel | 4 +- .../host/libs/config/fetcher_configs.cc | 2 +- .../host/libs/image_aggregator/BUILD.bazel | 2 +- .../libs/image_aggregator/sparse_image.cc | 2 +- base/cvd/cuttlefish/posix/BUILD.bazel | 11 +++++ base/cvd/cuttlefish/posix/realpath.cc | 44 +++++++++++++++++++ base/cvd/cuttlefish/posix/realpath.h | 26 +++++++++++ 16 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 base/cvd/cuttlefish/posix/realpath.cc create mode 100644 base/cvd/cuttlefish/posix/realpath.h diff --git a/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel b/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel index 9a206b0a62d..6cd6074620b 100644 --- a/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel +++ b/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel @@ -120,6 +120,7 @@ cf_cc_library( "//cuttlefish/files:file_device_id", "//cuttlefish/files:file_exists", "//cuttlefish/files:link_or_copy", + "//cuttlefish/posix:realpath", "//cuttlefish/posix:rename", "//cuttlefish/posix:strerror", "//cuttlefish/result", diff --git a/base/cvd/cuttlefish/common/libs/utils/files.cpp b/base/cvd/cuttlefish/common/libs/utils/files.cpp index 120ab093198..203f813ba16 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files.cpp +++ b/base/cvd/cuttlefish/common/libs/utils/files.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -59,6 +58,7 @@ #include "cuttlefish/files/file_device_id.h" #include "cuttlefish/files/file_exists.h" #include "cuttlefish/files/link_or_copy.h" +#include "cuttlefish/posix/realpath.h" #include "cuttlefish/posix/rename.h" #include "cuttlefish/posix/strerror.h" #include "cuttlefish/result/result.h" @@ -222,18 +222,6 @@ std::string AbsolutePath(std::string_view path) { return absl::StrCat(*real_cwd, "/", path); } -Result RealPath(const std::string& path) { - // NOLINTNEXTLINE(misc-include-cleaner): - std::array buffer{}; - char* res; - do { - res = realpath(path.c_str(), buffer.data()); - } while (res == nullptr && errno == EINTR); - CF_EXPECTF(res != nullptr, "Could not get real path for path \"{}\": {}", - path, StrError(errno)); - return std::string(buffer.data()); -} - off_t FileSize(const std::string& path) { struct stat st{}; if (stat(path.c_str(), &st) == -1) { diff --git a/base/cvd/cuttlefish/common/libs/utils/files.h b/base/cvd/cuttlefish/common/libs/utils/files.h index d80b38d123d..e2ed23cff55 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files.h +++ b/base/cvd/cuttlefish/common/libs/utils/files.h @@ -71,8 +71,6 @@ Result FileOwner(const std::string& path); // argument. std::string AbsolutePath(std::string_view path); -Result RealPath(const std::string& path); - std::string CurrentDirectory(); struct FileSizes { diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel b/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel index 6770358752f..76a9988d12b 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/BUILD.bazel @@ -170,6 +170,7 @@ cf_cc_library( "//cuttlefish/host/commands/cvd/instances:instance_manager", "//cuttlefish/host/commands/cvd/instances/lock", "//cuttlefish/host/commands/cvd/utils", + "//cuttlefish/posix:realpath", "//cuttlefish/posix:strerror", "//cuttlefish/posix:symlink", "//cuttlefish/result", 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 b72e793d975..8db76815af6 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/create.cpp @@ -65,6 +65,7 @@ #include "cuttlefish/host/commands/cvd/instances/instance_manager.h" #include "cuttlefish/host/commands/cvd/instances/local_instance_group.h" #include "cuttlefish/host/commands/cvd/utils/common.h" +#include "cuttlefish/posix/realpath.h" #include "cuttlefish/posix/strerror.h" #include "cuttlefish/posix/symlink.h" #include "cuttlefish/result/result.h" diff --git a/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel b/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel index b8f120ad1e0..badab95e0c3 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel @@ -15,6 +15,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:files", "//cuttlefish/files:directory_exists", "//cuttlefish/files:file_exists", + "//cuttlefish/posix:realpath", "//cuttlefish/result", ], ) diff --git a/base/cvd/cuttlefish/host/commands/cvd/instances/config_path.cpp b/base/cvd/cuttlefish/host/commands/cvd/instances/config_path.cpp index 7e50cb5c2ec..a303840076e 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/instances/config_path.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/instances/config_path.cpp @@ -16,9 +16,12 @@ #include "cuttlefish/host/commands/cvd/instances/config_path.h" +#include + #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/files/directory_exists.h" #include "cuttlefish/files/file_exists.h" +#include "cuttlefish/posix/realpath.h" namespace cuttlefish { diff --git a/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel b/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel index 0e4da0571a3..3d8ef70ac22 100644 --- a/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/command_util/BUILD.bazel @@ -42,6 +42,7 @@ cf_cc_library( "//cuttlefish/host/libs/config:config_instance_derived", "//cuttlefish/host/libs/config:cuttlefish_config", "//cuttlefish/posix:readlink", + "//cuttlefish/posix:realpath", "//cuttlefish/posix:strerror", "//cuttlefish/posix:symlink", "//cuttlefish/result", 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 f8974b03dc3..de9f7f4c4e3 100644 --- a/base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc +++ b/base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc @@ -36,6 +36,7 @@ #include "cuttlefish/files/directory_exists.h" #include "cuttlefish/files/file_exists.h" #include "cuttlefish/posix/readlink.h" +#include "cuttlefish/posix/realpath.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 767656889aa..c2004bb71b6 100644 --- a/base/cvd/cuttlefish/host/libs/config/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/config/BUILD.bazel @@ -279,13 +279,11 @@ cf_cc_library( name = "fetcher_configs", srcs = ["fetcher_configs.cc"], hdrs = ["fetcher_configs.h"], - depend_on_what_you_use_enabled = False, deps = [ - "//cuttlefish/common/libs/utils:files", "//cuttlefish/files:file_exists", "//cuttlefish/host/libs/config:fetcher_config", + "//cuttlefish/posix:realpath", "//cuttlefish/result", - "//libbase", "@abseil-cpp//absl/log", "@abseil-cpp//absl/strings", "@abseil-cpp//absl/types:span", diff --git a/base/cvd/cuttlefish/host/libs/config/fetcher_configs.cc b/base/cvd/cuttlefish/host/libs/config/fetcher_configs.cc index 99a556dc1a3..6dcedba1395 100644 --- a/base/cvd/cuttlefish/host/libs/config/fetcher_configs.cc +++ b/base/cvd/cuttlefish/host/libs/config/fetcher_configs.cc @@ -25,9 +25,9 @@ #include "absl/strings/str_cat.h" #include "absl/types/span.h" -#include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/files/file_exists.h" #include "cuttlefish/host/libs/config/fetcher_config.h" +#include "cuttlefish/posix/realpath.h" #include "cuttlefish/result/result.h" namespace cuttlefish { diff --git a/base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel b/base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel index 22dd5ac5bc0..e35d2d11241 100644 --- a/base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel @@ -127,9 +127,9 @@ cf_cc_library( hdrs = ["sparse_image.h"], deps = [ "//cuttlefish/common/libs/fs", - "//cuttlefish/common/libs/utils:files", "//cuttlefish/host/libs/config:known_paths", "//cuttlefish/host/libs/image_aggregator:disk_image", + "//cuttlefish/posix:realpath", "//cuttlefish/posix:rename", "//cuttlefish/process:execute", "//cuttlefish/result", diff --git a/base/cvd/cuttlefish/host/libs/image_aggregator/sparse_image.cc b/base/cvd/cuttlefish/host/libs/image_aggregator/sparse_image.cc index d9220a01e09..a57bd942465 100644 --- a/base/cvd/cuttlefish/host/libs/image_aggregator/sparse_image.cc +++ b/base/cvd/cuttlefish/host/libs/image_aggregator/sparse_image.cc @@ -31,8 +31,8 @@ #include "sparse/sparse.h" #include "cuttlefish/common/libs/fs/shared_fd.h" -#include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/host/libs/config/known_paths.h" +#include "cuttlefish/posix/realpath.h" #include "cuttlefish/posix/rename.h" #include "cuttlefish/process/execute.h" #include "cuttlefish/result/result.h" diff --git a/base/cvd/cuttlefish/posix/BUILD.bazel b/base/cvd/cuttlefish/posix/BUILD.bazel index 12f6fa6cdcf..88425388e87 100644 --- a/base/cvd/cuttlefish/posix/BUILD.bazel +++ b/base/cvd/cuttlefish/posix/BUILD.bazel @@ -42,3 +42,14 @@ cf_cc_library( "//cuttlefish/result", ], ) + +cf_cc_library( + name = "realpath", + srcs = ["realpath.cc"], + hdrs = ["realpath.h"], + deps = [ + "//cuttlefish/posix:strerror", + "//cuttlefish/result:expect", + "//cuttlefish/result:result_type", + ], +) diff --git a/base/cvd/cuttlefish/posix/realpath.cc b/base/cvd/cuttlefish/posix/realpath.cc new file mode 100644 index 00000000000..c91c8886570 --- /dev/null +++ b/base/cvd/cuttlefish/posix/realpath.cc @@ -0,0 +1,44 @@ +/* + * 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/posix/realpath.h" + +#include +#include +#include +#include + +#include +#include + +#include "cuttlefish/posix/strerror.h" +#include "cuttlefish/result/expect.h" +#include "cuttlefish/result/result_type.h" + +namespace cuttlefish { + +Result RealPath(const std::string& path) { + std::array buffer{}; + char* res; + do { + res = realpath(path.c_str(), buffer.data()); + } while (res == nullptr && errno == EINTR); + CF_EXPECTF(res != nullptr, "Could not get real path for path \"{}\": {}", + path, StrError(errno)); + return std::string(buffer.data()); +} + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/posix/realpath.h b/base/cvd/cuttlefish/posix/realpath.h new file mode 100644 index 00000000000..87a47f728db --- /dev/null +++ b/base/cvd/cuttlefish/posix/realpath.h @@ -0,0 +1,26 @@ +/* + * 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 + +#include "cuttlefish/result/result_type.h" + +namespace cuttlefish { + +Result RealPath(const std::string& path); + +} // namespace cuttlefish