regressions: Enable both tests for bug 576879 (not fixed).
[libguestfs.git] / daemon / headtail.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26
27 #include "guestfs_protocol.h"
28 #include "daemon.h"
29 #include "actions.h"
30
31 static char **
32 headtail (const char *prog, const char *flag, const char *n, const char *path)
33 {
34   char *out, *err;
35   int fd, flags, r;
36   char **lines;
37
38   CHROOT_IN;
39   fd = open (path, O_RDONLY);
40   CHROOT_OUT;
41
42   if (fd == -1) {
43     reply_with_perror ("%s", path);
44     return NULL;
45   }
46
47   flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd;
48   r = commandf (&out, &err, flags, prog, flag, n, NULL);
49   if (r == -1) {
50     reply_with_error ("%s %s %s: %s", prog, flag, n, err);
51     free (out);
52     free (err);
53     return NULL;
54   }
55
56   free (err);
57
58 #if 0
59   /* Split it at the first whitespace. */
60   len = strcspn (out, " \t\n");
61   out[len] = '\0';
62 #endif
63
64   lines = split_lines (out);
65   free (out);
66   if (lines == NULL) return NULL;
67
68   return lines;
69 }
70
71 char **
72 do_head (const char *path)
73 {
74   return headtail ("head", "-n", "10", path);
75 }
76
77 char **
78 do_tail (const char *path)
79 {
80   return headtail ("tail", "-n", "10", path);
81 }
82
83 char **
84 do_head_n (int n, const char *path)
85 {
86   char nbuf[16];
87
88   snprintf (nbuf, sizeof nbuf, "%d", n);
89
90   return headtail ("head", "-n", nbuf, path);
91 }
92
93 char **
94 do_tail_n (int n, const char *path)
95 {
96   char nbuf[16];
97
98   if (n >= 0)
99     snprintf (nbuf, sizeof nbuf, "%d", n);
100   else
101     snprintf (nbuf, sizeof nbuf, "+%d", -n);
102
103   return headtail ("tail", "-n", nbuf, path);
104 }