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