-#define __XSTR(x) #x
-#define __STR(x) __XSTR(x)
-static const char* __TAC_DIR = __STR(TAC_DIR);
-#undef __STR
-#undef __XSTR
-
-typedef struct Metadata {
- const char *key;
- const char *value;
-} Metadata;
-
-std::vector<std::string> nugetPackagesAssembliesSha;
-std::vector<std::string> tacDB;
-std::vector<std::string> createDirectories;
-std::vector<std::string> updateTac;
-std::string status = "";
-std::string rootPath;
-std::string execName;
-std::string binPath;
-static sqlite3 *tac_db = NULL;
-
-bool metadataCheck(GList *list)
-{
- GList *tag = NULL;
- Metadata *mdInfo = NULL;
- tag = g_list_first(list);
- mdInfo = (Metadata*)tag->data;
- if (strcmp(mdInfo->key, TAC_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
- _DBG("Prefer nuget cache set TRUE");
- if (initializePluginManager("normal")) {
- _ERR("Fail to initialize PluginManager");
- return false;
- }
- if (initializePathManager(std::string(), std::string(), std::string())) {
- _ERR("Fail to initialize PathManger");
- return false;
- }
- return true;
- }
- return false;
-}
-
-bool appTypeCheck(std::string pkgId)
-{
- uid_t uid = 0;
- if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
- _ERR("Failed to get UID");
- return false;
- }
-
- pkgmgrinfo_pkginfo_h handle;
- int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
- if (ret != PMINFO_R_OK) {
- _ERR("Failed to get pkg info");
- return false;
- }
-
- bool isDotnetAppType = false;
- auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
- char* type = nullptr;
- bool* dotnet = static_cast<bool*>(userData);
- if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) {
- _ERR("Failed to get app type : %s", type);
- return -1;
- }
- if (strcmp(type, "dotnet") == 0) {
- *dotnet = true;
- }
- return 0;
- };
-
- if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
- _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
- pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
- return false;
- }
-
- pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
- return isDotnetAppType;
-}
-
-void SHA256(std::string path, char outputBuffer[65])
-{
- FILE *file = fopen(path.c_str(), "rb");
- if (!file) {
- return;
- }
-
- unsigned char hash[SHA256_DIGEST_LENGTH];
- SHA256_CTX sha256;
- SHA256_Init(&sha256);
- int bytesRead = 0;
- const int bufSize = 32768;
- char *buffer = (char*)malloc(bufSize);
- if (!buffer) {
- fclose(file);
- return;
- }
-
- while ((bytesRead = fread(buffer, 1, bufSize, file))) {
- SHA256_Update(&sha256, buffer, bytesRead);
- }
- SHA256_Final(hash, &sha256);
- for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
- snprintf(outputBuffer + (i * 2), 3, "%02x", hash[i]);
- }
- outputBuffer[64] = 0;
-
- fclose(file);
- free(buffer);
-}
-
-void createSHA256Info(std::string sha256Info, std::string nugetPackage)
-{
- std::ofstream ofs(sha256Info, std::ios::app);
- int assembly_count = 0;
- for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
- std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
- std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
- std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
- std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
- if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
- ofs << assembly << ":" << sha << std::endl;
- assembly_count++;
- }
- }
- ofs << assembly_count << std::endl;
- ofs.close();
-}
-
-int compareSHA256Info(std::string sha256Info, std::string nugetPackage)
-{
- int compare_count = 0;
- int assembly_count = 0;
- std::string sha256_count = "0";
- for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
- std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
- std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
- std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
- std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
- if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
- assembly_count++;
- std::ifstream ifs(sha256Info);
- std::string get_str;
- if (ifs.is_open()) {
- while (getline(ifs, get_str)) {
- if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
- compare_count++;
- }
- sha256_count = get_str;
- }
- ifs.close();
- }
- }
- }
- if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
- !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
- _INFO("Same nuget : %s", nugetPackage.c_str());
- return 1;
- }
- return 0;
-}
-
-int copyNCreateSymlink(std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir)
-{
- uid_t g_uid = 0;
- gid_t g_gid = 0;
- if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
- _ERR("Failed to get UID");
- return -1;
- }
-
- std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
- if (!createDir(tac_dir)) {
- _ERR("Cannot create directory: %s", tac_dir.c_str());
- return -1;
- }
-
- for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
- std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
- std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
- std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
- if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
- if (bf::exists(concatPath(binPath, assembly))) {
- if (isCreateTacDir) {
- if (!copyFile(concatPath(binPath, assembly), concatPath(tacVersionDir, assembly))) {
- _ERR("Failed to copy of %s", assembly.c_str());
- return -1;
- }
- }
- bf::create_symlink(concatPath(tacVersionDir, assembly), concatPath(tac_dir, assembly));
- if (lchown(concatPath(tac_dir, assembly).c_str(), g_uid, g_gid)) {
- _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
- return -1;
- }
- if (!removeFile(concatPath(binPath, assembly))) {
- _ERR("Failed to remove of %s", assembly.c_str());
- return -1;
- }
- }
- }
- }
- return 0;
-}
-
-void depsJsonCheck() {
- for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
- std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':'));
- std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1);
- tacDB.push_back(nuget_package);
- char buffer[65] = {0};
- SHA256(concatPath(binPath, assembly_name), buffer);
- nugetPackagesAssembliesSha.push_back(nuget_package + ":" + assembly_name + ":" + buffer);
- _INFO("Assembly : [%s] / SHA256 : [%s]", assembly_name.c_str(), buffer);
- }
- std::sort(tacDB.begin(), tacDB.end());
- tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
-}
-