1 /*
2  * Copyright (C) 2019 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 "CrateManager.h"
18 
19 #ifdef ENABLE_STORAGE_CRATES
20 
21 #include <android-base/logging.h>
22 #include <android-base/stringprintf.h>
23 #include <android/log.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <libgen.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <sys/xattr.h>
30 #include <unistd.h>
31 
32 #include <fstream>
33 #include <string>
34 #include <utils.h>
35 
36 #include "utils.h"
37 
38 using android::base::StringPrintf;
39 
40 namespace android {
41 namespace installd {
42 
CrateManager(const char * uuid,userid_t userId,const std::string & packageName)43 CrateManager::CrateManager(const char* uuid, userid_t userId, const std::string& packageName) {
44     mPackageName = packageName;
45     mRoot = create_data_user_ce_package_path(uuid, userId, (const char*)packageName.c_str());
46     mCratedFoldersRoot = StringPrintf("%s/crates", mRoot.c_str());
47 }
48 
~CrateManager()49 CrateManager::~CrateManager() {}
50 
getValidatedCratedPath(std::string path)51 static std::string getValidatedCratedPath(std::string path) {
52     size_t pos = path.rfind("/");
53     if (pos == std::string::npos) {
54         return path;
55     }
56 
57     return path.substr(pos + 1, path.length());
58 }
59 
traverseChildDir(const std::string & targetDir,std::function<void (FTSENT *)> & onVisitChildDir)60 void CrateManager::traverseChildDir(const std::string& targetDir,
61     std::function<void(FTSENT*)>& onVisitChildDir) {
62     char* argv[] = {(char*)targetDir.c_str(), nullptr};
63     FTS* fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr);
64     if (fts == nullptr) {
65         PLOG(WARNING) << "Failed to fts_open " << targetDir;
66         return;
67     }
68 
69     FTSENT* p;
70     while ((p = fts_read(fts)) != nullptr) {
71         switch (p->fts_info) {
72             case FTS_D:
73                 if (p->fts_level == 1) {
74                     onVisitChildDir(p);
75                 }
76                 break;
77             default:
78                 break;
79         }
80 
81         if (p->fts_level == 1) {
82             fts_set(fts, p, FTS_SKIP);
83         }
84     }
85     fts_close(fts);
86 }
87 
traverseAllPackagesForUser(const std::unique_ptr<std::string> & uuid,userid_t userId,std::function<void (FTSENT *)> & onHandlingPackage)88 void CrateManager::traverseAllPackagesForUser(
89         const std::unique_ptr<std::string>& uuid, userid_t userId,
90         std::function<void(FTSENT*)>& onHandlingPackage) {
91     const char* uuid_ = uuid ? uuid->c_str() : nullptr;
92 
93     auto ce_path = create_data_user_ce_path(uuid_, userId);
94     traverseChildDir(ce_path, onHandlingPackage);
95 }
96 
createCrate(CratedFolder cratedFolder,std::function<void (CratedFolder,std::unique_ptr<CrateMetadata> &)> & onCreateCrate)97 void CrateManager::createCrate(
98         CratedFolder cratedFolder,
99         std::function<void(CratedFolder, std::unique_ptr<CrateMetadata>&)>& onCreateCrate) {
100     const char* path = cratedFolder->fts_path;
101     if (path == nullptr || *path == '\0') {
102         return;
103     }
104 
105     std::unique_ptr<CrateMetadata> crateMetadata = std::make_unique<CrateMetadata>();
106     crateMetadata->uid = cratedFolder->fts_statp->st_uid;
107     crateMetadata->packageName = mPackageName;
108     crateMetadata->id = getValidatedCratedPath(path);
109 
110     onCreateCrate(cratedFolder, crateMetadata);
111 }
112 
traverseAllCrates(std::function<void (CratedFolder,std::unique_ptr<CrateMetadata> &)> & onCreateCrate)113 void CrateManager::traverseAllCrates(std::function<void(CratedFolder, std::unique_ptr<CrateMetadata>&)>& onCreateCrate) {
114     std::function<void(FTSENT*)> onVisitCrateDir = [&](FTSENT* cratedFolder) -> void {
115         createCrate(cratedFolder, onCreateCrate);
116     };
117     traverseChildDir(mCratedFoldersRoot, onVisitCrateDir);
118 }
119 
120 #if CRATE_DEBUG
dump(std::unique_ptr<CrateMetadata> & CrateMetadata)121 void CrateManager::dump(std::unique_ptr<CrateMetadata>& CrateMetadata) {
122     LOG(DEBUG) << "CrateMetadata = {"
123             << "uid : \"" << CrateMetadata->uid
124             << "\", packageName : \"" << CrateMetadata->packageName
125             << "\", id : \"" << CrateMetadata->id
126             << "\"}";
127 }
128 #endif
129 
130 } // namespace installd
131 } // namespace android
132 
133 #endif // ENABLE_STORAGE_CRATES