Now using statfs() to determine the amount of free disk space

This commit is contained in:
Klaus Schmidinger 2002-03-31 20:51:06 +02:00
parent ca29ed0ca8
commit 94849cfde8
3 changed files with 14 additions and 22 deletions

View File

@ -242,3 +242,6 @@ Bernd Zierath <b.zierath@ebv.com>
Truls Slevigen <truls@slevigen.no> Truls Slevigen <truls@slevigen.no>
for translating OSD texts to the Norwegian language for translating OSD texts to the Norwegian language
Ruben Nunez Francisco <ruben.nunez@tang-it.com>
for implementing FreeDiskSpaceMB() without external 'df' command

View File

@ -1155,3 +1155,5 @@ Video Disk Recorder Revision History
- Added units to Setup parameters. - Added units to Setup parameters.
- Changed time entry in the 'Jump' command during replay, so that it is filled - Changed time entry in the 'Jump' command during replay, so that it is filled
up from right to left. 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).

31
tools.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "tools.h"
@ -13,6 +13,7 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/vfs.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "i18n.h" #include "i18n.h"
@ -243,34 +244,20 @@ const char *AddDirectory(const char *DirName, const char *FileName)
return buf; return buf;
} }
#define DFCMD "df -m -P '%s'"
int FreeDiskSpaceMB(const char *Directory, int *UsedMB) int FreeDiskSpaceMB(const char *Directory, int *UsedMB)
{ {
//TODO Find a simpler way to determine the amount of free disk space!
if (UsedMB) if (UsedMB)
*UsedMB = 0; *UsedMB = 0;
int Free = 0; int Free = 0;
char *cmd = NULL; struct statfs statFs;
asprintf(&cmd, DFCMD, Directory); if (statfs(Directory, &statFs) == 0) {
FILE *p = popen(cmd, "r"); int blocksPerMeg = 1024 * 1024 / statFs.f_bsize;
if (p) { if (UsedMB)
char *s; *UsedMB = (statFs.f_blocks - statFs.f_bfree) / blocksPerMeg;
while ((s = readline(p)) != NULL) { Free = statFs.f_bavail / blocksPerMeg;
if (strchr(s, '/')) {
int used, available;
sscanf(s, "%*s %*d %d %d", &used, &available);
if (UsedMB)
*UsedMB = used;
Free = available;
break;
}
}
pclose(p);
} }
else else
esyslog(LOG_ERR, "ERROR: can't open pipe for cmd '%s'", cmd); LOG_ERROR_STR(Directory);
delete cmd;
return Free; return Free;
} }