fish: Add -N option for making prepared disk images.
[libguestfs.git] / fish / destpaths.c
1 /* guestfish - the filesystem interactive shell
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 <stddef.h>
24 #include <string.h>
25
26 #ifdef HAVE_LIBREADLINE
27 #include <readline/readline.h>
28 #endif
29
30 #include <guestfs.h>
31
32 #include "fish.h"
33
34 #ifdef HAVE_LIBREADLINE
35 // From gnulib's xalloc.h:
36 /* Return 1 if an array of N objects, each of size S, cannot exist due
37    to size arithmetic overflow.  S must be positive and N must be
38    nonnegative.  This is a macro, not an inline function, so that it
39    works correctly even when SIZE_MAX < N.
40
41    By gnulib convention, SIZE_MAX represents overflow in size
42    calculations, so the conservative dividend to use here is
43    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
44    However, malloc (SIZE_MAX) fails on all known hosts where
45    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
46    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
47    branch when S is known to be 1.  */
48 # define xalloc_oversized(n, s) \
49     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
50 #endif
51
52 /* Readline completion for paths on the guest filesystem, also for
53  * devices and LVM names.
54  */
55
56 int complete_dest_paths = 1;
57
58 struct word {
59   char *name;
60   int is_dir;
61 };
62
63 #ifdef HAVE_LIBREADLINE
64 static void
65 free_words (struct word *words, size_t nr_words)
66 {
67   size_t i;
68
69   /* NB. 'words' array is NOT NULL-terminated. */
70   for (i = 0; i < nr_words; ++i)
71     free (words[i].name);
72   free (words);
73 }
74 #endif
75
76 char *
77 complete_dest_paths_generator (const char *text, int state)
78 {
79 #ifdef HAVE_LIBREADLINE
80
81   static size_t len, index;
82   static struct word *words = NULL;
83   static size_t nr_words = 0;
84   guestfs_error_handler_cb old_error_cb;
85   void *old_error_cb_data;
86
87   /* Temporarily replace the error handler so that messages don't
88    * get printed to stderr while we are issuing commands.
89    */
90 #define SAVE_ERROR_CB                                                   \
91   old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data);     \
92   guestfs_set_error_handler (g, NULL, NULL);
93
94   /* Restore error handler. */
95 #define RESTORE_ERROR_CB                                                \
96   guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
97
98   if (!state) {
99     char **strs;
100
101     len = strlen (text);
102     index = 0;
103
104     if (words) free_words (words, nr_words);
105
106     words = NULL;
107     nr_words = 0;
108
109     SAVE_ERROR_CB
110
111 /* Silently do nothing if an allocation fails */
112 #define APPEND_STRS_AND_FREE                                            \
113   do {                                                                  \
114     if (strs) {                                                         \
115       size_t i;                                                         \
116       size_t n = count_strings (strs);                                  \
117                                                                         \
118       if ( n > 0 && ! xalloc_oversized (nr_words + n, sizeof (struct word))) { \
119         struct word *w;                                                 \
120         w = realloc (words, sizeof (struct word) * (nr_words + n));     \
121                                                                         \
122         if (w == NULL) {                                                \
123           free_words (words, nr_words);                                 \
124           words = NULL;                                                 \
125           nr_words = 0;                                                 \
126         } else {                                                        \
127           words = w;                                                    \
128           for (i = 0; i < n; ++i) {                                     \
129             words[nr_words].name = strs[i];                             \
130             words[nr_words].is_dir = 0;                                 \
131             nr_words++;                                                 \
132           }                                                             \
133         }                                                               \
134       }                                                                 \
135       free (strs);                                                      \
136     }                                                                   \
137   } while (0)
138
139     /* Is it a device? */
140     if (len < 5 || STREQLEN (text, "/dev/", 5)) {
141       /* Get a list of everything that can possibly begin with /dev/ */
142       strs = guestfs_list_devices (g);
143       APPEND_STRS_AND_FREE;
144
145       strs = guestfs_list_partitions (g);
146       APPEND_STRS_AND_FREE;
147
148       strs = guestfs_lvs (g);
149       APPEND_STRS_AND_FREE;
150     }
151
152     if (len < 1 || text[0] == '/') {
153       /* If we've got a partial path already, we need to list everything
154        * in that directory, otherwise list everything in /
155        */
156       char *p, *dir;
157       struct guestfs_dirent_list *dirents;
158
159       p = strrchr (text, '/');
160       dir = p && p > text ? strndup (text, p - text) : strdup ("/");
161       if (dir) {
162         dirents = guestfs_readdir (g, dir);
163
164         /* Prepend directory to names before adding them to the list
165          * of words.
166          */
167         if (dirents) {
168           size_t i;
169
170           for (i = 0; i < dirents->len; ++i) {
171             int err;
172
173             if (STRNEQ (dirents->val[i].name, ".") &&
174                 STRNEQ (dirents->val[i].name, "..")) {
175               if (STREQ (dir, "/"))
176                 err = asprintf (&p, "/%s", dirents->val[i].name);
177               else
178                 err = asprintf (&p, "%s/%s", dir, dirents->val[i].name);
179               if (err >= 0) {
180                 if (!xalloc_oversized (nr_words+1, sizeof (struct word))) {
181                   struct word *w;
182
183                   w = realloc (words, sizeof (struct word) * (nr_words+1));
184                   if (w == NULL) {
185                     free_words (words, nr_words);
186                     words = NULL;
187                     nr_words = 0;
188                   }
189                   else {
190                     words = w;
191                     words[nr_words].name = p;
192                     words[nr_words].is_dir = dirents->val[i].ftyp == 'd';
193                     nr_words++;
194                   }
195                 }
196               }
197             }
198           }
199
200           guestfs_free_dirent_list (dirents);
201         }
202       }
203     }
204
205     /* else ...  In theory we could complete other things here such as VG
206      * names.  At the moment we don't do that.
207      */
208
209     RESTORE_ERROR_CB
210   }
211
212   /* This inhibits ordinary (local filename) completion. */
213   rl_attempted_completion_over = 1;
214
215   /* Complete the string. */
216   while (index < nr_words) {
217     struct word *word;
218
219     word = &words[index];
220     index++;
221
222     if (STRCASEEQLEN (word->name, text, len)) {
223       if (word->is_dir)
224         rl_completion_append_character = '/';
225
226       return strdup (word->name);
227     }
228   }
229
230 #endif /* HAVE_LIBREADLINE */
231
232   return NULL;
233 }