Skip to content

Commit 8a676d2

Browse files
committed
Reduce usage of c_str() in assemble_cvd
Either removes it entirely when the callee doesn't need a `const char*`, or moves the `c_str()` call closer to the libc function that requires it. Bug: b/470444522
1 parent 5761173 commit 8a676d2

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Result<void> CreateLegacySymlinks(
104104
"crosvm_openwrt.log",
105105
"crosvm_openwrt_boot.log"};
106106
for (const auto& log_file : log_files) {
107-
auto symlink_location = instance.PerInstancePath(log_file.c_str());
107+
auto symlink_location = instance.PerInstancePath(log_file);
108108
auto log_target = "logs/" + log_file; // Relative path
109109
if (FileExists(symlink_location, /* follow_symlinks */ false)) {
110110
CF_EXPECT(RemoveFile(symlink_location),

base/cvd/cuttlefish/host/commands/assemble_cvd/boot_config.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ size_t WriteEnvironment(const CuttlefishConfig::InstanceSpecific& instance,
120120
}
121121

122122
std::string env_str = env.str();
123-
std::ofstream file_out(env_path.c_str(), std::ios::binary);
123+
std::ofstream file_out(env_path, std::ios::binary);
124124
file_out << env_str;
125125

126126
if (!file_out.good()) {

base/cvd/cuttlefish/host/commands/assemble_cvd/clean.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Result<void> CleanPriorFiles(const std::string& path,
6868
continue;
6969
}
7070
std::string entity_path = path + "/" + entity_name;
71-
CF_EXPECT(CleanPriorFiles(entity_path.c_str(), preserving),
71+
CF_EXPECT(CleanPriorFiles(entity_path, preserving),
7272
"CleanPriorFiles for \""
7373
<< path << "\" failed on recursing into \"" << entity_path
7474
<< "\"");

base/cvd/cuttlefish/host/commands/assemble_cvd/disk_image_flags_vectorization.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Result<void> DiskImageFlagsVectorization(
217217
const std::string new_boot_image_path =
218218
const_instance.PerInstancePath("boot_repacked.img");
219219
// change the new flag value to corresponding instance
220-
instance.set_new_boot_image(new_boot_image_path.c_str());
220+
instance.set_new_boot_image(new_boot_image_path);
221221
}
222222

223223
instance.set_data_image(system_image_dir.ForIndex(instance_index) +
@@ -233,7 +233,7 @@ Result<void> DiskImageFlagsVectorization(
233233
// Repack the vendor boot images if kernels and/or ramdisks are passed in.
234234
if (has_initramfs) {
235235
// change the new flag value to corresponding instance
236-
instance.set_new_vendor_boot_image(new_vendor_boot_image_path.c_str());
236+
instance.set_new_vendor_boot_image(new_vendor_boot_image_path);
237237
}
238238
}
239239

base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,14 @@ Result<std::vector<int>> GetFlagIntValueForInstances(
221221
if (instance_index < default_value_vec.size()) {
222222
default_value = default_value_vec[instance_index];
223223
}
224-
CF_EXPECT(android::base::ParseInt(default_value,
225-
&value_vec[instance_index]),
226-
"Failed to parse value \"" << default_value << "\" for " << flag_name);
224+
CF_EXPECTF(
225+
android::base::ParseInt(default_value, &value_vec[instance_index]),
226+
"Failed to parse value '{}' for '{}'", default_value, flag_name);
227227
} else {
228-
CF_EXPECT(android::base::ParseInt(flag_vec[instance_index].c_str(),
229-
&value_vec[instance_index]),
230-
"Failed to parse value \"" << flag_vec[instance_index] << "\" for " << flag_name);
228+
CF_EXPECTF(android::base::ParseInt(flag_vec[instance_index],
229+
&value_vec[instance_index]),
230+
"Failed to parse value '{}' for '{}'",
231+
flag_vec[instance_index], flag_name);
231232
}
232233
}
233234
}

base/cvd/cuttlefish/host/commands/assemble_cvd/flags/mcu_config_path.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Result<Json::Value> McuConfigPathFlag::JsonForIndex(
7777
CF_EXPECT(FileExists(mcu_cfg_path), "MCU config file does not exist");
7878

7979
std::string content;
80-
CF_EXPECTF(ReadFileToString(mcu_cfg_path.c_str(), &content,
80+
CF_EXPECTF(ReadFileToString(mcu_cfg_path, &content,
8181
/* follow_symlinks */ true),
8282
"Failed to read mcu config file '{}'", mcu_cfg_path);
8383

base/cvd/cuttlefish/host/commands/assemble_cvd/guest_config.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Result<void> ParseGuestConfigTxt(const std::string& guest_config_path,
209209
if (res_output_audio_streams_count.ok()) {
210210
std::string output_audio_streams_count_str =
211211
res_output_audio_streams_count.value();
212-
CF_EXPECT(android::base::ParseInt(output_audio_streams_count_str.c_str(),
212+
CF_EXPECT(android::base::ParseInt(output_audio_streams_count_str,
213213
&guest_config.output_audio_streams_count),
214214
"Failed to parse value \"" << output_audio_streams_count_str
215215
<< "\" for output audio stream count");
@@ -229,7 +229,7 @@ Result<void> ParseGuestConfigTxt(const std::string& guest_config_path,
229229
GetAndroidInfoConfig(guest_config_path, "blank_data_image_mb");
230230
if (res_blank_data_image_mb.ok()) {
231231
std::string res_blank_data_image_mb_str = res_blank_data_image_mb.value();
232-
CF_EXPECT(android::base::ParseInt(res_blank_data_image_mb_str.c_str(),
232+
CF_EXPECT(android::base::ParseInt(res_blank_data_image_mb_str,
233233
&guest_config.blank_data_image_mb),
234234
"Failed to parse value \"" << res_blank_data_image_mb_str
235235
<< "\" for blank data image size");

base/cvd/cuttlefish/host/commands/assemble_cvd/vendor_dlkm_utils.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ constexpr size_t RoundUp(size_t a, size_t divisor) {
5656
}
5757

5858
template <typename Container>
59-
bool WriteLinesToFile(const Container& lines, const char* path) {
59+
bool WriteLinesToFile(const Container& lines, const std::string& path) {
6060
android::base::unique_fd fd(
61-
open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640));
61+
open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640));
6262
if (!fd.ok()) {
6363
PLOG(ERROR) << "Failed to open " << path;
6464
return false;
@@ -78,10 +78,11 @@ bool WriteLinesToFile(const Container& lines, const char* path) {
7878
}
7979

8080
// Generate a filesystem_config.txt for all files in |fs_root|
81-
Result<bool> WriteFsConfig(const char* output_path, const std::string& fs_root,
81+
Result<bool> WriteFsConfig(const std::string& output_path,
82+
const std::string& fs_root,
8283
const std::string& mount_point) {
83-
android::base::unique_fd fd(
84-
open(output_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
84+
android::base::unique_fd fd(open(
85+
output_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
8586
if (!fd.ok()) {
8687
PLOG(ERROR) << "Failed to open " << output_path;
8788
return false;
@@ -347,15 +348,15 @@ Result<void> BuildDlkmImage(const std::string& src_dir, const bool is_erofs,
347348

348349
const std::string mount_point = "/" + partition_name;
349350
const std::string fs_config = output_image + ".fs_config";
350-
CF_EXPECT(WriteFsConfig(fs_config.c_str(), src_dir, mount_point));
351+
CF_EXPECT(WriteFsConfig(fs_config, src_dir, mount_point));
351352

352353
const std::string file_contexts_bin = output_image + ".file_contexts";
353354
if (partition_name == "system_dlkm") {
354-
CF_EXPECT(GenerateFileContexts(file_contexts_bin.c_str(), mount_point,
355+
CF_EXPECT(GenerateFileContexts(file_contexts_bin, mount_point,
355356
"system_dlkm_file"));
356357
} else {
357-
CF_EXPECT(GenerateFileContexts(file_contexts_bin.c_str(), mount_point,
358-
"vendor_file"));
358+
CF_EXPECT(
359+
GenerateFileContexts(file_contexts_bin, mount_point, "vendor_file"));
359360
}
360361

361362
// We are using directory size as an estimate of final image size. To avoid
@@ -513,19 +514,19 @@ Result<void> SplitRamdiskModules(const std::string& ramdisk_path,
513514
// Write updated modules.dep and modules.load files
514515
CF_EXPECT(WriteDepsToFile(FilterDependencies(deps, ramdisk_modules),
515516
module_base_dir + "/modules.dep"));
516-
CF_EXPECT(WriteLinesToFile(ramdisk_modules, module_load_file.c_str()));
517+
CF_EXPECT(WriteLinesToFile(ramdisk_modules, module_load_file));
517518

518519
CF_EXPECT(WriteDepsToFile(
519520
UpdateGKIModulePaths(FilterOutDependencies(deps, ramdisk_modules),
520521
system_dlkm_modules),
521522
vendor_modules_dir + "/modules.dep"));
522523
CF_EXPECT(WriteLinesToFile(vendor_dlkm_modules,
523-
(vendor_modules_dir + "/modules.load").c_str()));
524+
vendor_modules_dir + "/modules.load"));
524525

525526
CF_EXPECT(WriteDepsToFile(FilterDependencies(deps, system_dlkm_modules),
526527
system_modules_dir + "/modules.dep"));
527528
CF_EXPECT(WriteLinesToFile(system_dlkm_modules,
528-
(system_modules_dir + "/modules.load").c_str()));
529+
system_modules_dir + "/modules.load"));
529530
PackRamdisk(ramdisk_stage_dir, ramdisk_path);
530531
return {};
531532
}

0 commit comments

Comments
 (0)