Remove temporary files and folders when NI generation fails
authorj-h.choi <j-h.choi@samsung.com>
Mon, 18 Nov 2024 06:53:12 +0000 (15:53 +0900)
committer조웅석/MDE Lab(SR)/삼성전자 <ws77.cho@samsung.com>
Tue, 14 Jan 2025 00:10:07 +0000 (09:10 +0900)
Change-Id: I42d4b393684efef79d28198860303e6997de9304

NativeLauncher/tool/ni_common.cc

index 0bc29baab39644ff2870815f6a07241cd952eb9b..3754715ba317d88d35006f1fcf57a0521eca2242 100644 (file)
@@ -703,6 +703,19 @@ static void renameAppNITmpPath(NIOption* opt)
        }
 }
 
+static void removeAppNITmpPath(NIOption* opt)
+{
+       std::string niTmpPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_TMP_DIR;
+
+       if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
+               niTmpPath = replaceAll(niTmpPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+       }
+
+       if (isDirectory(niTmpPath)) {
+               removeAll(niTmpPath);
+       }
+}
+
 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
 {
        ni_error_e ret = NI_ERROR_NONE;
@@ -782,24 +795,42 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
                }
        }
 
-       if (ret == NI_ERROR_NONE) {
-               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
-                       for (auto &dll : dllList) {
-                               std::string tmpFile;
-                               std::string niFile = getNIFilePath(dll, opt);
-                               tmpFile = niFile + ".tmp";
-
-                               if (exist(tmpFile)) {
-                                       moveFile(tmpFile, niFile);
-                                       _SOUT("Native image %s generated successfully.", niFile.c_str());
-                               }
+       if (ret != NI_ERROR_NONE) {
+               for (auto &dll : dllList) {
+                       std::string niFile1 = getNIFilePath(dll, opt);
+                       std::string niFile2 = replaceAll(niFile1, "/.native_image_tmp/", "/");
+                       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                               removeFile(niFile1 + ".tmp");
+                               removeFile(niFile2 + ".tmp");
+                       } else {
+                               removeFile(niFile1);
+                               removeFile(niFile2);
                        }
                }
+
                if (opt->flags & NI_FLAGS_APPNI) {
-                       renameAppNITmpPath(opt);
+                       removeAppNITmpPath(opt);
+               }
+               return ret;
+       }
+
+       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               for (auto &dll : dllList) {
+                       std::string tmpFile;
+                       std::string niFile = getNIFilePath(dll, opt);
+                       tmpFile = niFile + ".tmp";
+
+                       if (exist(tmpFile)) {
+                               moveFile(tmpFile, niFile);
+                               _SOUT("Native image %s generated successfully.", niFile.c_str());
+                       }
                }
        }
 
+       if (opt->flags & NI_FLAGS_APPNI) {
+               renameAppNITmpPath(opt);
+       }
+
        return ret;
 }
 
@@ -1232,6 +1263,10 @@ ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
                                                        _SERR("Failed to remove app ni dir [%s]", path.c_str());
                                                }
                                        }
+                                       std::string niTmpPath = replaceAll(path, "/.native_image", "/.native_image_tmp");
+                                       if (isDirectory(niTmpPath)) {
+                                               removeAll(niTmpPath);
+                                       }
                                }
                        }
                }