[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    AW: [PATCH v2 8/9] loop:refactor: extract subfunction set_loop_dev()
From:       Walter Harms <wharms () bfs ! de>
Date:       2022-11-18 12:48:00
Message-ID: ff1fe15a42ae46ba9c02dfc00c3a4442 () bfs ! de
[Download RAW message or body]

i am wondering, maybe question for the maintainer.
would it hurt to open it O_RDONLY in the first place ?

cm2c

________________________________________
Von: busybox <busybox-bounces@busybox.net> im Auftrag von Xiaoming Ni <nixiaoming@huawei.com>
Gesendet: Freitag, 18. November 2022 13:14:47
An: busybox@busybox.net; vda.linux@googlemail.com; cand@gmx.com; explorer09@gmail.com
Cc: wangle6@huawei.com
Betreff: [PATCH v2 8/9] loop:refactor: extract subfunction set_loop_dev()

Step 8 of micro-refactoring the set_loop():
    Extract subfunction open_file() from set_loop_dev()

function                                             old     new   delta
set_loop                                             700     716     +16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 16/0)               Total: 16 bytes

Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com>
---
 libbb/loop.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/libbb/loop.c b/libbb/loop.c
index 67e16ddb0..ddb92fce3 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -201,6 +201,26 @@ static int do_stat_and_mknod(const char *dev, const char *device, int i)
        return 0;
 }

+static int set_loop_dev(const char *dev, int *mode, int ffd, bb_loop_info *loopinfo)
+{
+       int rc;
+       /* Open the sucker and check its loopiness */
+       int lfd = open(dev, *mode);
+       if (lfd < 0 && errno == EROFS) {
+               *mode = O_RDONLY;
+               lfd = open(dev, *mode);
+       }
+       if (lfd < 0) {
+               return lfd;
+       }
+       rc = set_loop_info(ffd, lfd, loopinfo);
+       if (rc == lfd) {
+               return lfd;
+       }
+       close(lfd);
+       return -1;
+}
+

 /* Returns opened fd to the loop device, <0 on error.
  * *device is loop device to use, or if *device==NULL finds a loop device to
@@ -211,7 +231,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
 {
        char dev[LOOP_NAMESIZE];
        char *try;
-       int i, lfd, ffd, mode, rc;
+       int i, ffd, mode, rc;
        bb_loop_info loopinfo;

        ffd = open_file(file, flags, &mode);
@@ -242,30 +262,19 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
                        break;
                }
  open_lfd:
-               /* Open the sucker and check its loopiness */
-               lfd = rc = open(try, mode);
-               if (lfd < 0 && errno == EROFS) {
-                       mode = O_RDONLY;
-                       lfd = rc = open(try, mode);
-               }
-               if (lfd < 0) {
+               rc = set_loop_dev(try, &mode, ffd, &loopinfo);
+               if (rc == -1) {
                        if (errno == ENXIO) {
                                /* Happens if loop module is not loaded */
                                /* rc is -1; */
                                break;
                        }
-                       goto try_next_loopN;
-               }
-               rc = set_loop_info(ffd, lfd, &loopinfo);
-               if (rc == lfd) {
+               } else {
                        /* SUCCESS! */
                        if (!*device)
                                *device = xstrdup(try);
                        break;
                }
-               close(lfd);
- try_next_loopN:
-               rc = -1;
                if (*device) /* was looking for a particular "/dev/loopN"? */
                        break; /* yes, do not try other names */
        } /* for() */
--
2.27.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic