Add ROU to RW case to pkg_upgrade 44/285844/6
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 20 Dec 2022 06:05:31 +0000 (15:05 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Fri, 16 Jun 2023 04:31:13 +0000 (13:31 +0900)
old_pkg: RO updated pkg
new_pkg: RO removed, only RW remained

Change-Id: I51e820e96fa6eed18bff0561565f446cf4699059
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
packaging/pkgmgr-tool.spec
src/pkg_upgrade/CMakeLists.txt
src/pkg_upgrade/include/common_type.hh
src/pkg_upgrade/include/db_upgrader.hh [new file with mode: 0644]
src/pkg_upgrade/src/db_upgrader.cc [new file with mode: 0644]
src/pkg_upgrade/src/pkg_finder.cc
src/pkg_upgrade/src/pkg_upgrader_factory.cc
tests/mock/pkgmgr_info_mock.cc
tests/mock/pkgmgr_info_mock.h

index 9422904..af1e541 100644 (file)
@@ -33,6 +33,7 @@ PKG_CHECK_MODULES(PKGMGR_PARSER_DEPS REQUIRED pkgmgr-parser)
 PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info)
 PKG_CHECK_MODULES(PKGMGR_INSTALLER_DEPS REQUIRED pkgmgr-installer)
 PKG_CHECK_MODULES(INIPARSER_DEPS REQUIRED iniparser)
+PKG_CHECK_MODULES(TIZEN_DATABASE_DEPS REQUIRED tizen-database)
 PKG_CHECK_MODULES(TZPLATFORM_DEPS REQUIRED libtzplatform-config)
 PKG_CHECK_MODULES(AUL_DEPS REQUIRED aul)
 PKG_CHECK_MODULES(STORAGE_DEPS REQUIRED storage)
@@ -46,7 +47,7 @@ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs -pie" )
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -Wall -Werror")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE")
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -ffunction-sections -fdata-sections -fmerge-all-constants -fPIE")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Werror -ffunction-sections -fdata-sections -fmerge-all-constants -fPIE")
 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -fPIE")
 SET(CMAKE_C_FLAGS_RELEASE "-O2 -fPIE")
 
index c54caf4..7988f91 100644 (file)
@@ -29,6 +29,7 @@ BuildRequires:  pkgconfig(aul)
 BuildRequires:  pkgconfig(storage)
 BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(gmock)
+BuildRequires:  tizen-database-devel
 BuildRequires:  pkgmgr-info-parser-devel
 BuildRequires:  pkgmgr-info-parser
 BuildRequires:  fdupes
index 581b7ae..f5b621f 100755 (executable)
@@ -9,6 +9,7 @@ ADD_EXECUTABLE(${TARGET_PKG_UPGRADE} ${SRCS})
 APPLY_PKG_CONFIG(${TARGET_PKG_UPGRADE} PUBLIC
   PKGMGR_INFO_DEPS
   PKGMGR_PARSER_DEPS
+  TIZEN_DATABASE_DEPS
   TZPLATFORM_DEPS
   SMACK_DEPS
   DLOG_DEPS
@@ -17,4 +18,3 @@ APPLY_PKG_CONFIG(${TARGET_PKG_UPGRADE} PUBLIC
 
 # Install
 INSTALL(TARGETS ${TARGET_PKG_UPGRADE} DESTINATION bin)
-
index 9ebbc89..05a7598 100644 (file)
@@ -53,9 +53,10 @@ enum class PkgVersionCmpResult {
 class PkgContext {
  public:
   PkgContext(std::string id, std::string version, std::string type,
-      bool read_only, bool removable = false)
+      bool read_only, bool update = false, bool removable = false)
       : id_(std::move(id)), version_(std::move(version)),
-        type_(std::move(type)), read_only_(read_only), removable_(removable) {
+        type_(std::move(type)), read_only_(read_only), update_(update),
+        removable_(removable) {
     if (read_only)
       removable_ = false;
   }
@@ -75,6 +76,7 @@ class PkgContext {
     return PkgLocation::RW;
   }
   bool IsReadOnly() const { return read_only_; }
+  bool IsUpdate() const { return update_; }
   bool IsRemovable() const { return removable_; }
 
  private:
@@ -82,6 +84,7 @@ class PkgContext {
   std::string version_;
   std::string type_;
   bool read_only_;
+  bool update_;  // update means ReadonlyUpdated pkg
   bool removable_;
 };
 
diff --git a/src/pkg_upgrade/include/db_upgrader.hh b/src/pkg_upgrade/include/db_upgrader.hh
new file mode 100644 (file)
index 0000000..8b82f62
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef DB_UPGRADER_HH_
+#define DB_UPGRADER_HH_
+
+#include <memory>
+
+#include "pkg_upgrader.hh"
+
+namespace common_fota {
+
+class DbUpgrader : public PkgUpgrader {
+ public:
+  DbUpgrader(const PkgContext& context, PkgOperation pkg_op);
+  virtual ~DbUpgrader() = default;
+  bool Upgrade() override;
+
+  bool SetRwPkg(const std::string& pkgid);
+
+ private:
+  std::string pkgid_;
+};
+
+}  // common_fota
+
+#endif  // DB_UPGRADER_HH_
\ No newline at end of file
diff --git a/src/pkg_upgrade/src/db_upgrader.cc b/src/pkg_upgrade/src/db_upgrader.cc
new file mode 100644 (file)
index 0000000..a756310
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "db_upgrader.hh"
+
+#include <sqlite3.h>
+
+#include <tizen-database/database.hpp>
+
+#include "common_type.hh"
+#include "logging.hh"
+
+namespace {
+
+constexpr char kDbPath[] = "/opt/dbspace/.pkgmgr_parser.db";
+constexpr char kSetRwPkgQuery[] = R"(
+UPDATE package_info
+  SET package_preload='false', package_system='false', package_update='false'
+  WHERE package=?
+)";
+
+}  // namespace
+
+namespace common_fota {
+
+using tizen_base::Database;
+using tizen_base::DbException;
+
+DbUpgrader::DbUpgrader(const PkgContext& context, PkgOperation pkg_op)
+    : PkgUpgrader(context, pkg_op), pkgid_(context.GetId()) {
+}
+
+bool DbUpgrader::Upgrade() {
+  if (GetLocation() == PkgLocation::RW) {
+    if (!SetRwPkg(pkgid_)) {
+      LOG(ERROR) << "Failed to update pkg " << pkgid_;
+      return false;
+    }
+    LOG(INFO) << "Preloaded files of " << pkgid_ << " has been removed. "
+              << "This package will be non-preload RW package";
+  }
+  return true;
+}
+
+bool DbUpgrader::SetRwPkg(const std::string& pkgid) {
+  try {
+    Database database(kDbPath, SQLITE_OPEN_READWRITE);
+    auto q = Database::Sql(kSetRwPkgQuery).Bind(pkgid);
+    auto guard = database.CreateTransactionGuard();
+    auto r = database.Exec(q);
+    if (!r) {
+      LOG(ERROR) << "Update db failed: " << static_cast<int>(r);
+      return false;
+    }
+    guard.Commit();
+  } catch (DbException& e) {
+    LOG(ERROR) << "Exception during db operation: " << e.msg();
+    return false;
+  }
+
+  return true;
+}
+
+}  // namespace common_fota
index e539317..fbf95de 100644 (file)
@@ -132,8 +132,13 @@ int PkgFinder::PkgidListCb(const pkgmgrinfo_pkginfo_h handle, void* user_data) {
   if (pkgmgrinfo_pkginfo_get_type(handle, &type) != PMINFO_R_OK)
     return 0;
 
+  bool update = false;
+  if (pkgmgrinfo_pkginfo_is_update(handle, &update) != PMINFO_R_OK)
+    return 0;
+
   PkgFinder* finder = static_cast<PkgFinder*>(user_data);
-  finder->old_pkgs_.emplace_back(pkgid, version, type, finder->read_only_);
+  finder->old_pkgs_.emplace_back(pkgid, version, type, finder->read_only_,
+      update);
 
   return 0;
 }
@@ -338,9 +343,9 @@ void PkgFinder::AddRwPkgInfoFromFile(const char* info_str) {
       });
 
   if (removable == "false")
-    new_pkgs_.emplace_back(pkgid, version, type, false, false);
+    new_pkgs_.emplace_back(pkgid, version, type, false, false, false);
   else
-    new_pkgs_.emplace_back(pkgid, version, type, false, true);
+    new_pkgs_.emplace_back(pkgid, version, type, false, false, true);
 }
 
 string PkgFinder::GetToken(const char* pBuf, const char* pKey) {
index 3e3499b..42f17cb 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <memory>
 
+#include "db_upgrader.hh"
 #include "logging.hh"
 #include "ro2rw_upgrader.hh"
 #include "ro_upgrader.hh"
@@ -131,6 +132,9 @@ list<unique_ptr<PkgUpgrader>> PkgUpgraderFactory::Merge(
       if (old_pkg.IsReadOnly()) {
         pkgs.emplace_back(std::make_unique<SimpleUpgrader>(old_pkg,
             PkgOperation::UNINSTALL));
+      } else if (old_pkg.IsUpdate()) {
+        pkgs.emplace_back(std::make_unique<DbUpgrader>(old_pkg,
+            PkgOperation::COMPLEX));
       }
     }
   }
index f73cb5b..ca428c5 100644 (file)
@@ -61,6 +61,12 @@ extern "C" int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle,
       handle, version);
 }
 
+extern "C" int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle,
+    bool *update) {
+  return MOCK_HOOK_P2(PkgMgrInfoMock, pkgmgrinfo_pkginfo_is_update,
+      handle, update);
+}
+
 extern "C" int pkgmgrinfo_pkginfo_get_usr_pkginfo(
     const char* pkgid, uid_t uid, pkgmgrinfo_pkginfo_h* handle) {
   return MOCK_HOOK_P3(
index bbc2a67..f008ae9 100644 (file)
@@ -40,6 +40,7 @@ class PkgMgrInfoMock : public virtual ModuleMock {
   MOCK_METHOD2(pkgmgrinfo_pkginfo_get_type, int(pkgmgrinfo_pkginfo_h, char**));
   MOCK_METHOD2(pkgmgrinfo_pkginfo_get_version,
       int(pkgmgrinfo_pkginfo_h, char**));
+  MOCK_METHOD2(pkgmgrinfo_pkginfo_is_update, int(pkgmgrinfo_pkginfo_h, bool*));
   MOCK_METHOD3(pkgmgrinfo_pkginfo_get_usr_pkginfo,
       int(const char*, uid_t, pkgmgrinfo_pkginfo_h*));
   MOCK_METHOD1(pkgmgrinfo_pkginfo_destroy_pkginfo, int(pkgmgrinfo_pkginfo_h));