1 /*
2 * Copyright (C) 2023 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
17 #include "host/commands/snapshot_util_cvd/snapshot_taker.h"
18
19 #include <cstdlib>
20 #include <string>
21 #include <string_view>
22 #include <unordered_map>
23
24 #include <android-base/file.h>
25 #include <android-base/strings.h>
26
27 #include "common/libs/fs/shared_buf.h"
28 #include "common/libs/fs/shared_fd.h"
29 #include "common/libs/utils/environment.h"
30 #include "common/libs/utils/files.h"
31 #include "common/libs/utils/json.h"
32 #include "common/libs/utils/result.h"
33 #include "common/libs/utils/users.h"
34 #include "host/libs/command_util/snapshot_utils.h"
35 #include "host/libs/config/cuttlefish_config.h"
36
37 namespace cuttlefish {
38
39 /**
40 * cp -r <cuttlefish home dir> <snapshot_path>
41 * write meta info for cvd: i.e. HOME, group name, instance names
42 * returns the path of generated snapshot json file
43 */
HandleHostGroupSnapshot(const std::string & path)44 Result<std::string> HandleHostGroupSnapshot(const std::string& path) {
45 const auto cuttlefish_home = StringFromEnv("HOME", "");
46 CF_EXPECT(!cuttlefish_home.empty(),
47 "\"HOME\" environment variable must be set.");
48
49 const std::string snapshot_path = CF_EXPECT(EmulateAbsolutePath(InputPathForm{
50 .current_working_dir = CurrentDirectory(),
51 .home_dir = CF_EXPECT(SystemWideUserHome()),
52 .path_to_convert = path,
53 .follow_symlink = false,
54 }));
55
56 auto* cuttlefish_config = CuttlefishConfig::Get();
57 CF_EXPECT(cuttlefish_config != nullptr, "Cannot find cuttlefish_config.json");
58
59 const auto cuttlefish_root = cuttlefish_config->root_dir();
60 CF_EXPECTF(android::base::StartsWith(cuttlefish_root, cuttlefish_home),
61 "Cuttlefish Root directory \"{}\" "
62 "is not subdirectory of cuttlefish home \"{}\".",
63 cuttlefish_root, cuttlefish_home);
64
65 // cp -r HOME snapshot_path
66 auto always_true = [](const std::string&) -> bool { return true; };
67 CF_EXPECTF(CopyDirectoryRecursively(cuttlefish_root, snapshot_path,
68 /* verify_dest_dir_empty */ true,
69 /* predicate */ std::move(always_true)),
70 "\"cp -r {} {} failed.\"", cuttlefish_root, snapshot_path);
71
72 const auto meta_json =
73 CF_EXPECTF(CreateMetaInfo(*cuttlefish_config, snapshot_path),
74 "Failed to create ", kMetaInfoJsonFileName);
75 const auto serialized_meta_json = meta_json.toStyledString();
76 LOG(DEBUG) << "Generated " << kMetaInfoJsonFileName << ": " << std::endl
77 << std::endl
78 << serialized_meta_json;
79
80 // write meta_json to file
81 const auto meta_json_path = SnapshotMetaJsonPath(snapshot_path);
82 CF_EXPECTF(
83 android::base::WriteStringToFile(serialized_meta_json, meta_json_path,
84 /* follow_symlinks */ true),
85 "Failed to write the meta information in json to \"{}\"", meta_json_path);
86
87 // mkdir guest_snapshot under the snapshot directory
88 CF_EXPECT(meta_json.isMember(kGuestSnapshotField));
89 auto meta_json_guest_snapshot = meta_json[kGuestSnapshotField];
90 for (const auto& instance : cuttlefish_config->Instances()) {
91 CF_EXPECT(meta_json_guest_snapshot.isMember(instance.id()));
92 const std::string new_dir_path =
93 snapshot_path + "/" +
94 meta_json_guest_snapshot[instance.id()].asString();
95 CF_EXPECTF(EnsureDirectoryExists(new_dir_path),
96 "Failed to create instance guest snapshot directory {}",
97 new_dir_path);
98 }
99 return meta_json_path;
100 }
101
102 } // namespace cuttlefish
103