1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/* fstype.c -- determine type of file systems that files are on
Copyright (C) 1990, 91, 92, 93, 94, 2000, 2004 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Written by David MacKenzie <djm@gnu.org>.
*
* Converted to use gnulib's read_file_system_list()
* by James Youngman <jay@gnu.org> (which saves a lot
* of manual hacking of configure.in).
*/
#include <config.h>
#include <errno.h>
#include <stdbool.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <sys/stat.h>
/* The presence of unistd.h is assumed by gnulib these days, so we
* might as well assume it too.
*/
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_SYS_MNTIO_H
#include <sys/mntio.h>
#endif
#ifdef HAVE_SYS_MKDEV_H
#include <sys/mkdev.h>
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#else
extern int errno;
#endif
#include "defs.h"
#include "../gnulib/lib/dirname.h"
#include "xalloc.h"
#include "modetype.h"
/* Need declaration of function `xstrtoumax' */
#include "../gnulib/lib/xstrtol.h"
#include "extendbuf.h"
#include "mountlist.h"
#include "error.h"
#if ENABLE_NLS
# include <libintl.h>
# define _(Text) gettext (Text)
#else
# define _(Text) Text
#endif
#ifdef gettext_noop
# define N_(String) gettext_noop (String)
#else
/* See locate.c for explanation as to why not use (String) */
# define N_(String) String
#endif
static char *file_system_type_uncached PARAMS((const struct stat *statp, const char *path));
/* Get MNTTYPE_IGNORE if it is available. */
#if HAVE_MNTENT_H
# include <mntent.h>
#endif
#if HAVE_SYS_MNTTAB_H
# include <stdio.h>
# include <sys/mnttab.h>
#endif
static void
free_file_system_list(struct mount_entry *p)
{
while (p)
{
struct mount_entry *pnext = p->me_next;
free(p->me_devname);
free(p->me_mountdir);
if(p->me_type_malloced)
free(p->me_type);
p->me_next = NULL;
free(p);
p = pnext;
}
}
#ifdef AFS
#include <netinet/in.h>
#include <afs/venus.h>
#if __STDC__
/* On SunOS 4, afs/vice.h defines this to rely on a pre-ANSI cpp. */
#undef _VICEIOCTL
#define _VICEIOCTL(id) ((unsigned int ) _IOW('V', id, struct ViceIoctl))
#endif
#ifndef _IOW
/* AFS on Solaris 2.3 doesn't get this definition. */
#include <sys/ioccom.h>
#endif
static int
in_afs (char *path)
{
static char space[2048];
struct ViceIoctl vi;
vi.in_size = 0;
vi.out_size = sizeof (space);
vi.out = space;
if (pioctl (path, VIOC_FILE_CELL_NAME, &vi, 1)
&& (errno == EINVAL || errno == ENOENT))
return 0;
return 1;
}
#endif /* AFS */
/* Nonzero if the current file system's type is known. */
static int fstype_known = 0;
/* Return a static string naming the type of file system that the file PATH,
described by STATP, is on.
RELPATH is the file name relative to the current directory.
Return "unknown" if its file system type is unknown. */
char *
filesystem_type (const struct stat *statp, const char *path)
{
static char *current_fstype = NULL;
static dev_t current_dev;
if (current_fstype != NULL)
{
if (fstype_known && statp->st_dev == current_dev)
return current_fstype; /* Cached value. */
free (current_fstype);
}
current_dev = statp->st_dev;
current_fstype = file_system_type_uncached (statp, path);
return current_fstype;
}
static int
set_fstype_devno(struct mount_entry *p)
{
struct stat stbuf;
if (p->me_dev == (dev_t)-1)
{
set_stat_placeholders(&stbuf);
if (0 == (options.xstat)(p->me_mountdir, &stbuf))
{
p->me_dev = stbuf.st_dev;
return 0;
}
else
{
return -1;
}
}
return 0; /* not needed */
}
static struct mount_entry *
must_read_fs_list(bool need_fs_type)
{
struct mount_entry *entries = read_file_system_list(need_fs_type);
if (NULL == entries)
{
/* We cannot determine for sure which file we were trying to
* use because gnulib has extracted all that stuff away.
* Hence we cannot issue a specific error message here.
*/
error(1, 0, "Cannot read mounted file system list");
}
return entries;
}
/* Return a newly allocated string naming the type of file system that the
file PATH, described by STATP, is on.
RELPATH is the file name relative to the current directory.
Return "unknown" if its file system type is unknown. */
static char *
file_system_type_uncached (const struct stat *statp, const char *path)
{
struct mount_entry *entries, *entry;
char *type;
(void) path;
#ifdef AFS
if (in_afs(path))
{
fstype_known = 1;
return xstrdup("afs");
}
#endif
entries = must_read_fs_list(true);
for (type=NULL, entry=entries; entry; entry=entry->me_next)
{
#ifdef MNTTYPE_IGNORE
if (!strcmp (entry->me_type, MNTTYPE_IGNORE))
continue;
#endif
set_fstype_devno(entry);
if (entry->me_dev == statp->st_dev)
{
type = xstrdup(entry->me_type);
break;
}
}
free_file_system_list(entries);
/* Don't cache unknown values. */
fstype_known = (type != NULL);
return type ? type : xstrdup(_("unknown"));
}
char *
get_mounted_filesystems (void)
{
char *result = NULL;
size_t alloc_size = 0u;
size_t used = 0u;
struct mount_entry *entries, *entry;
entries = must_read_fs_list(false);
for (entry=entries; entry; entry=entry->me_next)
{
size_t len;
#ifdef MNTTYPE_IGNORE
if (!strcmp (entry->me_type, MNTTYPE_IGNORE))
continue;
#endif
set_fstype_devno(entry);
len = strlen(entry->me_mountdir) + 1;
result = extendbuf(result, used+len, &alloc_size);
strcpy(&result[used], entry->me_mountdir);
used += len; /* len already includes one for the \0 */
}
free_file_system_list(entries);
return result;
}
dev_t *
get_mounted_devices (size_t *n)
{
size_t alloc_size = 0u;
size_t used = 0u;
struct mount_entry *entries, *entry;
dev_t *result = NULL;
/* Use read_file_system_list() rather than must_read_fs_list()
* because on some system this is always called at startup,
* and find should only exit fatally if it needs to use the
* result of this operation. If we can't get the fs list
* but we never need the information, there is no need to fail.
*/
for (entry = entries = read_file_system_list(false);
entry;
entry = entry->me_next)
{
result = extendbuf(result, sizeof(dev_t)*(used+1), &alloc_size);
set_fstype_devno(entry);
result[used] = entry->me_dev;
++used;
}
free_file_system_list(entries);
*n = used;
return result;
}
|