s3: Fix a "Invalid (state->nread >= 0)" warning
[Samba/id10ts.git] / source3 / smbd / statvfs.c
blob1e72a8e48b73675acbd123244382ec49a9aaf803
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;
155 /* Good defaults for Linux filesystems are case sensitive
156 * and case preserving.
158 statbuf->FsCapabilities =
159 FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
161 return result;
163 #endif
166 sys_statvfs() is an abstraction layer over system-dependent statvfs()/statfs()
167 for particular POSIX systems. Due to controversy of what is considered more important
168 between LSB and FreeBSD/POSIX.1 (IEEE Std 1003.1-2001) we need to abstract the interface
169 so that particular OS would use its prefered interface.
171 int sys_statvfs(const char *path, vfs_statvfs_struct *statbuf)
173 #if defined(DARWINOS)
174 return darwin_statvfs(path, statbuf);
175 #elif defined(BSD) && defined(BSD_STATVFS_BSIZE)
176 return bsd_statvfs(path, statbuf);
177 #elif defined(STAT_STATVFS) && defined(HAVE_FSID_INT)
178 return linux_statvfs(path, statbuf);
179 #else
180 /* BB change this to return invalid level */
181 #ifdef EOPNOTSUPP
182 return EOPNOTSUPP;
183 #else
184 return -1;
185 #endif /* EOPNOTSUPP */
186 #endif /* LINUX */