X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libopkg%2Fopkg_utils.c;h=d39690eeda1e8d3f667f83c492144eeaaa633337;hb=df095ad14d4217ab3223862f3a6b9a2134c709b2;hp=6a827e063b65ee7c57316874eb346a55b33082b5;hpb=733d8409723a397b4ced39ea7aaab8bc8b4af5d9;p=oweals%2Fopkg-lede.git diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index 6a827e0..d39690e 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -18,113 +18,59 @@ #include "includes.h" #include #include -#include +#include #include "opkg_utils.h" #include "pkg.h" #include "pkg_hash.h" +#include "libbb/libbb.h" void print_pkg_status(pkg_t * pkg, FILE * file); -int get_available_blocks(char * filesystem) +unsigned long +get_available_kbytes(char * filesystem) { - struct statfs sfs; + struct statvfs f; - if(statfs(filesystem, &sfs)){ - fprintf(stderr, "bad statfs\n"); - return 0; - } - /* fprintf(stderr, "reported fs type %x\n", sfs.f_type); */ - return ((sfs.f_bavail * sfs.f_bsize) / 1024); -} - -char **read_raw_pkgs_from_file(const char *file_name) -{ - FILE *fp; - char **ret; - - if(!(fp = fopen(file_name, "r"))){ - fprintf(stderr, "can't get %s open for read\n", file_name); - return NULL; - } - - ret = read_raw_pkgs_from_stream(fp); - - fclose(fp); + if (statvfs(filesystem, &f) == -1) { + opkg_perror(ERROR, "Failed to statvfs for %s", filesystem); + return 0; + } - return ret; -} + // Actually ((sfs.f_bavail * sfs.f_frsize) / 1024) + // and here we try to avoid overflow. + if (f.f_frsize >= 1024) + return (f.f_bavail * (f.f_frsize / 1024)); + else if (f.f_frsize > 0) + return f.f_bavail / (1024 / f.f_frsize); -char **read_raw_pkgs_from_stream(FILE *fp) -{ - char **raw = NULL, *buf, *scout; - int count = 0; - size_t size = 512; - - buf = calloc (1, size); - - while (fgets(buf, size, fp)) { - while (strlen (buf) == (size - 1) - && buf[size-2] != '\n') { - size_t o = size - 1; - size *= 2; - buf = realloc (buf, size); - if (fgets (buf + o, size - o, fp) == NULL) - break; - } - - if(!(count % 50)) - raw = realloc(raw, (count + 50) * sizeof(char *)); - - if((scout = strchr(buf, '\n'))) - *scout = '\0'; - - raw[count++] = strdup(buf); - } - - raw = realloc(raw, (count + 1) * sizeof(char *)); - raw[count] = NULL; + opkg_msg(ERROR, "Unknown block size for target filesystem.\n"); - free (buf); - - return raw; + return 0; } /* something to remove whitespace, a hash pooper */ -char *trim_alloc(char *line) +char *trim_xstrdup(const char *src) { - char *new; - char *dest, *src, *end; - - new = calloc(1, strlen(line) + 1); - if ( new == NULL ){ - fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__); - return NULL; - } - dest = new, src = line, end = line + (strlen(line) - 1); + 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++; - *end = '\0'; - strcpy(new, src); - /* this does from the first space - * blasting away any versions stuff in depends - while(src && - !isspace(*src) && - *src) - *dest++ = *src++; - *dest = '\0'; - */ - - return new; + + /* xstrndup will NULL terminate for us */ + return xstrndup(src, end-src); } int line_is_blank(const char *line) @@ -137,47 +83,3 @@ int line_is_blank(const char *line) } return 1; } - -void push_error_list(struct errlist ** errors, char * msg){ - struct errlist *err_lst_tmp; - - - err_lst_tmp = calloc (1, sizeof (err_lst_tmp) ); - err_lst_tmp->errmsg=strdup(msg) ; - err_lst_tmp->next = *errors; - *errors = err_lst_tmp; -} - - -void reverse_error_list(struct errlist **errors){ - struct errlist *result=NULL; - struct errlist *current= *errors; - struct errlist *next; - - while ( current != NULL ) { - next = current->next; - current->next=result; - result=current; - current=next; - } - *errors=result; - -} - - -void free_error_list(){ -struct errlist *err_tmp_lst; - - err_tmp_lst = error_list; - - while (err_tmp_lst != NULL) { - free(err_tmp_lst->errmsg); - err_tmp_lst = error_list->next; - free(error_list); - error_list = err_tmp_lst; - } - - -} - -