2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
18 #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
23 #include "base/logging.h"
24 #include "base/macros.h"
25 #include "base/stringprintf.h"
26 #include "base/unix_file/fd_file.h"
36 // This file contains helper code for ImageSpace. It has most of the file-system
37 // related code, including handling A/B OTA.
41 // Delete the directory and its (regular or link) contents. If the recurse flag is true, delete
42 // sub-directories recursively.
43 static void DeleteDirectoryContents(const std::string& dir, bool recurse) {
44 if (!OS::DirectoryExists(dir.c_str())) {
47 DIR* c_dir = opendir(dir.c_str());
48 if (c_dir == nullptr) {
49 PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
53 for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
54 const char* name = de->d_name;
55 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
58 // We only want to delete regular files and symbolic links.
59 std::string file = StringPrintf("%s/%s", dir.c_str(), name);
60 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
61 if (de->d_type == DT_DIR) {
63 DeleteDirectoryContents(file, recurse);
64 // Try to rmdir the directory.
65 if (rmdir(file.c_str()) != 0) {
66 PLOG(ERROR) << "Unable to rmdir " << file;
70 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
73 // Try to unlink the file.
74 if (unlink(file.c_str()) != 0) {
75 PLOG(ERROR) << "Unable to unlink " << file;
79 CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
85 // We are relocating or generating the core image. We should get rid of everything. It is all
86 // out-of-date. We also don't really care if this fails since it is just a convenience.
87 // Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
88 // Note this should only be used during first boot.
89 static void PruneDalvikCache(InstructionSet isa) {
91 // Prune the base /data/dalvik-cache.
92 impl::DeleteDirectoryContents(GetDalvikCacheOrDie(".", false), false);
93 // Prune /data/dalvik-cache/<isa>.
94 impl::DeleteDirectoryContents(GetDalvikCacheOrDie(GetInstructionSetString(isa), false), false);
96 // Be defensive. There should be a runtime created here, but this may be called in a test.
97 if (Runtime::Current() != nullptr) {
98 Runtime::Current()->SetPrunedDalvikCache(true);
102 // We write out an empty file to the zygote's ISA specific cache dir at the start of
103 // every zygote boot and delete it when the boot completes. If we find a file already
104 // present, it usually means the boot didn't complete. We wipe the entire dalvik
105 // cache if that's the case.
106 static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
107 const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
108 const std::string boot_marker = isa_subdir + "/.booting";
109 const char* file_name = boot_marker.c_str();
111 uint32_t num_failed_boots = 0;
112 std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
113 if (file.get() == nullptr) {
114 file.reset(OS::CreateEmptyFile(file_name));
116 if (file.get() == nullptr) {
117 int saved_errno = errno;
118 PLOG(WARNING) << "Failed to create boot marker.";
119 if (saved_errno != ENOSPC) {
123 LOG(WARNING) << "Pruning dalvik cache because of low-memory situation.";
124 impl::DeleteDirectoryContents(isa_subdir, false);
127 file.reset(OS::OpenFileReadWrite(file_name));
128 if (file == nullptr) {
129 PLOG(WARNING) << "Failed to create boot marker.";
134 if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
135 PLOG(WARNING) << "Failed to read boot marker.";
141 if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
142 LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
143 impl::DeleteDirectoryContents(isa_subdir, false);
147 VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
149 if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
150 PLOG(WARNING) << "Failed to write boot marker.";
155 if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
156 PLOG(WARNING) << "Failed to write boot marker.";
161 if (file->FlushCloseOrErase() != 0) {
162 PLOG(WARNING) << "Failed to flush boot marker.";
170 #endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_