]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/fstype.c
Added support for TRU64 Extended Attributes interface.
[bacula/bacula] / bacula / src / findlib / fstype.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2007 Free Software Foundation Europe e.V.
5
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
11    in the file LICENSE.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28 /*
29  *  Implement routines to determine file system types.
30  *
31  *   Written by Preben 'Peppe' Guldberg, December MMIV
32  *
33  *   Version $Id$
34  */
35
36
37 #ifndef TEST_PROGRAM
38
39 #include "bacula.h"
40 #include "find.h"
41
42 #else /* Set up for testing a stand alone program */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #define SUPPORTEDOSES \
48    "HAVE_DARWIN_OS\n" \
49    "HAVE_FREEBSD_OS\n" \
50    "HAVE_HPUX_OS\n" \
51    "HAVE_IRIX_OS\n" \
52    "HAVE_LINUX_OS\n" \
53    "HAVE_NETBSD_OS\n" \
54    "HAVE_OPENBSD_OS\n" \
55    "HAVE_SUN_OS\n" \
56    "HAVE_TRU64_OS\n" \
57    "HAVE_WIN32\n"
58 #define false              0
59 #define true               1
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)
65 #endif
66
67 /*
68  * These functions should be implemented for each OS
69  *
70  * bool fstype(const char *fname, char *fs, int fslen);
71  */
72 #if defined(HAVE_DARWIN_OS) || \
73     defined(HAVE_FREEBSD_OS) || \
74     defined(HAVE_OPENBSD_OS)
75
76 #include <sys/param.h>
77 #include <sys/mount.h>
78
79 bool fstype(const char *fname, char *fs, int fslen)
80 {
81    struct statfs st;
82
83    if (statfs(fname, &st) == 0) {
84       bstrncpy(fs, st.f_fstypename, fslen);
85       return true;
86    }
87
88    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
89    return false;
90 }
91
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>
97 #else
98 #define statvfs statfs
99 #endif
100
101 bool fstype(const char *fname, char *fs, int fslen)
102 {
103    struct statvfs st;
104
105    if (statvfs(fname, &st) == 0) {
106       bstrncpy(fs, st.f_fstypename, fslen);
107       return true;
108    }
109
110    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
111    return false;
112 }
113
114 #elif defined(HAVE_HPUX_OS) || \
115       defined(HAVE_IRIX_OS)
116
117 #include <sys/types.h>
118 #include <sys/statvfs.h>
119
120 bool fstype(const char *fname, char *fs, int fslen)
121 {
122    struct statvfs st;
123
124    if (statvfs(fname, &st) == 0) {
125       bstrncpy(fs, st.f_basetype, fslen);
126       return true;
127    }
128
129    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
130    return false;
131 }
132
133 #elif defined(HAVE_LINUX_OS) || \
134       defined(HAVE_TRU64_OS)
135
136 #include <sys/stat.h>
137 #include "lib/mntent_cache.h"
138
139 bool fstype(const char *fname, char *fs, int fslen)
140 {
141    struct stat st;
142    mntent_cache_entry_t *mce;
143
144    if (lstat(fname, &st) == 0) {
145       if ((mce = find_mntent_mapping(st.st_dev)) != NULL) {
146          bstrncpy(fs, mce->fstype, fslen);
147          return true;
148       }
149       return false;
150    }
151
152    Dmsg1(50, "lstat() failed for \"%s\"\n", fname);
153    return false;
154 }
155
156 #elif defined(HAVE_SUN_OS)
157
158 #include <sys/types.h>
159 #include <sys/stat.h>
160
161 bool fstype(const char *fname, char *fs, int fslen)
162 {
163    struct stat st;
164
165    if (lstat(fname, &st) == 0) {
166       bstrncpy(fs, st.st_fstype, fslen);
167       return true;
168    }
169
170    Dmsg1(50, "lstat() failed for \"%s\"\n", fname);
171    return false;
172 }
173
174 #elif defined (HAVE_WIN32)
175 /* Windows */
176
177 bool fstype(const char *fname, char *fs, int fslen)
178 {
179    DWORD componentlength;
180    DWORD fsflags;
181    CHAR rootpath[4];
182    UINT oldmode;
183    BOOL result;
184
185    /* Copy Drive Letter, colon, and backslash to rootpath */
186    bstrncpy(rootpath, fname, sizeof(rootpath));
187
188    /* We don't want any popups if there isn't any media in the drive */
189    oldmode = SetErrorMode(SEM_FAILCRITICALERRORS);
190
191    result = GetVolumeInformation(rootpath, NULL, 0, NULL, &componentlength, &fsflags, fs, fslen);
192
193    SetErrorMode(oldmode);
194
195    if (result) {
196       /* Windows returns NTFS, FAT, etc.  Make it lowercase to be consistent with other OSes */
197       lcase(fs);
198    } else {
199       Dmsg2(10, "GetVolumeInformation() failed for \"%s\", Error = %d.\n", rootpath, GetLastError());
200    }
201
202    return result != 0;
203 }
204
205 /* Windows */
206
207 #else    /* No recognised OS */
208
209 bool fstype(const char *fname, char *fs, int fslen)
210 {
211    Dmsg0(10, "!!! fstype() not implemented for this OS. !!!\n");
212 #ifdef TEST_PROGRAM
213    Dmsg1(10, "Please define one of the following when compiling:\n\n%s\n",
214          SUPPORTEDOSES);
215    exit(EXIT_FAILURE);
216 #endif
217
218    return false;
219 }
220 #endif
221
222 /*
223  * Compare function build on top of fstype, OS independent.
224  *
225  * bool fstype_equals(const char *fname, const char *fstypename);
226  */
227 bool fstype_equals(const char *fname, const char *fstypename)
228 {
229    char fs_typename[128];
230
231    if (fstype(fname, fs_typename, sizeof(fs_typename))) {
232       return bstrcmp(fs_typename, fstypename);
233    }
234
235    return false;
236 }
237
238 #ifdef TEST_PROGRAM
239 int main(int argc, char **argv)
240 {
241    char *p;
242    char fs[1000];
243    int status = 0;
244
245    if (argc < 2) {
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",
249             p, p);
250       return EXIT_FAILURE;
251    }
252    while (*++argv) {
253       if (!fstype(*argv, fs, sizeof(fs))) {
254          status = EXIT_FAILURE;
255       } else {
256          printf("%s\t%s\n", fs, *argv);
257       }
258    }
259    return status;
260 }
261 #endif