Classify rpk package by checking manifest in file 08/316408/3
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 21 Aug 2024 04:31:51 +0000 (13:31 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Wed, 21 Aug 2024 23:23:46 +0000 (08:23 +0900)
To support installation in resource package where file extension is not rpk

Change-Id: Ibf6c44c86c25a6d15e4f1a7f8eb3aa0e3600b242
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/unified/unified_installer_factory.cc

index c8bffe9eaf2fd9f02601c2b944cd7dbd2455c3ab..5ac027ca5988214f4a03908c6a66707b4226d417 100644 (file)
@@ -53,40 +53,6 @@ std::string GetPkgTypeFromPkgid(const std::string& pkgid, uid_t uid) {
   return ValidatePkgType(query.Type());
 }
 
-std::string GetPkgTypeFromPath(const std::string& path) {
-  std::string type;
-
-  if (fs::path(path).extension() == ".rpk")
-    return "rpk";
-
-  unzFile uf = unzOpen(path.c_str());
-  if (!uf) {
-    LOG(ERROR) << "Failed to open zip file: " << path;
-    return type;
-  }
-  for (const auto& it : kTypeMap) {
-    if (unzLocateFile(uf, it.first, 0) == UNZ_OK) {
-      LOG(DEBUG) << "Found: " << it.first;
-      type = it.second;
-      break;
-    }
-  }
-  if (type.empty())
-    LOG(ERROR) << "Cannot get pkg type";
-  unzClose(uf);
-
-  return ValidatePkgType(type);
-}
-
-std::string GetPkgTypeFromFilename(const fs::path& name) {
-  std::string filename = name.string();
-  std::string token;
-  std::stringstream ss(filename);
-
-  std::getline(ss, token, '-');
-  return ValidatePkgType(token);
-}
-
 int MoveToChildElement(xmlTextReaderPtr reader, int depth) {
   int ret = xmlTextReaderRead(reader);
   int cur = xmlTextReaderDepth(reader);
@@ -118,42 +84,18 @@ int MoveToChildElement(xmlTextReaderPtr reader, int depth) {
   return ret;
 }
 
-std::string GetPkgTypeFromXml(const std::string& info, uid_t uid,
-    bool is_preload) {
-  fs::path xml_path =
-      fs::path(getUserManifestPath(uid, is_preload))
-      / fs::path(info);
-  xml_path += ".xml";
-
-  if (!fs::exists(xml_path)) {
-    LOG(ERROR) << "xml path [" << xml_path << "] is not exist";
-    return "";
-  }
-
-  const xmlChar* node;
-  xmlTextReaderPtr reader;
-  xmlChar* tmp = nullptr;
-
-  reader = xmlReaderForFile(xml_path.c_str(), nullptr, 0);
-
-  if (!reader) {
-    LOG(ERROR) << "xmlReaderForFile value is NULL";
-    return {};
-  }
-  std::unique_ptr<xmlTextReader, decltype(xmlFreeTextReader)*> reader_auto(
-      reader, xmlFreeTextReader);
-
-  if (!MoveToChildElement(reader_auto.get(), -1))
+std::string GetPkgTypeFromXmlTextReader(xmlTextReaderPtr reader) {
+  if (!MoveToChildElement(reader, -1))
     return {};
 
-  node = xmlTextReaderConstName(reader_auto.get());
+  const xmlChar* node = xmlTextReaderConstName(reader);
   if (!node) {
     LOG(ERROR) << "xmlTextReaderConstName value is NULL";
     return {};
   }
 
   if (!strcmp(reinterpret_cast<const char*>(node), kManifestKey)) {
-    tmp = xmlTextReaderGetAttribute(reader_auto.get(),
+    xmlChar* tmp = xmlTextReaderGetAttribute(reader,
         reinterpret_cast<const xmlChar*>(kType));
     if (tmp) {
       std::string ret = reinterpret_cast<const char*>(tmp);
@@ -161,7 +103,7 @@ std::string GetPkgTypeFromXml(const std::string& info, uid_t uid,
       return ValidatePkgType(ret);
     }
 
-    tmp = xmlTextReaderGetAttribute(reader_auto.get(),
+    tmp = xmlTextReaderGetAttribute(reader,
         reinterpret_cast<const xmlChar*>(kResType));
     if (tmp) {
       xmlFree(tmp);
@@ -172,6 +114,99 @@ std::string GetPkgTypeFromXml(const std::string& info, uid_t uid,
   return {};
 }
 
+bool HasRpkAttribute(unzFile uf) {
+  unz_file_info info;
+  if (unzGetCurrentFileInfo(uf, &info,
+      nullptr, 0, nullptr, 0, nullptr, 0) != UNZ_OK) {
+    LOG(ERROR) << "unzGetCurrentFileInfo fail";
+    return false;
+  }
+
+  if (unzOpenCurrentFile(uf) != UNZ_OK) {
+    LOG(ERROR) << "unzOpenCurrentFile fail";
+    return false;
+  }
+
+  std::vector<char> buf(info.uncompressed_size);
+  int readsize = unzReadCurrentFile(
+      uf, reinterpret_cast<void*>(buf.data()), info.uncompressed_size);
+  unzCloseCurrentFile(uf);
+  if (readsize != static_cast<long long>(info.uncompressed_size)) {
+    LOG(ERROR) << "readsize : " << readsize;
+    return false;
+  }
+
+  xmlTextReaderPtr reader = xmlReaderForMemory(buf.data(),
+      info.uncompressed_size, nullptr, nullptr, 0);
+  if (!reader) {
+    LOG(ERROR) << "xmlReaderForMemory value is NULL";
+    return {};
+  }
+  std::unique_ptr<xmlTextReader, decltype(xmlFreeTextReader)*> reader_auto(
+      reader, xmlFreeTextReader);
+
+  return GetPkgTypeFromXmlTextReader(reader) == kRpkTypeStr;
+}
+
+std::string GetPkgTypeFromPath(const std::string& path) {
+  std::string type;
+
+  if (fs::path(path).extension() == ".rpk")
+    return "rpk";
+
+  unzFile uf = unzOpen(path.c_str());
+  if (!uf) {
+    LOG(ERROR) << "Failed to open zip file: " << path;
+    return type;
+  }
+  for (const auto& it : kTypeMap) {
+    if (unzLocateFile(uf, it.first, 0) == UNZ_OK) {
+      LOG(DEBUG) << "Found: " << it.first;
+      type = it.second;
+      if (type == "tpk" && HasRpkAttribute(uf))
+        type = "rpk";
+      break;
+    }
+  }
+  if (type.empty())
+    LOG(ERROR) << "Cannot get pkg type";
+  unzClose(uf);
+
+  return ValidatePkgType(type);
+}
+
+std::string GetPkgTypeFromFilename(const fs::path& name) {
+  std::string filename = name.string();
+  std::string token;
+  std::stringstream ss(filename);
+
+  std::getline(ss, token, '-');
+  return ValidatePkgType(token);
+}
+
+std::string GetPkgTypeFromXml(const std::string& info, uid_t uid,
+    bool is_preload) {
+  fs::path xml_path =
+      fs::path(getUserManifestPath(uid, is_preload))
+      / fs::path(info);
+  xml_path += ".xml";
+
+  if (!fs::exists(xml_path)) {
+    LOG(ERROR) << "xml path [" << xml_path << "] is not exist";
+    return "";
+  }
+
+  xmlTextReaderPtr reader = xmlReaderForFile(xml_path.c_str(), nullptr, 0);
+  if (!reader) {
+    LOG(ERROR) << "xmlReaderForFile value is NULL";
+    return {};
+  }
+  std::unique_ptr<xmlTextReader, decltype(xmlFreeTextReader)*> reader_auto(
+      reader, xmlFreeTextReader);
+
+  return GetPkgTypeFromXmlTextReader(reader);
+}
+
 }  // namespace
 
 namespace common_installer {