Add some error checking on fputs.
[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     if (fputs(buffer, stream) == EOF) {
41         free(buffer);
42         return EINVAL;
43     }
44
45     free(buffer);
46
47     return 0;
48 }
49
50 int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
51 {
52     return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
53 }
54
55 int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
56                                                  const char *dir,
57                                                  const char *prefix)
58 {
59     char *dir_with_prefix;
60     char *buffer = NULL;
61
62     sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
63
64     buffer = deb_extract(pkg->local_filename, stderr,
65                 extract_control_tar_gz
66                 | extract_all_to_fs| extract_preserve_date
67                 | extract_unconditional,
68                 dir_with_prefix, NULL);
69
70     free(dir_with_prefix);
71
72     /* BUG: How do we know if deb_extract worked or not? This is a
73        defect in the current deb_extract from what I can tell. */
74
75     if (buffer) {
76         free(buffer);
77     }
78     return 0;
79 }
80
81 int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
82 {
83     char *buffer = NULL;
84     buffer = deb_extract(pkg->local_filename, stderr,
85                 extract_data_tar_gz
86                 | extract_all_to_fs| extract_preserve_date
87                 | extract_unconditional,
88                 dir, NULL);
89
90     /* BUG: How do we know if deb_extract worked or not? This is a
91        defect in the current deb_extract from what I can tell. */
92
93     if (buffer) {
94         free(buffer);
95     }
96     return 0;
97 }
98
99 int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
100 {
101     char *buffer = NULL;
102     /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the
103        data file names with a '.' as the first character. I've taught
104        opkg how to cope with the presence or absence of the '.', but
105        this may trip up dpkg.
106
107        For all I know, this could actually be a bug in opkg-build. So,
108        I'll have to try installing some .debs and comparing the *.list
109        files.
110
111        If we wanted to, we could workaround the deb_extract behavior
112        right here, by writing to a tmpfile, then munging things as we
113        wrote to the actual stream. */
114     buffer = deb_extract(pkg->local_filename, file,
115                  extract_quiet | extract_data_tar_gz | extract_list,
116                  NULL, NULL);
117
118     /* BUG: How do we know if deb_extract worked or not? This is a
119        defect in the current deb_extract from what I can tell. */
120     if (buffer) {
121         free(buffer);
122     }
123     return 0;
124 }