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., 675 Mass Ave, Cambridge, MA 02139, 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_insert (const char *path, const char *label, int before)
220 r = aug_insert (aug, path, label, before);
222 reply_with_error ("Augeas insert failed");
233 do_aug_rm (const char *path)
240 r = aug_rm (aug, path);
242 reply_with_error ("Augeas rm failed");
253 do_aug_mv (const char *src, const char *dest)
260 r = aug_mv (aug, src, dest);
262 reply_with_error ("Augeas mv failed");
273 do_aug_match (const char *path)
276 char **matches = NULL;
282 r = aug_match (aug, path, &matches);
284 reply_with_error ("Augeas match failed");
288 /* This returns an array of length r, which we must extend
289 * and add a terminating NULL.
291 vp = realloc (matches, sizeof (char *) * (r+1));
293 reply_with_perror ("realloc");
300 return matches; /* Caller frees. */
302 NOT_AVAILABLE (NULL);
312 if (aug_save (aug) == -1) {
313 reply_with_error ("Augeas save failed");
329 if (aug_load (aug) == -1) {
330 reply_with_error ("Augeas load failed");
340 /* Simpler version of aug-match, which also sorts the output. */
342 do_aug_ls (const char *path)
351 ABS_PATH (path, return NULL);
356 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
357 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
362 /* we know path must be "/" because of ABS_PATH above */
363 matches = do_aug_match ("/");
365 len += 3; /* / * + terminating \0 */
368 reply_with_perror ("malloc");
372 snprintf (buf, len, "%s/*", path);
373 matches = do_aug_match (buf);
378 return NULL; /* do_aug_match has already sent the error */
380 sort_strings (matches, count_strings ((void *) matches));
381 return matches; /* Caller frees. */
383 NOT_AVAILABLE (NULL);