#include "common/paths.h"
#include "common/utils/file_util.h"
+#include "common/utils/user_util.h"
#include "common/shared_dirs.h"
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+namespace ci = common_installer;
+
namespace {
const char kExternalMemoryMountPoint[] = ".mmc";
+bool RemoveContents(const bf::path& path) {
+ for (bf::directory_iterator iter(path);
+ iter != bf::directory_iterator(); ++iter) {
+ if (!ci::RemoveAll(iter->path()))
+ return false;
+ }
+ return true;
+}
+
+bool BackupContents(const bf::path& src, const bf::path& backup_path) {
+ if (!ci::CopyDir(src, backup_path,
+ ci::FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS, false))
+ return false;
+ for (bf::directory_iterator iter(src);
+ iter != bf::directory_iterator(); ++iter) {
+ if (iter->path().filename() == "res") {
+ if (!RemoveContents(iter->path()))
+ return false;
+ continue;
+ }
+
+ if (!ci::RemoveAll(iter->path()))
+ return false;
+ }
+
+ return true;
+}
+
} // namespace
namespace common_installer {
namespace backup {
-namespace bf = boost::filesystem;
-namespace bs = boost::system;
-
Step::Status StepCopyBackup::precheck() {
if (context_->pkgid.get().empty()) {
LOG(ERROR) << "pkgid attribute is empty";
return Status::APP_DIR_ERROR;
RemoveContent();
-
return Status::OK;
}
}
}
+ if (iter->path() == context_->GetPkgPath() / "shared") {
+ if (!BackupContents(iter->path(), backup_path_ / "shared"))
+ return false;
+ continue;
+ }
if (bf::is_directory(iter->path())) {
if (!MoveDir(iter->path(), backup_path_ / iter->path().filename())) {
LOG(ERROR) << "Fail to backup package directory of: " << iter->path();
LOG(ERROR) << "Cannot create package directory";
return false;
}
- if (!MoveDir(context_->unpacked_dir_path.get(), install_path_,
- FSFlag::FS_MERGE_SKIP)) {
- LOG(ERROR) << "Fail to copy tmp dir: " << context_->unpacked_dir_path.get()
- << " to dst dir: " << install_path_;
- return false;
+ for (bf::directory_iterator iter(context_->unpacked_dir_path.get());
+ iter != bf::directory_iterator(); ++iter) {
+ if (bf::is_directory(iter->path())) {
+ if (!MoveDir(iter->path(), install_path_ / iter->path().filename(),
+ FS_MERGE_SKIP)) {
+ LOG(ERROR) << "Fail to copy tmp dir: " << iter->path()
+ << " to dst dir: " << install_path_;
+ return false;
+ }
+ } else {
+ if (!MoveFile(iter->path(), install_path_ / iter->path().filename())) {
+ LOG(ERROR) << "Fail to copy tmp dir: " << iter->path()
+ << " to dst dir: " << install_path_;
+ return false;
+ }
+ }
}
+ // If other application tries to access shared/res of package being installed,
+ // it will be failed due to permission deny. Set its permission before
+ // StepChangeOwnershipAndPermission to prevent it.
+ uid_t uid = context_->uid.get();
+ boost::optional<gid_t> gid = common_installer::GetGidByUid(uid);
+ if (!gid) {
+ LOG(ERROR) << "Failed to get gid";
+ return false;
+ }
+ if (bf::exists(install_path_ / "shared/res") &&
+ !common_installer::SetOwnershipAll(
+ install_path_ / "shared/res", uid, *gid)) {
+ LOG(ERROR) << "Failed to set ownership";
+ return false;
+ }
LOG(INFO) << "Successfully move: " << context_->unpacked_dir_path.get()
<< " to: " << install_path_ << " directory";