Skip to content

Commit

Permalink
added duc_get_size() to convert struct duc_size to off_t depending on…
Browse files Browse the repository at this point in the history
… given duc_size_type
  • Loading branch information
zevv committed Feb 23, 2016
1 parent c2cd6da commit b37a8b1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/duc/cmd-ls.c
Expand Up @@ -72,7 +72,7 @@ static void ls_one(duc_dir *dir, int level, int *prefix)
struct duc_dirent *e;
while( (e = duc_dir_read(dir, st)) != NULL) {

off_t size = opt_apparent ? e->size.apparent : e->size.actual;
off_t size = duc_get_size(&e->size, st);

if(size > max_size) max_size = size;
size_t l = strlen(e->name);
Expand All @@ -93,7 +93,7 @@ static void ls_one(duc_dir *dir, int level, int *prefix)

if(opt_dirs_only && e->type != DUC_FILE_TYPE_DIR) continue;

off_t size = opt_apparent ? e->size.apparent : e->size.actual;
off_t size = duc_get_size(&e->size, st);

if(opt_recursive) {
if(n == 0) prefix[level] = 1;
Expand Down
18 changes: 9 additions & 9 deletions src/duc/cmd-ui.c
Expand Up @@ -89,10 +89,7 @@ static duc_dir *do_dir(duc_dir *dir, int depth)
off_t size_max = 1;
struct duc_dirent *e;
while( (e = duc_dir_read(dir, st)) != NULL) {
off_t size;
if(st == DUC_SIZE_TYPE_COUNT) size = e->size.count;
if(st == DUC_SIZE_TYPE_APPARENT) size = e->size.apparent;
if(st == DUC_SIZE_TYPE_ACTUAL) size = e->size.actual;
off_t size = duc_get_size(&e->size, st);
if(size > size_max) size_max = size;
}

Expand Down Expand Up @@ -127,7 +124,13 @@ static duc_dir *do_dir(duc_dir *dir, int depth)
duc_human_number(count, opt_bytes, cnt, sizeof cnt);
attrset(attr_bar);
mvhline(rows-1, 0, ' ', cols);
mvprintw(rows-1, 0, " Total %sB in %s files/directories", siz, cnt);
mvprintw(rows-1, 0, " Total %sB in %s files/directories (", siz, cnt);
switch(st) {
case DUC_SIZE_TYPE_APPARENT: printw("apparent size"); break;
case DUC_SIZE_TYPE_ACTUAL: printw("actual size"); break;
case DUC_SIZE_TYPE_COUNT: printw("file count"); break;
}
printw(")");
attrset(0);

/* Draw dirents */
Expand All @@ -146,10 +149,7 @@ static duc_dir *do_dir(duc_dir *dir, int depth)

if(e) {

off_t size;
if(st == DUC_SIZE_TYPE_COUNT) size = e->size.count;
if(st == DUC_SIZE_TYPE_APPARENT) size = e->size.apparent;
if(st == DUC_SIZE_TYPE_ACTUAL) size = e->size.actual;
off_t size = duc_get_size(&e->size, st);;

size_t max_size_len = opt_bytes ? 12 : 7;

Expand Down
6 changes: 3 additions & 3 deletions src/libduc-graph/graph.c
Expand Up @@ -210,7 +210,7 @@ static int do_dir(duc_graph *g, duc_dir *dir, int level, double r1, double a1_di
} else {
duc_dir_get_size(dir, &tmp);
}
size_total = g->size_type == DUC_SIZE_TYPE_APPARENT ? tmp.apparent : tmp.actual;
size_total = duc_get_size(&tmp, g->size_type);
if(size_total == 0) return 0;

struct duc_dirent *e;
Expand All @@ -219,7 +219,7 @@ static int do_dir(duc_graph *g, duc_dir *dir, int level, double r1, double a1_di
off_t size_max = 0;

while( (e = duc_dir_read(dir, g->size_type)) != NULL) {
off_t size = (g->size_type == DUC_SIZE_TYPE_APPARENT) ? e->size.apparent : e->size.actual;
off_t size = duc_get_size(&e->size, g->size_type);
if(size < size_min) size_min = size;
if(size > size_max) size_max = size;
}
Expand All @@ -231,7 +231,7 @@ static int do_dir(duc_graph *g, duc_dir *dir, int level, double r1, double a1_di

/* size_rel is size relative to total, size_nrel is size relative to min and max */

off_t size = (g->size_type == DUC_SIZE_TYPE_APPARENT) ? e->size.apparent : e->size.actual;
off_t size = duc_get_size(&e->size, g->size_type);

double size_rel = (double)size / size_total;
double size_nrel = (size_max - size_min) ? ((double)size - size_min) / (size_max - size_min) : 1;
Expand Down
14 changes: 14 additions & 0 deletions src/libduc/duc.c
Expand Up @@ -219,6 +219,20 @@ int duc_human_duration(struct timeval start, struct timeval stop, char *buf, siz
}


off_t duc_get_size(struct duc_size *size, duc_size_type st)
{
switch(st) {
case DUC_SIZE_TYPE_APPARENT:
return size->apparent;
case DUC_SIZE_TYPE_ACTUAL:
return size->actual;
case DUC_SIZE_TYPE_COUNT:
return size->count;
}
return 0;
}


void *duc_malloc(size_t s)
{
void *p = malloc(s);
Expand Down
1 change: 1 addition & 0 deletions src/libduc/duc.h
Expand Up @@ -165,6 +165,7 @@ int duc_dir_close(duc_dir *dir);
* Helper functions
*/

off_t duc_get_size(struct duc_size *size, duc_size_type st);
int duc_human_number(double v, int exact, char *buf, size_t maxlen);
int duc_human_size(const struct duc_size *size, duc_size_type st, int exact, char *buf, size_t maxlen);
int duc_human_duration(struct timeval start, struct timeval end, char *buf, size_t maxlen);
Expand Down

0 comments on commit b37a8b1

Please sign in to comment.