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/libs/command_util/snapshot_utils.h"
18 
19 #include <unistd.h>
20 #include <utime.h>
21 
22 #include <cstdlib>
23 #include <cstring>
24 #include <string>
25 #include <unordered_map>
26 
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/strings.h>
30 
31 #include "common/libs/fs/shared_fd.h"
32 #include "common/libs/utils/files.h"
33 #include "common/libs/utils/json.h"
34 #include "common/libs/utils/result.h"
35 
36 namespace cuttlefish {
37 namespace {
38 
IsFifo(const struct stat & file_stat)39 bool IsFifo(const struct stat& file_stat) {
40   return S_ISFIFO(file_stat.st_mode);
41 }
42 
IsSocket(const struct stat & file_stat)43 bool IsSocket(const struct stat& file_stat) {
44   return S_ISSOCK(file_stat.st_mode);
45 }
46 
IsSymlink(const struct stat & file_stat)47 bool IsSymlink(const struct stat& file_stat) {
48   return S_ISLNK(file_stat.st_mode);
49 }
50 
IsRegular(const struct stat & file_stat)51 bool IsRegular(const struct stat& file_stat) {
52   return S_ISREG(file_stat.st_mode);
53 }
54 
55 // assumes that src_dir_path and dest_dir_path exist and both are
56 // existing directories or links to the directories. Also they are
57 // different directories.
CopyDirectoryImpl(const std::string & src_dir_path,const std::string & dest_dir_path,const std::function<bool (const std::string &)> & predicate)58 Result<void> CopyDirectoryImpl(
59     const std::string& src_dir_path, const std::string& dest_dir_path,
60     const std::function<bool(const std::string&)>& predicate) {
61   // create an empty dest_dir_path with the same permission as src_dir_path
62   // and then, recursively copy the contents
63   LOG(DEBUG) << "Making sure " << dest_dir_path
64              << " exists and is effectively a directory.";
65   CF_EXPECTF(EnsureDirectoryExists(dest_dir_path),
66              "Directory {} cannot to be created; it does not exist, either.",
67              dest_dir_path);
68   const auto src_contents = CF_EXPECT(DirectoryContents(src_dir_path));
69   for (const auto& src_base_path : src_contents) {
70     if (!predicate(src_dir_path + "/" + src_base_path)) {
71       continue;
72     }
73     std::string src_path = src_dir_path + "/" + src_base_path;
74     std::string dest_path = dest_dir_path + "/" + src_base_path;
75 
76     LOG(DEBUG) << "Handling... " << src_path;
77 
78     struct stat src_stat;
79     CF_EXPECTF(lstat(src_path.data(), &src_stat) != -1, "Failed in lstat({})",
80                src_path);
81     if (IsSymlink(src_stat)) {
82       std::string target;
83       CF_EXPECTF(android::base::Readlink(src_path, &target),
84                  "Readlink failed for {}", src_path);
85       LOG(DEBUG) << "Creating link from " << dest_path << " to " << target;
86       if (FileExists(dest_path, /* follow_symlink */ false)) {
87         CF_EXPECTF(RemoveFile(dest_path), "Failed to unlink/remove file \"{}\"",
88                    dest_path);
89       }
90       CF_EXPECTF(symlink(target.data(), dest_path.data()) == 0,
91                  "Creating symbolic link from {} to {} failed: {}", dest_path,
92                  target, strerror(errno));
93       continue;
94     }
95 
96     if (IsFifo(src_stat) || IsSocket(src_stat)) {
97       LOG(DEBUG) << "Ignoring a named pipe or socket " << src_path;
98       continue;
99     }
100 
101     if (DirectoryExists(src_path)) {
102       LOG(DEBUG) << "Recursively calling CopyDirectoryImpl(" << src_path << ", "
103                  << dest_path << ")";
104       CF_EXPECT(CopyDirectoryImpl(src_path, dest_path, predicate));
105       LOG(DEBUG) << "Returned from Recursive call CopyDirectoryImpl("
106                  << src_path << ", " << dest_path << ")";
107       continue;
108     }
109 
110     CF_EXPECTF(IsRegular(src_stat),
111                "File {} must be directory, link, socket, pipe or regular."
112                "{} is none of those",
113                src_path, src_path);
114 
115     CF_EXPECTF(Copy(src_path, dest_path), "Copy from {} to {} failed", src_path,
116                dest_path);
117 
118     auto dest_fd = SharedFD::Open(dest_path, O_RDONLY);
119     CF_EXPECT(dest_fd->IsOpen(), "Failed to open \"" << dest_path << "\"");
120     // Copy the mtime from the src file. The mtime of the disk image files can
121     // be important because we later validate that the disk overlays are not
122     // older than the disk components.
123     const struct timespec times[2] = {
124 #if defined(__APPLE__)
125       src_stat.st_atimespec,
126       src_stat.st_mtimespec
127 #else
128       src_stat.st_atim,
129       src_stat.st_mtim,
130 #endif
131     };
132     if (dest_fd->Futimens(times) != 0) {
133       return CF_ERR("futimens(\""
134                     << dest_path << "\", ...) failed: " << dest_fd->StrError());
135     }
136   }
137   return {};
138 }
139 
140 /*
141  * Returns Realpath(path) if successful, or the absolute path of "path"
142  *
143  * If emulating absolute path fails, "path" is returned as is.
144  */
RealpathOrSelf(const std::string & path)145 std::string RealpathOrSelf(const std::string& path) {
146   std::string output;
147   if (android::base::Realpath(path, &output)) {
148     return output;
149   }
150   struct InputPathForm input_form {
151     .path_to_convert = path, .follow_symlink = true,
152   };
153   auto absolute_path = EmulateAbsolutePath(input_form);
154   return absolute_path.ok() ? *absolute_path : path;
155 }
156 
157 }  // namespace
158 
CopyDirectoryRecursively(const std::string & src_dir_path,const std::string & dest_dir_path,const bool verify_dest_dir_empty,std::function<bool (const std::string &)> predicate)159 Result<void> CopyDirectoryRecursively(
160     const std::string& src_dir_path, const std::string& dest_dir_path,
161     const bool verify_dest_dir_empty,
162     std::function<bool(const std::string&)> predicate) {
163   CF_EXPECTF(FileExists(src_dir_path),
164              "A file/directory \"{}\" does not exist.", src_dir_path);
165   CF_EXPECTF(DirectoryExists(src_dir_path), "\"{}\" is not a directory.",
166              src_dir_path);
167   if (verify_dest_dir_empty) {
168     CF_EXPECTF(!FileExists(dest_dir_path, /* follow symlink */ false),
169                "Delete the destination directory \"{}\" first", dest_dir_path);
170   }
171 
172   std::string dest_final_target = RealpathOrSelf(dest_dir_path);
173   std::string src_final_target = RealpathOrSelf(src_dir_path);
174   if (dest_final_target == src_final_target) {
175     LOG(DEBUG) << "\"" << src_dir_path << "\" and \"" << dest_dir_path
176                << "\" are effectively the same.";
177     return {};
178   }
179 
180   LOG(INFO) << "Copy from \"" << src_final_target << "\" to \""
181             << dest_final_target << "\"";
182 
183   /**
184    * On taking snapshot, we should delete dest_dir first. On Restoring,
185    * we don't delete the runtime directory, eventually. We could, however,
186    * start with deleting it.
187    */
188   CF_EXPECT(CopyDirectoryImpl(src_final_target, dest_final_target, predicate));
189   return {};
190 }
191 
InstanceGuestSnapshotPath(const Json::Value & meta_json,const std::string & instance_id)192 Result<std::string> InstanceGuestSnapshotPath(const Json::Value& meta_json,
193                                               const std::string& instance_id) {
194   CF_EXPECTF(meta_json.isMember(kSnapshotPathField),
195              "The given json is missing : {}", kSnapshotPathField);
196   const std::string snapshot_path = meta_json[kSnapshotPathField].asString();
197 
198   const std::vector<std::string> guest_snapshot_path_selectors{
199       kGuestSnapshotField, instance_id};
200   const auto guest_snapshot_dir = CF_EXPECTF(
201       GetValue<std::string>(meta_json, guest_snapshot_path_selectors),
202       "root[\"{}\"][\"{}\"] is missing in \"{}\"", kGuestSnapshotField,
203       instance_id, kMetaInfoJsonFileName);
204   auto snapshot_path_direct_parent = snapshot_path + "/" + guest_snapshot_dir;
205   LOG(DEBUG) << "Returning snapshot path : " << snapshot_path_direct_parent;
206   return snapshot_path_direct_parent;
207 }
208 
CreateMetaInfo(const CuttlefishConfig & cuttlefish_config,const std::string & snapshot_path)209 Result<Json::Value> CreateMetaInfo(const CuttlefishConfig& cuttlefish_config,
210                                    const std::string& snapshot_path) {
211   Json::Value meta_info;
212   meta_info[kSnapshotPathField] = snapshot_path;
213 
214   const auto cuttlefish_home = StringFromEnv("HOME", "");
215   CF_EXPECT(!cuttlefish_home.empty(),
216             "\"HOME\" environment variable must be set.");
217   meta_info[kCfHomeField] = cuttlefish_home;
218 
219   const auto instances = cuttlefish_config.Instances();
220   // "id" -> relative path of instance_dir from cuttlefish_home
221   //         + kGuestSnapshotField
222   // e.g. "2" -> cuttlefish/instances/cvd-2/guest_snapshot
223   std::unordered_map<std::string, std::string>
224       id_to_relative_guest_snapshot_dir;
225   for (const auto& instance : instances) {
226     const std::string instance_snapshot_dir =
227         instance.instance_dir() + "/" + kGuestSnapshotField;
228     std::string_view sv_relative_path(instance_snapshot_dir);
229 
230     CF_EXPECTF(android::base::ConsumePrefix(&sv_relative_path, cuttlefish_home),
231                "Instance Guest Snapshot Directory \"{}\""
232                "is not a subdirectory of \"{}\"",
233                instance_snapshot_dir, cuttlefish_home);
234     if (!sv_relative_path.empty() && sv_relative_path.at(0) == '/') {
235       sv_relative_path.remove_prefix(1);
236     }
237     id_to_relative_guest_snapshot_dir[instance.id()] =
238         std::string(sv_relative_path);
239   }
240 
241   Json::Value snapshot_mapping;
242   // 2 -> cuttlefish/instances/cvd-2
243   // relative path to cuttlefish_home
244   for (const auto& [id_str, relative_guest_snapshot_dir] :
245        id_to_relative_guest_snapshot_dir) {
246     snapshot_mapping[id_str] = relative_guest_snapshot_dir;
247   }
248   meta_info[kGuestSnapshotField] = snapshot_mapping;
249   return meta_info;
250 }
251 
SnapshotMetaJsonPath(const std::string & snapshot_path)252 std::string SnapshotMetaJsonPath(const std::string& snapshot_path) {
253   return snapshot_path + "/" + kMetaInfoJsonFileName;
254 }
255 
LoadMetaJson(const std::string & snapshot_path)256 Result<Json::Value> LoadMetaJson(const std::string& snapshot_path) {
257   auto meta_json_path = SnapshotMetaJsonPath(snapshot_path);
258   auto meta_json = CF_EXPECT(LoadFromFile(meta_json_path));
259   return meta_json;
260 }
261 
GuestSnapshotDirectories(const std::string & snapshot_path)262 Result<std::vector<std::string>> GuestSnapshotDirectories(
263     const std::string& snapshot_path) {
264   auto meta_json = CF_EXPECT(LoadMetaJson(snapshot_path));
265   CF_EXPECT(meta_json.isMember(kGuestSnapshotField));
266   const auto& guest_snapshot_dir_jsons = meta_json[kGuestSnapshotField];
267   std::vector<std::string> id_strs = guest_snapshot_dir_jsons.getMemberNames();
268   std::vector<std::string> guest_snapshot_paths;
269   for (const auto& id_str : id_strs) {
270     CF_EXPECT(guest_snapshot_dir_jsons.isMember(id_str));
271     std::string path_suffix = guest_snapshot_dir_jsons[id_str].asString();
272     guest_snapshot_paths.push_back(snapshot_path + "/" + path_suffix);
273   }
274   return guest_snapshot_paths;
275 }
276 
277 }  // namespace cuttlefish
278