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

function                                             old     new   delta
set_loop                                             760     758      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-2)               Total: -2 bytes

Signed-off-by: Xiaoming Ni <nixiaom...@huawei.com>
---
 libbb/loop.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/libbb/loop.c b/libbb/loop.c
index 750642ade..c517ceb13 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -96,6 +96,22 @@ int FAST_FUNC get_free_loop(void)
        return loopdevno; /* can be -1 if error */
 }
 
+static int open_file(const char *file, unsigned flags, int *mode)
+{
+       int ffd;
+       /* open the file.  barf if this doesn't work.  */
+       *mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
+ retry_open_ffd:
+       ffd = open(file, *mode);
+       if (ffd < 0) {
+               if (*mode != O_RDONLY) {
+                       *mode = O_RDONLY;
+                       goto retry_open_ffd;
+               }
+       }
+       return ffd;
+}
+
 /* 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
  * mount it on and sets *device to a strdup of that loop device name.
@@ -109,15 +125,8 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
        struct stat statbuf;
        int i, lfd, ffd, mode, rc;
 
-       /* Open the file.  Barf if this doesn't work.  */
-       mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
- open_ffd:
-       ffd = open(file, mode);
+       ffd = open_file(file, flags, &mode);
        if (ffd < 0) {
-               if (mode != O_RDONLY) {
-                       mode = O_RDONLY;
-                       goto open_ffd;
-               }
                return -errno;
        }
 
-- 
2.27.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to