Use the filename arg as a base for the temp file. Clean up function while here.
[oweals/opkg-lede.git] / libopkg / pkg.c
index 1802ff0f5f2bd41736a583ad7a3f22591f44234d..0c0a7d9e8127e2360b753416a83e3712e47a9f0f 100644 (file)
@@ -315,26 +315,48 @@ void pkg_deinit(pkg_t *pkg)
        pkg->tags = NULL;
 }
 
        pkg->tags = NULL;
 }
 
-int pkg_init_from_file(pkg_t *pkg, const char *filename)
+int
+pkg_init_from_file(pkg_t *pkg, const char *filename)
 {
 {
-     int err;
-     FILE *control_file;
+       int fd, err = 0;
+       FILE *control_file;
+       char *control_path;
 
 
-     err = pkg_init(pkg);
-     if (err) { return err; }
+       pkg_init(pkg);
 
 
-     pkg->local_filename = xstrdup(filename);
-    
-     control_file = tmpfile();
-     err = pkg_extract_control_file_to_stream(pkg, control_file);
-     if (err) { return err; }
+       pkg->local_filename = xstrdup(filename);
+
+       sprintf_alloc(&control_path, "%s.control.XXXXXX", filename);
+       fd = mkstemp(control_path);
+       if (fd == -1) {
+               perror_msg("%s: mkstemp(%s)", __FUNCTION__, control_path);
+               err = -1;
+               goto err0;
+       }
 
 
-     rewind(control_file);
-     pkg_parse_from_stream(pkg, control_file, PFM_ALL);
+       control_file = fdopen(fd, "rw");
+       if (control_file == NULL) {
+               perror_msg("%s: fdopen", __FUNCTION__, control_path);
+               close(fd);
+               err = -1;
+               goto err1;
+       }
 
 
-     fclose(control_file);
+       err = pkg_extract_control_file_to_stream(pkg, control_file);
+       if (err)
+               goto err2;
 
 
-     return 0;
+       rewind(control_file);
+       pkg_parse_from_stream(pkg, control_file, PFM_ALL);
+
+err2:
+       fclose(control_file);
+err1:
+       unlink(control_path);
+err0:
+       free(control_path);
+
+       return err;
 }
 
 /* Merge any new information in newpkg into oldpkg */
 }
 
 /* Merge any new information in newpkg into oldpkg */
@@ -969,14 +991,14 @@ pkg_version_str_alloc(pkg_t *pkg)
        return version;
 }
 
        return version;
 }
 
-str_list_t *pkg_get_installed_files(pkg_t *pkg)
+str_list_t *pkg_get_installed_files(opkg_conf_t *conf, pkg_t *pkg)
 {
      int err;
      char *list_file_name = NULL;
      FILE *list_file = NULL;
      char *line;
      char *installed_file_name;
 {
      int err;
      char *list_file_name = NULL;
      FILE *list_file = NULL;
      char *line;
      char *installed_file_name;
-     int rootdirlen;
+     int rootdirlen = 0;
 
      pkg->installed_files_ref_cnt++;
 
 
      pkg->installed_files_ref_cnt++;
 
@@ -1024,7 +1046,9 @@ str_list_t *pkg_get_installed_files(pkg_t *pkg)
          free(list_file_name);
      }
 
          free(list_file_name);
      }
 
-     rootdirlen = strlen( pkg->dest->root_dir );
+     if (conf->offline_root)
+          rootdirlen = strlen(conf->offline_root);
+
      while (1) {
          char *file_name;
        
      while (1) {
          char *file_name;
        
@@ -1035,22 +1059,24 @@ str_list_t *pkg_get_installed_files(pkg_t *pkg)
          str_chomp(line);
          file_name = line;
 
          str_chomp(line);
          file_name = line;
 
-         /* Take pains to avoid uglies like "/./" in the middle of file_name. */
-         if( strncmp( pkg->dest->root_dir, 
-                      file_name, 
-                      rootdirlen ) ) {
+         if (pkg->state_status == SS_NOT_INSTALLED) {
               if (*file_name == '.') {
                    file_name++;
               }
               if (*file_name == '/') {
                    file_name++;
               }
               if (*file_name == '.') {
                    file_name++;
               }
               if (*file_name == '/') {
                    file_name++;
               }
-
-              /* Freed in pkg_free_installed_files */
-              sprintf_alloc(&installed_file_name, "%s%s", pkg->dest->root_dir, file_name);
+              sprintf_alloc(&installed_file_name, "%s%s",
+                              pkg->dest->root_dir, file_name);
          } else {
          } else {
-              // already contains root_dir as header -> ABSOLUTE
-              sprintf_alloc(&installed_file_name, "%s", file_name);
+              if (conf->offline_root &&
+                      strncmp(conf->offline_root, file_name, rootdirlen)) {
+                   sprintf_alloc(&installed_file_name, "%s%s",
+                                   conf->offline_root, file_name);
+              } else {
+                   // already contains root_dir as header -> ABSOLUTE
+                   sprintf_alloc(&installed_file_name, "%s", file_name);
+              }
          }
          str_list_append(pkg->installed_files, installed_file_name);
           free(installed_file_name);
          }
          str_list_append(pkg->installed_files, installed_file_name);
           free(installed_file_name);
@@ -1191,8 +1217,10 @@ int pkg_run_script(opkg_conf_t *conf, pkg_t *pkg,
 
      sprintf_alloc(&cmd, "%s %s", path, args);
      free(path);
 
      sprintf_alloc(&cmd, "%s %s", path, args);
      free(path);
-
-     err = xsystem(cmd);
+     {
+         const char *argv[] = {"sh", "-c", cmd, NULL};
+         err = xsystem(argv);
+     }
      free(cmd);
 
      if (err) {
      free(cmd);
 
      if (err) {
@@ -1394,7 +1422,7 @@ int pkg_info_preinstall_check(opkg_conf_t *conf)
      pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
      for (i = 0; i < installed_pkgs->len; i++) {
          pkg_t *pkg = installed_pkgs->pkgs[i];
      pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
      for (i = 0; i < installed_pkgs->len; i++) {
          pkg_t *pkg = installed_pkgs->pkgs[i];
-         str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
+         str_list_t *installed_files = pkg_get_installed_files(conf, pkg); /* this causes installed_files to be cached */
          str_list_elt_t *iter, *niter;
          if (installed_files == NULL) {
               opkg_message(conf, OPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
          str_list_elt_t *iter, *niter;
          if (installed_files == NULL) {
               opkg_message(conf, OPKG_ERROR, "No installed files for pkg %s\n", pkg->name);