]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/paths.c
abb29a237e80684ea9541abc9f73c405dedf6858
[thirdparty/xfsprogs-dev.git] / libfrog / paths.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2005-2006 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include <paths.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include "paths.h"
16 #include "input.h"
17 #include "projects.h"
18 #include <limits.h>
19
20 extern char *progname;
21
22 int fs_count;
23 int xfs_fs_count;
24 struct fs_path *fs_table;
25 struct fs_path *fs_path;
26
27 char *mtab_file;
28 #define PROC_MOUNTS "/proc/self/mounts"
29
30 static int
31 fs_device_number(
32 const char *name,
33 dev_t *devnum)
34 {
35 struct stat sbuf;
36
37 if (stat(name, &sbuf) < 0)
38 return errno;
39 /*
40 * We want to match st_rdev if the path provided is a device
41 * special file. Otherwise we are looking for the the
42 * device id for the containing filesystem, in st_dev.
43 */
44 if (S_ISBLK(sbuf.st_mode) || S_ISCHR(sbuf.st_mode))
45 *devnum = sbuf.st_rdev;
46 else
47 *devnum = sbuf.st_dev;
48
49 return 0;
50 }
51
52 /*
53 * Find the FS table entry for the given path. The "flags" argument
54 * is a mask containing FS_MOUNT_POINT or FS_PROJECT_PATH (or both)
55 * to indicate the type of table entry sought.
56 * fs_table_lookup() finds the fs table entry for the filesystem hosting
57 * the file represented in the "dir" argument. To compare against actual
58 * mount point entries, use fs_table_lookup_mount() instead.
59 */
60 struct fs_path *
61 fs_table_lookup(
62 const char *dir,
63 uint flags)
64 {
65 uint i;
66 dev_t dev = 0;
67
68 if (fs_device_number(dir, &dev))
69 return NULL;
70
71 for (i = 0; i < fs_count; i++) {
72 if (flags && !(flags & fs_table[i].fs_flags))
73 continue;
74 if (fs_table[i].fs_datadev == dev)
75 return &fs_table[i];
76 }
77 return NULL;
78 }
79
80 static struct fs_path *
81 __fs_table_lookup_mount(
82 const char *dir,
83 const char *blkdev)
84 {
85 uint i;
86 char rpath[PATH_MAX];
87 char dpath[PATH_MAX];
88
89 if (!dir && !blkdev)
90 return NULL;
91
92 if (dir && !realpath(dir, dpath))
93 return NULL;
94 if (blkdev && !realpath(blkdev, dpath))
95 return NULL;
96
97 for (i = 0; i < fs_count; i++) {
98 if (fs_table[i].fs_flags != FS_MOUNT_POINT)
99 continue;
100 if (dir && !realpath(fs_table[i].fs_dir, rpath))
101 continue;
102 if (blkdev && !realpath(fs_table[i].fs_name, rpath))
103 continue;
104 if (strcmp(rpath, dpath) == 0)
105 return &fs_table[i];
106 }
107 return NULL;
108 }
109
110 /*
111 * Find the FS table entry describing an actual mount for the given path.
112 * Unlike fs_table_lookup(), fs_table_lookup_mount() compares the "dir"
113 * argument to actual mount point entries in the table. Accordingly, it
114 * will find matches only if the "dir" argument is indeed mounted.
115 */
116 struct fs_path *
117 fs_table_lookup_mount(
118 const char *dir)
119 {
120 return __fs_table_lookup_mount(dir, NULL);
121 }
122
123 /*
124 * Find the FS table entry describing an actual mount for the block device.
125 * Unlike fs_table_lookup(), fs_table_lookup_blkdev() compares the "bdev"
126 * argument to actual mount point names in the table. Accordingly, it
127 * will find matches only if the "bdev" argument is indeed mounted.
128 */
129 struct fs_path *
130 fs_table_lookup_blkdev(
131 const char *bdev)
132 {
133 return __fs_table_lookup_mount(NULL, bdev);
134 }
135
136 static int
137 fs_table_insert(
138 char *dir,
139 uint prid,
140 uint flags,
141 char *fsname,
142 char *fslog,
143 char *fsrt)
144 {
145 dev_t datadev, logdev, rtdev;
146 struct fs_path *tmp_fs_table;
147 int error;
148
149 datadev = logdev = rtdev = 0;
150 error = fs_device_number(dir, &datadev);
151 if (error)
152 goto out_nodev;
153 if (fslog) {
154 error = fs_device_number(fslog, &logdev);
155 if (error)
156 goto out_nodev;
157 }
158 if (fsrt) {
159 error = fs_device_number(fsrt, &rtdev);
160 if (error)
161 goto out_nodev;
162 }
163
164 if (!platform_test_xfs_path(dir))
165 flags |= FS_FOREIGN;
166
167 /*
168 * Make copies of the directory and data device path.
169 * The log device and real-time device, if non-null,
170 * are already the result of strdup() calls so we
171 * don't need to duplicate those. In fact, this
172 * function is assumed to "consume" both of those
173 * pointers, meaning if an error occurs they will
174 * both get freed.
175 */
176 error = ENOMEM;
177 dir = strdup(dir);
178 if (!dir)
179 goto out_nodev;
180 fsname = strdup(fsname);
181 if (!fsname)
182 goto out_noname;
183
184 tmp_fs_table = realloc(fs_table, sizeof(fs_path_t) * (fs_count + 1));
185 if (!tmp_fs_table)
186 goto out_norealloc;
187 fs_table = tmp_fs_table;
188
189 /* Put foreign filesystems at the end, xfs filesystems at the front */
190 if (flags & FS_FOREIGN || fs_count == 0) {
191 fs_path = &fs_table[fs_count];
192 } else {
193 /* move foreign fs entries down, insert new one just before */
194 memmove(&fs_table[xfs_fs_count + 1], &fs_table[xfs_fs_count],
195 sizeof(fs_path_t)*(fs_count - xfs_fs_count));
196 fs_path = &fs_table[xfs_fs_count];
197 }
198 fs_path->fs_dir = dir;
199 fs_path->fs_prid = prid;
200 fs_path->fs_flags = flags;
201 fs_path->fs_name = fsname;
202 fs_path->fs_log = fslog;
203 fs_path->fs_rt = fsrt;
204 fs_path->fs_datadev = datadev;
205 fs_path->fs_logdev = logdev;
206 fs_path->fs_rtdev = rtdev;
207 fs_count++;
208 if (!(flags & FS_FOREIGN))
209 xfs_fs_count++;
210
211 return 0;
212
213 out_norealloc:
214 free(fsname);
215 out_noname:
216 free(dir);
217 out_nodev:
218 /* "Consume" fslog and fsrt even if there's an error */
219 free(fslog);
220 free(fsrt);
221
222 return error;
223 }
224
225 /* Remove all the cached entries in the fs table. */
226 void
227 fs_table_destroy(void)
228 {
229 int i;
230 struct fs_path *fsp;
231
232 for (i = 0, fsp = fs_table; i < fs_count; i++, fsp++) {
233 free(fsp->fs_name);
234 free(fsp->fs_dir);
235 free(fsp->fs_log);
236 free(fsp->fs_rt);
237 }
238
239 fs_count = 0;
240 xfs_fs_count = 0;
241 free(fs_table);
242 fs_table = NULL;
243 }
244
245 /*
246 * Table iteration (cursor-based) interfaces
247 */
248
249 /*
250 * Initialize an fs_table cursor. If a directory path is supplied,
251 * the cursor is set up to appear as though the table contains only
252 * a single entry which represents the directory specified.
253 * Otherwise it is set up to prepare for visiting all entries in the
254 * global table, starting with the first. "flags" can be either
255 * FS_MOUNT_POINT or FS_PROJECT_PATH to limit what type of entries
256 * will be selected by fs_cursor_next_entry(). 0 can be used as a
257 * wild card (selecting either type).
258 */
259 void
260 fs_cursor_initialise(
261 char *dir,
262 uint flags,
263 fs_cursor_t *cur)
264 {
265 fs_path_t *path;
266
267 memset(cur, 0, sizeof(*cur));
268 if (dir) {
269 if ((path = fs_table_lookup(dir, flags)) == NULL)
270 return;
271 cur->local = *path;
272 cur->count = 1;
273 cur->table = &cur->local;
274 } else {
275 cur->count = fs_count;
276 cur->table = fs_table;
277 }
278 cur->flags = flags;
279 }
280
281 /*
282 * Use the cursor to find the next entry in the table having the
283 * type specified by the cursor's "flags" field.
284 */
285 struct fs_path *
286 fs_cursor_next_entry(
287 fs_cursor_t *cur)
288 {
289 while (cur->index < cur->count) {
290 fs_path_t *next = &cur->table[cur->index++];
291
292 if (!cur->flags || (cur->flags & next->fs_flags))
293 return next;
294 }
295 return NULL;
296 }
297
298
299 #if defined(HAVE_GETMNTENT)
300 #include <mntent.h>
301
302 /*
303 * Determines whether the "logdev" or "rtdev" mount options are
304 * present for the given mount point. If so, the value for each (a
305 * device path) is returned in the pointers whose addresses are
306 * provided. The pointers are assigned NULL for an option not
307 * present. Note that the path buffers returned are allocated
308 * dynamically and it is the caller's responsibility to free them.
309 */
310 static int
311 fs_extract_mount_options(
312 struct mntent *mnt,
313 char **logp,
314 char **rtp)
315 {
316 char *fslog, *fsrt;
317
318 /*
319 * Extract log device and realtime device from mount options.
320 *
321 * Note: the glibc hasmntopt implementation requires that the
322 * character in mnt_opts immediately after the search string
323 * must be a NULL ('\0'), a comma (','), or an equals ('=').
324 * Therefore we cannot search for 'logdev=' directly.
325 */
326 if ((fslog = hasmntopt(mnt, "logdev")) && fslog[6] == '=')
327 fslog += 7;
328 if ((fsrt = hasmntopt(mnt, "rtdev")) && fsrt[5] == '=')
329 fsrt += 6;
330
331 /* Do this only after we've finished processing mount options */
332 if (fslog) {
333 fslog = strndup(fslog, strcspn(fslog, " ,"));
334 if (!fslog)
335 goto out_nomem;
336 }
337 if (fsrt) {
338 fsrt = strndup(fsrt, strcspn(fsrt, " ,"));
339 if (!fsrt) {
340 free(fslog);
341 goto out_nomem;
342 }
343 }
344 *logp = fslog;
345 *rtp = fsrt;
346
347 return 0;
348
349 out_nomem:
350 *logp = NULL;
351 *rtp = NULL;
352 fprintf(stderr, _("%s: unable to extract mount options for \"%s\"\n"),
353 progname, mnt->mnt_dir);
354 return ENOMEM;
355 }
356
357 /*
358 * If *path is NULL, initialize the fs table with all xfs mount points in mtab
359 * If *path is specified, search for that path in mtab
360 *
361 * Everything - path, devices, and mountpoints - are boiled down to realpath()
362 * for comparison, but fs_table is populated with what comes from getmntent.
363 */
364 static int
365 fs_table_initialise_mounts(
366 char *path)
367 {
368 struct mntent *mnt;
369 FILE *mtp;
370 char *fslog, *fsrt;
371 int error, found;
372 char rpath[PATH_MAX], rmnt_fsname[PATH_MAX], rmnt_dir[PATH_MAX];
373
374 error = found = 0;
375 fslog = fsrt = NULL;
376
377 if (!mtab_file) {
378 mtab_file = PROC_MOUNTS;
379 if (access(mtab_file, R_OK) != 0)
380 mtab_file = MOUNTED;
381 }
382
383 if ((mtp = setmntent(mtab_file, "r")) == NULL)
384 return ENOENT;
385
386 /* Use realpath to resolve symlinks, relative paths, etc */
387 if (path)
388 if (!realpath(path, rpath))
389 return errno;
390
391 while ((mnt = getmntent(mtp)) != NULL) {
392 if (!strcmp(mnt->mnt_type, "autofs"))
393 continue;
394 if (!realpath(mnt->mnt_dir, rmnt_dir))
395 continue;
396 if (!realpath(mnt->mnt_fsname, rmnt_fsname))
397 continue;
398
399 if (path &&
400 ((strcmp(rpath, rmnt_dir) != 0) &&
401 (strcmp(rpath, rmnt_fsname) != 0)))
402 continue;
403 if (fs_extract_mount_options(mnt, &fslog, &fsrt))
404 continue;
405 (void) fs_table_insert(mnt->mnt_dir, 0, FS_MOUNT_POINT,
406 mnt->mnt_fsname, fslog, fsrt);
407 if (path) {
408 found = 1;
409 break;
410 }
411 }
412 endmntent(mtp);
413
414 if (path && !found)
415 error = ENXIO;
416
417 return error;
418 }
419
420 #else
421 # error "How do I extract info about mounted filesystems on this platform?"
422 #endif
423
424 /*
425 * Given a directory, match it up to a filesystem mount point.
426 */
427 static struct fs_path *
428 fs_mount_point_from_path(
429 const char *dir)
430 {
431 fs_cursor_t cursor;
432 fs_path_t *fs;
433 dev_t dev = 0;
434
435 if (fs_device_number(dir, &dev))
436 return NULL;
437
438 fs_cursor_initialise(NULL, FS_MOUNT_POINT, &cursor);
439 while ((fs = fs_cursor_next_entry(&cursor))) {
440 if (fs->fs_datadev == dev)
441 break;
442 }
443 return fs;
444 }
445
446 static void
447 fs_table_insert_mount(
448 char *mount)
449 {
450 int error;
451
452 error = fs_table_initialise_mounts(mount);
453 if (error)
454 fprintf(stderr, _("%s: cannot setup path for mount %s: %s\n"),
455 progname, mount, strerror(error));
456 }
457
458 static int
459 fs_table_initialise_projects(
460 char *project)
461 {
462 fs_project_path_t *path;
463 fs_path_t *fs;
464 prid_t prid = 0;
465 int error = 0, found = 0;
466
467 if (project)
468 prid = prid_from_string(project);
469
470 setprpathent();
471 while ((path = getprpathent()) != NULL) {
472 if (project && prid != path->pp_prid)
473 continue;
474 fs = fs_mount_point_from_path(path->pp_pathname);
475 if (!fs) {
476 fprintf(stderr, _("%s: cannot find mount point for path `%s': %s\n"),
477 progname, path->pp_pathname, strerror(errno));
478 continue;
479 }
480 (void) fs_table_insert(path->pp_pathname, path->pp_prid,
481 FS_PROJECT_PATH, fs->fs_name,
482 NULL, NULL);
483 if (project) {
484 found = 1;
485 break;
486 }
487 }
488 endprpathent();
489
490 if (project && !found)
491 error = ENOENT;
492
493 return error;
494 }
495
496 static void
497 fs_table_insert_project(
498 char *project)
499 {
500 int error;
501
502 error = fs_table_initialise_projects(project);
503 if (error)
504 fprintf(stderr, _("%s: cannot setup path for project %s: %s\n"),
505 progname, project, strerror(error));
506 }
507
508 /*
509 * Initialize fs_table to contain the given set of mount points and
510 * projects. If mount_count is zero, mounts is ignored and the
511 * table is populated with mounted filesystems. If project_count is
512 * zero, projects is ignored and the table is populated with all
513 * projects defined in the projects file.
514 */
515 void
516 fs_table_initialise(
517 int mount_count,
518 char *mounts[],
519 int project_count,
520 char *projects[])
521 {
522 int error;
523 int i;
524
525 if (mount_count) {
526 for (i = 0; i < mount_count; i++)
527 fs_table_insert_mount(mounts[i]);
528 } else {
529 error = fs_table_initialise_mounts(NULL);
530 if (error)
531 goto out_error;
532 }
533 if (project_count) {
534 for (i = 0; i < project_count; i++)
535 fs_table_insert_project(projects[i]);
536 } else {
537 error = fs_table_initialise_projects(NULL);
538 if (error)
539 goto out_error;
540 }
541
542 return;
543
544 out_error:
545 fprintf(stderr, _("%s: cannot initialise path table: %s\n"),
546 progname, strerror(error));
547 }
548
549 int
550 fs_table_insert_project_path(
551 char *dir,
552 prid_t prid)
553 {
554 fs_path_t *fs;
555 int error = 0;
556
557 fs = fs_mount_point_from_path(dir);
558 if (fs)
559 error = fs_table_insert(dir, prid, FS_PROJECT_PATH,
560 fs->fs_name, NULL, NULL);
561 else
562 error = ENOENT;
563
564 return error;
565 }