blob: 4cd6b161894ca1069a5df3ebd2ef0ddbe63a1fb7 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include <mntent.h>
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000012
Eric Andersenaad1a882001-03-16 22:47:14 +000013/*
14 * Given a block device, find the mount table entry if that block device
15 * is mounted.
16 *
17 * Given any other file (or directory), find the mount table entry for its
18 * filesystem.
19 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020struct mntent* FAST_FUNC find_mount_point(const char *name, const char *table)
Eric Andersenaad1a882001-03-16 22:47:14 +000021{
22 struct stat s;
23 dev_t mountDevice;
24 FILE *mountTable;
25 struct mntent *mountEntry;
26
27 if (stat(name, &s) != 0)
28 return 0;
29
30 if ((s.st_mode & S_IFMT) == S_IFBLK)
31 mountDevice = s.st_rdev;
32 else
33 mountDevice = s.st_dev;
34
35
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000036 mountTable = setmntent(table ? table : bb_path_mtab_file, "r");
37 if (!mountTable)
Eric Andersenaad1a882001-03-16 22:47:14 +000038 return 0;
39
40 while ((mountEntry = getmntent(mountTable)) != 0) {
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000041 if (strcmp(name, mountEntry->mnt_dir) == 0
42 || strcmp(name, mountEntry->mnt_fsname) == 0
43 ) { /* String match. */
Eric Andersenaad1a882001-03-16 22:47:14 +000044 break;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000045 }
Eric Andersenaad1a882001-03-16 22:47:14 +000046 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
47 break;
48 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
49 break;
50 }
51 endmntent(mountTable);
52 return mountEntry;
53}