1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <cf3.defs.h>
26 
27 #ifdef HAVE_SYS_STATFS_H
28 # include <sys/statfs.h>
29 #endif
30 #ifdef HAVE_SYS_VFS_H
31 # include <sys/vfs.h>
32 #endif
33 
34 #ifdef HAVE_SYS_STATVFS_H
35 # include <sys/statvfs.h>
36 #endif
37 
38 /************************************************************************/
39 
40 #ifndef __MINGW32__
41 
GetDiskUsage(char * file,CfSize type)42 off_t GetDiskUsage(char *file, CfSize type)
43 {
44 # if defined __sun || defined sco || defined __OpenBSD__ || (defined(__NetBSD__) && __NetBSD_Version__ >= 200040000)
45     struct statvfs buf;
46 # else
47     struct statfs buf;
48 # endif
49     int64_t used = 0, avail = 0;
50     int capacity = 0;
51 
52     memset(&buf, 0, sizeof(buf));
53 
54 # if defined __sun || defined sco || defined __OpenBSD__ || (defined(__NetBSD__) && __NetBSD_Version__ >= 200040000)
55     if (statvfs(file, &buf) != 0)
56     {
57         Log(LOG_LEVEL_ERR, "Couldn't get filesystem info for %s (statvfs)", file);
58         return CF_INFINITY;
59     }
60 # elif defined __SCO_DS || defined _CRAY || (defined(__NetBSD__) && __NetBSD_Version__ >= 200040000)
61     if (statfs(file, &buf, sizeof(struct statfs), 0) != 0)
62     {
63         Log(LOG_LEVEL_ERR, "Couldn't get filesystem info for %s (statfs)", file);
64         return CF_INFINITY;
65     }
66 # else
67     if (statfs(file, &buf) != 0)
68     {
69         Log(LOG_LEVEL_ERR, "Couldn't get filesystem info for '%s'. (statfs: %s)", file, GetErrorStr());
70         return CF_INFINITY;
71     }
72 # endif
73 
74 # if defined __sun
75     used = (buf.f_blocks - buf.f_bfree) * (int64_t)buf.f_frsize;
76     avail = buf.f_bavail * (int64_t)buf.f_frsize;
77 # endif
78 
79 # if defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __hpux || defined __APPLE__
80     used = (buf.f_blocks - buf.f_bfree) * (int64_t)buf.f_bsize;
81     avail = buf.f_bavail * (int64_t)buf.f_bsize;
82 # endif
83 
84 # if defined _AIX || defined __SCO_DS || defined _CRAY
85     used = (buf.f_blocks - buf.f_bfree) * (int64_t)buf.f_bsize;
86     avail = buf.f_bfree * (int64_t)buf.f_bsize;
87 # endif
88 
89 # if defined __linux__
90     used = (buf.f_blocks - buf.f_bfree) * (int64_t)buf.f_bsize;
91     avail = buf.f_bavail * (int64_t)buf.f_bsize;
92 # endif
93 
94     capacity = (double) (avail) / (double) (avail + used) * 100;
95 
96     Log(LOG_LEVEL_DEBUG, "GetDiskUsage(%s) = %jd/%jd", file, (intmax_t) avail, (intmax_t) capacity);
97 
98     if (type == CF_SIZE_ABS)
99     {
100         // TODO: This should be handled better by actually returning a bigger
101         // data type, but for now just protect against overflow by hitting the
102         // ceiling.
103         if (sizeof(off_t) < sizeof(int64_t) && avail > 0x7fffffffLL)
104         {
105             return 0x7fffffff;
106         }
107         else
108         {
109             return avail;
110         }
111     }
112     else
113     {
114         return capacity;
115     }
116 }
117 
118 #endif /* __MINGW32__ */
119