Remove boost dependency
[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 <filesystem>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "common/installer_context.h"
13 #include "common/shared_dirs.h"
14 #include "common/utils/file_util.h"
15 #include "common/utils/user_util.h"
16
17 namespace fs = std::filesystem;
18 namespace ci = common_installer;
19
20 namespace {
21
22 const char kDataDir[] = "data";
23 const char kCacheDir[] = "cache";
24 const char kSharedDir[] = ".shared";
25 const char kSubssesionDir[] = "subsession";
26
27 bool RemoveContents(const fs::path& path) {
28   if (!fs::exists(path))
29     return true;
30
31   for (fs::directory_iterator iter(path); iter != fs::directory_iterator();
32       ++iter) {
33     if (!ci::RemoveAll(iter->path()))
34       return false;
35   }
36
37   return true;
38 }
39
40 }  // namespace
41
42 namespace common_installer {
43 namespace filesystem {
44
45 Step::Status StepRemoveUserData::process() {
46   // Currently this step is only for ReadonlyUpdateUninstall mode.
47   if (context_->keep_rwdata.get())
48     return Step::Status::OK;
49
50   UserList list = ci::GetUserList();
51   for (const auto& l : list) {
52     uid_t uid = std::get<0>(l);
53     fs::path owner_apps_rw(std::get<2>(l) / "apps_rw");
54     std::vector<fs::path> apps_rw_paths;
55     apps_rw_paths.push_back(std::move(owner_apps_rw));
56     for (auto& lw_user : GetLightUserList(uid))
57       apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
58                               "apps_rw");
59
60     for (auto& apps_rw : apps_rw_paths) {
61       if (!RemoveContents(apps_rw / context_->pkgid.get() / kDataDir)) {
62         LOG(ERROR) << "Failed to remove contents of data dir";
63         return Step::Status::APP_DIR_ERROR;
64       }
65       if (!RemoveContents(apps_rw / context_->pkgid.get() / kCacheDir)) {
66         LOG(ERROR) << "Failed to remove contents of cache dir";
67         return Step::Status::APP_DIR_ERROR;
68       }
69         if (!RemoveContents(apps_rw / kSharedDir / context_->pkgid.get() /
70                             kDataDir)) {
71         LOG(ERROR) << "Failed to remove contents of shared/data dir";
72         return Step::Status::APP_DIR_ERROR;
73       }
74     }
75   }
76
77   return Step::Status::OK;
78 }
79
80 }  // namespace filesystem
81 }  // namespace common_installer