1/* 2 Unix SMB/CIFS implementation. 3 functions to calculate the free disk space 4 Copyright (C) Andrew Tridgell 1998-2000 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19*/ 20 21#include "includes.h" 22 23 24/* Return the number of TOSIZE-byte blocks used by 25 BLOCKS FROMSIZE-byte blocks, rounding away from zero. 26*/ 27static SMB_BIG_UINT adjust_blocks(SMB_BIG_UINT blocks, SMB_BIG_UINT fromsize, SMB_BIG_UINT tosize) 28{ 29 if (fromsize == tosize) /* e.g., from 512 to 512 */ 30 return blocks; 31 else if (fromsize > tosize) /* e.g., from 2048 to 512 */ 32 return blocks * (fromsize / tosize); 33 else /* e.g., from 256 to 512 */ 34 return (blocks + 1) / (tosize / fromsize); 35} 36 37/* this does all of the system specific guff to get the free disk space. 38 It is derived from code in the GNU fileutils package, but has been 39 considerably mangled for use here 40 41 results are returned in *dfree and *dsize, in 512 byte units 42*/ 43int sys_fsusage(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize) 44{ 45#ifdef STAT_STATFS3_OSF1 46#define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512) 47 struct statfs fsd; 48 49 if (statfs (path, &fsd, sizeof (struct statfs)) != 0) 50 return -1; 51#endif /* STAT_STATFS3_OSF1 */ 52 53#ifdef STAT_STATFS2_FS_DATA /* Ultrix */ 54#define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)1024, (SMB_BIG_UINT)512) 55 struct fs_data fsd; 56 57 if (statfs (path, &fsd) != 1) 58 return -1; 59 60 (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot); 61 (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen); 62#endif /* STAT_STATFS2_FS_DATA */ 63 64#ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */ 65#define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512) 66 struct statfs fsd; 67 68 if (statfs (path, &fsd) < 0) 69 return -1; 70 71#ifdef STATFS_TRUNCATES_BLOCK_COUNTS 72 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the 73 struct statfs are truncated to 2GB. These conditions detect that 74 truncation, presumably without botching the 4.1.1 case, in which 75 the values are not truncated. The correct counts are stored in 76 undocumented spare fields. */ 77 if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0) { 78 fsd.f_blocks = fsd.f_spare[0]; 79 fsd.f_bfree = fsd.f_spare[1]; 80 fsd.f_bavail = fsd.f_spare[2]; 81 } 82#endif /* STATFS_TRUNCATES_BLOCK_COUNTS */ 83#endif /* STAT_STATFS2_BSIZE */ 84 85 86#ifdef STAT_STATFS2_FSIZE /* 4.4BSD */ 87#define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512) 88 89 struct statfs fsd; 90 91 if (statfs (path, &fsd) < 0) 92 return -1; 93#endif /* STAT_STATFS2_FSIZE */ 94 95#ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */ 96# if _AIX || defined(_CRAY) 97# define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512) 98# ifdef _CRAY 99# define f_bavail f_bfree 100# endif 101# else 102# define CONVERT_BLOCKS(B) ((SMB_BIG_UINT)B) 103# ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx */ 104# ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */ 105# define f_bavail f_bfree 106# endif 107# endif 108# endif 109 110 struct statfs fsd; 111 112 if (statfs (path, &fsd, sizeof fsd, 0) < 0) 113 return -1; 114 /* Empirically, the block counts on most SVR3 and SVR3-derived 115 systems seem to always be in terms of 512-byte blocks, 116 no matter what value f_bsize has. */ 117 118#endif /* STAT_STATFS4 */ 119 120#if defined(STAT_STATVFS) || defined(STAT_STATVFS64) /* SVR4 */ 121# define CONVERT_BLOCKS(B) \ 122 adjust_blocks ((SMB_BIG_UINT)(B), fsd.f_frsize ? (SMB_BIG_UINT)fsd.f_frsize : (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512) 123 124#ifdef STAT_STATVFS64 125 struct statvfs64 fsd; 126 if (statvfs64(path, &fsd) < 0) return -1; 127#else 128 struct statvfs fsd; 129 if (statvfs(path, &fsd) < 0) return -1; 130#endif 131 132 /* f_frsize isn't guaranteed to be supported. */ 133 134#endif /* STAT_STATVFS */ 135 136#ifndef CONVERT_BLOCKS 137 /* we don't have any dfree code! */ 138 return -1; 139#else 140#if !defined(STAT_STATFS2_FS_DATA) 141 /* !Ultrix */ 142 (*dsize) = CONVERT_BLOCKS (fsd.f_blocks); 143 (*dfree) = CONVERT_BLOCKS (fsd.f_bavail); 144#endif /* not STAT_STATFS2_FS_DATA */ 145#endif 146 147 return 0; 148} 149