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