Implementations of 'cat', 'ls', and some cleanups.
[libguestfs.git] / daemon / ls.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 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 static int
33 compare (const void *vp1, const void *vp2)
34 {
35   char * const *p1 = (char * const *) vp1;
36   char * const *p2 = (char * const *) vp2;
37   return strcmp (*p1, *p2);
38 }
39
40 char **
41 do_ls (const char *path)
42 {
43   char **r = NULL;
44   int size = 0, alloc = 0;
45   DIR *dir;
46   struct dirent *d;
47
48   NEED_ROOT (NULL);
49   ABS_PATH (path, NULL);
50
51   CHROOT_IN;
52   dir = opendir (path);
53   CHROOT_OUT;
54
55   if (!dir) {
56     reply_with_perror ("opendir: %s", path);
57     return NULL;
58   }
59
60   while ((d = readdir (dir)) != NULL) {
61     if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
62       continue;
63
64     if (add_string (&r, &size, &alloc, d->d_name) == -1) {
65       closedir (dir);
66       return NULL;
67     }
68   }
69
70   if (add_string (&r, &size, &alloc, NULL) == -1) {
71     closedir (dir);
72     return NULL;
73   }
74
75   if (closedir (dir) == -1) {
76     reply_with_perror ("closedir: %s", path);
77     free_strings (r);
78     return NULL;
79   }
80
81   qsort (r, size-1, sizeof (char *), compare);
82   return r;
83 }
84
85 char *
86 do_ll (const char *path)
87 {
88   int r, len;
89   char *out, *err;
90   char *spath;
91
92   //NEED_ROOT
93   ABS_PATH (path, NULL);
94
95   /* This exposes the /sysroot, because we can't chroot and run the ls
96    * command (since 'ls' won't necessarily exist in the chroot).  This
97    * command is not meant for serious use anyway, just for quick
98    * interactive sessions.  For the same reason, you can also "escape"
99    * the sysroot (eg. 'll /..').
100    */
101   len = strlen (path) + 9;
102   spath = malloc (len);
103   if (!spath) {
104     reply_with_perror ("malloc");
105     return NULL;
106   }
107   snprintf (spath, len, "/sysroot%s", path);
108
109   r = command (&out, &err, "ls", "-la", spath, NULL);
110   free (spath);
111   if (r == -1) {
112     reply_with_error ("%s", err);
113     free (out);
114     free (err);
115     return NULL;
116   }
117
118   free (err);
119   return out;                   /* caller frees */
120 }