2001-03-17 04:17:14 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2001-10-24 10:30:29 +05:30
|
|
|
* Utility routines.
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2006-07-10 17:11:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2007-04-14 05:29:52 +05:30
|
|
|
/* Find block device /dev/XXX which contains specified file
|
|
|
|
* We handle /dev/dir/dir/dir too, at a cost of ~80 more bytes code */
|
|
|
|
|
|
|
|
/* Do not reallocate all this stuff on each recursion */
|
|
|
|
enum { DEVNAME_MAX = 256 };
|
|
|
|
struct arena {
|
|
|
|
struct stat st;
|
|
|
|
dev_t dev;
|
|
|
|
/* Was PATH_MAX, but we recurse _/dev_. We can assume
|
|
|
|
* people are not crazy enough to have mega-deep tree there */
|
|
|
|
char devpath[DEVNAME_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *find_block_device_in_dir(struct arena *ap)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2007-04-14 05:29:52 +05:30
|
|
|
char *retpath = NULL;
|
|
|
|
int len, rem;
|
|
|
|
|
|
|
|
dir = opendir(ap->devpath);
|
|
|
|
if (!dir)
|
|
|
|
return NULL;
|
Major rewrite of mount, umount, losetup. Untangled lots of code, shrunk
things down a bit, fixed a number of funky corner cases, added support for
several new features (things like mount --move, mount --bind, lazy unounts,
automatic detection of loop mounts, and so on). Probably broke several
other things, but it's fixable. (Bang on it, tell me what doesn't work for
you...)
Note: you no longer need to say "-o loop". It does that for you when
necessary.
Still need to add "user mount" support, which involves making mount suid. Not
too hard to do under the new infrastructure, just haven't done it yet...
The previous code had the following notes, that belong in the version
control comments:
- * 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
- * searches through fstab when -a is passed
- * will try mounting stuff with all fses when passed -t auto
- *
- * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
- *
- * 1999-10-07 Erik Andersen <andersen@codepoet.org>.
- * Rewrite of a lot of code. Removed mtab usage (I plan on
- * putting it back as a compile-time option some time),
- * major adjustments to option parsing, and some serious
- * dieting all around.
- *
- * 1999-11-06 mtab support is back - andersee
- *
- * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
- * mount to add loop support.
- *
- * 2000-04-30 Dave Cinege <dcinege@psychosis.com>
- * Rewrote fstab while loop and lower mount section. Can now do
- * single mounts from fstab. Can override fstab options for single
- * mount. Common mount_one call for single mounts and 'all'. Fixed
- * mtab updating and stale entries. Removed 'remount' default.
- *
2005-08-11 02:05:54 +05:30
|
|
|
|
2007-04-14 05:29:52 +05:30
|
|
|
len = strlen(ap->devpath);
|
|
|
|
rem = DEVNAME_MAX-2 - len;
|
|
|
|
if (rem <= 0)
|
2006-12-26 16:12:51 +05:30
|
|
|
return NULL;
|
2007-04-14 05:29:52 +05:30
|
|
|
ap->devpath[len++] = '/';
|
|
|
|
|
2006-12-26 16:12:51 +05:30
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
2007-04-14 05:29:52 +05:30
|
|
|
safe_strncpy(ap->devpath + len, entry->d_name, rem);
|
2007-04-22 04:58:03 +05:30
|
|
|
/* lstat: do not follow links */
|
|
|
|
if (lstat(ap->devpath, &ap->st) != 0)
|
2007-04-14 05:29:52 +05:30
|
|
|
continue;
|
|
|
|
if (S_ISBLK(ap->st.st_mode) && ap->st.st_rdev == ap->dev) {
|
|
|
|
retpath = xstrdup(ap->devpath);
|
Major rewrite of mount, umount, losetup. Untangled lots of code, shrunk
things down a bit, fixed a number of funky corner cases, added support for
several new features (things like mount --move, mount --bind, lazy unounts,
automatic detection of loop mounts, and so on). Probably broke several
other things, but it's fixable. (Bang on it, tell me what doesn't work for
you...)
Note: you no longer need to say "-o loop". It does that for you when
necessary.
Still need to add "user mount" support, which involves making mount suid. Not
too hard to do under the new infrastructure, just haven't done it yet...
The previous code had the following notes, that belong in the version
control comments:
- * 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
- * searches through fstab when -a is passed
- * will try mounting stuff with all fses when passed -t auto
- *
- * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
- *
- * 1999-10-07 Erik Andersen <andersen@codepoet.org>.
- * Rewrite of a lot of code. Removed mtab usage (I plan on
- * putting it back as a compile-time option some time),
- * major adjustments to option parsing, and some serious
- * dieting all around.
- *
- * 1999-11-06 mtab support is back - andersee
- *
- * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
- * mount to add loop support.
- *
- * 2000-04-30 Dave Cinege <dcinege@psychosis.com>
- * Rewrote fstab while loop and lower mount section. Can now do
- * single mounts from fstab. Can override fstab options for single
- * mount. Common mount_one call for single mounts and 'all'. Fixed
- * mtab updating and stale entries. Removed 'remount' default.
- *
2005-08-11 02:05:54 +05:30
|
|
|
break;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2007-04-14 05:29:52 +05:30
|
|
|
if (S_ISDIR(ap->st.st_mode)) {
|
|
|
|
/* Do not recurse for '.' and '..' */
|
|
|
|
if (DOT_OR_DOTDOT(entry->d_name))
|
|
|
|
continue;
|
|
|
|
retpath = find_block_device_in_dir(ap);
|
|
|
|
if (retpath)
|
|
|
|
break;
|
|
|
|
}
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
Major rewrite of mount, umount, losetup. Untangled lots of code, shrunk
things down a bit, fixed a number of funky corner cases, added support for
several new features (things like mount --move, mount --bind, lazy unounts,
automatic detection of loop mounts, and so on). Probably broke several
other things, but it's fixable. (Bang on it, tell me what doesn't work for
you...)
Note: you no longer need to say "-o loop". It does that for you when
necessary.
Still need to add "user mount" support, which involves making mount suid. Not
too hard to do under the new infrastructure, just haven't done it yet...
The previous code had the following notes, that belong in the version
control comments:
- * 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
- * searches through fstab when -a is passed
- * will try mounting stuff with all fses when passed -t auto
- *
- * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
- *
- * 1999-10-07 Erik Andersen <andersen@codepoet.org>.
- * Rewrite of a lot of code. Removed mtab usage (I plan on
- * putting it back as a compile-time option some time),
- * major adjustments to option parsing, and some serious
- * dieting all around.
- *
- * 1999-11-06 mtab support is back - andersee
- *
- * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
- * mount to add loop support.
- *
- * 2000-04-30 Dave Cinege <dcinege@psychosis.com>
- * Rewrote fstab while loop and lower mount section. Can now do
- * single mounts from fstab. Can override fstab options for single
- * mount. Common mount_one call for single mounts and 'all'. Fixed
- * mtab updating and stale entries. Removed 'remount' default.
- *
2005-08-11 02:05:54 +05:30
|
|
|
closedir(dir);
|
2001-03-17 04:17:14 +05:30
|
|
|
|
Major rewrite of mount, umount, losetup. Untangled lots of code, shrunk
things down a bit, fixed a number of funky corner cases, added support for
several new features (things like mount --move, mount --bind, lazy unounts,
automatic detection of loop mounts, and so on). Probably broke several
other things, but it's fixable. (Bang on it, tell me what doesn't work for
you...)
Note: you no longer need to say "-o loop". It does that for you when
necessary.
Still need to add "user mount" support, which involves making mount suid. Not
too hard to do under the new infrastructure, just haven't done it yet...
The previous code had the following notes, that belong in the version
control comments:
- * 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
- * searches through fstab when -a is passed
- * will try mounting stuff with all fses when passed -t auto
- *
- * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
- *
- * 1999-10-07 Erik Andersen <andersen@codepoet.org>.
- * Rewrite of a lot of code. Removed mtab usage (I plan on
- * putting it back as a compile-time option some time),
- * major adjustments to option parsing, and some serious
- * dieting all around.
- *
- * 1999-11-06 mtab support is back - andersee
- *
- * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
- * mount to add loop support.
- *
- * 2000-04-30 Dave Cinege <dcinege@psychosis.com>
- * Rewrote fstab while loop and lower mount section. Can now do
- * single mounts from fstab. Can override fstab options for single
- * mount. Common mount_one call for single mounts and 'all'. Fixed
- * mtab updating and stale entries. Removed 'remount' default.
- *
2005-08-11 02:05:54 +05:30
|
|
|
return retpath;
|
|
|
|
}
|
2007-04-14 05:29:52 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC find_block_device(const char *path)
|
2007-04-14 05:29:52 +05:30
|
|
|
{
|
|
|
|
struct arena a;
|
|
|
|
|
|
|
|
if (stat(path, &a.st) != 0)
|
|
|
|
return NULL;
|
|
|
|
a.dev = S_ISBLK(a.st.st_mode) ? a.st.st_rdev : a.st.st_dev;
|
|
|
|
strcpy(a.devpath, "/dev");
|
|
|
|
return find_block_device_in_dir(&a);
|
|
|
|
}
|