From 94849cfde892c015615ef6745400cc21e2cd0c79 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 31 Mar 2002 20:51:06 +0200 Subject: [PATCH] Now using statfs() to determine the amount of free disk space --- CONTRIBUTORS | 3 +++ HISTORY | 2 ++ tools.c | 31 +++++++++---------------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a93cef82..cbd0284a 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -242,3 +242,6 @@ Bernd Zierath Truls Slevigen for translating OSD texts to the Norwegian language + +Ruben Nunez Francisco + for implementing FreeDiskSpaceMB() without external 'df' command diff --git a/HISTORY b/HISTORY index 5279b759..61e52fc5 100644 --- a/HISTORY +++ b/HISTORY @@ -1155,3 +1155,5 @@ Video Disk Recorder Revision History - Added units to Setup parameters. - Changed time entry in the 'Jump' command during replay, so that it is filled up from right to left. +- Now using statfs() to determine the amount of free disk space, which avoids + the use of an external 'df' command (thanks to Ruben Nunez Francisco). diff --git a/tools.c b/tools.c index 682a521c..099fb7d9 100644 --- a/tools.c +++ b/tools.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.61 2002/03/23 15:48:08 kls Exp $ + * $Id: tools.c 1.62 2002/03/31 20:51:06 kls Exp $ */ #include "tools.h" @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "i18n.h" @@ -243,34 +244,20 @@ const char *AddDirectory(const char *DirName, const char *FileName) return buf; } -#define DFCMD "df -m -P '%s'" - int FreeDiskSpaceMB(const char *Directory, int *UsedMB) { - //TODO Find a simpler way to determine the amount of free disk space! if (UsedMB) *UsedMB = 0; int Free = 0; - char *cmd = NULL; - asprintf(&cmd, DFCMD, Directory); - FILE *p = popen(cmd, "r"); - if (p) { - char *s; - while ((s = readline(p)) != NULL) { - if (strchr(s, '/')) { - int used, available; - sscanf(s, "%*s %*d %d %d", &used, &available); - if (UsedMB) - *UsedMB = used; - Free = available; - break; - } - } - pclose(p); + struct statfs statFs; + if (statfs(Directory, &statFs) == 0) { + int blocksPerMeg = 1024 * 1024 / statFs.f_bsize; + if (UsedMB) + *UsedMB = (statFs.f_blocks - statFs.f_bfree) / blocksPerMeg; + Free = statFs.f_bavail / blocksPerMeg; } else - esyslog(LOG_ERR, "ERROR: can't open pipe for cmd '%s'", cmd); - delete cmd; + LOG_ERROR_STR(Directory); return Free; }