From 20f5363a70f2b3622c643f6e165fb56e614dec8b Mon Sep 17 00:00:00 2001 From: dockes Date: Fri, 8 Jun 2007 15:30:01 +0000 Subject: [PATCH] fsocc --- src/utils/pathut.cpp | 63 +++++++++++++++++++++++++++++++++++++++++--- src/utils/pathut.h | 8 +++++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/utils/pathut.cpp b/src/utils/pathut.cpp index 04374f21..357dd44f 100644 --- a/src/utils/pathut.cpp +++ b/src/utils/pathut.cpp @@ -1,5 +1,5 @@ #ifndef lint -static char rcsid[] = "@(#$Id: pathut.cpp,v 1.15 2007-02-06 14:16:43 dockes Exp $ (C) 2004 J.F.Dockes"; +static char rcsid[] = "@(#$Id: pathut.cpp,v 1.16 2007-06-08 15:30:01 dockes Exp $ (C) 2004 J.F.Dockes"; #endif /* * This program is free software; you can redistribute it and/or modify @@ -23,6 +23,7 @@ static char rcsid[] = "@(#$Id: pathut.cpp,v 1.15 2007-02-06 14:16:43 dockes Exp #include #include #include +#include #include #include @@ -35,6 +36,40 @@ using std::stack; #include "pathut.h" +#ifndef STATFS_INCLUDE +#define STATFS_INCLUDE +#endif + +#include STATFS_INCLUDE + +bool fsocc(const string &path, int *pc, long *blocks) +{ + struct statfs buf; + if (statfs(path.c_str(), &buf) != 0) { + return false; + } + // used blocks + double fpc = 0.0; +#define FSOCC_USED (double(buf.f_blocks - buf.f_bfree)) +#define FSOCC_TOTAVAIL (FSOCC_USED + double(buf.f_bavail)) + if (FSOCC_TOTAVAIL > 0) { + fpc = 100.0 * FSOCC_USED / FSOCC_TOTAVAIL; + } + *pc = int(fpc); + if (blocks) { + *blocks = 0; +#define FSOCC_MB (1024*1024) + if (buf.f_bsize > 0) { + int ratio = buf.f_bsize > FSOCC_MB ? buf.f_bsize / FSOCC_MB : + FSOCC_MB / buf.f_bsize; + + *blocks = buf.f_bsize > FSOCC_MB ? long(buf.f_bavail) * ratio : + long(buf.f_bavail) / ratio; + } + } + return true; +} + static const char *tmplocation() { const char *tmpdir = getenv("RECOLL_TMPDIR"); @@ -352,8 +387,12 @@ const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub", "~nosuch", "~nosuch/sub"}; int nttvec = sizeof(ttvec) / sizeof(string); +const char *thisprog; + int main(int argc, const char **argv) { + thisprog = *argv++;argc--; + string s; list::const_iterator it; #if 0 @@ -388,18 +427,34 @@ int main(int argc, const char **argv) path_canon(canontst[i]) << "'" << endl; } #endif -#if 1 - if (argc != 3) { +#if 0 + if (argc != 2) { fprintf(stderr, "Usage: trpathut \n"); exit(1); } - string dir=argv[1], pattern=argv[2]; + string dir = *argv++;argc--; + string pattern = *argv++;argc--; list matched = path_dirglob(dir, pattern); for (it = matched.begin(); it != matched.end();it++) { cout << *it << endl; } #endif +#if 1 + if (argc != 1) { + fprintf(stderr, "Usage: fsocc: trpathut \n"); + exit(1); + } + string path = *argv++;argc--; + + int pc; + long blocks; + if (!fsocc(path, &pc, &blocks)) { + fprintf(stderr, "fsocc failed\n"); + return 1; + } + printf("pc %d, megabytes %ld\n", pc, blocks); +#endif return 0; } diff --git a/src/utils/pathut.h b/src/utils/pathut.h index 97c4955c..7c45ac79 100644 --- a/src/utils/pathut.h +++ b/src/utils/pathut.h @@ -16,7 +16,7 @@ */ #ifndef _PATHUT_H_INCLUDED_ #define _PATHUT_H_INCLUDED_ -/* @(#$Id: pathut.h,v 1.12 2007-02-06 14:16:43 dockes Exp $ (C) 2004 J.F.Dockes */ +/* @(#$Id: pathut.h,v 1.13 2007-06-08 15:30:01 dockes Exp $ (C) 2004 J.F.Dockes */ #include #include @@ -55,6 +55,12 @@ extern string url_encode(const string url, /// Stat parameter and check if it's a directory extern bool path_isdir(const string& path); +/** A small wrapper around statfs et al, to return percentage of disk + occupation */ +bool fsocc(const string &path, int *pc, // Percent occupied + long *avmbs = 0 // Mbs available to non-superuser + ); + /// Create temporary directory extern bool maketmpdir(string& tdir, string& reason);