opkg: remove opkg.h in preperation for new API
[project/opkg-lede.git] / libopkg / pkg_extract.c
1 /* pkg_extract.c - the itsy 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 /* XXX: QUESTION: Is there a way to do this directly with deb_extract now? */
41 fputs(buffer, stream);
42 free(buffer);
43
44 return 0;
45 }
46
47 int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
48 {
49 return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
50 }
51
52 int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
53 const char *dir,
54 const char *prefix)
55 {
56 char *dir_with_prefix;
57
58 sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
59
60 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 /* XXX: BUG: how do we know if deb_extract worked or not? This is
69 a defect in the current deb_extract from what I can tell.
70
71 Once this is fixed, audit all calls to deb_extract. */
72 return 0;
73 }
74
75 int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
76 {
77 deb_extract(pkg->local_filename, stderr,
78 extract_data_tar_gz
79 | extract_all_to_fs| extract_preserve_date
80 | extract_unconditional,
81 dir, NULL);
82
83 /* BUG: How do we know if deb_extract worked or not? This is a
84 defect in the current deb_extract from what I can tell. */
85 return 0;
86 }
87
88 int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
89 {
90 int err=0;
91 char *line, *data_file;
92 FILE *file;
93 FILE *tmp;
94
95 file = fopen(file_name, "w");
96 if (file == NULL) {
97 fprintf(stderr, "%s: ERROR: Failed to open %s for writing.\n",
98 __FUNCTION__, file_name);
99 return EINVAL;
100 }
101
102 tmp = tmpfile();
103 if (pkg->installed_files) {
104 str_list_elt_t *elt;
105 for (elt = pkg->installed_files->head; elt; elt = elt->next) {
106 fprintf(file, "%s\n", elt->data);
107 }
108 } else {
109 err = pkg_extract_data_file_names_to_stream(pkg, tmp);
110 if (err) {
111 fclose(file);
112 fclose(tmp);
113 return err;
114 }
115
116 /* Fixup data file names by removing the initial '.' */
117 rewind(tmp);
118 while (1) {
119 line = file_read_line_alloc(tmp);
120 if (line == NULL) {
121 break;
122 }
123
124 data_file = line;
125 if (*data_file == '.') {
126 data_file++;
127 }
128
129 if (*data_file != '/') {
130 fputs("/", file);
131 }
132
133 /* I have no idea why, but this is what dpkg does */
134 if (strcmp(data_file, "/\n") == 0) {
135 fputs("/.\n", file);
136 } else {
137 fputs(data_file, file);
138 }
139 }
140 }
141 fclose(tmp);
142 fclose(file);
143
144 return err;
145 }
146
147 int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
148 {
149 /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the
150 data file names with a '.' as the first character. I've taught
151 opkg how to cope with the presence or absence of the '.', but
152 this may trip up dpkg.
153
154 For all I know, this could actually be a bug in opkg-build. So,
155 I'll have to try installing some .debs and comparing the *.list
156 files.
157
158 If we wanted to, we could workaround the deb_extract behavior
159 right here, by writing to a tmpfile, then munging things as we
160 wrote to the actual stream. */
161 deb_extract(pkg->local_filename, file,
162 extract_quiet | extract_data_tar_gz | extract_list,
163 NULL, NULL);
164
165 /* BUG: How do we know if deb_extract worked or not? This is a
166 defect in the current deb_extract from what I can tell. */
167 return 0;
168 }