2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Implement routines to determine file system types.
31 * Written by Preben 'Peppe' Guldberg, December MMIV
42 #else /* Set up for testing a stand alone program */
47 #define SUPPORTEDOSES \
60 #define bstrncpy strncpy
61 #define bstrcmp(s1, s2) !strcmp(s1, s2)
62 #define Dmsg0(n,s) fprintf(stderr, s)
63 #define Dmsg1(n,s,a1) fprintf(stderr, s, a1)
64 #define Dmsg2(n,s,a1,a2) fprintf(stderr, s, a1, a2)
68 * These functions should be implemented for each OS
70 * bool fstype(const char *fname, char *fs, int fslen);
72 #if defined(HAVE_DARWIN_OS) || \
73 defined(HAVE_FREEBSD_OS) || \
74 defined(HAVE_OPENBSD_OS)
76 #include <sys/param.h>
77 #include <sys/mount.h>
79 bool fstype(const char *fname, char *fs, int fslen)
83 if (statfs(fname, &st) == 0) {
84 bstrncpy(fs, st.f_fstypename, fslen);
88 Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
92 #elif defined(HAVE_NETBSD_OS)
93 #include <sys/param.h>
94 #include <sys/mount.h>
95 #ifdef HAVE_SYS_STATVFS_H
96 #include <sys/statvfs.h>
98 #define statvfs statfs
101 bool fstype(const char *fname, char *fs, int fslen)
105 if (statvfs(fname, &st) == 0) {
106 bstrncpy(fs, st.f_fstypename, fslen);
110 Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
114 #elif defined(HAVE_HPUX_OS) || \
115 defined(HAVE_IRIX_OS)
117 #include <sys/types.h>
118 #include <sys/statvfs.h>
120 bool fstype(const char *fname, char *fs, int fslen)
124 if (statvfs(fname, &st) == 0) {
125 bstrncpy(fs, st.f_basetype, fslen);
129 Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
133 #elif defined(HAVE_LINUX_OS) || \
134 defined(HAVE_OSF1_OS)
136 #include <sys/stat.h>
137 #include "lib/mntent_cache.h"
139 bool fstype(const char *fname, char *fs, int fslen)
142 mntent_cache_entry_t *mce;
144 if (lstat(fname, &st) == 0) {
145 if ((mce = find_mntent_mapping(st.st_dev)) != NULL) {
146 bstrncpy(fs, mce->fstype, fslen);
152 Dmsg1(50, "lstat() failed for \"%s\"\n", fname);
156 #elif defined(HAVE_SUN_OS)
158 #include <sys/types.h>
159 #include <sys/stat.h>
161 bool fstype(const char *fname, char *fs, int fslen)
165 if (lstat(fname, &st) == 0) {
166 bstrncpy(fs, st.st_fstype, fslen);
170 Dmsg1(50, "lstat() failed for \"%s\"\n", fname);
174 #elif defined (HAVE_WIN32)
177 bool fstype(const char *fname, char *fs, int fslen)
179 DWORD componentlength;
185 /* Copy Drive Letter, colon, and backslash to rootpath */
186 bstrncpy(rootpath, fname, sizeof(rootpath));
188 /* We don't want any popups if there isn't any media in the drive */
189 oldmode = SetErrorMode(SEM_FAILCRITICALERRORS);
191 result = GetVolumeInformation(rootpath, NULL, 0, NULL, &componentlength, &fsflags, fs, fslen);
193 SetErrorMode(oldmode);
196 /* Windows returns NTFS, FAT, etc. Make it lowercase to be consistent with other OSes */
199 Dmsg2(10, "GetVolumeInformation() failed for \"%s\", Error = %d.\n", rootpath, GetLastError());
207 #else /* No recognised OS */
209 bool fstype(const char *fname, char *fs, int fslen)
211 Dmsg0(10, "!!! fstype() not implemented for this OS. !!!\n");
213 Dmsg1(10, "Please define one of the following when compiling:\n\n%s\n",
223 * Compare function build on top of fstype, OS independent.
225 * bool fstype_equals(const char *fname, const char *fstypename);
227 bool fstype_equals(const char *fname, const char *fstypename)
229 char fs_typename[128];
231 if (fstype(fname, fs_typename, sizeof(fs_typename))) {
232 return bstrcmp(fs_typename, fstypename);
239 int main(int argc, char **argv)
246 p = (argc < 1) ? "fstype" : argv[0];
247 printf("usage:\t%s path ...\n"
248 "\t%s prints the file system type and pathname of the paths.\n",
253 if (!fstype(*argv, fs, sizeof(fs))) {
254 status = EXIT_FAILURE;
256 printf("%s\t%s\n", fs, *argv);