home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / fsusage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  5.1 KB  |  195 lines

  1. /* fsusage.c -- Return space usage of mounted filesystems.  */
  2.  
  3. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22.  
  23. #include <sys/types.h>
  24.  
  25. #include "xio.h"
  26. #include "fsusage.h"
  27.  
  28. int statfs ();
  29.  
  30. #ifdef HAVE_SYS_PARAM_H
  31. #include <sys/param.h>
  32. #endif
  33.  
  34. #ifdef HAVE_SYS_MOUNT_H
  35. #include <sys/mount.h>
  36. #endif
  37.  
  38. #ifdef HAVE_SYS_VFS_H
  39. #include <sys/vfs.h>
  40. #endif
  41.  
  42. #ifdef HAVE_SYS_FILSYS_H
  43. #include <sys/filsys.h>         /* SVR2.  */
  44. #endif
  45.  
  46. #ifdef HAVE_SYS_STATFS_H
  47. #include <sys/statfs.h>
  48. #endif
  49.  
  50. #ifdef HAVE_DUSTAT_H            /* AIX PS/2.  */
  51. #include <sys/dustat.h>
  52. #endif
  53.  
  54. #ifdef HAVE_SYS_STATVFS_H       /* SVR4.  */
  55. #include <sys/statvfs.h>
  56. int statvfs ();
  57. #endif
  58.  
  59. /* Return the number of TOSIZE-byte blocks used by
  60.    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
  61.    TOSIZE must be positive.  Return -1 if FROMSIZE is not positive.  */
  62.  
  63. long
  64. fs_adjust_blocks (blocks, fromsize, tosize)
  65.      long blocks;
  66.      int fromsize, tosize;
  67. {
  68.   if (tosize <= 0)
  69.     abort ();
  70.   if (fromsize <= 0)
  71.     return -1;
  72.  
  73.   if (fromsize == tosize)       /* E.g., from 512 to 512.  */
  74.     return blocks;
  75.   else if (fromsize > tosize)   /* E.g., from 2048 to 512.  */
  76.     return blocks * (fromsize / tosize);
  77.   else                          /* E.g., from 256 to 512.  */
  78.     return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
  79. }
  80.  
  81. /* Fill in the fields of FSP with information about space usage for
  82.    the filesystem on which PATH resides.
  83.    Return 0 if successful, -1 if not. */
  84.  
  85. int
  86. get_fs_usage (path, fsp)
  87.      char *path;
  88.      struct fs_usage *fsp;
  89. {
  90. #if defined (STAT_STATFS3_OSF1)
  91.   struct statfs fsd;
  92.  
  93.   if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
  94.     return -1;
  95. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
  96. #endif /* STAT_STATFS3_OSF1 */
  97.  
  98. #ifdef STAT_STATFS2_FS_DATA     /* Ultrix.  */
  99.   struct fs_data fsd;
  100.  
  101.   if (statfs (path, &fsd) != 1)
  102.     return -1;
  103. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
  104.   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
  105.   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
  106.   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
  107.   fsp->fsu_files = fsd.fd_req.gtot;
  108.   fsp->fsu_ffree = fsd.fd_req.gfree;
  109. #endif
  110.  
  111. #ifdef STAT_STATFS2_BSIZE       /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
  112.   struct statfs fsd;
  113.  
  114.   if (statfs (path, &fsd) < 0)
  115.     return -1;
  116. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
  117. #endif
  118.  
  119. #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD.  */
  120.   struct statfs fsd;
  121.  
  122.   if (statfs (path, &fsd) < 0)
  123.     return -1;
  124. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
  125. #endif
  126.  
  127. #ifdef STAT_STATFS4             /* SVR3, Dynix, Irix, AIX.  */
  128.   struct statfs fsd;
  129.  
  130.   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
  131.     return -1;
  132.   /* Empirically, the block counts on most SVR3 and SVR3-derived
  133.      systems seem to always be in terms of 512-byte blocks,
  134.      no matter what value f_bsize has.  */
  135. # if _AIX
  136. #  define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
  137. # else
  138. #  define CONVERT_BLOCKS(b) (b)
  139. #  ifndef _SEQUENT_             /* _SEQUENT_ is DYNIX/ptx.  */
  140. #   ifndef DOLPHIN              /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
  141. #    define f_bavail f_bfree
  142. #   endif
  143. #  endif
  144. # endif
  145. #endif
  146.  
  147. #ifdef STAT_STATVFS             /* SVR4.  */
  148.   struct statvfs fsd;
  149.  
  150.   if (statvfs (path, &fsd) < 0)
  151.     return -1;
  152.   /* f_frsize isn't guaranteed to be supported.  */
  153. #define CONVERT_BLOCKS(b) \
  154.   fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
  155. #endif
  156.  
  157. #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2.  */
  158.   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
  159.   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
  160.   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
  161.   fsp->fsu_files = fsd.f_files;
  162.   fsp->fsu_ffree = fsd.f_ffree;
  163. #endif
  164.  
  165.   return 0;
  166. }
  167.  
  168. #if defined(_AIX) && defined(_I386)
  169. /* AIX PS/2 does not supply statfs.  */
  170.  
  171. int
  172. statfs (path, fsb)
  173.      char *path;
  174.      struct statfs *fsb;
  175. {
  176.   struct stat stats;
  177.   struct dustat fsd;
  178.  
  179.   if (xstat (path, &stats))
  180.     return -1;
  181.   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  182.     return -1;
  183.   fsb->f_type   = 0;
  184.   fsb->f_bsize  = fsd.du_bsize;
  185.   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  186.   fsb->f_bfree  = fsd.du_tfree;
  187.   fsb->f_bavail = fsd.du_tfree;
  188.   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
  189.   fsb->f_ffree  = fsd.du_tinode;
  190.   fsb->f_fsid.val[0] = fsd.du_site;
  191.   fsb->f_fsid.val[1] = fsd.du_pckno;
  192.   return 0;
  193. }
  194. #endif /* _AIX && _I386 */
  195.