diff mbox

[RFC,04/16] block: Move filename_decompose to block.c

Message ID 1453804705-7205-5-git-send-email-famz@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Fam Zheng Jan. 26, 2016, 10:38 a.m. UTC
With the return value decoupled from VMDK, it can be reused by other block
code.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block.c               | 40 ++++++++++++++++++++++++++++++++++++++++
 block/vmdk.c          | 40 ----------------------------------------
 include/block/block.h |  2 ++
 3 files changed, 42 insertions(+), 40 deletions(-)

Comments

Eric Blake Jan. 27, 2016, 4:07 p.m. UTC | #1
On 01/26/2016 03:38 AM, Fam Zheng wrote:
> With the return value decoupled from VMDK, it can be reused by other block
> code.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block.c               | 40 ++++++++++++++++++++++++++++++++++++++++
>  block/vmdk.c          | 40 ----------------------------------------
>  include/block/block.h |  2 ++
>  3 files changed, 42 insertions(+), 40 deletions(-)
> 

> +++ b/block.c
> @@ -144,6 +144,46 @@ int path_is_absolute(const char *path)
>  #endif
>  }
>  
> +int filename_decompose(const char *filename, char *path, char *prefix,
> +                       char *postfix, size_t buf_len, Error **errp)
> +{
> +    const char *p, *q;
> +
> +    if (filename == NULL || !strlen(filename)) {
> +        error_setg(errp, "No filename provided");
> +        return -EINVAL;
> +    }
> +    p = strrchr(filename, '/');
> +    if (p == NULL) {
> +        p = strrchr(filename, '\\');
> +    }

I know this is just code motion, but it feels like it does the wrong
thing on Unix boxes (trying too hard to appease Windows boxes).  Is that
something that needs to be independently addressed?

But as for this patch, the code motion is fine.
Reviewed-by: Eric Blake <eblake@redhat.com>
John Snow Feb. 9, 2016, 8:56 p.m. UTC | #2
On 01/26/2016 05:38 AM, Fam Zheng wrote:
> With the return value decoupled from VMDK, it can be reused by other block
> code.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block.c               | 40 ++++++++++++++++++++++++++++++++++++++++
>  block/vmdk.c          | 40 ----------------------------------------
>  include/block/block.h |  2 ++
>  3 files changed, 42 insertions(+), 40 deletions(-)
> 
> diff --git a/block.c b/block.c
> index fa6ad1d..78db342 100644
> --- a/block.c
> +++ b/block.c
> @@ -144,6 +144,46 @@ int path_is_absolute(const char *path)
>  #endif
>  }
>  
> +int filename_decompose(const char *filename, char *path, char *prefix,
> +                       char *postfix, size_t buf_len, Error **errp)
> +{
> +    const char *p, *q;
> +
> +    if (filename == NULL || !strlen(filename)) {
> +        error_setg(errp, "No filename provided");
> +        return -EINVAL;
> +    }
> +    p = strrchr(filename, '/');
> +    if (p == NULL) {
> +        p = strrchr(filename, '\\');
> +    }
> +    if (p == NULL) {
> +        p = strrchr(filename, ':');
> +    }
> +    if (p != NULL) {
> +        p++;
> +        if (p - filename >= buf_len) {
> +            return -EINVAL;
> +        }
> +        pstrcpy(path, p - filename + 1, filename);
> +    } else {
> +        p = filename;
> +        path[0] = '\0';
> +    }
> +    q = strrchr(p, '.');
> +    if (q == NULL) {
> +        pstrcpy(prefix, buf_len, p);
> +        postfix[0] = '\0';
> +    } else {
> +        if (q - p >= buf_len) {
> +            return -EINVAL;
> +        }
> +        pstrcpy(prefix, q - p + 1, p);
> +        pstrcpy(postfix, buf_len, q);
> +    }
> +    return 0;
> +}
> +
>  /* if filename is absolute, just copy it to dest. Otherwise, build a
>     path to it by considering it is relative to base_path. URL are
>     supported. */
> diff --git a/block/vmdk.c b/block/vmdk.c
> index f8f7fcf..505e0c2 100644
> --- a/block/vmdk.c
> +++ b/block/vmdk.c
> @@ -1764,46 +1764,6 @@ exit:
>      return ret;
>  }
>  
> -static int filename_decompose(const char *filename, char *path, char *prefix,
> -                              char *postfix, size_t buf_len, Error **errp)
> -{
> -    const char *p, *q;
> -
> -    if (filename == NULL || !strlen(filename)) {
> -        error_setg(errp, "No filename provided");
> -        return VMDK_ERROR;
> -    }
> -    p = strrchr(filename, '/');
> -    if (p == NULL) {
> -        p = strrchr(filename, '\\');
> -    }
> -    if (p == NULL) {
> -        p = strrchr(filename, ':');
> -    }
> -    if (p != NULL) {
> -        p++;
> -        if (p - filename >= buf_len) {
> -            return VMDK_ERROR;
> -        }
> -        pstrcpy(path, p - filename + 1, filename);
> -    } else {
> -        p = filename;
> -        path[0] = '\0';
> -    }
> -    q = strrchr(p, '.');
> -    if (q == NULL) {
> -        pstrcpy(prefix, buf_len, p);
> -        postfix[0] = '\0';
> -    } else {
> -        if (q - p >= buf_len) {
> -            return VMDK_ERROR;
> -        }
> -        pstrcpy(prefix, q - p + 1, p);
> -        pstrcpy(postfix, buf_len, q);
> -    }
> -    return VMDK_OK;
> -}
> -
>  static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
>  {
>      int idx = 0;
> diff --git a/include/block/block.h b/include/block/block.h
> index bfb76f8..b9b30cb 100644
> --- a/include/block/block.h
> +++ b/include/block/block.h
> @@ -449,6 +449,8 @@ int bdrv_is_snapshot(BlockDriverState *bs);
>  
>  int path_has_protocol(const char *path);
>  int path_is_absolute(const char *path);
> +int filename_decompose(const char *filename, char *path, char *prefix,
> +                       char *postfix, size_t buf_len, Error **errp);
>  void path_combine(char *dest, int dest_size,
>                    const char *base_path,
>                    const char *filename);
> 

Reviewed-by: John Snow <jsnow@redhat.com>
diff mbox

Patch

diff --git a/block.c b/block.c
index fa6ad1d..78db342 100644
--- a/block.c
+++ b/block.c
@@ -144,6 +144,46 @@  int path_is_absolute(const char *path)
 #endif
 }
 
+int filename_decompose(const char *filename, char *path, char *prefix,
+                       char *postfix, size_t buf_len, Error **errp)
+{
+    const char *p, *q;
+
+    if (filename == NULL || !strlen(filename)) {
+        error_setg(errp, "No filename provided");
+        return -EINVAL;
+    }
+    p = strrchr(filename, '/');
+    if (p == NULL) {
+        p = strrchr(filename, '\\');
+    }
+    if (p == NULL) {
+        p = strrchr(filename, ':');
+    }
+    if (p != NULL) {
+        p++;
+        if (p - filename >= buf_len) {
+            return -EINVAL;
+        }
+        pstrcpy(path, p - filename + 1, filename);
+    } else {
+        p = filename;
+        path[0] = '\0';
+    }
+    q = strrchr(p, '.');
+    if (q == NULL) {
+        pstrcpy(prefix, buf_len, p);
+        postfix[0] = '\0';
+    } else {
+        if (q - p >= buf_len) {
+            return -EINVAL;
+        }
+        pstrcpy(prefix, q - p + 1, p);
+        pstrcpy(postfix, buf_len, q);
+    }
+    return 0;
+}
+
 /* if filename is absolute, just copy it to dest. Otherwise, build a
    path to it by considering it is relative to base_path. URL are
    supported. */
diff --git a/block/vmdk.c b/block/vmdk.c
index f8f7fcf..505e0c2 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -1764,46 +1764,6 @@  exit:
     return ret;
 }
 
-static int filename_decompose(const char *filename, char *path, char *prefix,
-                              char *postfix, size_t buf_len, Error **errp)
-{
-    const char *p, *q;
-
-    if (filename == NULL || !strlen(filename)) {
-        error_setg(errp, "No filename provided");
-        return VMDK_ERROR;
-    }
-    p = strrchr(filename, '/');
-    if (p == NULL) {
-        p = strrchr(filename, '\\');
-    }
-    if (p == NULL) {
-        p = strrchr(filename, ':');
-    }
-    if (p != NULL) {
-        p++;
-        if (p - filename >= buf_len) {
-            return VMDK_ERROR;
-        }
-        pstrcpy(path, p - filename + 1, filename);
-    } else {
-        p = filename;
-        path[0] = '\0';
-    }
-    q = strrchr(p, '.');
-    if (q == NULL) {
-        pstrcpy(prefix, buf_len, p);
-        postfix[0] = '\0';
-    } else {
-        if (q - p >= buf_len) {
-            return VMDK_ERROR;
-        }
-        pstrcpy(prefix, q - p + 1, p);
-        pstrcpy(postfix, buf_len, q);
-    }
-    return VMDK_OK;
-}
-
 static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
 {
     int idx = 0;
diff --git a/include/block/block.h b/include/block/block.h
index bfb76f8..b9b30cb 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -449,6 +449,8 @@  int bdrv_is_snapshot(BlockDriverState *bs);
 
 int path_has_protocol(const char *path);
 int path_is_absolute(const char *path);
+int filename_decompose(const char *filename, char *path, char *prefix,
+                       char *postfix, size_t buf_len, Error **errp);
 void path_combine(char *dest, int dest_size,
                   const char *base_path,
                   const char *filename);