2001-03-16 22:47:14 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2001-10-24 05:00:29 +00:00
|
|
|
* Utility routines.
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
2006-07-10 11:41:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2007-04-13 23:59:52 +00:00
|
|
|
/* 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-16 22:47:14 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2007-04-13 23:59:52 +00:00
|
|
|
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-10 20:35:54 +00:00
|
|
|
|
2007-04-13 23:59:52 +00:00
|
|
|
len = strlen(ap->devpath);
|
|
|
|
rem = DEVNAME_MAX-2 - len;
|
|
|
|
if (rem <= 0)
|
2006-12-26 10:42:51 +00:00
|
|
|
return NULL;
|
2007-04-13 23:59:52 +00:00
|
|
|
ap->devpath[len++] = '/';
|
|
|
|
|
2006-12-26 10:42:51 +00:00
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
2007-04-13 23:59:52 +00:00
|
|
|
safe_strncpy(ap->devpath + len, entry->d_name, rem);
|
2007-04-21 23:28:03 +00:00
|
|
|
/* lstat: do not follow links */
|
|
|
|
if (lstat(ap->devpath, &ap->st) != 0)
|
2007-04-13 23:59:52 +00:00
|
|
|
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-10 20:35:54 +00:00
|
|
|
break;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2007-04-13 23:59:52 +00:00
|
|
|
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-16 22:47:14 +00:00
|
|
|
}
|
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-10 20:35:54 +00:00
|
|
|
closedir(dir);
|
2001-03-16 22:47:14 +00:00
|
|
|
|
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-10 20:35:54 +00:00
|
|
|
return retpath;
|
|
|
|
}
|
2007-04-13 23:59:52 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC find_block_device(const char *path)
|
2007-04-13 23:59:52 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|