hw/s390x/ccw: Cleanup basename() and dirname()
cover hw/s390x/ccw: Cleanup basename() and dirname()
From: Zhao Liu <[email protected]>
As commit 3e015d815b3f ("use g_path_get_basename instead of basename") said, g_path_get_basename() and g_path_get_dirname() should be preferred over basename() and dirname(), since g_path_get_basename() and g_path_get_dirname() are portable utility functions that have the advantage of not modifing the string argument.
But commit 3e015d815b3f missed a use of dirname() and basename() in hw/s390x/ccw.
And basename() (in vfio/container) caused compile breakage with the Musl C library [1].
To avoid similar breakage and improve portability, replace basename() and dirname() with g_path_get_basename() and g_path_get_dirname().
[1]: https://lore.kernel.org/all/[email protected]/
---
Zhao Liu (2):
hw/s390x/ccw: Replace basename() with g_path_get_basename() hw/s390x/ccw: Replace dirname() with g_path_get_dirname()
hw/s390x/s390-ccw.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 hw/s390x/ccw: Replace basename() with g_path_get_basename()
From: Zhao Liu <[email protected]>
g_path_get_basename() is a portable utility function that has the advantage of not modifing the string argument, so it should be preferred over basename().
And also to avoid potential compile breakage with the Musl C library similar to [1], replace basename() with g_path_get_basename().
[1]: https://lore.kernel.org/all/[email protected]/
---
Suggested by credit:
Cédric: Referred his description of similar cleanup in vfio/container.
--- hw/s390x/s390-ccw.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/s390x/s390-ccw.c +3 -2
@@ -76,7 +76,8 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, Error **errp) {unsigned int cssid, ssid, devid; -char dev_path[PATH_MAX ] = {0 }, *tmp; +char dev_path[PATH_MAX ] = {0 }; + g_autofree char *tmp = NULL;if (!sysfsdev) {error_setg (errp,"No host device provided" );
@@ -92,7 +93,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, cdev->mdevid =g_path_get_basename (dev_path); - tmp =basename (dirname (dev_path)); + tmp =g_path_get_basename (dirname (dev_path));if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) {error_setg_errno (errp, errno,"Failed to read %s" , tmp);return ;
Signature · 1 line
2.34.1
Re: [PATCH 1/2] hw/s390x/ccw: Replace basename() with g_path_get_basename()
Quoted 6 lines · Zhao Liu
From: Zhao Liu <[email protected]>
g_path_get_basename()is a portable utility function that has the advantage of not modifing the string argument, so it should be preferred overbasename().
Quoted 7 lines
And also to avoid potential compile breakage with the Musl C library similar to [1], replace
basename()withg_path_get_basename().[1]: https://lore.kernel.org/all/[email protected]/
Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhao Liu <[email protected]>
Thanks,
C.
Quoted 30 lines
---
Suggested by credit:
Cédric: Referred his description of similar cleanup in vfio/container.--- hw/s390x/s390-ccw.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)hw/s390x/s390-ccw.c +3
-2@@ -76,7 +76,8 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, Error **errp) {unsigned int cssid, ssid, devid; -char dev_path[PATH_MAX ] = {0 }, *tmp; +char dev_path[PATH_MAX ] = {0 }; + g_autofree char *tmp = NULL;if (!sysfsdev) {error_setg (errp,"No host device provided" );@@ -92,7 +93,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, cdev->mdevid =g_path_get_basename (dev_path); - tmp =basename (dirname (dev_path)); + tmp =g_path_get_basename (dirname (dev_path));if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) {error_setg_errno (errp, errno,"Failed to read %s" , tmp);return ;
PATCH 2/2 hw/s390x/ccw: Replace dirname() with g_path_get_dirname()
In reply to Zhao LiuFrom: Zhao Liu <[email protected]>
As commit 3e015d815b3f ("use g_path_get_basename instead of basename") said, g_path_get_dirname() should be preferred over dirname() since the former is a portable utility function that has the advantage of not modifing the string argument.
Replace dirname() with g_path_get_dirname().
---
Suggested by credit:
Cédric: Referred his words for g_path_get_basename() and
g_path_get_dirname() has the same advantage.
--- hw/s390x/s390-ccw.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/s390-ccw.c +3 -1
@@ -77,6 +77,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, {unsigned int cssid, ssid, devid;char dev_path[PATH_MAX ] = {0 }; + g_autofree char *tmp_dir = NULL; g_autofree char *tmp = NULL;if (!sysfsdev) {
@@ -93,7 +94,8 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, cdev->mdevid =g_path_get_basename (dev_path); - tmp =g_path_get_basename (dirname (dev_path)); + tmp_dir =g_path_get_dirname (dev_path); + tmp =g_path_get_basename (tmp_dir);if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) {error_setg_errno (errp, errno,"Failed to read %s" , tmp);return ;
Signature · 1 line
2.34.1
Re: [PATCH 2/2] hw/s390x/ccw: Replace dirname() with g_path_get_dirname()
Quoted 11 lines · Zhao Liu
From: Zhao Liu <[email protected]>
As commit
3e015d815b3f("useg_path_get_basenameinstead of basename") said,g_path_get_dirname()should be preferred overdirname()since the former is a portable utility function that has the advantage of not modifing the string argument.Replace
dirname()withg_path_get_dirname().Suggested-by Cédric Le Goater <[email protected]>Signed-off-by Zhao Liu <[email protected]>
Thanks,
C.
Quoted 30 lines
---
Suggested by credit:
Cédric: Referred his words forg_path_get_basename()and
g_path_get_dirname()has the same advantage.--- hw/s390x/s390-ccw.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/s390x/s390-ccw.c +3
-1@@ -77,6 +77,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, {unsigned int cssid, ssid, devid;char dev_path[PATH_MAX ] = {0 }; + g_autofree char *tmp_dir = NULL; g_autofree char *tmp = NULL;if (!sysfsdev) {@@ -93,7 +94,8 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, cdev->mdevid =g_path_get_basename (dev_path); - tmp =g_path_get_basename (dirname (dev_path)); + tmp_dir =g_path_get_dirname (dev_path); + tmp =g_path_get_basename (tmp_dir);if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) {error_setg_errno (errp, errno,"Failed to read %s" , tmp);return ;
Quoted 23 lines · Zhao Liu
From: Zhao Liu <[email protected]>
As commit
3e015d815b3f("useg_path_get_basenameinstead of basename")
said,g_path_get_basename()andg_path_get_dirname()should be preferred
overbasename()anddirname(), sinceg_path_get_basename()andg_path_get_dirname()are portable utility functions that have the advantage of not modifing the string argument.But commit
3e015d815b3fmissed a use ofdirname()andbasename()in hw/s390x/ccw.And
basename()(in vfio/container) caused compile breakage with the Musl
C library [1].To avoid similar breakage and improve portability, replace
basename()anddirname()withg_path_get_basename()andg_path_get_dirname().
Quoted 8 lines
---
Zhao Liu (2):
hw/s390x/ccw: Replacebasename()withg_path_get_basename()hw/s390x/ccw: Replacedirname()withg_path_get_dirname()hw/s390x/s390-ccw.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 33 lines · Eric Farman
Date: Thu, 21 Dec 2023 16:29:31 -0500
From: Eric Farman <[email protected]>
Subject: Re: [PATCH 0/2] hw/s390x/ccw: Cleanupbasename()anddirname()Quoted 28 lines · Zhao Liu
From: Zhao Liu <[email protected]>
As commit
3e015d815b3f("useg_path_get_basenameinstead of basename")
said,g_path_get_basename()andg_path_get_dirname()should be preferred
overbasename()anddirname(), sinceg_path_get_basename()andg_path_get_dirname()are portable utility functions that have the advantage of not modifing the string argument.But commit
3e015d815b3fmissed a use ofdirname()andbasename()in hw/s390x/ccw.And
basename()(in vfio/container) caused compile breakage with the Musl
C library [1].To avoid similar breakage and improve portability, replace
basename()anddirname()withg_path_get_basename()andg_path_get_dirname().[1]:
https://lore.kernel.org/all/[email protected]/---
Zhao Liu (2):
hw/s390x/ccw: Replacebasename()withg_path_get_basename()hw/s390x/ccw: Replacedirname()withg_path_get_dirname()
Quoted 5 lines
Quoted 3 lines
hw/s390x/s390-ccw.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)Reviewed-by Eric Farman <[email protected]>
Thanks Eric!
-Zhao