From 33c7e93565efbb0c07d9aa0f8b72ca737111f338 Mon Sep 17 00:00:00 2001 From: ticktock35 Date: Fri, 03 Apr 2009 04:21:44 -0400 Subject: Try to avoid long unsigned int overflow. When free size is more than 4G, long unsigned int may enounter overflow git-svn-id: http://opkg.googlecode.com/svn/trunk@210 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index 6a827e0..fb27b40 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -26,16 +26,24 @@ void print_pkg_status(pkg_t * pkg, FILE * file); -int get_available_blocks(char * filesystem) +long unsigned int get_available_blocks(char * filesystem) { - struct statfs sfs; + struct statfs sfs; - 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); + 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; } char **read_raw_pkgs_from_file(const char *file_name) diff --git a/libopkg/opkg_utils.h b/libopkg/opkg_utils.h index c3e90a9..226c7d1 100644 --- a/libopkg/opkg_utils.h +++ b/libopkg/opkg_utils.h @@ -25,7 +25,7 @@ void push_error_list(struct errlist **errors,char * msg); void reverse_error_list(struct errlist **errors); void free_error_list(); -int get_available_blocks(char * filesystem); +long unsigned int get_available_blocks(char * filesystem); char **read_raw_pkgs_from_file(const char *file_name); char **read_raw_pkgs_from_stream(FILE *fp); char *trim_alloc(char * line); -- cgit v0.9.1