summaryrefslogtreecommitdiffstats
path: root/src/file.c
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-08-17 11:07:33 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-08-17 16:08:13 +0100
commit735ce57cda24e2e2f41418d9a9ad34944fefdfb9 (patch)
treee320bcbf51dad6e798260601b62685e46e281b1d /src/file.c
parent96d3ac28d636290e9478ac27226862e627682025 (diff)
downloadlibguestfs-735ce57cda24e2e2f41418d9a9ad34944fefdfb9.tar.gz
libguestfs-735ce57cda24e2e2f41418d9a9ad34944fefdfb9.tar.xz
libguestfs-735ce57cda24e2e2f41418d9a9ad34944fefdfb9.zip
guestfs_read_lines: Reimplement to avoid protocol limits.
This also makes a larger test suite for this command.
Diffstat (limited to 'src/file.c')
-rw-r--r--src/file.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/file.c b/src/file.c
index 84edc403..13f08cd6 100644
--- a/src/file.c
+++ b/src/file.c
@@ -123,6 +123,72 @@ guestfs__read_file (guestfs_h *g, const char *path, size_t *size_r)
}
char **
+guestfs__read_lines (guestfs_h *g, const char *file)
+{
+ size_t i, count, size, len;
+ char *buf = NULL;
+ char **ret = NULL;
+
+ /* Read the whole file into memory. */
+ buf = guestfs__read_file (g, file, &size);
+ if (buf == NULL)
+ return NULL;
+
+ /* 'buf' contains the list of strings, separated by LF or CRLF
+ * characters. Convert this to a list of lines. Note we have to
+ * handle the cases where the buffer is zero length and where the
+ * final string is not terminated.
+ */
+ count = 0;
+ for (i = 0; i < size; ++i)
+ if (buf[i] == '\n')
+ count++;
+ if (size > 0 && buf[size-1] != '\n')
+ count++;
+
+ ret = malloc ((count + 1) * sizeof (char *));
+ if (!ret) {
+ perrorf (g, "malloc");
+ goto err;
+ }
+
+ count = 0;
+ if (size > 0) {
+ ret[count++] = buf;
+ for (i = 0; i < size; ++i) {
+ if (buf[i] == '\n') {
+ buf[i] = '\0';
+ if (i+1 < size)
+ ret[count++] = &buf[i+1];
+ }
+ }
+ }
+ ret[count] = NULL;
+
+ /* Duplicate the strings, and remove the trailing \r characters if any. */
+ for (i = 0; ret[i] != NULL; ++i) {
+ ret[i] = strdup (ret[i]);
+ if (ret[i] == NULL) {
+ perrorf (g, "strdup");
+ while (i > 0)
+ free (ret[--i]);
+ goto err;
+ }
+ len = strlen (ret[i]);
+ if (len > 0 && ret[i][len-1] == '\r')
+ ret[i][len-1] = '\0';
+ }
+ free (buf);
+
+ return ret;
+
+ err:
+ free (buf);
+ free (ret);
+ return NULL;
+}
+
+char **
guestfs__find (guestfs_h *g, const char *directory)
{
int fd = -1;