On Friday 13 April 2007 17:35, Kirill K. Smirnov wrote:
> Hi,
> I have embedded system with root filesystem /dev/mtdblock/3
> df does not display it in list.
> The attached patch deals with this problem.

                char devpath[PATH_MAX];
-               sprintf(devpath,"/dev/%s", entry->d_name);
+               sprintf(devpath,"%s/%s", path, entry->d_name);

From sheer paranoia I am adding overflow check.

Allocating on stack ~1K *for each iteration* is another thing
I would like to get rid of. (Actually, for glibc it's 4K).

                if (!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == 
dev) {
                        retpath = xstrdup(devpath);
                        break;
                }
+               else if (!stat(devpath, &st) &&
+                        S_ISDIR(st.st_mode) &&
+                        entry->d_name[0]!='.')
+               {
+                       retpath = find_block_device_in_dir(dev, devpath);
+                       if (retpath) break;
+               }

"else" is superfluous. You do stat twice. Why '.names' are not ok?

Please try attached version.
--
vda
diff -d -urpN busybox.2/libbb/find_root_device.c busybox.3/libbb/find_root_device.c
--- busybox.2/libbb/find_root_device.c	2007-04-11 09:07:57.000000000 +0200
+++ busybox.3/libbb/find_root_device.c	2007-04-13 22:59:51.000000000 +0200
@@ -9,26 +9,59 @@
 
 #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 */
+struct arena {
+	struct stat st;
+	dev_t dev;
+	char devpath[PATH_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;
 
-	if (stat(path, &st) || !(dir = opendir("/dev")))
+	dir = opendir(ap->devpath);
+	if (!dir)
 		return NULL;
-	dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev;
+
+	len = strlen(ap->devpath);
+	rem = PATH_MAX-2 - len;
+	if (rem <= 0)
+		return NULL;
+	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)) {
+			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);
+}
_______________________________________________
busybox mailing list
[EMAIL PROTECTED]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to