Skip to contentSkip to conversation

hw/s390x/ccw: Cleanup basename() and dirname()

PATCH In review Zhao Liu 3 participants 7 messages Dec 21, 2023 – Jan 2, 2024

lore
Share & tools
lore

Zhao Liu

author
#0 lore raw

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
author
#1 lore raw

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
index e2d86d96e728..ab7022a3abe8 100644
@@ -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
Reviewed-by on v1 1/2
#2 lore raw

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 over basename().

Quoted 7 lines

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]/

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
index e2d86d96e728..ab7022a3abe8 100644
@@ -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;
author
#3 lore raw

PATCH 2/2 hw/s390x/ccw: Replace dirname() with g_path_get_dirname()

In reply to Zhao Liu

From: 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
index ab7022a3abe8..5261e66724f1 100644
@@ -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
Reviewed-by on v1 2/2
#4 lore raw

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 ("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().

Thanks,

C.

Quoted 30 lines

---
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
index ab7022a3abe8..5261e66724f1 100644
@@ -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;
Reviewed-by on the v1 series
#5 lore raw
In reply to Zhao Liu
Quoted 23 lines · Zhao Liu

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]/

Quoted 8 lines

---
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(-)
author
#6 lore raw
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: Cleanup basename() and dirname()

Quoted 28 lines · Zhao Liu

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()

Quoted 5 lines
Quoted 3 lines

 hw/s390x/s390-ccw.c | 7 +++++--

 1 file changed, 5 insertions(+), 2 deletions(-)

Thanks Eric!

-Zhao