1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "db/clean_up.h"
16 
17 #include <android-base/file.h>
18 
19 #include <cstdio>
20 #include <filesystem>
21 #include <fstream>
22 #include <iostream>
23 #include <limits>
24 #include <optional>
25 #include <string>
26 #include <vector>
27 
28 #include "db/file_models.h"
29 #include "db/models.h"
30 
31 namespace iorap::db {
32 
CleanUpFilesForActivity(const db::DbHandle & db,const db::VersionedComponentName & vcn)33 void CleanUpFilesForActivity(const db::DbHandle& db,
34                              const db::VersionedComponentName& vcn) {
35   LOG(DEBUG) << "Clean up files for activity " << vcn.GetActivity();
36   // Remove perfetto traces.
37   std::vector<db::RawTraceModel> raw_traces =
38       db::RawTraceModel::SelectByVersionedComponentName(db, vcn);
39   for (db::RawTraceModel raw_trace : raw_traces) {
40     std::string file_path = raw_trace.file_path;
41     LOG(DEBUG) << "Remove file: " << file_path;
42     std::filesystem::remove(file_path.c_str());
43     raw_trace.Delete();
44   }
45 
46   // Remove compiled traces.
47   std::optional<db::PrefetchFileModel> prefetch_file =
48       db::PrefetchFileModel::SelectByVersionedComponentName(db, vcn);
49 
50   if (prefetch_file) {
51     std::string file_path = prefetch_file->file_path;
52     LOG(DEBUG) << "Remove file: " << file_path;
53     std::filesystem::remove(file_path.c_str());
54     prefetch_file->Delete();
55   }
56 }
57 
CleanUpFilesForPackage(const db::DbHandle & db,int package_id,const std::string & package_name,int64_t version)58 void CleanUpFilesForPackage(const db::DbHandle& db,
59                             int package_id,
60                             const std::string& package_name,
61                             int64_t version) {
62   LOG(DEBUG) << "Clean up files for package " << package_name << " with version "
63              << version;
64   std::vector<db::ActivityModel> activities =
65       db::ActivityModel::SelectByPackageId(db, package_id);
66 
67   for (db::ActivityModel activity : activities) {
68     db::VersionedComponentName vcn{package_name, activity.name, version};
69     CleanUpFilesForActivity(db, vcn);
70   }
71 }
72 
CleanUpFilesForPackage(const db::DbHandle & db,const std::string & package_name,int64_t version)73 void CleanUpFilesForPackage(const db::DbHandle& db,
74                             const std::string& package_name,
75                             int64_t version) {
76   std::optional<PackageModel> package =
77         PackageModel::SelectByNameAndVersion(db, package_name.c_str(), version);
78 
79   if (!package) {
80     LOG(DEBUG) << "No package to clean up " << package_name << " with version " << version;
81     return;
82   }
83 
84   CleanUpFilesForPackage(db, package->id, package_name, version);
85 }
86 
CleanUpFilesForDb(const db::DbHandle & db)87 void CleanUpFilesForDb(const db::DbHandle& db) {
88   std::vector<db::PackageModel> packages = db::PackageModel::SelectAll(db);
89   for (db::PackageModel package : packages) {
90       CleanUpFilesForPackage(db, package.id, package.name, package.version);
91   }
92 }
93 
CleanUpFilesForPackage(const std::string & db_path,const std::string & package_name)94 void CleanUpFilesForPackage(const std::string& db_path,
95                             const std::string& package_name) {
96   iorap::db::SchemaModel db_schema = db::SchemaModel::GetOrCreate(db_path);
97   db::DbHandle db{db_schema.db()};
98   CleanUpFilesForPackage(db, package_name);
99 }
100 
101 
CleanUpFilesForPackage(const db::DbHandle & db,const std::string & package_name)102 void CleanUpFilesForPackage(const db::DbHandle& db,
103                             const std::string& package_name) {
104   std::vector<PackageModel> packages = PackageModel::SelectByName(db, package_name.c_str());;
105 
106   for (PackageModel& package : packages) {
107     CleanUpFilesForPackage(db, package.id, package.name, package.version);
108   }
109 
110   if (packages.empty()) {
111     LOG(DEBUG) << "No package rows to clean up " << package_name;
112   }
113 }
114 
115 }  // namespace iorap::db
116