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);
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);
return ValidatePkgType(ret);
}
- tmp = xmlTextReaderGetAttribute(reader_auto.get(),
+ tmp = xmlTextReaderGetAttribute(reader,
reinterpret_cast<const xmlChar*>(kResType));
if (tmp) {
xmlFree(tmp);
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 {