Put the tmp file under conf->tmp_dir.
[oweals/opkg-lede.git] / libopkg / pkg.c
index 78b6cadba1abef5c032fc5e15ea2b2a72df8737a..5d705c7bb147c0d436ff611a61c4deb57821b434 100644 (file)
@@ -315,26 +315,48 @@ void pkg_deinit(pkg_t *pkg)
        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 */
@@ -969,9 +991,12 @@ pkg_version_str_alloc(pkg_t *pkg)
        return version;
 }
 
+/*
+ * XXX: this should be broken into two functions
+ */
 str_list_t *pkg_get_installed_files(opkg_conf_t *conf, pkg_t *pkg)
 {
-     int err;
+     int err, fd;
      char *list_file_name = NULL;
      FILE *list_file = NULL;
      char *line;
@@ -997,27 +1022,42 @@ str_list_t *pkg_get_installed_files(opkg_conf_t *conf, pkg_t *pkg)
             file. In other words, change deb_extract so that it can
             simply return the file list as a char *[] rather than
             insisting on writing in to a FILE * as it does now. */
-         list_file = tmpfile();
+         sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
+                                         conf->tmp_dir, pkg->name);
+         fd = mkstemp(list_file_name);
+         if (fd == -1) {
+              opkg_message(conf, OPKG_ERROR, "%s: mkstemp(%s): %s",
+                              __FUNCTION__, list_file_name, strerror(errno));
+              free(list_file_name);
+              return pkg->installed_files;
+         }
+         list_file = fdopen(fd, "rw");
+         if (list_file == NULL) {
+              opkg_message(conf, OPKG_ERROR, "%s: fdopen: %s",
+                              __FUNCTION__, strerror(errno));
+              close(fd);
+              unlink(list_file_name);
+              free(list_file_name);
+              return pkg->installed_files;
+         }
          err = pkg_extract_data_file_names_to_stream(pkg, list_file);
          if (err) {
+              opkg_message(conf, OPKG_ERROR, "%s: Error extracting file list "
+                              "from %s: %s\n", __FUNCTION__,
+                              pkg->local_filename, strerror(err));
               fclose(list_file);
-              fprintf(stderr, "%s: Error extracting file list from %s: %s\n",
-                      __FUNCTION__, pkg->local_filename, strerror(err));
+              unlink(list_file_name);
+              free(list_file_name);
               return pkg->installed_files;
          }
          rewind(list_file);
      } else {
          sprintf_alloc(&list_file_name, "%s/%s.list",
                        pkg->dest->info_dir, pkg->name);
-         if (! file_exists(list_file_name)) {
-              free(list_file_name);
-              return pkg->installed_files;
-         }
-
          list_file = fopen(list_file_name, "r");
          if (list_file == NULL) {
-              fprintf(stderr, "WARNING: Cannot open %s: %s\n",
-                      list_file_name, strerror(errno));
+              opkg_message(conf, OPKG_ERROR, "%s: fopen(%s): %s\n",
+                      __FUNCTION__, list_file_name, strerror(errno));
               free(list_file_name);
               return pkg->installed_files;
          }
@@ -1037,7 +1077,7 @@ str_list_t *pkg_get_installed_files(opkg_conf_t *conf, pkg_t *pkg)
          str_chomp(line);
          file_name = line;
 
-         if (pkg->state_status == SS_NOT_INSTALLED) {
+         if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
               if (*file_name == '.') {
                    file_name++;
               }
@@ -1063,6 +1103,11 @@ str_list_t *pkg_get_installed_files(opkg_conf_t *conf, pkg_t *pkg)
 
      fclose(list_file);
 
+     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
+         unlink(list_file_name);
+         free(list_file_name);
+     }
+
      return pkg->installed_files;
 }
 
@@ -1195,8 +1240,10 @@ int pkg_run_script(opkg_conf_t *conf, pkg_t *pkg,
 
      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) {