Skip to content

Commit 0b80b02

Browse files
committed
Move ReadLink from files.cpp to cuttlefish/posix
Assisted-by: Jetski:Gemini Next
1 parent 0fb33ee commit 0b80b02

9 files changed

Lines changed: 96 additions & 17 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
@@ -258,6 +258,7 @@ cf_cc_library(
258258
deps = [
259259
"//cuttlefish/common/libs/fs",
260260
"//cuttlefish/common/libs/utils:files",
261+
"//cuttlefish/posix:readlink",
261262
"//cuttlefish/result",
262263
"//libbase",
263264
"@abseil-cpp//absl/log",

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,6 @@ Result<std::string> ReadFileContents(const std::string& filepath) {
509509
file->StrError());
510510
return file_content;
511511
}
512-
513-
Result<std::string> ReadLink(const std::string& path) {
514-
std::vector<char> buf(4096);
515-
while (true) {
516-
ssize_t size = readlink(path.c_str(), buf.data(), buf.size());
517-
CF_EXPECTF(size != -1, "readlink(\"{}\") failed: {}", path,
518-
StrError(errno));
519-
if (static_cast<size_t>(size) < buf.size()) {
520-
return std::string(buf.data(), size);
521-
}
522-
buf.resize(buf.size() * 2);
523-
}
524-
}
525-
526512
Result<void> WriteNewFile(const std::string& filepath, std::string_view content,
527513
mode_t mode) {
528514
CF_EXPECTF(!FileExists(filepath), "File already exists: {}", filepath);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Result<std::string> RenameFile(const std::string& current_filepath,
7272
const std::string& target_filepath);
7373
std::string ReadFile(const std::string& file);
7474
Result<std::string> ReadFileContents(const std::string& filepath);
75-
Result<std::string> ReadLink(const std::string& path);
7675
Result<void> WriteNewFile(const std::string& filepath, std::string_view content,
7776
mode_t mode = S_IRWXU | S_IRGRP | S_IROTH);
7877
bool MakeFileExecutable(const std::string& path);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "cuttlefish/common/libs/fs/shared_buf.h"
3838
#include "cuttlefish/common/libs/fs/shared_fd.h"
3939
#include "cuttlefish/common/libs/utils/files.h"
40+
#include "cuttlefish/posix/readlink.h"
4041
#include "cuttlefish/result/result.h"
4142

4243
namespace cuttlefish {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ cf_cc_library(
3737
"//cuttlefish/common/libs/utils:json",
3838
"//cuttlefish/host/libs/config:config_instance_derived",
3939
"//cuttlefish/host/libs/config:cuttlefish_config",
40+
"//cuttlefish/posix:readlink",
4041
"//cuttlefish/posix:strerror",
4142
"//cuttlefish/posix:symlink",
4243
"//cuttlefish/result",

base/cvd/cuttlefish/host/libs/command_util/snapshot_utils.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
#include <string>
2525
#include <unordered_map>
2626

27-
#include <android-base/file.h>
28-
#include "absl/strings/strip.h"
2927
#include "absl/log/log.h"
28+
#include "absl/strings/strip.h"
29+
#include "android-base/file.h"
3030

3131
#include "cuttlefish/common/libs/fs/shared_fd.h"
3232
#include "cuttlefish/common/libs/utils/environment.h"
3333
#include "cuttlefish/common/libs/utils/files.h"
3434
#include "cuttlefish/common/libs/utils/json.h"
35+
#include "cuttlefish/posix/readlink.h"
3536
#include "cuttlefish/posix/symlink.h"
3637
#include "cuttlefish/result/result.h"
3738

base/cvd/cuttlefish/posix/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ cf_cc_library(
3232
"//cuttlefish/result",
3333
],
3434
)
35+
36+
cf_cc_library(
37+
name = "readlink",
38+
srcs = ["readlink.cc"],
39+
hdrs = ["readlink.h"],
40+
deps = [
41+
"//cuttlefish/posix:strerror",
42+
"//cuttlefish/result",
43+
],
44+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2026 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "cuttlefish/posix/readlink.h"
17+
18+
#include <errno.h>
19+
#include <unistd.h>
20+
21+
#include <string>
22+
#include <string_view>
23+
#include <vector>
24+
25+
#include "cuttlefish/posix/strerror.h"
26+
#include "cuttlefish/result/result.h"
27+
28+
namespace cuttlefish {
29+
30+
Result<std::string> ReadLink(const char* path) {
31+
std::vector<char> buf(4096);
32+
while (true) {
33+
ssize_t size = readlink(path, buf.data(), buf.size());
34+
CF_EXPECTF(size != -1, "readlink('{}') failed: {}", path, StrError(errno));
35+
if (static_cast<size_t>(size) < buf.size()) {
36+
return std::string(buf.data(), size);
37+
}
38+
buf.resize(buf.size() * 2);
39+
}
40+
}
41+
42+
Result<std::string> ReadLink(const std::string& path) {
43+
return ReadLink(path.c_str());
44+
}
45+
46+
Result<std::string> ReadLink(std::string_view path) {
47+
return ReadLink(std::string(path));
48+
}
49+
50+
} // namespace cuttlefish
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2026 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <string>
19+
#include <string_view>
20+
21+
#include "cuttlefish/result/result.h"
22+
23+
namespace cuttlefish {
24+
25+
// Wrapper for readlink(2).
26+
Result<std::string> ReadLink(const char* path);
27+
Result<std::string> ReadLink(const std::string& path);
28+
Result<std::string> ReadLink(std::string_view path);
29+
30+
} // namespace cuttlefish

0 commit comments

Comments
 (0)