Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Utility routines. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 12 | /* Find block device /dev/XXX which contains specified file |
| 13 | * We handle /dev/dir/dir/dir too, at a cost of ~80 more bytes code */ |
| 14 | |
| 15 | /* Do not reallocate all this stuff on each recursion */ |
| 16 | enum { DEVNAME_MAX = 256 }; |
| 17 | struct arena { |
| 18 | struct stat st; |
| 19 | dev_t dev; |
| 20 | /* Was PATH_MAX, but we recurse _/dev_. We can assume |
| 21 | * people are not crazy enough to have mega-deep tree there */ |
| 22 | char devpath[DEVNAME_MAX]; |
| 23 | }; |
| 24 | |
| 25 | static char *find_block_device_in_dir(struct arena *ap) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | { |
| 27 | DIR *dir; |
| 28 | struct dirent *entry; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 29 | char *retpath = NULL; |
| 30 | int len, rem; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 32 | len = strlen(ap->devpath); |
| 33 | rem = DEVNAME_MAX-2 - len; |
| 34 | if (rem <= 0) |
| 35 | return NULL; |
Denys Vlasenko | 2272129 | 2010-12-28 10:25:03 +0100 | [diff] [blame] | 36 | |
| 37 | dir = opendir(ap->devpath); |
| 38 | if (!dir) |
| 39 | return NULL; |
| 40 | |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 41 | ap->devpath[len++] = '/'; |
| 42 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 43 | while ((entry = readdir(dir)) != NULL) { |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 44 | safe_strncpy(ap->devpath + len, entry->d_name, rem); |
Denis Vlasenko | 706fdc9 | 2007-04-21 23:28:03 +0000 | [diff] [blame] | 45 | /* lstat: do not follow links */ |
| 46 | if (lstat(ap->devpath, &ap->st) != 0) |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 47 | continue; |
| 48 | if (S_ISBLK(ap->st.st_mode) && ap->st.st_rdev == ap->dev) { |
| 49 | retpath = xstrdup(ap->devpath); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 50 | break; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 51 | } |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 52 | if (S_ISDIR(ap->st.st_mode)) { |
| 53 | /* Do not recurse for '.' and '..' */ |
| 54 | if (DOT_OR_DOTDOT(entry->d_name)) |
| 55 | continue; |
| 56 | retpath = find_block_device_in_dir(ap); |
| 57 | if (retpath) |
| 58 | break; |
| 59 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 61 | closedir(dir); |
| 62 | |
| 63 | return retpath; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 64 | } |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 66 | char* FAST_FUNC find_block_device(const char *path) |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 67 | { |
| 68 | struct arena a; |
| 69 | |
| 70 | if (stat(path, &a.st) != 0) |
| 71 | return NULL; |
| 72 | a.dev = S_ISBLK(a.st.st_mode) ? a.st.st_rdev : a.st.st_dev; |
| 73 | strcpy(a.devpath, "/dev"); |
| 74 | return find_block_device_in_dir(&a); |
| 75 | } |