Skip to contentSkip to conversation

Set errno in is_loopdev on error

PATCH In review Tobias Stoeckmann 2 participants 2 messages Aug 30, 2016 – Aug 31, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Set errno in is_loopdev on error

The function is_loopdev does not set errno if the supplied string does not reference a valid loop device. Fix this to avoid an error message like this one:

losetup: /: failed to use device: Success

I prefer this one:

losetup: /: failed to use device: No such device

---
 lib/loopdev.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/loopdev.c +6 -5
index b3941dd..a57e7a7 100644
@@ -633,12 +633,13 @@ int is_loopdev(const char *device)
 {
 	struct stat st;
 
-	if (!device)
-		return 0;
-
-	return (stat(device, &st) == 0 &&
+	if (device && stat(device, &st) == 0 &&
 		S_ISBLK(st.st_mode) &&
-		major(st.st_rdev) == LOOPDEV_MAJOR);
+		major(st.st_rdev) == LOOPDEV_MAJOR)
+		return 1;
+
+	errno = ENODEV;
+	return 0;
 }
 
 /*
Signature · 1 line
2.9.3
maintainer
#1 lore raw
Quoted 2 lines · Tobias Stoeckmann
 lib/loopdev.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Good catch. Applied, thanks.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com