3939namespace cuttlefish {
4040namespace {
4141
42+ Result<void > Substitute (const std::string& target,
43+ const std::string& full_link_name) {
44+ if (!FileExists (target)) {
45+ LOG (WARNING) << " Target file " << target << " missing; not making "
46+ << " substitution " << target << " to " << full_link_name;
47+ return {};
48+ }
49+
50+ if (FileExists (full_link_name)) {
51+ CF_EXPECTF (unlink (full_link_name.c_str ()) == 0 , " {}" , StrError (errno));
52+ }
53+
54+ CF_EXPECT (Symlink (target, full_link_name));
55+ return {};
56+ }
57+
4258Result<void > SubstituteWithFlag (
4359 const std::string& target_dir,
4460 const std::vector<std::string>& host_substitutions) {
@@ -93,11 +109,7 @@ Result<void> SubstituteWithFlag(
93109 for (const std::string& substitution : host_substitutions) {
94110 std::string source = fmt::format (" {}/{}" , bin_dir_parent, substitution);
95111 std::string to_substitute = fmt::format (" {}/{}" , target_dir, substitution);
96- // TODO: schuffelen - relax this check after migration completes
97- CF_EXPECTF (FileExists (to_substitute),
98- " Cannot substitute '{}', does not exist" , to_substitute);
99- CF_EXPECTF (unlink (to_substitute.c_str ()) == 0 , " {}" , StrError (errno));
100- CF_EXPECT (Symlink (source, to_substitute));
112+ CF_EXPECT (Substitute (source, to_substitute));
101113 }
102114 }
103115
@@ -118,29 +130,6 @@ Result<std::string> GetCuttlefishCommonDir() {
118130 return cvd_exe.substr (0 , cvd_exe.size () - std::string (" /bin/cvd" ).size ());
119131}
120132
121- Result<void > Substitute (const std::string& target_dir,
122- const std::string& link_name) {
123- static const std::string common_dir = CF_EXPECT (GetCuttlefishCommonDir ());
124- std::string target = fmt::format (" {}/{}" , common_dir, link_name);
125- std::string full_link_name = fmt::format (" {}/{}" , target_dir, link_name);
126-
127- if (!FileExists (target)) {
128- LOG (WARNING) << " Target file " << target << " missing; not making "
129- << " substitution " << target << " to " << full_link_name;
130- return {};
131- }
132-
133- if (!FileExists (full_link_name)) {
134- LOG (WARNING) << " Link file " << full_link_name << " missing; not making "
135- << " substitution " << target << " to " << full_link_name;
136- return {};
137- }
138-
139- CF_EXPECTF (unlink (full_link_name.c_str ()) == 0 , " {}" , StrError (errno));
140- CF_EXPECT (Symlink (target, full_link_name));
141- return {};
142- }
143-
144133bool SubstituteCheckTargetExists (const fetch::HostPkgMigrationConfig& config,
145134 std::string_view target_keyword) {
146135 for (int j = 0 ; j < config.symlinks_size (); j++) {
@@ -165,6 +154,7 @@ Result<void> SubstituteWithMarker(const std::string& target_dir,
165154 " failed parsing debian_substitution_marker file" );
166155 auto run_cvd_substituted =
167156 SubstituteCheckTargetExists (config, kRunCvdKeyword );
157+ static const std::string common_dir = CF_EXPECT (GetCuttlefishCommonDir ());
168158 for (int j = 0 ; j < config.symlinks_size (); j++) {
169159 // TODO(b/452945156): The sensors simulator is launched by run_cvd, so these
170160 // two components must always be substituted together. Between May 2025 and
@@ -182,7 +172,11 @@ Result<void> SubstituteWithMarker(const std::string& target_dir,
182172 " substituted as well." ;
183173 continue ;
184174 }
185- CF_EXPECT (Substitute (target_dir, config.symlinks (j).link_name ()));
175+
176+ std::string link_name = config.symlinks (j).link_name ();
177+ std::string target = fmt::format (" {}/{}" , common_dir, link_name);
178+ std::string full_link_name = fmt::format (" {}/{}" , target_dir, link_name);
179+ CF_EXPECT (Substitute (target, full_link_name));
186180 }
187181 return {};
188182}
0 commit comments