Fix static analysis issue
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_remove_user_data.cc
1 // Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/filesystem/step_remove_user_data.h"
6
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "common/installer_context.h"
12 #include "common/shared_dirs.h"
13 #include "common/utils/file_util.h"
14 #include "common/utils/user_util.h"
15
16 namespace bf = boost::filesystem;
17 namespace ci = common_installer;
18
19 namespace {
20
21 const char kDataDir[] = "data";
22 const char kCacheDir[] = "cache";
23 const char kSharedDir[] = ".shared";
24 const char kSubssesionDir[] = "subsession";
25
26 bool RemoveContents(const bf::path& path) {
27   if (!bf::exists(path))
28     return true;
29
30   for (bf::directory_iterator iter(path); iter != bf::directory_iterator();
31       ++iter) {
32     if (!ci::RemoveAll(iter->path()))
33       return false;
34   }
35
36   return true;
37 }
38
39 }  // namespace
40
41 namespace common_installer {
42 namespace filesystem {
43
44 Step::Status StepRemoveUserData::process() {
45   // Currently this step is only for ReadonlyUpdateUninstall mode.
46   if (context_->keep_rwdata.get())
47     return Step::Status::OK;
48
49   UserList list = ci::GetUserList();
50   for (const auto& l : list) {
51     uid_t uid = std::get<0>(l);
52     bf::path owner_apps_rw(std::get<2>(l) / "apps_rw");
53     std::vector<bf::path> apps_rw_paths;
54     apps_rw_paths.push_back(std::move(owner_apps_rw));
55     for (auto& lw_user : GetLightUserList(uid))
56       apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
57                               "apps_rw");
58
59     for (auto& apps_rw : apps_rw_paths) {
60       if (!RemoveContents(apps_rw / context_->pkgid.get() / kDataDir)) {
61         LOG(ERROR) << "Failed to remove contents of data dir";
62         return Step::Status::APP_DIR_ERROR;
63       }
64       if (!RemoveContents(apps_rw / context_->pkgid.get() / kCacheDir)) {
65         LOG(ERROR) << "Failed to remove contents of cache dir";
66         return Step::Status::APP_DIR_ERROR;
67       }
68         if (!RemoveContents(apps_rw / kSharedDir / context_->pkgid.get() /
69                             kDataDir)) {
70         LOG(ERROR) << "Failed to remove contents of shared/data dir";
71         return Step::Status::APP_DIR_ERROR;
72       }
73     }
74   }
75
76   return Step::Status::OK;
77 }
78
79 }  // namespace filesystem
80 }  // namespace common_installer