home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / slurp / part02 / space.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  5.1 KB  |  199 lines

  1. /*
  2.  * space - determine free space on a filesystem
  3.  *
  4.  * Copyright (C) 1992/93 Stephen Hebditch and others.
  5.  * TQM Communications, BCM Box 225, London, WC1N 3XX.
  6.  * steveh@orbital.demon.co.uk  +44 836 825962
  7.  *
  8.  * See README for more information and disclaimers
  9.  *
  10.  * This routine determines if there is enough space on the filesystem
  11.  * holding the news spool for a new batch of incoming articles.
  12.  * It is based on space.c in the NNTP reference implementation which
  13.  * credits Stan Barber <sob@bcm.tmc.edu>, Tad Guy <tadguy@cs.odu.edu>,
  14.  * Chris Jepeway <jepeway@utkcs2.cs.utk.edu> and Tom Lane <tgl@cs.cmu.edu>
  15.  * but has been heavily cleaned up and support for SVR4, Linux and
  16.  * BSDI added.
  17.  *
  18.  * $Id: space.c,v 1.5 1993/03/01 18:08:20 root Exp $
  19.  *
  20.  * $Log: space.c,v $
  21.  * Revision 1.5  1993/03/01  18:08:20  root
  22.  * Completely reworked from the original and heavily tidied up.
  23.  * Support added for SVR4 and Linux.
  24.  *
  25.  * Revision 1.4  1993/02/14  16:12:20  root
  26.  * Added support for BSDI.
  27.  *
  28.  */
  29.  
  30. #include "slurp.h"
  31.  
  32. #ifdef MINFREE
  33.  
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36.  
  37. #define DFREE_OK        0
  38. #define DFREE_INODES    1
  39. #define DFREE_BLOCKS    2
  40. #define DFREE_ERR        3
  41.  
  42. /*
  43.  * Definitions for use with dfree() for various UNIX families.
  44.  *
  45.  * statfilesys        Routine to call when trying to stat a file system
  46.  *                    to get the number of free blocks available.
  47.  * statfs_type        The data type into which statfs() wants to return
  48.  *                    useful information.
  49.  * bombed            Boolean expression returning 1 if a call to statfs()
  50.  *                    fails.
  51.  * blkavail            Given a statfs_type called fs, return number of free
  52.  *                    blocks available to a non-superuser.
  53.  * filavail            Given a statfs_type called fs, return number of free
  54.  *                    inodes available to a non-superuser.
  55.  */
  56.  
  57.   #if defined(SVR4)
  58.     #include <sys/statvfs.h>
  59.     #define statfilesys        statvfs
  60.     typedef struct statvfs     statfs_type;
  61.     #define bombed(call)    ((call) == -1)
  62.     #define blkavail(fs)    ((fs).f_bavail)
  63.     #define filavail(fs)    ((fs).f_favail)
  64.  
  65.   #elif defined(sun) || defined(hpux) || defined(pyr) || defined(hp300) || defined(NeXT) || defined(linux)
  66.     #include <sys/vfs.h>
  67.     #define statfilesys        statfs
  68.     typedef struct statfs     statfs_type;
  69.     #define bombed(call)    ((call) == -1)
  70.     #define blkavail(fs)    ((fs).f_bavail)
  71.     #define filavail(fs)    ((fs).f_ffree)
  72.  
  73.   #elif defined(apollo)
  74.     #include <sys/types.h>
  75.     #include <sys/statfs.h>
  76.     #define statfilesys(a,b) statfs (a, b, sizeof (struct statfs), 0)
  77.     typedef struct statfs     statfs_type;
  78.     #define bombed(call)    ((call) == -1)
  79.     #define blkavail(fs)    ((fs).f_bfree)
  80.     #define filavail(fs)    ((fs).f_ffree)
  81.  
  82.   #elif defined(ultrix)
  83.     #include <sys/mount.h>
  84.     typedef struct fs_data    statfs_type;
  85.     #define statfilesys        statfs
  86.     #define bombed(call)    ((call) <= 0)
  87.     #define blkavail(fs)    ((int)((fs).fd_req.bfreen))
  88.     #define filavail(fs)    ((int)((fs).fd_req.gfree))
  89.  
  90.   #elif defined(__bsdi__)
  91.     #include <sys/mount.h>
  92.     typedef struct statfs    statfs_type;
  93.     #define statfilesys        statfs
  94.     #define bombed(call)    ((call) < 0)
  95.     #define blkavail(fs)    ((int)((fs).f_bfree))
  96.     #define filavail(fs)    ((int)((fs).f_ffree))
  97.  
  98.   #elif defined(SVR3)
  99.     #include <ustat.h>
  100.     typedef struct ustat statfs_type;
  101.         int
  102.     statfilesys (char *dir, statfs_type *fs)
  103.         {
  104.         struct stat file;
  105.         if (stat (dir, &file))
  106.             return (-1);
  107.         if (ustat (file.st_dev, fs))
  108.             return (-2);
  109.         return (0);
  110.         }
  111.     #define bombed(call)    (call != 0)
  112.     #define blkavail(fs)    ((fs).f_tfree)
  113.     #define filavail(fs)    ((fs).f_tinode)    
  114.  
  115.   #elif defined(CMU_MACH)
  116.     #include <sys/ioctl.h>
  117.     typedef struct fsparam statfs_type;
  118.         int
  119.     statfilesys (char *dir, statfs_type *fs)
  120.         {
  121.         int fd;
  122.         fd = open (dir, O_RDONLY);
  123.         if (fd < 0)
  124.             return (-1);
  125.         if (ioctl (fd, FIOCFSPARAM, fs) < 0)
  126.             {
  127.             close (fd);
  128.             return(-2);
  129.             }
  130.         close (fd);
  131.         return (0);
  132.         }
  133.     #define bombed (call)    ((call) < 0)
  134.     #define blkavail (fs)    ((fs).fsp_free-((fs).fsp_size*(fs).fsp_minfree+99)/100)
  135.  
  136.   #else
  137.     SPACE DEFINITIONS NOT AVAILABLE FOR THIS MACHINE OR NOT SET CORRECTLY
  138.   #endif
  139.  
  140.  
  141. /*
  142.  * dfree - Return the free space available on the file system containing
  143.  * the specified directory. Space is measured in kilobytes. A negative
  144.  * value is returned if there is an error.
  145.  */
  146.  
  147.     static int
  148. dfree (char *location, int free_space)
  149.     {
  150.     statfs_type fsys;
  151.  
  152.     /* Return error if can't get file system info */
  153.     if (bombed (statfilesys (location, &fsys)))
  154.         return (DFREE_ERR);
  155.  
  156.     /* If able to test if free inodes then do so */
  157. #if defined(filfree) && defined(MINFILES)
  158.     if (filfree (fsys) < MINFILES )
  159.          return (DFREE_INODES);
  160. #endif
  161.  
  162.     /* Test if blocks are available */
  163.     if (blkavail (fsys) < free_space)
  164.         return (DFREE_BLOCKS);
  165.  
  166.     return (DFREE_OK);
  167.     }
  168.  
  169.  
  170. /*
  171.  * space - Returns 1 if there are a sufficient number of free blocks
  172.  * and inodes on the filesystem containing the news spool, or 0 if
  173.  * there are only a small number of blocks / inodes remaining.
  174.  */
  175.  
  176.     int
  177. space (int min_free)
  178.     {
  179.     switch (dfree (SPOOLDIR, min_free))
  180.         {
  181.         case DFREE_OK:
  182.             return (1);
  183.         case DFREE_ERR:
  184.             log_ret ("dfree failed due to system call error");
  185.             break;
  186.         case DFREE_INODES:
  187.             log_msg ("no inodes on %s", SPOOLDIR);
  188.             break;
  189.         case DFREE_BLOCKS:
  190.             log_msg ("no space on %s", SPOOLDIR);
  191.             break;
  192.         }
  193.     return (0);
  194.     }
  195.  
  196. #endif /* MINFREE */
  197.  
  198. /* END-OF-FILE */
  199.