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 (char *root, int flags)
66 len = strlen (root) + 9;
69 reply_with_perror ("malloc");
72 snprintf (buf, len, "/sysroot%s", root);
74 aug = aug_init (buf, NULL, flags);
78 reply_with_error ("Augeas initialization failed");
84 reply_with_error ("%s is not available", __func__);
100 reply_with_error ("%s is not available", __func__);
106 do_aug_defvar (char *name, char *expr)
108 #ifdef HAVE_AUG_DEFVAR
113 r = aug_defvar (aug, name, expr);
115 reply_with_error ("Augeas defvar failed");
120 reply_with_error ("%s is not available", __func__);
125 guestfs_aug_defnode_ret *
126 do_aug_defnode (char *name, char *expr, char *val)
128 #ifdef HAVE_AUG_DEFNODE
129 static guestfs_aug_defnode_ret r;
134 r.nrnodes = aug_defnode (aug, name, expr, val, &created);
135 if (r.nrnodes == -1) {
136 reply_with_error ("Augeas defnode failed");
142 reply_with_error ("%s is not available", __func__);
148 do_aug_get (char *path)
151 const char *value = NULL;
157 r = aug_get (aug, path, &value);
159 reply_with_error ("no matching node");
163 reply_with_error ("Augeas get failed");
167 /* value can still be NULL here, eg. try with path == "/augeas".
168 * I don't understand this case, and it seems to contradict the
172 reply_with_error ("Augeas returned NULL match");
176 /* The value is an internal Augeas string, so we must copy it. GC FTW. */
179 reply_with_perror ("strdup");
183 return v; /* Caller frees. */
185 reply_with_error ("%s is not available", __func__);
191 do_aug_set (char *path, char *val)
198 r = aug_set (aug, path, val);
200 reply_with_error ("Augeas set failed");
206 reply_with_error ("%s is not available", __func__);
212 do_aug_insert (char *path, char *label, int before)
219 r = aug_insert (aug, path, label, before);
221 reply_with_error ("Augeas insert failed");
227 reply_with_error ("%s is not available", __func__);
233 do_aug_rm (char *path)
240 r = aug_rm (aug, path);
242 reply_with_error ("Augeas rm failed");
248 reply_with_error ("%s is not available", __func__);
254 do_aug_mv (char *src, char *dest)
261 r = aug_mv (aug, src, dest);
263 reply_with_error ("Augeas mv failed");
269 reply_with_error ("%s is not available", __func__);
275 do_aug_match (char *path)
278 char **matches = NULL;
284 r = aug_match (aug, path, &matches);
286 reply_with_error ("Augeas match failed");
290 /* This returns an array of length r, which we must extend
291 * and add a terminating NULL.
293 vp = realloc (matches, sizeof (char *) * (r+1));
295 reply_with_perror ("realloc");
302 return matches; /* Caller frees. */
304 reply_with_error ("%s is not available", __func__);
315 if (aug_save (aug) == -1) {
316 reply_with_error ("Augeas save failed");
322 reply_with_error ("%s is not available", __func__);
333 if (aug_load (aug) == -1) {
334 reply_with_error ("Augeas load failed");
340 reply_with_error ("%s is not available", __func__);
345 /* Simpler version of aug-match, which also sorts the output. */
347 do_aug_ls (char *path)
356 ABS_PATH (path, NULL);
361 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
362 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
367 /* we know path must be "/" because of ABS_PATH above */
368 matches = do_aug_match ("/");
370 len += 3; /* / * + terminating \0 */
373 reply_with_perror ("malloc");
377 snprintf (buf, len, "%s/*", path);
378 matches = do_aug_match (buf);
383 return NULL; /* do_aug_match has already sent the error */
385 sort_strings (matches, count_strings (matches));
386 return matches; /* Caller frees. */
388 reply_with_error ("%s is not available", __func__);