reduce a big memory leak
[oweals/opkg-lede.git] / libopkg / pkg_extract.c
1 /* pkg_extract.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include <errno.h>
20
21 #include "pkg_extract.h"
22
23 #include "libbb/libbb.h"
24 #include "file_util.h"
25 #include "sprintf_alloc.h"
26
27 /* assuage libb functions */
28 const char *applet_name = "opkg";
29
30 int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
31 {
32     char *buffer = deb_extract(pkg->local_filename, stderr,
33                                extract_control_tar_gz
34                                | extract_one_to_buffer,
35                                NULL, "./control");
36     if (buffer == NULL) {
37         return EINVAL;
38     }
39
40     fputs(buffer, stream);
41     free(buffer);
42
43     return 0;
44 }
45
46 int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
47 {
48     return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
49 }
50
51 int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
52                                                  const char *dir,
53                                                  const char *prefix)
54 {
55     char *dir_with_prefix;
56     char *buffer = NULL;
57
58     sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
59
60     buffer = deb_extract(pkg->local_filename, stderr,
61                 extract_control_tar_gz
62                 | extract_all_to_fs| extract_preserve_date
63                 | extract_unconditional,
64                 dir_with_prefix, NULL);
65
66     free(dir_with_prefix);
67
68     if (buffer == NULL) {
69         return EINVAL;
70     }
71     free(buffer);
72     return 0;
73 }
74
75 int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
76 {
77     char *buffer = NULL;
78     buffer = deb_extract(pkg->local_filename, stderr,
79                 extract_data_tar_gz
80                 | extract_all_to_fs| extract_preserve_date
81                 | extract_unconditional,
82                 dir, NULL);
83
84     if (buffer == NULL) {
85         return EINVAL;
86     }
87     free(buffer);
88     return 0;
89 }
90
91 int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
92 {
93      int err=0;
94      char *line, *data_file;
95      FILE *file;
96      FILE *tmp;
97
98      file = fopen(file_name, "w");
99      if (file == NULL) {
100           fprintf(stderr, "%s: ERROR: Failed to open %s for writing.\n",
101                   __FUNCTION__, file_name);
102           return EINVAL;
103      }
104
105      tmp = tmpfile();
106      if (pkg->installed_files) {
107           str_list_elt_t *elt;
108           for (elt = str_list_first(pkg->installed_files); elt; elt = str_list_next(pkg->installed_files, elt)) {
109                fprintf(file, "%s\n", (char *)elt->data);
110           }
111      } else {
112           err = pkg_extract_data_file_names_to_stream(pkg, tmp);
113           if (err) {
114                fclose(file);
115                fclose(tmp);
116                return err;
117           }
118
119           /* Fixup data file names by removing the initial '.' */
120           rewind(tmp);
121           while (1) {
122                line = file_read_line_alloc(tmp);
123                if (line == NULL) {
124                     break;
125                }
126
127                data_file = line;
128                if (*data_file == '.') {
129                     data_file++;
130                }
131
132                if (*data_file != '/') {
133                     fputs("/", file);
134                }
135
136                /* I have no idea why, but this is what dpkg does */
137                if (strcmp(data_file, "/\n") == 0) {
138                     fputs("/.\n", file);
139                } else {
140                     fputs(data_file, file);
141                }
142                free(line);
143                line=NULL;
144           }
145      }
146      fclose(tmp);
147      fclose(file);
148
149      return err;
150 }
151
152 int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
153 {
154     char *buffer = NULL;
155     /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the
156        data file names with a '.' as the first character. I've taught
157        opkg how to cope with the presence or absence of the '.', but
158        this may trip up dpkg.
159
160        For all I know, this could actually be a bug in opkg-build. So,
161        I'll have to try installing some .debs and comparing the *.list
162        files.
163
164        If we wanted to, we could workaround the deb_extract behavior
165        right here, by writing to a tmpfile, then munging things as we
166        wrote to the actual stream. */
167     buffer = deb_extract(pkg->local_filename, file,
168                  extract_quiet | extract_data_tar_gz | extract_list,
169                  NULL, NULL);
170
171     /* BUG: How do we know if deb_extract worked or not? This is a
172        defect in the current deb_extract from what I can tell. */
173     if (buffer == NULL) {
174         return EINVAL;
175     }
176     free(buffer);
177     return 0;
178 }