]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/fstype.c
Integrate Nicolas' patch for direct DVD support.
[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 /*
10    Copyright (C) 2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #ifndef TEST_PROGRAM
30
31 #include "bacula.h"
32 #include "find.h"
33
34 #else /* Set up for testing a stand alone program */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #define SUPPORTEDOSES \
40    "HAVE_DARWIN_OS\n" \
41    "HAVE_FREEBSD_OS\n" \
42    "HAVE_HPUX_OS\n" \
43    "HAVE_IRIX_OS\n" \
44    "HAVE_LINUX_OS\n" \
45    "HAVE_NETBSD_OS\n" \
46    "HAVE_OPENBSD_OS\n" \
47    "HAVE_SUN_OS\n"
48 #define POOLMEM            char
49 #define bstrdup            strdup
50 #define Dmsg0(n,s)         fprintf(stderr, s);
51 #define Dmsg1(n,s,a1)      fprintf(stderr, s, a1);
52 #define Dmsg2(n,s,a1,a2)   fprintf(stderr, s, a1, a2);
53 #endif
54
55 /*
56  * These functions should be implemented for each OS
57  *
58  *       POOLMEM *fstype(const char *fname);
59  */
60 #if defined(HAVE_DARWIN_OS) \
61    || defined(HAVE_FREEBSD_OS ) \
62    || defined(HAVE_NETBSD_OS) \
63    || defined(HAVE_OPENBSD_OS)
64
65 #include <sys/param.h>
66 #include <sys/mount.h>
67
68 bool fstype(const char *fname, char *fs, int fslen)
69 {
70    struct statfs st;
71    if (statfs(fname, &st) == 0) {
72       bstrncpy(fs, st.f_fstypename, fslen);
73       return true;
74    }
75    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
76    return false;
77 }
78
79 #elif defined(HAVE_HPUX_OS) \
80    || defined(HAVE_IRIX_OS)
81
82 #include <sys/types.h>
83 #include <sys/statvfs.h>
84
85 bool fstype(const char *fname, char *fs, int fslen)
86 {
87    struct statvfs st;
88    if (statvfs(fname, &st) == 0) {
89       bstrncpy(fs, st.f_basetype, fslen);
90       return true;
91    }
92    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
93    return false;
94 }
95
96 #elif defined(HAVE_LINUX_OS)
97
98 #include <sys/vfs.h>
99
100 bool fstype(const char *fname, char *fs, int fslen)
101 {
102    struct statfs st;
103    if (statfs(fname, &st) == 0) {
104       /*
105        * Values nicked from statfs(2), testing and
106        *
107        *    $ grep -r SUPER_MAGIC /usr/include/linux
108        *
109        * Entries are sorted on ("fsname")
110        */
111       switch (st.f_type) {
112
113 #if 0       /* These need confirmation */
114       case 0xadf5:         bstrncpy(fs, "adfs", fslen); return true;          /* ADFS_SUPER_MAGIC */
115       case 0xadff:         bstrncpy(fs, "affs", fslen); return true;          /* AFFS_SUPER_MAGIC */
116       case 0x6B414653:     bstrncpy(fs, "afs", fslen); return true;           /* AFS_FS_MAGIC */
117       case 0x0187:         bstrncpy(fs, "autofs", fslen); return true;        /* AUTOFS_SUPER_MAGIC */
118       case 0x62646576:     bstrncpy(fs, "bdev", fslen); return true;          /* ??? */
119       case 0x42465331:     bstrncpy(fs, "befs", fslen); return true;          /* BEFS_SUPER_MAGIC */
120       case 0x1BADFACE:     bstrncpy(fs, "bfs", fslen); return true;           /* BFS_MAGIC */
121       case 0x42494e4d:     bstrncpy(fs, "binfmt_misc", fslen); return true;   /* ??? */
122       case (('C'<<8)|'N'): bstrncpy(fs, "capifs", fslen); return true;        /* CAPIFS_SUPER_MAGIC */
123       case 0xFF534D42:     bstrncpy(fs, "cifs", fslen); return true;          /* CIFS_MAGIC_NUMBER */
124       case 0x73757245:     bstrncpy(fs, "coda", fslen); return true;          /* CODA_SUPER_MAGIC */
125       case 0x012ff7b7:     bstrncpy(fs, "coherent", fslen); return true;      /* COH_SUPER_MAGIC */
126       case 0x28cd3d45:     bstrncpy(fs, "cramfs", fslen); return true;        /* CRAMFS_MAGIC */
127       case 0x1373:         bstrncpy(fs, "devfs", fslen); return true;         /* DEVFS_SUPER_MAGIC */
128       case 0x1cd1:         bstrncpy(fs, "devpts", fslen); return true;        /* ??? */
129       case 0x414A53:       bstrncpy(fs, "efs", fslen); return true;           /* EFS_SUPER_MAGIC */
130       case 0x03111965:     bstrncpy(fs, "eventpollfs", fslen); return true;   /* EVENTPOLLFS_MAGIC */
131       case 0x137d:         bstrncpy(fs, "ext", fslen); return true;           /* EXT_SUPER_MAGIC */
132       case 0xef51:         bstrncpy(fs, "ext2", fslen); return true;          /* EXT2_OLD_SUPER_MAGIC */
133       case 0xBAD1DEA:      bstrncpy(fs, "futexfs", fslen); return true;       /* ??? */
134       case 0xaee71ee7:     bstrncpy(fs, "gadgetfs", fslen); return true;      /* GADGETFS_MAGIC */
135       case 0x00c0ffee:     bstrncpy(fs, "hostfs", fslen); return true;        /* HOSTFS_SUPER_MAGIC */
136       case 0xf995e849:     bstrncpy(fs, "hpfs", fslen); return true;          /* HPFS_SUPER_MAGIC */
137       case 0xb00000ee:     bstrncpy(fs, "hppfs", fslen); return true;         /* HPPFS_SUPER_MAGIC */
138       case 0x958458f6:     bstrncpy(fs, "hugetlbfs", fslen); return true;     /* HUGETLBFS_MAGIC */
139       case 0x12061983:     bstrncpy(fs, "hwgfs", fslen); return true;         /* HWGFS_MAGIC */
140       case 0x66726f67:     bstrncpy(fs, "ibmasmfs", fslen); return true;      /* IBMASMFS_MAGIC */
141       case 0x9660:         bstrncpy(fs, "iso9660", fslen); return true;       /* ISOFS_SUPER_MAGIC */
142       case 0x9660:         bstrncpy(fs, "isofs", fslen); return true;         /* ISOFS_SUPER_MAGIC */
143       case 0x07c0:         bstrncpy(fs, "jffs", fslen); return true;          /* JFFS_MAGIC_SB_BITMASK */
144       case 0x72b6:         bstrncpy(fs, "jffs2", fslen); return true;         /* JFFS2_SUPER_MAGIC */
145       case 0x3153464a:     bstrncpy(fs, "jfs", fslen); return true;           /* JFS_SUPER_MAGIC */
146       case 0x2468:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX2_SUPER_MAGIC */
147       case 0x2478:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX2_SUPER_MAGIC2 */
148       case 0x137f:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX_SUPER_MAGIC */
149       case 0x138f:         bstrncpy(fs, "minix", fslen); return true;         /* MINIX_SUPER_MAGIC2 */
150       case 0x19800202:     bstrncpy(fs, "mqueue", fslen); return true;        /* MQUEUE_MAGIC */
151       case 0x4d44:         bstrncpy(fs, "msdos", fslen); return true;         /* MSDOS_SUPER_MAGIC */
152       case 0x564c:         bstrncpy(fs, "ncpfs", fslen); return true;         /* NCP_SUPER_MAGIC */
153       case 0x6969:         bstrncpy(fs, "nfs", fslen); return true;           /* NFS_SUPER_MAGIC */
154       case 0x5346544e:     bstrncpy(fs, "ntfs", fslen); return true;          /* NTFS_SB_MAGIC */
155       case 0x9fa1:         bstrncpy(fs, "openpromfs", fslen); return true;    /* OPENPROM_SUPER_MAGIC */
156       case 0x6f70726f:     bstrncpy(fs, "oprofilefs", fslen); return true;    /* OPROFILEFS_MAGIC */
157       case 0xa0b4d889:     bstrncpy(fs, "pfmfs", fslen); return true;         /* PFMFS_MAGIC */
158       case 0x50495045:     bstrncpy(fs, "pipfs", fslen); return true;         /* PIPEFS_MAGIC */
159       case 0x9fa0:         bstrncpy(fs, "proc", fslen); return true;          /* PROC_SUPER_MAGIC */
160       case 0x002f:         bstrncpy(fs, "qnx4", fslen); return true;          /* QNX4_SUPER_MAGIC */
161       case 0x858458f6:     bstrncpy(fs, "ramfs", fslen); return true;         /* RAMFS_MAGIC */
162       case 0x52654973:     bstrncpy(fs, "reiserfs", fslen); return true;      /* REISERFS_SUPER_MAGIC */
163       case 0x7275:         bstrncpy(fs, "romfs", fslen); return true;         /* ROMFS_MAGIC */
164       case 0x858458f6:     bstrncpy(fs, "rootfs", fslen); return true;        /* RAMFS_MAGIC */
165       case 0x67596969:     bstrncpy(fs, "rpc_pipefs", fslen); return true;    /* RPCAUTH_GSSMAGIC */
166       case 0x517B:         bstrncpy(fs, "smbfs", fslen); return true;         /* SMB_SUPER_MAGIC */
167       case 0x534F434B:     bstrncpy(fs, "sockfs", fslen); return true;        /* SOCKFS_MAGIC */
168       case 0x62656572:     bstrncpy(fs, "sysfs", fslen); return true;         /* SYSFS_MAGIC */
169       case 0x012ff7b6:     bstrncpy(fs, "sysv2", fslen); return true;         /* SYSV2_SUPER_MAGIC */
170       case 0x012ff7b5:     bstrncpy(fs, "sysv4", fslen); return true;         /* SYSV4_SUPER_MAGIC */
171       case 0x858458f6:     bstrncpy(fs, "tmpfs", fslen); return true;         /* RAMFS_MAGIC */
172       case 0x01021994:     bstrncpy(fs, "tmpfs", fslen); return true;         /* TMPFS_MAGIC */
173       case 0x15013346:     bstrncpy(fs, "udf", fslen); return true;           /* UDF_SUPER_MAGIC */
174       case 0x00011954:     bstrncpy(fs, "ufs", fslen); return true;           /* UFS_MAGIC */
175       case 0x9fa2:         bstrncpy(fs, "usbdevfs", fslen); return true;      /* USBDEVICE_SUPER_MAGIC */
176       case 0xa501FCF5:     bstrncpy(fs, "vxfs", fslen); return true;          /* VXFS_SUPER_MAGIC */
177       case 0x012ff7b4:     bstrncpy(fs, "xenix", fslen); return true;         /* XENIX_SUPER_MAGIC */
178       case 0x58465342:     bstrncpy(fs, "xfs", fslen); return true;           /* XFS_SB_MAGIC */
179       case 0x012fd16d:     bstrncpy(fs, "xiafs", fslen); return true;         /* _XIAFS_SUPER_MAGIC */
180
181       case 0xef53:         bstrncpy(fs, "ext2", fslen); return true;          /* EXT2_SUPER_MAGIC */
182    /* case 0xef53:         ext2 and ext3 are the same */    /* EXT3_SUPER_MAGIC */
183 #else       /* Known good values */
184 #endif
185
186       default:
187          Dmsg2(10, "Unknown file system type \"0x%x\" for \"%s\".\n", st.f_type,
188                fname);
189          return false;
190       }
191    }
192    Dmsg1(50, "statfs() failed for \"%s\"\n", fname);
193    return false;
194 }
195
196 #elif defined(HAVE_SUN_OS)
197
198 #include <sys/types.h>
199 #include <sys/stat.h>
200
201 bool fstype(const char *fname, char *fs, int fslen)
202 {
203    struct stat st;
204    if (lstat(fname, &st) == 0) {
205       bstrncpy(fs, st.st_fstype, fslen);
206       return true;
207    }
208    Dmsg1(50, "lstat() failed for \"%s\"\n", fname);
209    return false;
210 }
211
212 #else    /* No recognised OS */
213
214 bool fstype(const char *fname, char *fs, int fslen)
215 {
216    Dmsg0(10, "!!! fstype() not implemented for this OS. !!!\n");
217
218 #ifdef TEST_PROGRAM
219    Dmsg1(10, "Please define one of the following when compiling:\n\n%s\n",
220          SUPPORTEDOSES);
221    exit(EXIT_FAILURE);
222 #endif
223
224    return false;
225 }
226 #endif
227
228 #ifdef TEST_PROGRAM
229 int main(int argc, char **argv)
230 {
231    char *p;
232    int status = 0;
233
234    if (argc < 2) {
235       p = (argc < 1) ? "fstype" : argv[0];
236       printf("usage:\t%s path ...\n"
237             "\t%s prints the file system type and pathname of the paths.\n",
238             p, p);
239       return EXIT_FAILURE;
240    }
241    while (*++argv) {
242       if ((p = fstype(*argv)) == NULL) {
243          status = EXIT_FAILURE;
244       } else {
245          printf("%s\t%s\n", p, *argv);
246       }
247    }
248    return status;
249 }
250 #endif