ccc3496666333caa414d96527c851e0dbfef8fb3
[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 = calloc (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 = realloc (buf, size);
81                if (fgets (buf + o, size - o, fp) == NULL)
82                     break;
83           }
84           
85           if(!(count % 50))
86                raw = realloc(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 = realloc(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(char *line)
104 {
105      char *new; 
106      char *dest, *src, *end;
107     
108      new = calloc(1, strlen(line) + 1);
109      if ( new == NULL ){
110         fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
111         return NULL;
112      }
113      dest = new, src = line, end = line + (strlen(line) - 1);
114
115      /* remove it from the front */    
116      while(src && 
117            isspace(*src) &&
118            *src)
119           src++;
120      /* and now from the back */
121      while((end > src) &&
122            isspace(*end))
123           end--;
124      end++;
125      *end = '\0';
126      strcpy(new, src);
127      /* this does from the first space
128       *  blasting away any versions stuff in depends
129       while(src && 
130       !isspace(*src) &&
131       *src)
132       *dest++ = *src++;
133       *dest = '\0';
134       */
135     
136      return new;
137 }
138
139 int line_is_blank(const char *line)
140 {
141      const char *s;
142
143      for (s = line; *s; s++) {
144           if (!isspace(*s))
145                return 0;
146      }
147      return 1;
148 }
149
150 static struct errlist *error_list_head, *error_list_tail;
151
152 /*
153  * XXX: this function should not allocate memory as it may be called to
154  *      print an error because we are out of memory.
155  */
156 void push_error_list(char * msg)
157 {
158         struct errlist *e;
159
160         e = calloc(1,  sizeof(struct errlist));
161         if (e == NULL) {
162                 fprintf(stderr, "%s: calloc: %s\n",
163                                 __FUNCTION__, strerror(errno));
164                 return;
165         }
166
167         e->errmsg = xstrdup(msg);
168         e->next = NULL;
169
170         if (error_list_head) {
171                 error_list_tail->next = e;
172                 error_list_tail = e;
173         } else {
174                 error_list_head = error_list_tail = e;
175         }
176 }
177
178 void free_error_list(void)
179 {
180         struct errlist *err, *err_tmp;
181
182         err = error_list_head;
183         while (err != NULL) {
184                 free(err->errmsg);
185                 err_tmp = err;
186                 err = err->next;
187                 free(err_tmp);
188         }
189 }
190
191 void print_error_list (void)
192 {
193         struct errlist *err = error_list_head;
194
195         if (err) {
196                 printf ("Collected errors:\n");
197                 /* Here we print the errors collected and free the list */
198                 while (err != NULL) {
199                         printf (" * %s", err->errmsg);
200                         err = err->next;
201                 }
202         }
203 }