[Libguestfs] [PATCH v2 2/5] daemon: move read_whole_file to common utils

Pino Toscano ptoscano at redhat.com
Mon Sep 23 16:23:18 UTC 2019


Move the read_whole_file function to the common utilities of the daemon,
so other parts can use it.  For this purpose, add an out parameter to
get the amount of bytes read.

Except from the parameter addition, this should be just refactoring.
---
 daemon/9p.c     | 58 +------------------------------------------------
 daemon/daemon.h |  1 +
 daemon/utils.c  | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 57 deletions(-)

diff --git a/daemon/9p.c b/daemon/9p.c
index 55644249d..ce73e2ba0 100644
--- a/daemon/9p.c
+++ b/daemon/9p.c
@@ -34,8 +34,6 @@
 
 #define BUS_PATH "/sys/bus/virtio/drivers/9pnet_virtio"
 
-static char *read_whole_file (const char *filename);
-
 /* https://bugzilla.redhat.com/show_bug.cgi?id=714981#c1 */
 char **
 do_list_9p (void)
@@ -82,7 +80,7 @@ do_list_9p (void)
        * the mount tag length to be unlimited (or up to 65536 bytes).
        * See: linux/include/linux/virtio_9p.h
        */
-      CLEANUP_FREE char *mount_tag = read_whole_file (mount_tag_path);
+      CLEANUP_FREE char *mount_tag = read_whole_file (mount_tag_path, NULL);
       if (mount_tag == 0)
         continue;
 
@@ -117,60 +115,6 @@ do_list_9p (void)
   return take_stringsbuf (&r);
 }
 
-/* Read whole file into dynamically allocated array.  If there is an
- * error, DON'T call reply_with_perror, just return NULL.  Returns a
- * \0-terminated string.
- */
-static char *
-read_whole_file (const char *filename)
-{
-  char *r = NULL;
-  size_t alloc = 0, size = 0;
-  int fd;
-
-  fd = open (filename, O_RDONLY|O_CLOEXEC);
-  if (fd == -1) {
-    perror (filename);
-    return NULL;
-  }
-
-  while (1) {
-    alloc += 256;
-    char *r2 = realloc (r, alloc);
-    if (r2 == NULL) {
-      perror ("realloc");
-      free (r);
-      close (fd);
-      return NULL;
-    }
-    r = r2;
-
-    /* The '- 1' in the size calculation ensures there is space below
-     * to add \0 to the end of the input.
-     */
-    ssize_t n = read (fd, r + size, alloc - size - 1);
-    if (n == -1) {
-      fprintf (stderr, "read: %s: %m\n", filename);
-      free (r);
-      close (fd);
-      return NULL;
-    }
-    if (n == 0)
-      break;
-    size += n;
-  }
-
-  if (close (fd) == -1) {
-    fprintf (stderr, "close: %s: %m\n", filename);
-    free (r);
-    return NULL;
-  }
-
-  r[size] = '\0';
-
-  return r;
-}
-
 /* Takes optional arguments, consult optargs_bitmask. */
 int
 do_mount_9p (const char *mount_tag, const char *mountpoint, const char *options)
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 66bfdc49e..170fb2537 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -84,6 +84,7 @@ extern int random_name (char *template);
 extern char *get_random_uuid (void);
 extern char *make_exclude_from_file (const char *function, char *const *excludes);
 extern int asprintf_nowarn (char **strp, const char *fmt, ...);
+extern char *read_whole_file (const char *filename, size_t *size_r);
 
 /* mountable functions (in utils.c) */
 extern char *mountable_to_string (const mountable_t *mountable);
diff --git a/daemon/utils.c b/daemon/utils.c
index c3f88bcab..118965225 100644
--- a/daemon/utils.c
+++ b/daemon/utils.c
@@ -827,3 +827,61 @@ cleanup_free_mountable (mountable_t *mountable)
     free (mountable->volume);
   }
 }
+
+/**
+ * Read whole file into dynamically allocated array.  If there is an
+ * error, DON'T call reply_with_perror, just return NULL.  Returns a
+ * C<\0>-terminated string.  C<size_r> can be specified to get the
+ * size of the returned data.
+ */
+char *
+read_whole_file (const char *filename, size_t *size_r)
+{
+  char *r = NULL;
+  size_t alloc = 0, size = 0;
+  int fd;
+
+  fd = open (filename, O_RDONLY|O_CLOEXEC);
+  if (fd == -1) {
+    perror (filename);
+    return NULL;
+  }
+
+  while (1) {
+    alloc += 256;
+    char *r2 = realloc (r, alloc);
+    if (r2 == NULL) {
+      perror ("realloc");
+      free (r);
+      close (fd);
+      return NULL;
+    }
+    r = r2;
+
+    /* The '- 1' in the size calculation ensures there is space below
+     * to add \0 to the end of the input.
+     */
+    ssize_t n = read (fd, r + size, alloc - size - 1);
+    if (n == -1) {
+      fprintf (stderr, "read: %s: %m\n", filename);
+      free (r);
+      close (fd);
+      return NULL;
+    }
+    if (n == 0)
+      break;
+    size += n;
+  }
+
+  if (close (fd) == -1) {
+    fprintf (stderr, "close: %s: %m\n", filename);
+    free (r);
+    return NULL;
+  }
+
+  r[size] = '\0';
+  if (size_r != NULL)
+    *size_r = size;
+
+  return r;
+}
-- 
2.21.0




More information about the Libguestfs mailing list