]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/fstype.c
Convert to pure GPL v2 license.
[bacula/bacula] / bacula / src / findlib / fstype.c
1 /*
2  *  Implement routines to determine file system types.
3  *
4  *   Written by Preben 'Peppe' Guldberg, December MMIV
5  *
6  *   Version $Id$
7  */
8 /*
9    Bacula® - The Network Backup Solution
10
11    Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
12
13    The main author of Bacula is Kern Sibbald, with contributions from
14    many others, a complete list can be found in the file AUTHORS.
15    This program is Free Software; you can redistribute it and/or
16    modify it under the terms of version two of the GNU General Public
17    License as published by the Free Software Foundation and included
18    in the file LICENSE.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28    02110-1301, USA.
29
30    Bacula® is a registered trademark of John Walker.
31    The licensor of Bacula is the Free Software Foundation Europe
32    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
33    Switzerland, email:ftf@fsfeurope.org.
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_WIN32\n"
57 #define false              0
58 #define true               1
59 #define bstrncpy           strncpy
60 #define Dmsg0(n,s)         fprintf(stderr, s)
61 #define Dmsg1(n,s,a1)      fprintf(stderr, s, a1)
62 #define Dmsg2(n,s,a1,a2)   fprintf(stderr, s, a1, a2)
63 #endif
64
65 /*
66  * These functions should be implemented for each OS
67  *
68  *       bool fstype(const char *fname, char *fs, int fslen);
69  */
70 #if defined(HAVE_DARWIN_OS) \
71    || defined(HAVE_FREEBSD_OS ) \
72    || defined(HAVE_OPENBSD_OS)
73
74 #include <sys/param.h>
75 #include <sys/mount.h>
76
77 bool fstype(const char *fname, char *fs, int fslen)
78 {
79    struct statfs st;
80    if (statfs(fname, &st) == 0) {
81       bstrncpy(fs, st.f_fstypename, fslen);
82       return true;
83    }
84    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
85    return false;
86 }
87 #elif defined(HAVE_NETBSD_OS)
88 #include <sys/param.h>
89 #include <sys/mount.h>
90 #ifdef HAVE_SYS_STATVFS_H
91 #include <sys/statvfs.h>
92 #else
93 #define statvfs statfs
94 #endif
95
96 bool fstype(const char *fname, char *fs, int fslen)
97 {
98    struct statvfs st;
99    if (statvfs(fname, &st) == 0) {
100       bstrncpy(fs, st.f_fstypename, fslen);
101       return true;
102    }
103    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
104    return false;
105 }
106 #elif defined(HAVE_HPUX_OS) \
107    || defined(HAVE_IRIX_OS)
108
109 #include <sys/types.h>
110 #include <sys/statvfs.h>
111
112 bool fstype(const char *fname, char *fs, int fslen)
113 {
114    struct statvfs st;
115    if (statvfs(fname, &st) == 0) {
116       bstrncpy(fs, st.f_basetype, fslen);
117       return true;
118    }
119    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
120    return false;
121 }
122
123 #elif defined(HAVE_LINUX_OS)
124
125 #include <sys/vfs.h>
126
127 bool fstype(const char *fname, char *fs, int fslen)
128 {
129    struct statfs st;
130    if (statfs(fname, &st) == 0) {
131       /*
132        * Values nicked from statfs(2), testing and
133        *
134        *    $ grep -r SUPER_MAGIC /usr/include/linux
135        *
136        * Entries are sorted on ("fsname")
137        */
138       switch (st.f_type) {
139
140       /* Known good values */
141       case 0xef53:         bstrncpy(fs, "ext2", fslen); return true;          /* EXT2_SUPER_MAGIC */
142    /* case 0xef53:         ext2 and ext3 are the same */                      /* EXT3_SUPER_MAGIC */
143       case 0x3153464a:     bstrncpy(fs, "jfs", fslen); return true;           /* JFS_SUPER_MAGIC */
144       case 0x5346544e:     bstrncpy(fs, "ntfs", fslen); return true;          /* NTFS_SB_MAGIC */
145       case 0x9fa0:         bstrncpy(fs, "proc", fslen); return true;          /* PROC_SUPER_MAGIC */
146       case 0x52654973:     bstrncpy(fs, "reiserfs", fslen); return true;      /* REISERFS_SUPER_MAGIC */
147       case 0x58465342:     bstrncpy(fs, "xfs", fslen); return true;           /* XFS_SB_MAGIC */
148       case 0x9fa2:         bstrncpy(fs, "usbdevfs", fslen); return true;      /* USBDEVICE_SUPER_MAGIC */
149       case 0x62656572:     bstrncpy(fs, "sysfs", fslen); return true;         /* SYSFS_MAGIC */
150       case 0x517B:         bstrncpy(fs, "smbfs", fslen); return true;         /* SMB_SUPER_MAGIC */
151       case 0x9660:         bstrncpy(fs, "iso9660", fslen); return true;       /* ISOFS_SUPER_MAGIC */
152
153 #if 0       /* These need confirmation */
154       case 0xadf5:         bstrncpy(fs, "adfs", fslen); return true;          /* ADFS_SUPER_MAGIC */
155       case 0xadff:         bstrncpy(fs, "affs", fslen); return true;          /* AFFS_SUPER_MAGIC */
156       case 0x6B414653:     bstrncpy(fs, "afs", fslen); return true;           /* AFS_FS_MAGIC */
157       case 0x0187:         bstrncpy(fs, "autofs", fslen); return true;        /* AUTOFS_SUPER_MAGIC */
158       case 0x62646576:     bstrncpy(fs, "bdev", fslen); return true;          /* ??? */
159       case 0x42465331:     bstrncpy(fs, "befs", fslen); return true;          /* BEFS_SUPER_MAGIC */
160       case 0x1BADFACE:     bstrncpy(fs, "bfs", fslen); return true;           /* BFS_MAGIC */
161       case 0x42494e4d:     bstrncpy(fs, "binfmt_misc", fslen); return true;   /* ??? */
162       case (('C'<<8)|'N'): bstrncpy(fs, "capifs", fslen); return true;        /* CAPIFS_SUPER_MAGIC */
163       case 0xFF534D42:     bstrncpy(fs, "cifs", fslen); return true;          /* CIFS_MAGIC_NUMBER */
164       case 0x73757245:     bstrncpy(fs, "coda", fslen); return true;          /* CODA_SUPER_MAGIC */
165       case 0x012ff7b7:     bstrncpy(fs, "coherent", fslen); return true;      /* COH_SUPER_MAGIC */
166       case 0x28cd3d45:     bstrncpy(fs, "cramfs", fslen); return true;        /* CRAMFS_MAGIC */
167       case 0x1373:         bstrncpy(fs, "devfs", fslen); return true;         /* DEVFS_SUPER_MAGIC */
168       case 0x1cd1:         bstrncpy(fs, "devpts", fslen); return true;        /* ??? */
169       case 0x414A53:       bstrncpy(fs, "efs", fslen); return true;           /* EFS_SUPER_MAGIC */
170       case 0x03111965:     bstrncpy(fs, "eventpollfs", fslen); return true;   /* EVENTPOLLFS_MAGIC */
171       case 0x137d:         bstrncpy(fs, "ext", fslen); return true;           /* EXT_SUPER_MAGIC */
172       case 0xef51:         bstrncpy(fs, "ext2", fslen); return true;          /* EXT2_OLD_SUPER_MAGIC */
173       case 0xBAD1DEA:      bstrncpy(fs, "futexfs", fslen); return true;       /* ??? */
174       case 0xaee71ee7:     bstrncpy(fs, "gadgetfs", fslen); return true;      /* GADGETFS_MAGIC */
175       case 0x00c0ffee:     bstrncpy(fs, "hostfs", fslen); return true;        /* HOSTFS_SUPER_MAGIC */
176       case 0xf995e849:     bstrncpy(fs, "hpfs", fslen); return true;          /* HPFS_SUPER_MAGIC */
177       case 0xb00000ee:     bstrncpy(fs, "hppfs", fslen); return true;         /* HPPFS_SUPER_MAGIC */
178       case 0x958458f6:     bstrncpy(fs, "hugetlbfs", fslen); return true;     /* HUGETLBFS_MAGIC */
179       case 0x12061983:     bstrncpy(fs, "hwgfs", fslen); return true;         /* HWGFS_MAGIC */
180       case 0x66726f67:     bstrncpy(fs, "ibmasmfs", fslen); return true;      /* IBMASMFS_MAGIC */
181       case 0x9660:         bstrncpy(fs, "isofs", fslen); return true;         /* ISOFS_SUPER_MAGIC */
182       case 0x07c0:         bstrncpy(fs, "jffs", fslen); return true;          /* JFFS_MAGIC_SB_BITMASK */
183       case 0x72b6:         bstrncpy(fs, "jffs2", fslen); return true;         /* JFFS2_SUPER_MAGIC */
184       case 0x2468:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX2_SUPER_MAGIC */
185       case 0x2478:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX2_SUPER_MAGIC2 */
186       case 0x137f:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX_SUPER_MAGIC */
187       case 0x138f:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX_SUPER_MAGIC2 */
188       case 0x19800202:     bstrncpy(fs, "mqueue", fslen); return true;        /* MQUEUE_MAGIC */
189       case 0x4d44:         bstrncpy(fs, "msdos", fslen); return true;         /* MSDOS_SUPER_MAGIC */
190       case 0x564c:         bstrncpy(fs, "ncpfs", fslen); return true;         /* NCP_SUPER_MAGIC */
191       case 0x6969:         bstrncpy(fs, "nfs", fslen); return true;           /* NFS_SUPER_MAGIC */
192       case 0x9fa1:         bstrncpy(fs, "openpromfs", fslen); return true;    /* OPENPROM_SUPER_MAGIC */
193       case 0x6f70726f:     bstrncpy(fs, "oprofilefs", fslen); return true;    /* OPROFILEFS_MAGIC */
194       case 0xa0b4d889:     bstrncpy(fs, "pfmfs", fslen); return true;         /* PFMFS_MAGIC */
195       case 0x50495045:     bstrncpy(fs, "pipfs", fslen); return true;         /* PIPEFS_MAGIC */
196       case 0x002f:         bstrncpy(fs, "qnx4", fslen); return true;          /* QNX4_SUPER_MAGIC */
197       case 0x858458f6:     bstrncpy(fs, "ramfs", fslen); return true;         /* RAMFS_MAGIC */
198       case 0x7275:         bstrncpy(fs, "romfs", fslen); return true;         /* ROMFS_MAGIC */
199       case 0x858458f6:     bstrncpy(fs, "rootfs", fslen); return true;        /* RAMFS_MAGIC */
200       case 0x67596969:     bstrncpy(fs, "rpc_pipefs", fslen); return true;    /* RPCAUTH_GSSMAGIC */
201       case 0x534F434B:     bstrncpy(fs, "sockfs", fslen); return true;        /* SOCKFS_MAGIC */
202       case 0x012ff7b6:     bstrncpy(fs, "sysv2", fslen); return true;         /* SYSV2_SUPER_MAGIC */
203       case 0x012ff7b5:     bstrncpy(fs, "sysv4", fslen); return true;         /* SYSV4_SUPER_MAGIC */
204       case 0x858458f6:     bstrncpy(fs, "tmpfs", fslen); return true;         /* RAMFS_MAGIC */
205       case 0x01021994:     bstrncpy(fs, "tmpfs", fslen); return true;         /* TMPFS_MAGIC */
206       case 0x15013346:     bstrncpy(fs, "udf", fslen); return true;           /* UDF_SUPER_MAGIC */
207       case 0x00011954:     bstrncpy(fs, "ufs", fslen); return true;           /* UFS_MAGIC */
208       case 0xa501FCF5:     bstrncpy(fs, "vxfs", fslen); return true;          /* VXFS_SUPER_MAGIC */
209       case 0x012ff7b4:     bstrncpy(fs, "xenix", fslen); return true;         /* XENIX_SUPER_MAGIC */
210       case 0x012fd16d:     bstrncpy(fs, "xiafs", fslen); return true;         /* _XIAFS_SUPER_MAGIC */
211 #endif
212
213       default:
214          Dmsg2(10, "Unknown file system type \"0x%x\" for \"%s\".\n", st.f_type,
215                fname);
216          return false;
217       }
218    }
219    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
220    return false;
221 }
222
223 #elif defined(HAVE_SUN_OS)
224
225 #include <sys/types.h>
226 #include <sys/stat.h>
227
228 bool fstype(const char *fname, char *fs, int fslen)
229 {
230    struct stat st;
231    if (lstat(fname, &st) == 0) {
232       bstrncpy(fs, st.st_fstype, fslen);
233       return true;
234    }
235    Dmsg1(50, "lstat() failed for \"%s\"\n", fname);
236    return false;
237 }
238
239 #elif defined (__digital__) && defined (__unix__)  /* Tru64 */
240 /* Tru64 */
241 #include <sys/stat.h>
242 #include <sys/mount.h>
243
244 bool fstype(const char *fname, char *fs, int fslen)
245 {
246    struct statfs st;
247    if (statfs((char *)fname, &st) == 0) {
248       switch (st.f_type) {
249       /* Known good values */
250       case 0xa:         bstrncpy(fs, "advfs", fslen); return true;        /* Tru64 AdvFS */
251       case 0xe:         bstrncpy(fs, "nfs", fslen); return true;          /* Tru64 NFS   */
252       default:
253          Dmsg2(10, "Unknown file system type \"0x%x\" for \"%s\".\n", st.f_type,
254                fname);
255          return false;
256       }
257    }
258    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
259    return false;
260 }
261 /* Tru64 */
262
263 #elif defined (HAVE_WIN32)
264 /* Windows */
265
266 bool fstype(const char *fname, char *fs, int fslen)
267 {
268    DWORD componentlength;
269    DWORD fsflags;
270    CHAR rootpath[4];
271    UINT oldmode;
272    BOOL result;
273
274    /* Copy Drive Letter, colon, and backslash to rootpath */
275    bstrncpy(rootpath, fname, sizeof(rootpath));
276
277    /* We don't want any popups if there isn't any media in the drive */
278    oldmode = SetErrorMode(SEM_FAILCRITICALERRORS);
279
280    result = GetVolumeInformation(rootpath, NULL, 0, NULL, &componentlength, &fsflags, fs, fslen);
281
282    SetErrorMode(oldmode);
283
284    if (result) {
285       /* Windows returns NTFS, FAT, etc.  Make it lowercase to be consistent with other OSes */
286       lcase(fs);
287    } else {
288       Dmsg2(10, "GetVolumeInformation() failed for \"%s\", Error = %d.\n", rootpath, GetLastError());
289    }
290
291    return result != 0;
292 }
293 /* Windows */
294
295 #else    /* No recognised OS */
296
297 bool fstype(const char *fname, char *fs, int fslen)
298 {
299    Dmsg0(10, "!!! fstype() not implemented for this OS. !!!\n");
300 #ifdef TEST_PROGRAM
301    Dmsg1(10, "Please define one of the following when compiling:\n\n%s\n",
302          SUPPORTEDOSES);
303    exit(EXIT_FAILURE);
304 #endif
305
306    return false;
307 }
308 #endif
309
310 #ifdef TEST_PROGRAM
311 int main(int argc, char **argv)
312 {
313    char *p;
314    char fs[1000];
315    int status = 0;
316
317    if (argc < 2) {
318       p = (argc < 1) ? "fstype" : argv[0];
319       printf("usage:\t%s path ...\n"
320             "\t%s prints the file system type and pathname of the paths.\n",
321             p, p);
322       return EXIT_FAILURE;
323    }
324    while (*++argv) {
325       if (!fstype(*argv, fs, sizeof(fs))) {
326          status = EXIT_FAILURE;
327       } else {
328          printf("%s\t%s\n", fs, *argv);
329       }
330    }
331    return status;
332 }
333 #endif