diff mbox

[1/2] libmtd: add `mtd_dev_present()' library function

Message ID 1327689046-1450-1-git-send-email-computersforpeace@gmail.com
State New, archived
Headers show

Commit Message

Brian Norris Jan. 27, 2012, 6:30 p.m. UTC
Will be used for `mtdinfo --all'

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
---
 include/libmtd.h |    9 +++++++++
 lib/libmtd.c     |   22 ++++++++++++----------
 2 files changed, 21 insertions(+), 10 deletions(-)

Comments

Artem Bityutskiy Feb. 2, 2012, 11:33 a.m. UTC | #1
On Fri, 2012-01-27 at 10:30 -0800, Brian Norris wrote:

> +int mtd_dev_present(libmtd_t desc, int mtd_num) {
> +	struct stat st;
> +	struct libmtd *lib = (struct libmtd *)desc;
> +	char file[strlen(lib->mtd) + 10];
> +
> +	sprintf(file, lib->mtd, mtd_num);
> +	return !stat(file, &st);
> +}

Thanks! However...

This will only work for relatively newer kernels where MTD has sysfs
support (2.6.30+). Older kernels have no MTD sysfs support and the sysfs
file you are stat()'ing won't exist, so this function will always return
an error.

I've added the following stub to 'mtd_dev_present()':

+       if (!lib->sysfs_supported)
+               /* TODO: add legacy_dev_present() function */
+               return 1;

And pushed your patches to mtd-utils.

This means old kernels won't be fixed, but at least they won't be broken
either. Do you have enough juice to add a 'legacy_def_present()'
function? Ideally, it should parse '/proc/mtd', but I think there are
functions already which just check for '/dev/mtdX' node, so you could
just stat it instead of the sysfs file, I guess. See
'legacy_get_dev_info1()' for example.
Brian Foster Feb. 2, 2012, 1:20 p.m. UTC | #2
On Thursday 02 February 2012 12:33:24 Artem Bityutskiy wrote:
> On Fri, 2012-01-27 at 10:30 -0800, Brian Norris wrote:
> > +int mtd_dev_present(libmtd_t desc, int mtd_num) {  [ ... ]
> 
> This will only work for relatively newer kernels where MTD has sysfs
> support (2.6.30+). Older kernels have no MTD sysfs support and the sysfs
> file you are stat()'ing won't exist, so this function will always return
> an error.

 This is a very plausible concern:  Our older system
 is mostly deployed with 2.6.26(or even older) kernel,
 albeit a 2.6.30 option exists.  It has multiple MTD
 devices, albeit due to the usage/configuration (and
 maybe the use of older mtd-utils?) I suspect the bug
 has never been observed.

 Our latest system uses 2.6.36, and is where the original
 UBI-related bug was observed.

cheers!
	-blf-
diff mbox

Patch

diff --git a/include/libmtd.h b/include/libmtd.h
index 07c304a..a78c8cb 100644
--- a/include/libmtd.h
+++ b/include/libmtd.h
@@ -106,6 +106,15 @@  libmtd_t libmtd_open(void);
 void libmtd_close(libmtd_t desc);
 
 /**
+ * mtd_dev_present - check whether a MTD device is present.
+ * @desc: MTD library descriptor
+ * @mtd_num: MTD device number to check
+ *
+ * This function returns %1 if MTD device is present and %0 if not.
+ */
+int mtd_dev_present(libmtd_t desc, int mtd_num);
+
+/**
  * mtd_get_info - get general MTD information.
  * @desc: MTD library descriptor
  * @info: the MTD device information is returned here
diff --git a/lib/libmtd.c b/lib/libmtd.c
index c6e5b20..9786e83 100644
--- a/lib/libmtd.c
+++ b/lib/libmtd.c
@@ -713,10 +713,18 @@  out_close:
 	return -1;
 }
 
+int mtd_dev_present(libmtd_t desc, int mtd_num) {
+	struct stat st;
+	struct libmtd *lib = (struct libmtd *)desc;
+	char file[strlen(lib->mtd) + 10];
+
+	sprintf(file, lib->mtd, mtd_num);
+	return !stat(file, &st);
+}
+
 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
 {
 	int ret;
-	struct stat st;
 	struct libmtd *lib = (struct libmtd *)desc;
 
 	memset(mtd, 0, sizeof(struct mtd_dev_info));
@@ -724,15 +732,9 @@  int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
 
 	if (!lib->sysfs_supported)
 		return legacy_get_dev_info1(mtd_num, mtd);
-	else {
-		char file[strlen(lib->mtd) + 10];
-
-		sprintf(file, lib->mtd, mtd_num);
-		if (stat(file, &st)) {
-			if (errno == ENOENT)
-				errno = ENODEV;
-			return -1;
-		}
+	else if (!mtd_dev_present(desc, mtd_num)) {
+		errno = ENODEV;
+		return -1;
 	}
 
 	if (dev_get_major(lib, mtd_num, &mtd->major, &mtd->minor))