waf: support --without-ad-dc for Heimdal (embedded and system) as well
[Samba/vl.git] / source3 / smbd / statvfs.c
blob5cefe2d4c23031bca62d0a3b2753eec479a7d1ab
1 /*
2 Unix SMB/CIFS implementation.
3 VFS API's statvfs abstraction
4 Copyright (C) Alexander Bokovoy 2005
5 Copyright (C) Steve French 2005
6 Copyright (C) James Peach 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "smbd/smbd.h"
26 #if defined(DARWINOS)
28 #include <sys/attr.h>
30 static int darwin_fs_capabilities(const char * path)
32 int caps = 0;
33 vol_capabilities_attr_t *vcaps;
34 struct attrlist attrlist;
35 char attrbuf[sizeof(u_int32_t) + sizeof(vol_capabilities_attr_t)];
37 #define FORMAT_CAP(vinfo, cap) \
38 ( ((vinfo)->valid[VOL_CAPABILITIES_FORMAT] & (cap)) && \
39 ((vinfo)->capabilities[VOL_CAPABILITIES_FORMAT] & (cap)) )
41 #define INTERFACE_CAP(vinfo, cap) \
42 ( ((vinfo)->valid[VOL_CAPABILITIES_INTERFACES] & (cap)) && \
43 ((vinfo)->capabilities[VOL_CAPABILITIES_INTERFACES] & (cap)) )
45 ZERO_STRUCT(attrlist);
46 attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
47 attrlist.volattr = ATTR_VOL_CAPABILITIES;
49 if (getattrlist(path, &attrlist, attrbuf, sizeof(attrbuf), 0) != 0) {
50 DEBUG(0, ("getattrlist for %s capabilities failed: %s\n",
51 path, strerror(errno)));
52 /* Return no capabilities on failure. */
53 return 0;
56 vcaps =
57 (vol_capabilities_attr_t *)(attrbuf + sizeof(u_int32_t));
59 if (FORMAT_CAP(vcaps, VOL_CAP_FMT_SPARSE_FILES)) {
60 caps |= FILE_SUPPORTS_SPARSE_FILES;
63 if (FORMAT_CAP(vcaps, VOL_CAP_FMT_CASE_SENSITIVE)) {
64 caps |= FILE_CASE_SENSITIVE_SEARCH;
67 if (FORMAT_CAP(vcaps, VOL_CAP_FMT_CASE_PRESERVING)) {
68 caps |= FILE_CASE_PRESERVED_NAMES;
71 if (INTERFACE_CAP(vcaps, VOL_CAP_INT_EXTENDED_SECURITY)) {
72 caps |= FILE_PERSISTENT_ACLS;
75 return caps;
78 static int darwin_statvfs(const char *path, vfs_statvfs_struct *statbuf)
80 struct statfs sbuf;
81 int ret;
83 ret = statfs(path, &sbuf);
84 if (ret != 0) {
85 return ret;
88 statbuf->OptimalTransferSize = sbuf.f_iosize;
89 statbuf->BlockSize = sbuf.f_bsize;
90 statbuf->TotalBlocks = sbuf.f_blocks;
91 statbuf->BlocksAvail = sbuf.f_bfree;
92 statbuf->UserBlocksAvail = sbuf.f_bavail;
93 statbuf->TotalFileNodes = sbuf.f_files;
94 statbuf->FreeFileNodes = sbuf.f_ffree;
95 statbuf->FsIdentifier = *(uint64_t *)(&sbuf.f_fsid); /* Ick. */
96 statbuf->FsCapabilities = darwin_fs_capabilities(sbuf.f_mntonname);
98 return 0;
100 #elif defined(BSD) && defined(BSD_STATVFS_BSIZE)
101 static int bsd_statvfs(const char *path, vfs_statvfs_struct *statbuf)
103 struct statfs statfs_buf;
104 int result;
106 result = statfs(path, &statfs_buf);
107 if (result != 0) {
108 return result;
111 statbuf->OptimalTransferSize = statfs_buf.f_iosize;
112 statbuf->BlockSize = statfs_buf.f_bsize;
113 statbuf->TotalBlocks = statfs_buf.f_blocks;
114 statbuf->BlocksAvail = statfs_buf.f_bfree;
115 statbuf->UserBlocksAvail = statfs_buf.f_bavail;
116 statbuf->TotalFileNodes = statfs_buf.f_files;
117 statbuf->FreeFileNodes = statfs_buf.f_ffree;
118 statbuf->FsIdentifier =
119 (((uint64_t) statfs_buf.f_fsid.val[0] << 32) & 0xffffffff00000000LL) |
120 (uint64_t) statfs_buf.f_fsid.val[1];
121 /* Try to extrapolate some of the fs flags into the
122 * capabilities
124 statbuf->FsCapabilities =
125 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
126 #ifdef MNT_ACLS
127 if (statfs_buf.f_flags & MNT_ACLS)
128 statbuf->FsCapabilities |= FILE_PERSISTENT_ACLS;
129 #endif
130 if (statfs_buf.f_flags & MNT_QUOTA)
131 statbuf->FsCapabilities |= FILE_VOLUME_QUOTAS;
132 if (statfs_buf.f_flags & MNT_RDONLY)
133 statbuf->FsCapabilities |= FILE_READ_ONLY_VOLUME;
135 return 0;
137 #elif defined(STAT_STATVFS) && defined(HAVE_FSID_INT)
138 static int linux_statvfs(const char *path, vfs_statvfs_struct *statbuf)
140 struct statvfs statvfs_buf;
141 int result;
143 result = statvfs(path, &statvfs_buf);
145 if (!result) {
146 statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
147 statbuf->BlockSize = statvfs_buf.f_bsize;
148 statbuf->TotalBlocks = statvfs_buf.f_blocks;
149 statbuf->BlocksAvail = statvfs_buf.f_bfree;
150 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
151 statbuf->TotalFileNodes = statvfs_buf.f_files;
152 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
153 statbuf->FsIdentifier = statvfs_buf.f_fsid;
154 /* Try to extrapolate some of the fs flags into the
155 * capabilities
157 statbuf->FsCapabilities =
158 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
159 #ifdef ST_QUOTA
160 if (statvfs_buf.f_flag & ST_QUOTA)
161 statbuf->FsCapabilities |= FILE_VOLUME_QUOTAS;
162 #endif
163 if (statvfs_buf.f_flag & ST_RDONLY)
164 statbuf->FsCapabilities |= FILE_READ_ONLY_VOLUME;
166 return result;
168 #endif
171 sys_statvfs() is an abstraction layer over system-dependent statvfs()/statfs()
172 for particular POSIX systems. Due to controversy of what is considered more important
173 between LSB and FreeBSD/POSIX.1 (IEEE Std 1003.1-2001) we need to abstract the interface
174 so that particular OS would use its prefered interface.
176 int sys_statvfs(const char *path, vfs_statvfs_struct *statbuf)
178 #if defined(DARWINOS)
179 return darwin_statvfs(path, statbuf);
180 #elif defined(BSD) && defined(BSD_STATVFS_BSIZE)
181 return bsd_statvfs(path, statbuf);
182 #elif defined(STAT_STATVFS) && defined(HAVE_FSID_INT)
183 return linux_statvfs(path, statbuf);
184 #else
185 /* BB change this to return invalid level */
186 #ifdef EOPNOTSUPP
187 return EOPNOTSUPP;
188 #else
189 return -1;
190 #endif /* EOPNOTSUPP */
191 #endif /* LINUX */