summaryrefslogtreecommitdiffstats
path: root/libopkg/opkg_utils.c
blob: 2bb7c2e476e411d174de146823c6fccb8c986d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* opkg_utils.c - the opkg package management system

   Steven M. Ayer
   
   Copyright (C) 2002 Compaq Computer Corporation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include "includes.h"
#include <errno.h>
#include <ctype.h>
#include <sys/vfs.h>

#include "opkg_utils.h"
#include "pkg.h"
#include "pkg_hash.h"
#include "libbb/libbb.h"

void print_pkg_status(pkg_t * pkg, FILE * file);

long unsigned int get_available_blocks(char * filesystem)
{
    struct statfs sfs;

    if(statfs(filesystem, &sfs)){
        fprintf(stderr, "bad statfs\n");
        return 0;
    }
    /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */

    // Actually ((sfs.f_bavail * sfs.f_bsize) / 1024) 
    // and here we try to avoid overflow. 
    if (sfs.f_bsize >= 1024) 
        return (sfs.f_bavail * (sfs.f_bsize / 1024));
    else if (sfs.f_bsize > 0)
        return sfs.f_bavail / (1024 / sfs.f_bsize);
    fprintf(stderr, "bad statfs f_bsize == 0\n");
    return 0;
}

/* something to remove whitespace, a hash pooper */
char *trim_xstrdup(const char *src)
{
     const char *end;

     /* remove it from the front */    
     while(src && 
	   isspace(*src) &&
	   *src)
	  src++;

     end = src + (strlen(src) - 1);

     /* and now from the back */
     while((end > src) &&
	   isspace(*end))
	  end--;

     end++;

     /* xstrndup will NULL terminate for us */
     return xstrndup(src, end-src);
}

int line_is_blank(const char *line)
{
     const char *s;

     for (s = line; *s; s++) {
	  if (!isspace(*s))
	       return 0;
     }
     return 1;
}

static struct errlist *error_list_head, *error_list_tail;

/*
 * XXX: this function should not allocate memory as it may be called to
 *      print an error because we are out of memory.
 */
void push_error_list(char * msg)
{
	struct errlist *e;

	e = xcalloc(1,  sizeof(struct errlist));
	e->errmsg = xstrdup(msg);
	e->next = NULL;

	if (error_list_head) {
		error_list_tail->next = e;
		error_list_tail = e;
	} else {
		error_list_head = error_list_tail = e;
	}
}

void free_error_list(void)
{
	struct errlist *err, *err_tmp;

	err = error_list_head;
	while (err != NULL) {
		free(err->errmsg);
		err_tmp = err;
		err = err->next;
		free(err_tmp);
	}
}

void print_error_list (void)
{
	struct errlist *err = error_list_head;

	if (err) {
		printf ("Collected errors:\n");
		/* Here we print the errors collected and free the list */
		while (err != NULL) {
			printf (" * %s", err->errmsg);
			err = err->next;
		}
	}
}