X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Ffile.c;h=43c875cc7eda5548b5252fb4cd637bdc913e0e59;hb=44da812b424f5e10e268d47149d012d49edf858b;hp=537618f2f3ef80eea81f3960fe5348b0f16d0c0a;hpb=1cf85b1e60e85c4940869c6291d75ac44a5bd190;p=libguestfs.git diff --git a/daemon/file.c b/daemon/file.c index 537618f..43c875c 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -18,7 +18,7 @@ #include -#define _GNU_SOURCE /* for futimens(2) */ +#define _GNU_SOURCE /* for futimens(2) and getline(3) */ #include #include @@ -128,3 +128,115 @@ do_cat (const char *path) return buf; /* caller will free */ } + +char ** +do_read_lines (const char *path) +{ + char **r = NULL; + int size = 0, alloc = 0; + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t n; + + NEED_ROOT (NULL); + ABS_PATH (path, NULL); + + CHROOT_IN; + fp = fopen (path, "r"); + CHROOT_OUT; + + if (!fp) { + reply_with_perror ("fopen: %s", path); + return NULL; + } + + while ((n = getline (&line, &len, fp)) != -1) { + /* Remove either LF or CRLF. */ + if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n') + line[n-2] = '\0'; + else if (n >= 1 && line[n-1] == '\n') + line[n-1] = '\0'; + + if (add_string (&r, &size, &alloc, line) == -1) { + free (line); + fclose (fp); + return NULL; + } + } + + free (line); + + if (add_string (&r, &size, &alloc, NULL) == -1) { + fclose (fp); + return NULL; + } + + if (fclose (fp) == EOF) { + reply_with_perror ("fclose: %s", path); + free_strings (r); + return NULL; + } + + return r; +} + +int +do_rm (const char *path) +{ + int r; + + NEED_ROOT (-1); + ABS_PATH (path, -1); + + CHROOT_IN; + r = unlink (path); + CHROOT_OUT; + + if (r == -1) { + reply_with_perror ("unlink: %s", path); + return -1; + } + + return 0; +} + +int +do_chmod (int mode, const char *path) +{ + int r; + + NEED_ROOT (-1); + ABS_PATH (path, -1); + + CHROOT_IN; + r = chmod (path, mode); + CHROOT_OUT; + + if (r == -1) { + reply_with_perror ("chmod: %s: 0%o", path, mode); + return -1; + } + + return 0; +} + +int +do_chown (int owner, int group, const char *path) +{ + int r; + + NEED_ROOT (-1); + ABS_PATH (path, -1); + + CHROOT_IN; + r = chown (path, owner, group); + CHROOT_OUT; + + if (r == -1) { + reply_with_perror ("chown: %s: %d.%d", path, owner, group); + return -1; + } + + return 0; +}