Main Page | Class List | Directories | File List | Class Members | File Members

df.c

Go to the documentation of this file.
00001 /* 
00002 
00003         Copyright (C) 1995
00004         Free Software Foundation, Inc.
00005 
00006    This file is part of GNU cfengine - written and maintained 
00007    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
00008    Dept. of Theoretical physics, University of Oslo
00009  
00010    This program is free software; you can redistribute it and/or modify it
00011    under the terms of the GNU General Public License as published by the
00012    Free Software Foundation; either version 2, or (at your option) any
00013    later version. 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018  
00019   You should have received a copy of the GNU General Public License
00020   along with this program; if not, write to the Free Software
00021   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
00022 
00023 */
00024 
00025 /*******************************************************************/
00026 /*                                                                 */
00027 /* Disk usage module, Demosthenes Skipitaris                       */
00028 /*                                                                 */
00029 /*******************************************************************/
00030 
00031 #include "../pub/getopt.h"
00032 #include "cf.defs.h"
00033 #include "cf.extern.h"
00034 
00035 #ifdef HAVE_SYS_STATFS_H
00036  #include <sys/statfs.h>
00037 #endif
00038 #ifdef HAVE_SYS_VFS_H
00039 #include <sys/vfs.h>
00040 #endif
00041 
00042 #ifdef HAVE_SYS_STATVFS_H
00043 #include <sys/statvfs.h>
00044 #endif
00045 
00046 /************************************************************************/
00047 
00048 int GetDiskUsage (file,type)
00049 
00050 char *file;
00051 enum cfsizes type;
00052 
00053 {
00054 #if defined SOLARIS || defined OSF || defined UNIXWARE 
00055     struct statvfs buf;
00056 #elif defined ULTRIX
00057     struct fs_data buf;
00058 #else
00059     struct statfs buf;
00060 #endif
00061     u_long blocksize = 1024, total = 0, used = 0, avail = 0;
00062     int capacity = 0;
00063 
00064     bzero (&buf, sizeof (buf));
00065 
00066     Verbose("Checking free space on %s\n",file);
00067 
00068 
00069 #if defined ULTRIX
00070     if (getmnt (NULL, &buf, sizeof (struct fs_data), STAT_ONE, file) == -1)
00071        {
00072        snprintf(OUTPUT,bufsize,"Couldn't get filesystem info for %s\n",file);
00073        CfLog(cferror,OUTPUT,"");
00074        return cfinfinity;
00075        }
00076 #elif defined SOLARIS || defined OSF || defined UNIXWARE 
00077     if (statvfs (file, &buf) != 0)
00078        {
00079        snprintf(OUTPUT,bufsize,"Couldn't get filesystem info for %s\n",file);
00080        CfLog(cferror,OUTPUT,"");
00081        return cfinfinity;
00082        }
00083 #elif defined IRIX || defined SCO || defined CFCRAY || defined UNIXWARE
00084     if (statfs (file, &buf, sizeof (struct statfs), 0) != 0)
00085        {
00086        snprintf(OUTPUT,bufsize,"Couldn't get filesystem info for %s\n",file);
00087        CfLog(cferror,OUTPUT,"");
00088        return cfinfinity;
00089        }
00090 #else
00091     if (statfs (file, &buf) != 0)
00092        {
00093        snprintf(OUTPUT,bufsize,"Couldn't get filesystem info for %s\n",file);
00094        CfLog(cferror,OUTPUT,"");
00095        return cfinfinity;
00096        }
00097 #endif
00098 
00099 #if defined ULTRIX
00100     total = buf.fd_btot;
00101     used = buf.fd_btot - buf.fd_bfree;
00102     avail = buf.fd_bfreen;
00103 #endif
00104 
00105 #if defined SOLARIS
00106     total = buf.f_blocks * (buf.f_frsize / blocksize);
00107     used = (buf.f_blocks - buf.f_bfree) * (buf.f_frsize / blocksize);
00108     avail = buf.f_bavail * (buf.f_frsize / blocksize);
00109 #endif
00110 
00111 #if defined NETBSD || defined FREEBSD || defined OPENBSD || defined SUNOS || defined HPuUX || defined DARWIN
00112     total = buf.f_blocks;
00113     used = buf.f_blocks - buf.f_bfree;
00114     avail = buf.f_bavail;
00115 #endif
00116 
00117 #if defined OSF
00118     total = (buf.f_blocks *  buf.f_frsize) / blocksize;
00119     used = ((buf.f_blocks - buf.f_bfree)* (buf.f_frsize) / blocksize);
00120     avail = (buf.f_bavail * buf.f_frsize) / blocksize;
00121 #endif
00122 
00123 #if defined AIX || defined SCO || defined CFCRAY || defined LINUX
00124     total = buf.f_blocks * (buf.f_bsize / blocksize);
00125     used = (buf.f_blocks - buf.f_bfree) * (buf.f_bsize / blocksize);
00126     avail = buf.f_bfree * (buf.f_bsize / blocksize);
00127 #endif
00128 
00129 #if defined IRIX
00130     /* Float fix by arjen@sara.nl */
00131     total = buf.f_blocks *  ((float)buf.f_bsize / blocksize);
00132     used = (buf.f_blocks - buf.f_bfree) * ((float)buf.f_bsize / blocksize);
00133     avail = buf.f_bfree *  ((float)buf.f_bsize/blocksize);
00134 #endif
00135    
00136 capacity = (double) (avail) / (double) (avail + used) * 100;
00137 
00138 Debug2("GetDiskUsage(%s) = %d/%d\n",file,avail,capacity);
00139     
00140 /* Free kilobytes */
00141 
00142 if (type == cfabs)
00143    {
00144    return avail;
00145    }
00146 else
00147    {
00148    return capacity;
00149    }
00150 }

© sourcejam.com 2005-2008