1 /* fstype.c -- determine type of filesystems that files are on
2 Copyright (C) 1990, 91, 92, 93, 94, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
33 # define _(Text) gettext (Text)
38 # define N_(String) gettext_noop (String)
40 # define N_(String) (String)
43 static char *filesystem_type_uncached
PARAMS((char *path
, char *relpath
, struct stat
*statp
));
45 #ifdef FSTYPE_MNTENT /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
48 # if defined(MNT_MNTTAB) /* HP-UX. */
49 # define MOUNTED MNT_MNTTAB
51 # if defined(MNTTABNAME) /* Dynix. */
52 # define MOUNTED MNTTABNAME
57 #ifdef FSTYPE_GETMNT /* Ultrix. */
58 #include <sys/param.h>
59 #include <sys/mount.h>
60 #include <sys/fs_types.h>
63 #ifdef FSTYPE_USG_STATFS /* SVR3. */
64 #include <sys/statfs.h>
65 #include <sys/fstyp.h>
68 #ifdef FSTYPE_STATVFS /* SVR4. */
69 #include <sys/statvfs.h>
70 #include <sys/fstyp.h>
73 #ifdef FSTYPE_STATFS /* 4.4BSD. */
74 #include <sys/param.h> /* NetBSD needs this. */
75 #include <sys/mount.h>
77 #ifndef MFSNAMELEN /* NetBSD defines this. */
82 #ifdef INITMOUNTNAMES /* Defined in 4.4BSD, not in NET/2. */
83 static char *mn
[] = INITMOUNTNAMES
;
84 if (t
>= 0 && t
<= MOUNT_MAXTYPE
)
88 #else /* !INITMOUNTNAMES */
126 #endif /* !INITMOUNTNAMES */
128 #endif /* !MFSNAMELEN */
129 #endif /* FSTYPE_STATFS */
131 #ifdef FSTYPE_AIX_STATFS /* AIX. */
132 #include <sys/vmount.h>
133 #include <sys/statfs.h>
135 #define FSTYPE_STATFS /* Otherwise like 4.4BSD. */
136 #define f_type f_vfstype
145 #if 0 /* NFS filesystems are actually MNT_AIX. */
158 #endif /* FSTYPE_AIX_STATFS */
161 #include <netinet/in.h>
162 #include <afs/venus.h>
164 /* On SunOS 4, afs/vice.h defines this to rely on a pre-ANSI cpp. */
166 #define _VICEIOCTL(id) ((unsigned int ) _IOW('V', id, struct ViceIoctl))
169 /* AFS on Solaris 2.3 doesn't get this definition. */
170 #include <sys/ioccom.h>
177 static char space
[2048];
181 vi
.out_size
= sizeof (space
);
184 if (pioctl (path
, VIOC_FILE_CELL_NAME
, &vi
, 1)
185 && (errno
== EINVAL
|| errno
== ENOENT
))
191 /* Nonzero if the current filesystem's type is known. */
192 static int fstype_known
= 0;
194 /* Return a static string naming the type of filesystem that the file PATH,
195 described by STATP, is on.
196 RELPATH is the file name relative to the current directory.
197 Return "unknown" if its filesystem type is unknown. */
200 filesystem_type (char *path
, char *relpath
, struct stat
*statp
)
202 static char *current_fstype
= NULL
;
203 static dev_t current_dev
;
205 if (current_fstype
!= NULL
)
207 if (fstype_known
&& statp
->st_dev
== current_dev
)
208 return current_fstype
; /* Cached value. */
209 free (current_fstype
);
211 current_dev
= statp
->st_dev
;
212 current_fstype
= filesystem_type_uncached (path
, relpath
, statp
);
213 return current_fstype
;
216 /* Return a newly allocated string naming the type of filesystem that the
217 file PATH, described by STATP, is on.
218 RELPATH is the file name relative to the current directory.
219 Return "unknown" if its filesystem type is unknown. */
222 filesystem_type_uncached (char *path
, char *relpath
, struct stat
*statp
)
226 #ifdef FSTYPE_MNTENT /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
227 char *table
= MOUNTED
;
231 mfp
= setmntent (table
, "r");
233 error (1, errno
, "%s", table
);
235 /* Find the entry with the same device number as STATP, and return
236 that entry's fstype. */
237 while (type
== NULL
&& (mnt
= getmntent (mfp
)))
241 struct stat disk_stats
;
243 #ifdef MNTTYPE_IGNORE
244 if (!strcmp (mnt
->mnt_type
, MNTTYPE_IGNORE
))
248 /* Newer systems like SunOS 4.1 keep the dev number in the mtab,
249 in the options string. For older systems, we need to stat the
250 directory that the filesystem is mounted on to get it.
252 Unfortunately, the HPUX 9.x mnttab entries created by automountq
253 contain a dev= option but the option value does not match the
254 st_dev value of the file (maybe the lower 16 bits match?). */
256 #if !defined(hpux) && !defined(__hpux__)
257 devopt
= strstr (mnt
->mnt_opts
, "dev=");
262 if (devopt
[0] == '0' && (devopt
[1] == 'x' || devopt
[1] == 'X'))
264 xstrtoumax (devopt
, NULL
, 16, &u
, NULL
);
268 #endif /* not hpux */
270 if (stat (mnt
->mnt_dir
, &disk_stats
) == -1) {
274 error (1, errno
, _("error in %s: %s"), table
, mnt
->mnt_dir
);
276 dev
= disk_stats
.st_dev
;
279 if (dev
== statp
->st_dev
)
280 type
= mnt
->mnt_type
;
283 if (endmntent (mfp
) == 0)
284 error (0, errno
, "%s", table
);
287 #ifdef FSTYPE_GETMNT /* Ultrix. */
292 && getmnt (&offset
, &fsd
, sizeof (fsd
), NOSTAT_MANY
, 0) > 0)
294 if (fsd
.fd_req
.dev
== statp
->st_dev
)
295 type
= gt_names
[fsd
.fd_req
.fstype
];
299 #ifdef FSTYPE_USG_STATFS /* SVR3. */
301 char typebuf
[FSTYPSZ
];
303 if (statfs (relpath
, &fss
, sizeof (struct statfs
), 0) == -1)
305 /* Don't die if a file was just removed. */
307 error (1, errno
, "%s", path
);
309 else if (!sysfs (GETFSTYP
, fss
.f_fstyp
, typebuf
))
313 #ifdef FSTYPE_STATVFS /* SVR4. */
316 if (statvfs (relpath
, &fss
) == -1)
318 /* Don't die if a file was just removed. */
320 error (1, errno
, "%s", path
);
323 type
= fss
.f_basetype
;
326 #ifdef FSTYPE_STATFS /* 4.4BSD. */
330 if (S_ISLNK (statp
->st_mode
))
331 p
= dirname (relpath
);
335 if (statfs (p
, &fss
) == -1)
337 /* Don't die if symlink to nonexisting file, or a file that was
340 error (1, errno
, "%s", path
);
344 #ifdef HAVE_F_FSTYPENAME_IN_STATFS
345 type
= xstrdup (fss
.f_fstypename
);
347 type
= fstype_to_string (fss
.f_type
);
355 if ((!type
|| !strcmp (type
, "xx")) && in_afs (relpath
))
359 /* An unknown value can be caused by an ENOENT error condition.
360 Don't cache those values. */
361 fstype_known
= (type
!= NULL
);
363 return xstrdup (type
? type
: _("unknown"));