From 13e709c53f700a18a660feebdf72c613a233bf48 Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Mon, 12 Sep 2011 02:13:47 +0200
Subject: losetup: implement -r option. Closes 4033.

function                                             old     new   delta
packed_usage                                       28595   28633     +38
losetup_main                                         285     290      +5
singlemount                                          906     908      +2
set_loop                                             674     672      -2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 libbb/loop.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

(limited to 'libbb')

diff --git a/libbb/loop.c b/libbb/loop.c
index b798932fa..b3a520848 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -84,7 +84,7 @@ int FAST_FUNC del_loop(const char *device)
    search will re-use an existing loop device already bound to that
    file/offset if it finds one.
  */
-int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset)
+int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, int ro)
 {
 	char dev[LOOP_NAMESIZE];
 	char *try;
@@ -93,11 +93,13 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
 	int i, dfd, ffd, mode, rc = -1;
 
 	/* Open the file.  Barf if this doesn't work.  */
-	mode = O_RDWR;
+	mode = ro ? O_RDONLY : O_RDWR;
 	ffd = open(file, mode);
 	if (ffd < 0) {
-		mode = O_RDONLY;
-		ffd = open(file, mode);
+		if (mode != O_RDONLY) {
+			mode = O_RDONLY;
+			ffd = open(file, mode);
+		}
 		if (ffd < 0)
 			return -errno;
 	}
-- 
cgit v1.2.3-55-g6feb