refactor: extract common logic 07/276707/1 accepted/tizen_7.0_unified accepted/tizen_7.0_unified_hotfix accepted/tizen_8.0_unified accepted/tizen_unified tizen tizen_7.0 tizen_7.0_hotfix tizen_8.0 accepted/tizen/7.0/unified/20221110.060110 accepted/tizen/7.0/unified/hotfix/20221116.105133 accepted/tizen/8.0/unified/20231005.093153 accepted/tizen/unified/20220624.143422 submit/tizen/20220623.063654 tizen_7.0_m2_release tizen_8.0_m2_release
authorDongHun Kwak <dh0128.kwak@samsung.com>
Thu, 23 Jun 2022 06:27:28 +0000 (15:27 +0900)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Thu, 23 Jun 2022 06:28:08 +0000 (15:28 +0900)
- Extract common logic
- Adjust code formating

Change-Id: Ia0136f4657c507301854c5ab82e8f6175fda50ac

tzdata-parser/src/tzdata-update-parser.c

index 81559d7..d5b6d49 100755 (executable)
  * 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 <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/xattr.h>
-
-#include <sys/wait.h>
-#include <errno.h>
-#include <stdlib.h>
-
-#include <unistd.h>
-#include <string.h>
+ */
 #include <dirent.h>
-#include <glib.h>
 #include <dlog.h>
+#include <errno.h>
+#include <glib.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/xattr.h>
+#include <unistd.h>
 
 #define WORKER "/etc/package-manager/parserlib/worker.sh"
 
-
-#define TZDATA_UPDATE_PARSER_TAG               "TZDATA_UPDATE_PARSER"
-
-#define DEBUG_LOG(frmt, args...)               \
-       do { SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\
-       __func__, ##args);} while (0)
-
-#define DEBUG_WARNING(frmt, args...)           \
-       do { SLOG(LOG_WARN,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\
-       __func__, ##args);} while (0)
-
-#define DEBUG_ERROR(frmt, args...)             \
-       do { SLOG(LOG_ERROR,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\
-       __func__, ##args);} while (0)
-
-#define TRACE_BEGIN do {\
-       SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[ENTER tzdata_update_parser] %s\n",\
-       __func__);} while (0)
-
-#define TRACE_END  do {\
-       SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[LEAVE tzdata_update_parser] %s\n",\
-       __func__);} while (0)
-
-
-static void alt_system()
-{
-       pid_t pid;
-       int status;
-       pid_t ret = -10000;
-       //char *const args[2] = {"/home/mjpark",  NULL};
-       //char **env;
-       //extern char **environ;
-       /* ... Sanitize arguments ... */
-       pid = fork();
-       if (pid == -1) {
-               /* Handle error */
-       } else if (pid != 0) {
-               while ((ret = waitpid(pid, &status, 0)) == -1) {
-                       if (errno != EINTR) {
-                               /* Handle error */
-                               break;
-                       }
-               }
-               if ((ret != -1) &&
-                               (!WIFEXITED(status) || !WEXITSTATUS(status)) ) {
-                       /* Report unexpected child status */
-               }
-               DEBUG_LOG(" ----> parent ? : %d ", ret);
-
-       } else {
-               /* ... Initialize env as a sanitized copy of environ ... */
-               //ret = execl("/home/mjpark/cprog/dir_copy/work.sh", "work.sh", (char *)0);
-               //if (execve("/bin/ls", args, env) == -1) {
-               //ret = execl("/bin/ls", "-1", (char *)0);
-               DEBUG_LOG(" ------------> run execl: %s ", WORKER);
-               ret = execl("/bin/bash", "bash", "-c",  WORKER, (char *) NULL);
-               if (ret) {
-                       /* Handle error */
-                       DEBUG_LOG("error occured : %s", WORKER);
-                       printf("error occured\n");
-                       _Exit(127);
-               } else {
-                       DEBUG_LOG("run OK !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
-               }
-       }
+#define TZDATA_UPDATE_PARSER_TAG "TZDATA_UPDATE_PARSER"
+
+#define DEBUG_LOG(frmt, args...)                                     \
+  do {                                                               \
+    SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG,                        \
+         "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \
+  } while (0)
+
+#define DEBUG_WARNING(frmt, args...)                                 \
+  do {                                                               \
+    SLOG(LOG_WARN, TZDATA_UPDATE_PARSER_TAG,                         \
+         "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \
+  } while (0)
+
+#define DEBUG_ERROR(frmt, args...)                                   \
+  do {                                                               \
+    SLOG(LOG_ERROR, TZDATA_UPDATE_PARSER_TAG,                        \
+         "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \
+  } while (0)
+
+#define TRACE_BEGIN                                      \
+  do {                                                   \
+    SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG,            \
+         "[ENTER tzdata_update_parser] %s\n", __func__); \
+  } while (0)
+
+#define TRACE_END                                        \
+  do {                                                   \
+    SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG,            \
+         "[LEAVE tzdata_update_parser] %s\n", __func__); \
+  } while (0)
+
+static void alt_system() {
+  pid_t pid;
+  int status;
+  pid_t ret = -10000;
+  // char *const args[2] = {"/home/mjpark",  NULL};
+  // char **env;
+  // extern char **environ;
+  /* ... Sanitize arguments ... */
+  pid = fork();
+  if (pid == -1) {
+    /* Handle error */
+  } else if (pid != 0) {
+    while ((ret = waitpid(pid, &status, 0)) == -1) {
+      if (errno != EINTR) {
+        /* Handle error */
+        break;
+      }
+    }
+    if ((ret != -1) && (!WIFEXITED(status) || !WEXITSTATUS(status))) {
+      /* Report unexpected child status */
+    }
+    DEBUG_LOG(" ----> parent ? : %d ", ret);
+  } else {
+    /* ... Initialize env as a sanitized copy of environ ... */
+    // ret = execl("/home/mjpark/cprog/dir_copy/work.sh", "work.sh", (char *)0);
+    // if (execve("/bin/ls", args, env) == -1) {
+    // ret = execl("/bin/ls", "-1", (char *)0);
+    DEBUG_LOG(" ------------> run execl: %s ", WORKER);
+    ret = execl("/bin/bash", "bash", "-c", WORKER, (char *)NULL);
+    if (ret) {
+      /* Handle error */
+      DEBUG_LOG("error occured : %s", WORKER);
+      printf("error occured\n");
+      _Exit(127);
+    } else {
+      DEBUG_LOG("run OK !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    }
+  }
 }
 
-//DEBUG_ERROR("srcdir length is not available \n");
-//DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
-int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       int ret;
-       pkgmgrinfo_pkginfo_h handle = NULL;
-       const char *app_root_path = NULL;
-       //const char *dest_path = NULL;
-       uid_t uid = 0;
-
-       pkgmgr_installer_info_get_target_uid(&uid);
-       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
-
-       if (ret < 0)
-       {
-               DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
-               return -1;
-       }
-
-       ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
-       if (ret < 0)
-       {
-               DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
-               goto FAIL;
-       }
-
-       if (appid == NULL)
-       {
-               DEBUG_ERROR("appid is NULL \n");
-               goto FAIL;
-       }
-
-
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       TRACE_END;
-       return ret;
+static int _get_pkgmgrinfo_handle(const char *pkgid,
+                                  pkgmgrinfo_pkginfo_h handle) {
+  int ret = 0;
+  uid_t uid = 0;
 
-FAIL:
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       TRACE_END;
-       return -1;
-}
+  pkgmgr_installer_info_get_target_uid(&uid);
+  ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
 
+  if (ret < 0) {
+    DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
+    return -1;
+  }
+  return ret;
+}
 
-int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       int ret = 0;
-       pkgmgrinfo_pkginfo_h handle = NULL;
-       const char* app_root_path = NULL;
-       //const char* dest_path = NULL;
-       uid_t uid = 0;
-
-       pkgmgr_installer_info_get_target_uid(&uid);
-       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
-
-       if (ret < 0)
-       {
-               DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
-               return -1;
-       }
-
-       ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
-       if (ret < 0)
-       {
-               DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
-               goto FAIL;
-       }
-
-       if (appid == NULL)
-       {
-               DEBUG_ERROR("appid is NULL \n");
-               goto FAIL;
-       }
-
-       DEBUG_LOG("-----------------------------------------------------");
-       DEBUG_LOG("pkgid : %s ", pkgid);
-       DEBUG_LOG("APP root path : %s ", app_root_path);
-       DEBUG_LOG("-----------------------------------------------------");
-       // RUN SCRIPT HERE
-       alt_system();
-       DEBUG_LOG("-----------------------------------------------------");
-
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       TRACE_END;
-       return ret;
+static int _pkgmgr_plugin_install(const char *pkgid, const char *appid,
+                                  GList *list, int upgrade_flag) {
+  TRACE_BEGIN;
+  int ret = 0;
+  pkgmgrinfo_pkginfo_h handle = NULL;
+  const char *app_root_path = NULL;
+  // const char* dest_path = NULL;
+  uid_t uid = 0;
+
+  if (_get_pkgmgrinfo_handle(pkgid, handle) < 0) return -1;
+
+  ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
+  if (ret < 0) {
+    DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
+    goto FAIL;
+  }
+
+  if (appid == NULL) {
+    DEBUG_ERROR("appid is NULL \n");
+    goto FAIL;
+  }
+
+  if (upgrade_flag == 1) {
+    DEBUG_LOG("-----------------------------------------------------");
+    DEBUG_LOG("pkgid : %s ", pkgid);
+    DEBUG_LOG("APP root path : %s ", app_root_path);
+    DEBUG_LOG("-----------------------------------------------------");
+    // RUN SCRIPT HERE
+    alt_system();
+    DEBUG_LOG("-----------------------------------------------------");
+  }
+
+  pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+  TRACE_END;
+  return ret;
 
 FAIL:
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       TRACE_END;
-       return -1;
+  pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+  TRACE_END;
+  return -1;
 }
 
+// DEBUG_ERROR("srcdir length is not available \n");
+// DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
+int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid,
+                                 GList *list) {
+  return _pkgmgr_plugin_install(pkgid, appid, list, 0);
+}
 
+int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid,
+                                 GList *list) {
+  return _pkgmgr_plugin_install(pkgid, appid, list, 1);
+}
 
-int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       pkgmgrinfo_pkginfo_h handle = NULL;
-       const char* app_root_path = NULL;
-       //const char *dest_path = NULL;
-       int ret;
-       uid_t uid = 0;
-
-       if (appid == NULL)
-       {
-               DEBUG_ERROR("appid is NULL \n");
-               return -1;
-       }
+int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid,
+                                   GList *list) {
+  TRACE_BEGIN;
+  pkgmgrinfo_pkginfo_h handle = NULL;
+  const char *app_root_path = NULL;
+  // const char *dest_path = NULL;
+  int ret;
+  uid_t uid = 0;
 
-       pkgmgr_installer_info_get_target_uid(&uid);
-       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
+  if (appid == NULL) {
+    DEBUG_ERROR("appid is NULL \n");
+    return -1;
+  }
 
-       if (ret < 0)
-       {
-               DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
-               return -1;
-       }
+  if (_get_pkgmgrinfo_handle(pkgid, handle) < 0) return -1;
 
-       ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
-       if (ret < 0)
-       {
-               DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
-               goto FAIL;
-       }
+  ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
+  if (ret < 0) {
+    DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
+    goto FAIL;
+  }
 
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       TRACE_END;
-       return ret;
+  pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+  TRACE_END;
+  return ret;
 
 FAIL:
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       TRACE_END;
-       return -1;
+  pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+  TRACE_END;
+  return -1;
 }
 
 #if USE_META_DATA
-int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
+int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid,
+                                   GList *list) {
+  TRACE_BEGIN;
+  return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
 }
 
-int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
+int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid,
+                                   GList *list) {
+  TRACE_BEGIN;
+  return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
 }
 
-int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
+int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid,
+                                     GList *list) {
+  TRACE_BEGIN;
+  return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
 }
 #endif
 
-int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
+int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid,
+                                          GList *list) {
+  TRACE_BEGIN;
+  return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
 }
 
-int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
+int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid,
+                                          GList *list) {
+  TRACE_BEGIN;
+  return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
 }
 
-int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
-{
-       TRACE_BEGIN;
-       return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
+int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid,
+                                            const char *appid, GList *list) {
+  TRACE_BEGIN;
+  return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
 }
 
-
 /* End of a file */