Util-Linux package development
 help / color / mirror / Atom feed
From: Tobias Stoeckmann <tobias@stoeckmann.org>
To: util-linux@vger.kernel.org
Subject: [PATCH] Set errno in is_loopdev on error
Date: Tue, 30 Aug 2016 21:00:38 +0200	[thread overview]
Message-ID: <20160830190038.GA32582@localhost> (raw)

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

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
 lib/loopdev.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/loopdev.c b/lib/loopdev.c
index b3941dd..a57e7a7 100644
--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -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;
 }
 
 /*
-- 
2.9.3


             reply	other threads:[~2016-08-30 19:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-30 19:00 Tobias Stoeckmann [this message]
2016-08-31  7:50 ` [PATCH] Set errno in is_loopdev on error Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160830190038.GA32582@localhost \
    --to=tobias@stoeckmann.org \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox