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.
34 /* The Augeas handle. We maintain a single handle per daemon, which
35 * is all that is necessary and reduces the complexity of the API
38 static augeas *aug = NULL;
41 #define NEED_AUG(errcode) \
44 reply_with_error ("%s: you must call 'aug-init' first to initialize Augeas", __func__); \
50 /* We need to rewrite the root path so it is based at /sysroot. */
52 do_aug_init (const char *root, int flags)
62 buf = sysroot_path (root);
64 reply_with_perror ("malloc");
68 aug = aug_init (buf, NULL, flags);
72 reply_with_error ("Augeas initialization failed");
78 reply_with_error ("%s is not available", __func__);
94 reply_with_error ("%s is not available", __func__);
100 do_aug_defvar (const char *name, const char *expr)
102 #ifdef HAVE_AUG_DEFVAR
107 r = aug_defvar (aug, name, expr);
109 reply_with_error ("Augeas defvar failed");
114 reply_with_error ("%s is not available", __func__);
119 guestfs_int_int_bool *
120 do_aug_defnode (const char *name, const char *expr, const char *val)
122 #ifdef HAVE_AUG_DEFNODE
123 static guestfs_int_int_bool r;
128 r.i = aug_defnode (aug, name, expr, val, &created);
130 reply_with_error ("Augeas defnode failed");
136 reply_with_error ("%s is not available", __func__);
142 do_aug_get (const char *path)
145 const char *value = NULL;
151 r = aug_get (aug, path, &value);
153 reply_with_error ("no matching node");
157 reply_with_error ("Augeas get failed");
161 /* value can still be NULL here, eg. try with path == "/augeas".
162 * I don't understand this case, and it seems to contradict the
166 reply_with_error ("Augeas returned NULL match");
170 /* The value is an internal Augeas string, so we must copy it. GC FTW. */
173 reply_with_perror ("strdup");
177 return v; /* Caller frees. */
179 reply_with_error ("%s is not available", __func__);
185 do_aug_set (const char *path, const char *val)
192 r = aug_set (aug, path, val);
194 reply_with_error ("Augeas set failed");
200 reply_with_error ("%s is not available", __func__);
206 do_aug_insert (const char *path, const char *label, int before)
213 r = aug_insert (aug, path, label, before);
215 reply_with_error ("Augeas insert failed");
221 reply_with_error ("%s is not available", __func__);
227 do_aug_rm (const char *path)
234 r = aug_rm (aug, path);
236 reply_with_error ("Augeas rm failed");
242 reply_with_error ("%s is not available", __func__);
248 do_aug_mv (const char *src, const char *dest)
255 r = aug_mv (aug, src, dest);
257 reply_with_error ("Augeas mv failed");
263 reply_with_error ("%s is not available", __func__);
269 do_aug_match (const char *path)
272 char **matches = NULL;
278 r = aug_match (aug, path, &matches);
280 reply_with_error ("Augeas match failed");
284 /* This returns an array of length r, which we must extend
285 * and add a terminating NULL.
287 vp = realloc (matches, sizeof (char *) * (r+1));
289 reply_with_perror ("realloc");
296 return matches; /* Caller frees. */
298 reply_with_error ("%s is not available", __func__);
309 if (aug_save (aug) == -1) {
310 reply_with_error ("Augeas save failed");
316 reply_with_error ("%s is not available", __func__);
327 if (aug_load (aug) == -1) {
328 reply_with_error ("Augeas load failed");
334 reply_with_error ("%s is not available", __func__);
339 /* Simpler version of aug-match, which also sorts the output. */
341 do_aug_ls (const char *path)
350 ABS_PATH (path, return NULL);
355 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
356 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
361 /* we know path must be "/" because of ABS_PATH above */
362 matches = do_aug_match ("/");
364 len += 3; /* / * + terminating \0 */
367 reply_with_perror ("malloc");
371 snprintf (buf, len, "%s/*", path);
372 matches = do_aug_match (buf);
377 return NULL; /* do_aug_match has already sent the error */
379 sort_strings (matches, count_strings ((void *) matches));
380 return matches; /* Caller frees. */
382 reply_with_error ("%s is not available", __func__);