summaryrefslogtreecommitdiff
path: root/find/fstype.c
blob: 8d5879c7509d434c4d5c28d64d6b1cab4f49a21e (plain)
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
314
/* fstype.c -- determine type of file systems that files are on
   Copyright (C) 1990-2022 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 <https://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).
 */

/* config.h must be included first. */
#include <config.h>

/* system headers. */
#include <errno.h>
#include <fcntl.h>
#if HAVE_MNTENT_H
# include <mntent.h>
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_SYS_MKDEV_H
# include <sys/mkdev.h>
#endif
#ifdef HAVE_SYS_MNTIO_H
# include <sys/mntio.h>
#endif
#if HAVE_SYS_MNTTAB_H
# include <sys/mnttab.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

/* gnulib headers. */
#include "dirname.h"
#include "xalloc.h"
#include "xstrtol.h"
#include "mountlist.h"

/* find headers. */
#include "defs.h"
#include "die.h"
#include "extendbuf.h"
#include "system.h"

static char *file_system_type_uncached (const struct stat *statp,
                                        const char *path,
                                        bool *fstype_known);


static void
free_file_system_list (struct mount_entry *p)
{
  while (p)
    {
      struct mount_entry *pnext = p->me_next;
      free_mount_entry (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 */

/* Read the mount list into a static cache, and return it.
   This is a wrapper around gnulib's read_file_system_list ()
   to avoid unnecessary reading of the mount list.  */
static struct mount_entry *
get_file_system_list (bool need_fs_type)
{
  /* Local cache for the mount list.  */
  static struct mount_entry *mount_list = NULL;

  /* Remember if the list contains the ME_TYPE members.  */
  static bool has_fstype = false;

  if (mount_list && ! has_fstype && need_fs_type)
    {
      free_file_system_list (mount_list);
      mount_list = NULL;
    }
  if (! mount_list)
    {
      mount_list = read_file_system_list(need_fs_type);
      has_fstype = need_fs_type;
    }
  return mount_list;
}

/* 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)
{
  /* Nonzero if the current file system's type is known.  */
  static bool fstype_known = false;

  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, &fstype_known);
  return current_fstype;
}

bool
is_used_fs_type(const char *name)
{
  if (0 == strcmp("afs", name))
    {
      /* I guess AFS may not appear in /etc/mtab (or equivalent) but still be in use,
	 so assume we always need to check for AFS.  */
      return true;
    }
  else
    {
      const struct mount_entry *entries = get_file_system_list(false);
      if (entries)
	{
	  const struct mount_entry *entry;
	  for (entry = entries; entry; entry = entry->me_next)
	    {
	      if (0 == strcmp(name, entry->me_type))
		return true;
	    }
	}
      else
	{
	  return true;
	}
    }
  return false;
}

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 */
}

/* 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,
                           bool *fstype_known)
{
  struct mount_entry *entries, *entry, *best;
  char *type;

  (void) path;

#ifdef AFS
  if (in_afs (path))
    {
      *fstype_known = true;
      return xstrdup ("afs");
    }
#endif

  best = NULL;
  entries = get_file_system_list (true);
  if (NULL == entries)
    {
      /* We cannot determine for sure which file we were trying to
       * use because gnulib has abstracted all that stuff away.
       * Hence we cannot issue a specific error message here.
       */
      die (EXIT_FAILURE, 0, _("Cannot read mounted file system list"));
    }
  for (type=NULL, entry=entries; entry; entry=entry->me_next)
    {
#ifdef MNTTYPE_IGNORE
      if (!strcmp (entry->me_type, MNTTYPE_IGNORE))
	continue;
#endif
      if (0 == set_fstype_devno (entry))
	{
	  if (entry->me_dev == statp->st_dev)
	    {
	      best = entry;
	      /* Don't exit the loop, because some systems (for
		 example Linux-based systems in which /etc/mtab is a
		 symlink to /proc/mounts) can have duplicate entries
		 in the filesystem list.  This happens most frequently
		 for /.
	      */
	    }
	}
    }
  if (best)
    {
      type = xstrdup (best->me_type);
    }

  /* Don't cache unknown values. */
  *fstype_known = (type != NULL);

  return type ? type : xstrdup (_("unknown"));
}


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;

  /* Ignore read_file_system_list () not returning a valid 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)
    {
      void *p = extendbuf (result, sizeof(dev_t)*(used+1), &alloc_size);
      if (p)
	{
	  result = p;
	  if (0 == set_fstype_devno (entry))
	    {
	      result[used] = entry->me_dev;
	      ++used;
	    }
	}
      else
	{
	  free (result);
	  result = NULL;
	}
    }
  free_file_system_list (entries);
  if (result)
    {
      *n = used;
    }
  return result;
}