e293197fa056d9963a81d54de3a367a61ec4f59f
[oweals/opkg-lede.git] / libopkg / opkg_utils.c
1 /* opkg_utils.c - the opkg package management system
2
3    Steven M. Ayer
4    
5    Copyright (C) 2002 Compaq Computer Corporation
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 #include <ctype.h>
21 #include <sys/vfs.h>
22
23 #include "opkg_utils.h"
24 #include "pkg.h"
25 #include "pkg_hash.h"
26 #include "libbb/libbb.h"
27
28 void print_pkg_status(pkg_t * pkg, FILE * file);
29
30 long unsigned int get_available_blocks(char * filesystem)
31 {
32     struct statfs sfs;
33
34     if(statfs(filesystem, &sfs)){
35         fprintf(stderr, "bad statfs\n");
36         return 0;
37     }
38     /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
39
40     // Actually ((sfs.f_bavail * sfs.f_bsize) / 1024) 
41     // and here we try to avoid overflow. 
42     if (sfs.f_bsize >= 1024) 
43         return (sfs.f_bavail * (sfs.f_bsize / 1024));
44     else if (sfs.f_bsize > 0)
45         return sfs.f_bavail / (1024 / sfs.f_bsize);
46     fprintf(stderr, "bad statfs f_bsize == 0\n");
47     return 0;
48 }
49
50 char **read_raw_pkgs_from_file(const char *file_name)
51 {
52      FILE *fp; 
53      char **ret;
54     
55      if(!(fp = fopen(file_name, "r"))){
56           fprintf(stderr, "can't get %s open for read\n", file_name);
57           return NULL;
58      }
59
60      ret = read_raw_pkgs_from_stream(fp);
61
62      fclose(fp);
63
64      return ret;
65 }
66
67 char **read_raw_pkgs_from_stream(FILE *fp)
68 {    
69      char **raw = NULL, *buf, *scout;
70      int count = 0;
71      size_t size = 512;
72      
73      buf = xcalloc(1, size);
74
75      while (fgets(buf, size, fp)) {
76           while (strlen (buf) == (size - 1)
77                  && buf[size-2] != '\n') {
78                size_t o = size - 1;
79                size *= 2;
80                buf = xrealloc (buf, size);
81                if (fgets (buf + o, size - o, fp) == NULL)
82                     break;
83           }
84           
85           if(!(count % 50))
86                raw = xrealloc(raw, (count + 50) * sizeof(char *));
87         
88           if((scout = strchr(buf, '\n')))
89                *scout = '\0';
90
91           raw[count++] = xstrdup(buf);
92      }
93     
94      raw = xrealloc(raw, (count + 1) * sizeof(char *));
95      raw[count] = NULL;
96
97      free (buf);
98     
99      return raw;
100 }
101
102 /* something to remove whitespace, a hash pooper */
103 char *trim_alloc(const char *src)
104 {
105      const char *end;
106
107      /* remove it from the front */    
108      while(src && 
109            isspace(*src) &&
110            *src)
111           src++;
112
113      end = src + (strlen(src) - 1);
114
115      /* and now from the back */
116      while((end > src) &&
117            isspace(*end))
118           end--;
119
120      end++;
121
122      /* xstrndup will NULL terminate for us */
123      return xstrndup(src, end-src);
124 }
125
126 int line_is_blank(const char *line)
127 {
128      const char *s;
129
130      for (s = line; *s; s++) {
131           if (!isspace(*s))
132                return 0;
133      }
134      return 1;
135 }
136
137 static struct errlist *error_list_head, *error_list_tail;
138
139 /*
140  * XXX: this function should not allocate memory as it may be called to
141  *      print an error because we are out of memory.
142  */
143 void push_error_list(char * msg)
144 {
145         struct errlist *e;
146
147         e = xcalloc(1,  sizeof(struct errlist));
148         e->errmsg = xstrdup(msg);
149         e->next = NULL;
150
151         if (error_list_head) {
152                 error_list_tail->next = e;
153                 error_list_tail = e;
154         } else {
155                 error_list_head = error_list_tail = e;
156         }
157 }
158
159 void free_error_list(void)
160 {
161         struct errlist *err, *err_tmp;
162
163         err = error_list_head;
164         while (err != NULL) {
165                 free(err->errmsg);
166                 err_tmp = err;
167                 err = err->next;
168                 free(err_tmp);
169         }
170 }
171
172 void print_error_list (void)
173 {
174         struct errlist *err = error_list_head;
175
176         if (err) {
177                 printf ("Collected errors:\n");
178                 /* Here we print the errors collected and free the list */
179                 while (err != NULL) {
180                         printf (" * %s", err->errmsg);
181                         err = err->next;
182                 }
183         }
184 }