s3-includes: only include system/filesys.h when needed.
[ab/samba.git/.git] / source3 / smbd / statvfs.c
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
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24
25 #if defined(LINUX) && defined(HAVE_FSID_INT)
26 static int linux_statvfs(const char *path, vfs_statvfs_struct *statbuf)
27 {
28         struct statvfs statvfs_buf;
29         int result;
30
31         result = statvfs(path, &statvfs_buf);
32
33         if (!result) {
34                 statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
35                 statbuf->BlockSize = statvfs_buf.f_bsize;
36                 statbuf->TotalBlocks = statvfs_buf.f_blocks;
37                 statbuf->BlocksAvail = statvfs_buf.f_bfree;
38                 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
39                 statbuf->TotalFileNodes = statvfs_buf.f_files;
40                 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
41                 statbuf->FsIdentifier = statvfs_buf.f_fsid;
42
43                 /* Good defaults for Linux filesystems are case sensitive
44                  * and case preserving.
45                  */
46                 statbuf->FsCapabilities =
47                     FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
48         }
49         return result;
50 }
51 #endif
52
53 #if defined(DARWINOS)
54
55 #include <sys/attr.h>
56
57 static int darwin_fs_capabilities(const char * path)
58 {
59         int caps = 0;
60         vol_capabilities_attr_t *vcaps;
61         struct attrlist attrlist;
62         char attrbuf[sizeof(u_int32_t) + sizeof(vol_capabilities_attr_t)];
63
64 #define FORMAT_CAP(vinfo, cap) \
65         ( ((vinfo)->valid[VOL_CAPABILITIES_FORMAT] & (cap)) && \
66            ((vinfo)->capabilities[VOL_CAPABILITIES_FORMAT] & (cap)) )
67
68 #define INTERFACE_CAP(vinfo, cap) \
69         ( ((vinfo)->valid[VOL_CAPABILITIES_INTERFACES] & (cap)) && \
70            ((vinfo)->capabilities[VOL_CAPABILITIES_INTERFACES] & (cap)) )
71
72         ZERO_STRUCT(attrlist);
73         attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
74         attrlist.volattr = ATTR_VOL_CAPABILITIES;
75
76         if (getattrlist(path, &attrlist, attrbuf, sizeof(attrbuf), 0) != 0) {
77                 DEBUG(0, ("getattrlist for %s capabilities failed: %s\n",
78                             path, strerror(errno)));
79                 /* Return no capabilities on failure. */
80                 return 0;
81         }
82
83         vcaps =
84             (vol_capabilities_attr_t *)(attrbuf + sizeof(u_int32_t));
85
86         if (FORMAT_CAP(vcaps, VOL_CAP_FMT_SPARSE_FILES)) {
87                 caps |= FILE_SUPPORTS_SPARSE_FILES;
88         }
89
90         if (FORMAT_CAP(vcaps, VOL_CAP_FMT_CASE_SENSITIVE)) {
91                 caps |= FILE_CASE_SENSITIVE_SEARCH;
92         }
93
94         if (FORMAT_CAP(vcaps, VOL_CAP_FMT_CASE_PRESERVING)) {
95                 caps |= FILE_CASE_PRESERVED_NAMES;
96         }
97
98         if (INTERFACE_CAP(vcaps, VOL_CAP_INT_EXTENDED_SECURITY)) {
99                 caps |= FILE_PERSISTENT_ACLS;
100         }
101
102         return caps;
103 }
104
105 static int darwin_statvfs(const char *path, vfs_statvfs_struct *statbuf)
106 {
107         struct statfs sbuf;
108         int ret;
109
110         ret = statfs(path, &sbuf);
111         if (ret != 0) {
112                 return ret;
113         }
114
115         statbuf->OptimalTransferSize = sbuf.f_iosize;
116         statbuf->BlockSize = sbuf.f_bsize;
117         statbuf->TotalBlocks = sbuf.f_blocks;
118         statbuf->BlocksAvail = sbuf.f_bfree;
119         statbuf->UserBlocksAvail = sbuf.f_bavail;
120         statbuf->TotalFileNodes = sbuf.f_files;
121         statbuf->FreeFileNodes = sbuf.f_ffree;
122         statbuf->FsIdentifier = *(uint64_t *)(&sbuf.f_fsid); /* Ick. */
123         statbuf->FsCapabilities = darwin_fs_capabilities(sbuf.f_mntonname);
124
125         return 0;
126 }
127 #endif
128
129 /* 
130  sys_statvfs() is an abstraction layer over system-dependent statvfs()/statfs()
131  for particular POSIX systems. Due to controversy of what is considered more important
132  between LSB and FreeBSD/POSIX.1 (IEEE Std 1003.1-2001) we need to abstract the interface
133  so that particular OS would use its preffered interface.
134 */
135 int sys_statvfs(const char *path, vfs_statvfs_struct *statbuf)
136 {
137 #if defined(LINUX) && defined(HAVE_FSID_INT)
138         return linux_statvfs(path, statbuf);
139 #elif defined(DARWINOS)
140         return darwin_statvfs(path, statbuf);
141 #else
142         /* BB change this to return invalid level */
143 #ifdef EOPNOTSUPP
144         return EOPNOTSUPP;
145 #else
146         return -1;
147 #endif /* EOPNOTSUPP */
148 #endif /* LINUX */
149
150 }