1 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/step/filesystem/step_remove_files.h"
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/system/error_code.hpp>
10 #include <sys/types.h>
11 #include <tzplatform_config.h>
17 #include "common/pkgmgr_query.h"
18 #include "common/utils/file_util.h"
20 namespace bs = boost::system;
21 namespace bf = boost::filesystem;
25 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
27 bool SkipRWDirectories(const bf::path& path) {
28 static const std::vector<std::string> dirs_to_ignore = {
35 return std::find(dirs_to_ignore.begin(), dirs_to_ignore.end(), path) !=
42 namespace common_installer {
43 namespace filesystem {
45 Step::Status StepRemoveFiles::precheck() {
46 if (!context_->manifest_data.get()) {
47 LOG(ERROR) << "manifest_data attribute is empty";
48 return Step::Status::MANIFEST_NOT_FOUND;
51 // Even though, the below checks can fail, StepRemoveFiles should still try
52 // to remove the files
53 if (context_->pkg_path.get().empty())
54 LOG(ERROR) << "pkg_path attribute is empty";
55 else if (!bf::exists(context_->pkg_path.get()))
56 LOG(ERROR) << "pkg_path ("
57 << context_->pkg_path.get()
58 << ") path does not exist";
60 return Step::Status::OK;
63 Step::Status StepRemoveFiles::process() {
64 bf::path pkg_path(context_->pkg_path.get());
66 // We need to unmount external storage before removing package directory
67 // because mount point is inside
68 if (context_->external_storage)
69 context_->external_storage->Commit();
71 bool is_keep_rwdata = false;
72 if (context_->request_mode.get() != RequestMode::GLOBAL) {
73 if (QueryIsPackageInstalled(context_->pkgid.get(), kGlobalUserUid) ||
74 context_->keep_rwdata.get())
75 is_keep_rwdata = true;
78 bool is_error = false;
81 for (bf::directory_iterator itr(pkg_path); itr != bf::directory_iterator();
83 if (bf::is_directory(itr->status())) {
84 if (SkipRWDirectories(itr->path().leaf())) {
85 LOG(DEBUG) << "Skipping remove dir:" << itr->path().c_str();
88 ret = RemoveAll(itr->path());
90 ret = Remove(itr->path());
96 if (!RemoveAll(pkg_path))
101 return Status::ERROR;
106 } // namespace filesystem
107 } // namespace common_installer