Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=74d8c870b17591bb028b6215a3241d7ac08ca019
commit 74d8c870b17591bb028b6215a3241d7ac08ca019 Author: James Buren <[email protected]> Date: Wed Sep 12 10:34:08 2012 -0500 add initial code for reading device stats diff --git a/src/block.c b/src/block.c new file mode 100644 index 0000000..58de7ef --- /dev/null +++ b/src/block.c @@ -0,0 +1,91 @@ +#include "local.h" + +struct device +{ + char *path; + long long size; + long long sectorsize; + long long alignment; + long long sectors; +}; + +extern struct device *device_open(const char *path) +{ + int fd = -1; + struct stat st = {0}; + long long size = 0; + long long sectorsize = 0; + long long alignment = 0; + long long sectors = 0; + struct device *device = 0; + + if(path == 0) + { + errno = EINVAL; + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + goto bail; + } + + if((fd = open(path,O_RDONLY)) == -1 || fstat(fd,&st) == -1) + { + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + goto bail; + } + + if(S_ISBLK(st.st_mode) && (ioctl(fd,BLKGETSIZE64,&size) == -1 || ioctl(fd,BLKSSZGET,§orsize) == -1)) + { + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + goto bail; + } + else if(S_ISREG(st.st_mode)) + { + size = st.st_size; + sectorsize = 512; + } + else + { + errno = EINVAL; + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + goto bail; + } + + alignment = MEBIBYTE / sectorsize; + + sectors = size / sectorsize; + + if(size <= 0 || sectorsize <= 0 || (MEBIBYTE % sectorsize) != 0 || (size % sectorsize) != 0) + { + errno = ERANGE; + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + goto bail; + } + + device = malloc0(sizeof(struct device)); + + device->path = strdup(path); + + device->size = size; + + device->sectorsize = sectorsize; + + device->alignment = alignment; + + device->sectors = sectors; + +bail: + + if(fd != -1) + close(fd); + + return device; +} + +extern void device_close(struct device *device) +{ + if(device == 0) + return; + + free(device->path); + + free(device); +} diff --git a/src/local.h b/src/local.h index a31ed03..f7ddaab 100644 --- a/src/local.h +++ b/src/local.h @@ -28,6 +28,8 @@ struct global bool netinstall; }; +struct device; + struct install { char *name; @@ -83,6 +85,8 @@ static inline long maxv(long *v,size_t size) return i; } +extern struct device *device_open(const char *path); +extern void device_close(struct device *device); extern int ui_main(int argc,char **argv); extern void ui_dialog_text(const char *title,const char *text); extern bool ui_dialog_yesno(const char *title,const char *text,bool defaultno); _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
