X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Ffs.c;h=2152552b6be8243dc4246e3d4a4d5ad2810841ea;hb=2d2a163728283108eb78c11d7b02bf5c54d88ea4;hp=ce59be0999ffa241e6498b7d3b50996ff8670dac;hpb=be2cd44e6d271b8e0e5ec88ad5581fccf29ca94c;p=monky diff --git a/src/fs.c b/src/fs.c index ce59be0..2152552 100644 --- a/src/fs.c +++ b/src/fs.c @@ -1,4 +1,7 @@ -/* Conky, a system monitor, based on torsmo +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * Conky, a system monitor, based on torsmo * * Any original torsmo code is licensed under the BSD license * @@ -7,7 +10,7 @@ * Please see COPYING for details * * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen - * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al. + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. * (see AUTHORS) * All rights reserved. * @@ -23,12 +26,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * - * $Id$ */ + */ #include "conky.h" +#include "logging.h" +#include "fs.h" +#include "specials.h" +#include "text_object.h" +#include #include -#include -#include #include #include #include @@ -46,7 +52,7 @@ #include #endif -#ifndef HAVE_STRUCT_STATFS_F_FSTYPENAME +#if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__) #include #endif @@ -59,15 +65,21 @@ static void update_fs_stat(struct fs_stat *fs); void get_fs_type(const char *path, char *result); -void update_fs_stats(void) +int update_fs_stats(void) { unsigned i; + static double last_fs_update = 0.0; + + if (current_update_time - last_fs_update < 13) + return 0; for (i = 0; i < MAX_FS_STATS; ++i) { if (fs_stats[i].set) { update_fs_stat(&fs_stats[i]); } } + last_fs_update = current_update_time; + return 0; } void clear_fs_stats(void) @@ -95,7 +107,7 @@ struct fs_stat *prepare_fs_stat(const char *s) } /* new path */ if (!new) { - ERR("too many fs stats"); + NORM_ERR("too many fs stats"); return 0; } strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE); @@ -119,20 +131,20 @@ static void update_fs_stat(struct fs_stat *fs) fs->avail = 0; fs->free = 0; strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE); - ERR("statfs '%s': %s", fs->path, strerror(errno)); + NORM_ERR("statfs '%s': %s", fs->path, strerror(errno)); } } void get_fs_type(const char *path, char *result) { -#ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME +#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__) struct statfs s; if (statfs(path, &s) == 0) { strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE); } else { - ERR("statfs '%s': %s", path, strerror(errno)); + NORM_ERR("statfs '%s': %s", path, strerror(errno)); } return; @@ -145,7 +157,7 @@ void get_fs_type(const char *path, char *result) char *slash; if (mtab == NULL) { - ERR("setmntent /etc/mtab: %s", strerror(errno)); + NORM_ERR("setmntent /etc/mtab: %s", strerror(errno)); strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE); return; } @@ -162,13 +174,13 @@ void get_fs_type(const char *path, char *result) fseek(mtab, 0, SEEK_SET); slash = strrchr(search_path, '/'); if (slash == NULL) - CRIT_ERR("invalid path '%s'", path); + CRIT_ERR(NULL, NULL, "invalid path '%s'", path); if (strlen(slash) == 1) /* trailing slash */ *(slash) = '\0'; else if (strlen(slash) > 1) *(slash + 1) = '\0'; else - CRIT_ERR("found a crack in the matrix!"); + CRIT_ERR(NULL, NULL, "found a crack in the matrix!"); } while (strlen(search_path) > 0); free(search_path); @@ -183,3 +195,69 @@ void get_fs_type(const char *path, char *result) strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE); } + +void init_fs_bar(struct text_object *obj, const char *arg) +{ + arg = scan_bar(obj, arg); + if (arg) { + while (isspace(*arg)) { + arg++; + } + if (*arg == '\0') { + arg = "/"; + } + } else { + arg = "/"; + } + obj->data.opaque = prepare_fs_stat(arg); +} + +static double get_fs_perc(struct fs_stat *fs, int get_free) +{ + double ret = 0.0; + + if(fs && fs->size) { + if(get_free) + ret = fs->avail; + else + ret = fs->size - fs->free; + ret /= fs->size; + } + + return ret; +} + +void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size) +{ + new_bar(obj, p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free_bar) * 255) ); +} + +void init_fs(struct text_object *obj, const char *arg) +{ + obj->data.opaque = prepare_fs_stat(arg ? arg : "/"); +} + +void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size) +{ + percent_print(p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free) * 100) ); +} + +#define HUMAN_PRINT_FS_GENERATOR(name, expr) \ +void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \ +{ \ + struct fs_stat *fs = obj->data.opaque; \ + if (fs) \ + human_readable(expr, p, p_max_size); \ +} + +HUMAN_PRINT_FS_GENERATOR(free, fs->avail) +HUMAN_PRINT_FS_GENERATOR(size, fs->size) +HUMAN_PRINT_FS_GENERATOR(used, fs->size - fs->free) + +void print_fs_type(struct text_object *obj, char *p, int p_max_size) +{ + struct fs_stat *fs = obj->data.opaque; + + if (fs) + snprintf(p, p_max_size, "%s", fs->type); +}