1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "optgroups.h"
35 /* The Augeas handle. We maintain a single handle per daemon, which
36 * is all that is necessary and reduces the complexity of the API
39 static augeas *aug = NULL;
41 #define NEED_AUG(errcode) \
44 reply_with_error ("%s: you must call 'aug-init' first to initialize Augeas", __func__); \
51 optgroup_augeas_available (void)
55 #else /* !HAVE_AUGEAS */
57 optgroup_augeas_available (void)
63 /* We need to rewrite the root path so it is based at /sysroot. */
65 do_aug_init (const char *root, int flags)
75 buf = sysroot_path (root);
77 reply_with_perror ("malloc");
81 aug = aug_init (buf, NULL, flags);
85 reply_with_error ("Augeas initialization failed");
111 do_aug_defvar (const char *name, const char *expr)
113 #ifdef HAVE_AUG_DEFVAR
118 r = aug_defvar (aug, name, expr);
120 reply_with_error ("Augeas defvar failed");
129 guestfs_int_int_bool *
130 do_aug_defnode (const char *name, const char *expr, const char *val)
132 #ifdef HAVE_AUG_DEFNODE
133 static guestfs_int_int_bool r;
138 r.i = aug_defnode (aug, name, expr, val, &created);
140 reply_with_error ("Augeas defnode failed");
146 NOT_AVAILABLE (NULL);
151 do_aug_get (const char *path)
154 const char *value = NULL;
160 r = aug_get (aug, path, &value);
162 reply_with_error ("no matching node");
166 reply_with_error ("Augeas get failed");
170 /* value can still be NULL here, eg. try with path == "/augeas".
171 * I don't understand this case, and it seems to contradict the
175 reply_with_error ("Augeas returned NULL match");
179 /* The value is an internal Augeas string, so we must copy it. GC FTW. */
182 reply_with_perror ("strdup");
186 return v; /* Caller frees. */
188 NOT_AVAILABLE (NULL);
193 do_aug_set (const char *path, const char *val)
200 r = aug_set (aug, path, val);
202 reply_with_error ("Augeas set failed");
213 do_aug_clear (const char *path)
220 r = aug_set (aug, path, NULL);
222 reply_with_error ("Augeas clear failed");
233 do_aug_insert (const char *path, const char *label, int before)
240 r = aug_insert (aug, path, label, before);
242 reply_with_error ("Augeas insert failed");
253 do_aug_rm (const char *path)
260 r = aug_rm (aug, path);
262 reply_with_error ("Augeas rm failed");
273 do_aug_mv (const char *src, const char *dest)
280 r = aug_mv (aug, src, dest);
282 reply_with_error ("Augeas mv failed");
293 do_aug_match (const char *path)
296 char **matches = NULL;
302 r = aug_match (aug, path, &matches);
304 reply_with_error ("Augeas match failed");
308 /* This returns an array of length r, which we must extend
309 * and add a terminating NULL.
311 vp = realloc (matches, sizeof (char *) * (r+1));
313 reply_with_perror ("realloc");
320 return matches; /* Caller frees. */
322 NOT_AVAILABLE (NULL);
332 if (aug_save (aug) == -1) {
333 reply_with_error ("Augeas save failed");
349 if (aug_load (aug) == -1) {
350 reply_with_error ("Augeas load failed");
360 /* Simpler version of aug-match, which also sorts the output. */
362 do_aug_ls (const char *path)
371 /* Note that path might also be a previously defined variable
372 * (defined with aug_defvar). See RHBZ#580016.
378 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
379 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
383 if (STREQ (path, "/"))
384 matches = do_aug_match ("/*");
386 len += 3; /* / * + terminating \0 */
389 reply_with_perror ("malloc");
393 snprintf (buf, len, "%s/*", path);
394 matches = do_aug_match (buf);
399 return NULL; /* do_aug_match has already sent the error */
401 sort_strings (matches, count_strings ((void *) matches));
402 return matches; /* Caller frees. */
404 NOT_AVAILABLE (NULL);