diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-13 23:59:52 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-13 23:59:52 +0000 |
commit | 16abcd90aefae8bdb9f7d80a555982dba6ca59b5 (patch) | |
tree | b7e23fd3c844849d188813323076c93105a57d4d /libbb | |
parent | 334fa9bcb50df9a03288be252096750dcec14404 (diff) | |
download | busybox-16abcd90aefae8bdb9f7d80a555982dba6ca59b5.tar.gz |
teach find_root_device to deal with /dev/ subdirs
(by "Kirill K. Smirnov" <lich@math.spbu.ru>)
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/concat_subpath_file.c | 2 | ||||
-rw-r--r-- | libbb/find_root_device.c | 59 | ||||
-rw-r--r-- | libbb/simplify_path.c | 11 |
3 files changed, 57 insertions, 15 deletions
diff --git a/libbb/concat_subpath_file.c b/libbb/concat_subpath_file.c index 6bbe49ab5..1c0058889 100644 --- a/libbb/concat_subpath_file.c +++ b/libbb/concat_subpath_file.c @@ -17,7 +17,7 @@ char *concat_subpath_file(const char *path, const char *f) { - if (f && *f == '.' && (!f[1] || (f[1] == '.' && !f[2]))) + if (f && DOT_OR_DOTDOT(f)) return NULL; return concat_path_file(path, f); } diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c index ea360eae5..7182102c7 100644 --- a/libbb/find_root_device.c +++ b/libbb/find_root_device.c @@ -9,26 +9,65 @@ #include "libbb.h" -char *find_block_device(const char *path) +/* 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) { DIR *dir; struct dirent *entry; - struct stat st; - dev_t dev; - char *retpath=NULL; + char *retpath = NULL; + int len, rem; + + dir = opendir(ap->devpath); + if (!dir) + return NULL; - if (stat(path, &st) || !(dir = opendir("/dev"))) + len = strlen(ap->devpath); + rem = DEVNAME_MAX-2 - len; + if (rem <= 0) return NULL; - dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev; + ap->devpath[len++] = '/'; + while ((entry = readdir(dir)) != NULL) { - char devpath[PATH_MAX]; - sprintf(devpath,"/dev/%s", entry->d_name); - if (!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) { - retpath = xstrdup(devpath); + safe_strncpy(ap->devpath + len, entry->d_name, rem); + if (stat(ap->devpath, &ap->st) != 0) + continue; + if (S_ISBLK(ap->st.st_mode) && ap->st.st_rdev == ap->dev) { + retpath = xstrdup(ap->devpath); break; } + 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; + } } closedir(dir); return retpath; } + +char *find_block_device(const char *path) +{ + 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); +} diff --git a/libbb/simplify_path.c b/libbb/simplify_path.c index 7e68e3911..29e371df6 100644 --- a/libbb/simplify_path.c +++ b/libbb/simplify_path.c @@ -26,13 +26,16 @@ char *bb_simplify_path(const char *path) if (*p == '/') { if (*s == '/') { /* skip duplicate (or initial) slash */ continue; - } else if (*s == '.') { - if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ + } + if (*s == '.') { + if (s[1] == '/' || !s[1]) { /* remove extra '.' */ continue; - } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { + } + if ((s[1] == '.') && (s[2] == '/' || !s[2])) { ++s; if (p > start) { - while (*--p != '/'); /* omit previous dir */ + while (*--p != '/') /* omit previous dir */ + continue; } continue; } |