Skip to content

Commit 95f42be

Browse files
committed
Extract directory creation list from assemble_cvd.cc
Bug: b/472550593
1 parent 58b04fb commit 95f42be

File tree

4 files changed

+129
-43
lines changed

4 files changed

+129
-43
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cf_cc_binary(
3838
"//cuttlefish/host/commands/assemble_cvd:flags",
3939
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
4040
"//cuttlefish/host/commands/assemble_cvd:instance_image_files",
41+
"//cuttlefish/host/commands/assemble_cvd:required_directories",
4142
"//cuttlefish/host/commands/assemble_cvd:resolve_instance_files",
4243
"//cuttlefish/host/commands/assemble_cvd:touchpad",
4344
"//cuttlefish/host/commands/assemble_cvd/disk:ap_composite_disk",
@@ -464,6 +465,16 @@ cf_cc_library(
464465
],
465466
)
466467

468+
cf_cc_library(
469+
name = "required_directories",
470+
srcs = ["required_directories.cc"],
471+
hdrs = ["required_directories.h"],
472+
deps = [
473+
"//cuttlefish/host/libs/config:config_constants",
474+
"//cuttlefish/host/libs/config:cuttlefish_config",
475+
],
476+
)
477+
467478
cf_cc_library(
468479
name = "resolve_instance_files",
469480
srcs = ["resolve_instance_files.cc"],

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

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "cuttlefish/host/commands/assemble_cvd/flags/vm_manager.h"
5555
#include "cuttlefish/host/commands/assemble_cvd/flags_defaults.h"
5656
#include "cuttlefish/host/commands/assemble_cvd/instance_image_files.h"
57+
#include "cuttlefish/host/commands/assemble_cvd/required_directories.h"
5758
#include "cuttlefish/host/commands/assemble_cvd/resolve_instance_files.h"
5859
#include "cuttlefish/host/commands/assemble_cvd/touchpad.h"
5960
#include "cuttlefish/host/libs/command_util/snapshot_utils.h"
@@ -62,7 +63,6 @@
6263
#include "cuttlefish/host/libs/config/custom_actions.h"
6364
#include "cuttlefish/host/libs/config/defaults/defaults.h"
6465
#include "cuttlefish/host/libs/config/fastboot/fastboot.h"
65-
#include "cuttlefish/host/libs/config/fetcher_config.h"
6666
#include "cuttlefish/host/libs/config/fetcher_configs.h"
6767
#include "cuttlefish/host/libs/feature/inject.h"
6868
#include "cuttlefish/posix/symlink.h"
@@ -399,18 +399,13 @@ Result<const CuttlefishConfig*> InitFilesystemAndCreateConfig(
399399
CF_EXPECT(CleanPriorFiles(preserving, clean_dirs),
400400
"Failed to clean prior files");
401401

402-
auto default_group = "cvdnetwork";
402+
std::string default_group = "cvdnetwork";
403403
const mode_t default_mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
404404

405-
CF_EXPECT(EnsureDirectoryExists(config.root_dir()));
406-
CF_EXPECT(EnsureDirectoryExists(config.assembly_dir()));
407-
CF_EXPECT(EnsureDirectoryExists(config.instances_dir()));
408-
CF_EXPECT(EnsureDirectoryExists(config.instances_uds_dir(), default_mode,
409-
default_group));
410-
CF_EXPECT(EnsureDirectoryExists(config.environments_dir(), default_mode,
411-
default_group));
412-
CF_EXPECT(EnsureDirectoryExists(config.environments_uds_dir(), default_mode,
413-
default_group));
405+
for (const std::string& dir : RequiredDirectories(config)) {
406+
CF_EXPECT(EnsureDirectoryExists(dir, default_mode, default_group));
407+
}
408+
414409
if (!snapshot_path.empty()) {
415410
SharedFD temp = SharedFD::Creat(config.AssemblyPath("restore"), 0660);
416411
if (!temp->IsOpen()) {
@@ -420,41 +415,9 @@ Result<const CuttlefishConfig*> InitFilesystemAndCreateConfig(
420415

421416
auto environment =
422417
const_cast<const CuttlefishConfig&>(config).ForDefaultEnvironment();
423-
424-
CF_EXPECT(EnsureDirectoryExists(environment.environment_dir(), default_mode,
425-
default_group));
426-
CF_EXPECT(EnsureDirectoryExists(environment.environment_uds_dir(),
427-
default_mode, default_group));
428-
CF_EXPECT(EnsureDirectoryExists(environment.PerEnvironmentLogPath(""),
429-
default_mode, default_group));
430-
CF_EXPECT(
431-
EnsureDirectoryExists(environment.PerEnvironmentGrpcSocketPath(""),
432-
default_mode, default_group));
433-
434418
LOG(INFO) << "Path for instance UDS: " << config.instances_uds_dir();
435419

436-
if (log->LinkAtCwd(config.AssemblyPath("assemble_cvd.log"))) {
437-
LOG(ERROR) << "Unable to persist assemble_cvd log at "
438-
<< config.AssemblyPath("assemble_cvd.log")
439-
<< ": " << log->StrError();
440-
}
441420
for (const auto& instance : config.Instances()) {
442-
// Create instance directory if it doesn't exist.
443-
CF_EXPECT(EnsureDirectoryExists(instance.instance_dir()));
444-
auto internal_dir = instance.instance_dir() + "/" + kInternalDirName;
445-
CF_EXPECT(EnsureDirectoryExists(internal_dir));
446-
auto shared_dir = instance.instance_dir() + "/" + kSharedDirName;
447-
CF_EXPECT(EnsureDirectoryExists(shared_dir));
448-
auto recording_dir = instance.instance_dir() + "/recording";
449-
CF_EXPECT(EnsureDirectoryExists(recording_dir));
450-
CF_EXPECT(EnsureDirectoryExists(instance.PerInstanceLogPath("")));
451-
452-
CF_EXPECT(EnsureDirectoryExists(instance.instance_uds_dir(), default_mode,
453-
default_group));
454-
CF_EXPECT(EnsureDirectoryExists(instance.instance_internal_uds_dir(),
455-
default_mode, default_group));
456-
CF_EXPECT(EnsureDirectoryExists(instance.PerInstanceGrpcSocketPath(""),
457-
default_mode, default_group));
458421
std::string vsock_dir = fmt::format("{}/vsock_{}_{}", TempDir(),
459422
instance.vsock_guest_cid(), getuid());
460423
if (DirectoryExists(vsock_dir, /* follow_symlinks */ false) &&
@@ -466,6 +429,13 @@ Result<const CuttlefishConfig*> InitFilesystemAndCreateConfig(
466429
// TODO(schuffelen): Move this code somewhere better
467430
CF_EXPECT(CreateLegacySymlinks(instance, environment));
468431
}
432+
433+
if (log->LinkAtCwd(config.AssemblyPath("assemble_cvd.log"))) {
434+
LOG(ERROR) << "Unable to persist assemble_cvd log at "
435+
<< config.AssemblyPath("assemble_cvd.log") << ": "
436+
<< log->StrError();
437+
}
438+
469439
CF_EXPECT(SaveConfig(config), "Failed to initialize configuration");
470440
}
471441

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Copyright (C) 2025 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/host/commands/assemble_cvd/required_directories.h"
17+
18+
#include <unistd.h>
19+
#include <string>
20+
#include <vector>
21+
22+
#include "cuttlefish/host/libs/config/config_constants.h"
23+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
24+
25+
namespace cuttlefish {
26+
namespace {
27+
28+
std::vector<std::string> RequiredDirectories(
29+
const CuttlefishConfig::EnvironmentSpecific& environment) {
30+
return {
31+
environment.environment_dir(), //
32+
environment.environment_uds_dir(), //
33+
environment.PerEnvironmentLogPath(""), //
34+
environment.PerEnvironmentGrpcSocketPath(""), //
35+
};
36+
}
37+
38+
std::vector<std::string> RequiredDirectories(
39+
const CuttlefishConfig::InstanceSpecific& instance) {
40+
return {
41+
instance.instance_dir(), //
42+
absl::StrCat(instance.instance_dir(), "/", kInternalDirName), //
43+
absl::StrCat(instance.instance_dir(), "/", kSharedDirName), //
44+
absl::StrCat(instance.instance_dir(), "/recording"), //
45+
instance.PerInstanceLogPath(""), //
46+
instance.instance_uds_dir(), //
47+
instance.instance_internal_uds_dir(), //
48+
instance.PerInstanceGrpcSocketPath(""), //
49+
};
50+
}
51+
52+
} // namespace
53+
54+
std::vector<std::string> RequiredDirectories(const CuttlefishConfig& config) {
55+
std::vector<std::string> required = {
56+
config.root_dir(), //
57+
config.assembly_dir(), //
58+
config.instances_dir(), //
59+
config.instances_uds_dir(), //
60+
config.environments_dir(), //
61+
config.environments_uds_dir(), //
62+
};
63+
64+
for (std::string& dir : RequiredDirectories(config.ForDefaultEnvironment())) {
65+
required.emplace_back(std::move(dir));
66+
}
67+
68+
for (const CuttlefishConfig::InstanceSpecific& instance :
69+
config.Instances()) {
70+
for (std::string& dir : RequiredDirectories(instance)) {
71+
required.emplace_back(std::move(dir));
72+
}
73+
}
74+
75+
return required;
76+
}
77+
78+
} // namespace cuttlefish
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright (C) 2025 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 <vector>
20+
21+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
22+
23+
namespace cuttlefish {
24+
25+
std::vector<std::string> RequiredDirectories(const CuttlefishConfig&);
26+
27+
} // namespace cuttlefish

0 commit comments

Comments
 (0)