Re: AW: [PATCH v2 4/9] loop:refactor: extract subfunction set_loop_info()

2022-11-18 Thread Xiaoming Ni

On 2022/11/18 20:27, Walter Harms wrote:

on other minor

  if (rc && errno == ENXIO)

turn it on its head safes one indent level

if ( !rc || errno != ENXIO )
return -1;   // failed to get loopinfo

jm2c


Here's for copy-paste consistency
Simplification has been implemented in patch 5 by extracting functions




Von: busybox  im Auftrag von Xiaoming Ni 

Gesendet: Freitag, 18. November 2022 13:14:43
An: busybox@busybox.net; vda.li...@googlemail.com; c...@gmx.com; 
explore...@gmail.com
Cc: wang...@huawei.com
Betreff: [PATCH v2 4/9] loop:refactor: extract subfunction set_loop_info()

Step 4 of micro-refactoring the set_loop():
 Extract subfunction set_loop_info() from set_loop()

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

Signed-off-by: Xiaoming Ni 
---
  libbb/loop.c | 91 +++-
  1 file changed, 47 insertions(+), 44 deletions(-)

diff --git a/libbb/loop.c b/libbb/loop.c
index 91c3a45c4..89adc4f94 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -126,6 +126,47 @@ static int open_file(const char *file, unsigned flags, int 
*mode)
 return ffd;
  }

+static int set_loop_info(int ffd, int lfd, const char *file,
+   unsigned long long offset, unsigned long long sizelimit, 
unsigned flags)
+{
+   int rc;
+   bb_loop_info loopinfo;
+
+   rc = ioctl(lfd, BB_LOOP_GET_STATUS, );
+
+   /* If device is free, try to claim it */
+   if (rc && errno == ENXIO) {
+   /* Associate free loop device with file */
+   if (ioctl(lfd, LOOP_SET_FD, ffd)) {
+   return -1;
+   }
+   memset(, 0, sizeof(loopinfo));
+   safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
+   loopinfo.lo_offset = offset;
+   loopinfo.lo_sizelimit = sizelimit;
+   /*
+* Used by mount to set LO_FLAGS_AUTOCLEAR.
+* LO_FLAGS_READ_ONLY is not set because RO is controlled by 
open type of the file.
+* Note that closing LO_FLAGS_AUTOCLEARed lfd before mount
+* is wrong (would free the loop device!)
+*/
+   loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
+   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
+   if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
+   /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
+   /* (this code path is not tested) */
+   loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
+   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
+   }
+   if (rc == 0) {
+   return lfd;
+   }
+   /* failure, undo LOOP_SET_FD */
+   ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
+   }
+   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
   * mount it on and sets *device to a strdup of that loop device name.
@@ -135,7 +176,6 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
  {
 char dev[LOOP_NAMESIZE];
 char *try;
-   bb_loop_info loopinfo;
 struct stat statbuf;
 int i, lfd, ffd, mode, rc;

@@ -190,49 +230,12 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
 }
 goto try_next_loopN;
 }
-
-   rc = ioctl(lfd, BB_LOOP_GET_STATUS, );
-
-   /* If device is free, try to claim it */
-   if (rc && errno == ENXIO) {
-   /* Associate free loop device with file */
-   if (ioctl(lfd, LOOP_SET_FD, ffd)) {
-   close(lfd);
-   /* Ouch. Are we racing with other mount? */
-   if (!*device) {
-//TODO: add "if (--failcount != 0) ..."?
-   continue;
-   } else {
-   break;
-   }
-   }
-   memset(, 0, sizeof(loopinfo));
-   safe_strncpy((char *)loopinfo.lo_file_name, file, 
LO_NAME_SIZE);
-   loopinfo.lo_offset = offset;
-   loopinfo.lo_sizelimit = sizelimit;
-   /*
-* Used by mount to set LO_FLAGS_AUTOCLEAR.
-  

AW: [PATCH v2 4/9] loop:refactor: extract subfunction set_loop_info()

2022-11-18 Thread Walter Harms
on other minor

 if (rc && errno == ENXIO)

turn it on its head safes one indent level

if ( !rc || errno != ENXIO )
   return -1;   // failed to get loopinfo

jm2c

Von: busybox  im Auftrag von Xiaoming Ni 

Gesendet: Freitag, 18. November 2022 13:14:43
An: busybox@busybox.net; vda.li...@googlemail.com; c...@gmx.com; 
explore...@gmail.com
Cc: wang...@huawei.com
Betreff: [PATCH v2 4/9] loop:refactor: extract subfunction set_loop_info()

Step 4 of micro-refactoring the set_loop():
Extract subfunction set_loop_info() from set_loop()

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

Signed-off-by: Xiaoming Ni 
---
 libbb/loop.c | 91 +++-
 1 file changed, 47 insertions(+), 44 deletions(-)

diff --git a/libbb/loop.c b/libbb/loop.c
index 91c3a45c4..89adc4f94 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -126,6 +126,47 @@ static int open_file(const char *file, unsigned flags, int 
*mode)
return ffd;
 }

+static int set_loop_info(int ffd, int lfd, const char *file,
+   unsigned long long offset, unsigned long long sizelimit, 
unsigned flags)
+{
+   int rc;
+   bb_loop_info loopinfo;
+
+   rc = ioctl(lfd, BB_LOOP_GET_STATUS, );
+
+   /* If device is free, try to claim it */
+   if (rc && errno == ENXIO) {
+   /* Associate free loop device with file */
+   if (ioctl(lfd, LOOP_SET_FD, ffd)) {
+   return -1;
+   }
+   memset(, 0, sizeof(loopinfo));
+   safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
+   loopinfo.lo_offset = offset;
+   loopinfo.lo_sizelimit = sizelimit;
+   /*
+* Used by mount to set LO_FLAGS_AUTOCLEAR.
+* LO_FLAGS_READ_ONLY is not set because RO is controlled by 
open type of the file.
+* Note that closing LO_FLAGS_AUTOCLEARed lfd before mount
+* is wrong (would free the loop device!)
+*/
+   loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
+   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
+   if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
+   /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
+   /* (this code path is not tested) */
+   loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
+   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
+   }
+   if (rc == 0) {
+   return lfd;
+   }
+   /* failure, undo LOOP_SET_FD */
+   ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
+   }
+   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
  * mount it on and sets *device to a strdup of that loop device name.
@@ -135,7 +176,6 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
 {
char dev[LOOP_NAMESIZE];
char *try;
-   bb_loop_info loopinfo;
struct stat statbuf;
int i, lfd, ffd, mode, rc;

@@ -190,49 +230,12 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
}
goto try_next_loopN;
}
-
-   rc = ioctl(lfd, BB_LOOP_GET_STATUS, );
-
-   /* If device is free, try to claim it */
-   if (rc && errno == ENXIO) {
-   /* Associate free loop device with file */
-   if (ioctl(lfd, LOOP_SET_FD, ffd)) {
-   close(lfd);
-   /* Ouch. Are we racing with other mount? */
-   if (!*device) {
-//TODO: add "if (--failcount != 0) ..."?
-   continue;
-   } else {
-   break;
-   }
-   }
-   memset(, 0, sizeof(loopinfo));
-   safe_strncpy((char *)loopinfo.lo_file_name, file, 
LO_NAME_SIZE);
-   loopinfo.lo_offset = offset;
-   loopinfo.lo_sizelimit = sizelimit;
-   /*
-* Used by mount to set LO_FLAGS_AUTOCLEAR.
-* LO_FLAGS_READ_ONLY is not set because RO is 
controlled by open type of the file.
-* Note that closing LO_FLAGS_AUTOCLEARed 

[PATCH v2 4/9] loop:refactor: extract subfunction set_loop_info()

2022-11-18 Thread Xiaoming Ni
Step 4 of micro-refactoring the set_loop():
Extract subfunction set_loop_info() from set_loop()

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

Signed-off-by: Xiaoming Ni 
---
 libbb/loop.c | 91 +++-
 1 file changed, 47 insertions(+), 44 deletions(-)

diff --git a/libbb/loop.c b/libbb/loop.c
index 91c3a45c4..89adc4f94 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -126,6 +126,47 @@ static int open_file(const char *file, unsigned flags, int 
*mode)
return ffd;
 }
 
+static int set_loop_info(int ffd, int lfd, const char *file,
+   unsigned long long offset, unsigned long long sizelimit, 
unsigned flags)
+{
+   int rc;
+   bb_loop_info loopinfo;
+
+   rc = ioctl(lfd, BB_LOOP_GET_STATUS, );
+
+   /* If device is free, try to claim it */
+   if (rc && errno == ENXIO) {
+   /* Associate free loop device with file */
+   if (ioctl(lfd, LOOP_SET_FD, ffd)) {
+   return -1;
+   }
+   memset(, 0, sizeof(loopinfo));
+   safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
+   loopinfo.lo_offset = offset;
+   loopinfo.lo_sizelimit = sizelimit;
+   /*
+* Used by mount to set LO_FLAGS_AUTOCLEAR.
+* LO_FLAGS_READ_ONLY is not set because RO is controlled by 
open type of the file.
+* Note that closing LO_FLAGS_AUTOCLEARed lfd before mount
+* is wrong (would free the loop device!)
+*/
+   loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
+   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
+   if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
+   /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
+   /* (this code path is not tested) */
+   loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
+   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
+   }
+   if (rc == 0) {
+   return lfd;
+   }
+   /* failure, undo LOOP_SET_FD */
+   ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
+   }
+   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
  * mount it on and sets *device to a strdup of that loop device name.
@@ -135,7 +176,6 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
 {
char dev[LOOP_NAMESIZE];
char *try;
-   bb_loop_info loopinfo;
struct stat statbuf;
int i, lfd, ffd, mode, rc;
 
@@ -190,49 +230,12 @@ int FAST_FUNC set_loop(char **device, const char *file, 
unsigned long long offse
}
goto try_next_loopN;
}
-
-   rc = ioctl(lfd, BB_LOOP_GET_STATUS, );
-
-   /* If device is free, try to claim it */
-   if (rc && errno == ENXIO) {
-   /* Associate free loop device with file */
-   if (ioctl(lfd, LOOP_SET_FD, ffd)) {
-   close(lfd);
-   /* Ouch. Are we racing with other mount? */
-   if (!*device) {
-//TODO: add "if (--failcount != 0) ..."?
-   continue;
-   } else {
-   break;
-   }
-   }
-   memset(, 0, sizeof(loopinfo));
-   safe_strncpy((char *)loopinfo.lo_file_name, file, 
LO_NAME_SIZE);
-   loopinfo.lo_offset = offset;
-   loopinfo.lo_sizelimit = sizelimit;
-   /*
-* Used by mount to set LO_FLAGS_AUTOCLEAR.
-* LO_FLAGS_READ_ONLY is not set because RO is 
controlled by open type of the file.
-* Note that closing LO_FLAGS_AUTOCLEARed lfd before 
mount
-* is wrong (would free the loop device!)
-*/
-   loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
-   rc = ioctl(lfd, BB_LOOP_SET_STATUS, );
-   if (rc != 0 && (loopinfo.lo_flags & 
BB_LO_FLAGS_AUTOCLEAR)) {
-   /* Old kernel, does not support 
LO_FLAGS_AUTOCLEAR? */
-   /* (this code path is not tested) */
-