1 /*
2 * Copyright (C) 2016 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 #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
18 #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
19
20 #include <dirent.h>
21 #include <dlfcn.h>
22
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25
26 #include "base/file_utils.h"
27 #include "base/logging.h" // For VLOG.
28 #include "base/macros.h"
29 #include "base/os.h"
30 #include "base/unix_file/fd_file.h"
31 #include "base/utils.h"
32 #include "runtime.h"
33 #include "runtime_globals.h"
34
35 namespace art {
36 namespace gc {
37 namespace space {
38
39 // This file contains helper code for ImageSpace. It has most of the file-system
40 // related code, including handling A/B OTA.
41
42 namespace impl {
43
44 // Delete the directory and its (regular or link) contents. If the recurse flag is true, delete
45 // sub-directories recursively.
DeleteDirectoryContents(const std::string & dir,bool recurse)46 static void DeleteDirectoryContents(const std::string& dir, bool recurse) {
47 if (!OS::DirectoryExists(dir.c_str())) {
48 return;
49 }
50 DIR* c_dir = opendir(dir.c_str());
51 if (c_dir == nullptr) {
52 PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
53 return;
54 }
55
56 for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
57 const char* name = de->d_name;
58 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
59 continue;
60 }
61 // We only want to delete regular files and symbolic links.
62 std::string file = android::base::StringPrintf("%s/%s", dir.c_str(), name);
63 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
64 if (de->d_type == DT_DIR) {
65 if (recurse) {
66 DeleteDirectoryContents(file, recurse);
67 // Try to rmdir the directory.
68 if (rmdir(file.c_str()) != 0) {
69 PLOG(ERROR) << "Unable to rmdir " << file;
70 }
71 }
72 } else {
73 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
74 }
75 } else {
76 // Try to unlink the file.
77 if (unlink(file.c_str()) != 0) {
78 PLOG(ERROR) << "Unable to unlink " << file;
79 }
80 }
81 }
82 CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
83 }
84
85 } // namespace impl
86
87
88 // We are relocating or generating the core image. We should get rid of everything. It is all
89 // out-of-date. We also don't really care if this fails since it is just a convenience.
90 // Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
91 // Note this should only be used during first boot.
PruneDalvikCache(InstructionSet isa)92 static void PruneDalvikCache(InstructionSet isa) {
93 CHECK_NE(isa, InstructionSet::kNone);
94 // Prune the base /data/dalvik-cache.
95 // Note: GetDalvikCache may return the empty string if the directory doesn't
96 // exist. It is safe to pass "" to DeleteDirectoryContents, so this is okay.
97 impl::DeleteDirectoryContents(GetDalvikCache("."), false);
98 // Prune /data/dalvik-cache/<isa>.
99 impl::DeleteDirectoryContents(GetDalvikCache(GetInstructionSetString(isa)), false);
100
101 // Be defensive. There should be a runtime created here, but this may be called in a test.
102 if (Runtime::Current() != nullptr) {
103 Runtime::Current()->SetPrunedDalvikCache(true);
104 }
105 }
106
107 } // namespace space
108 } // namespace gc
109 } // namespace art
110
111 #endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
112