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.
30 /* The Augeas handle. We maintain a single handle per daemon, which
31 * is all that is necessary and reduces the complexity of the API
34 static augeas *aug = NULL;
36 #define NEED_AUG(errcode) \
39 reply_with_error ("%s: you must call 'aug-init' first to initialize Augeas", __func__); \
45 /* We need to rewrite the root path so it is based at /sysroot. */
47 do_aug_init (const char *root, int flags)
60 len = strlen (root) + 9;
63 reply_with_perror ("malloc");
66 snprintf (buf, len, "/sysroot%s", root);
68 aug = aug_init (buf, NULL, flags);
72 reply_with_error ("Augeas initialization failed");
91 do_aug_defvar (const char *name, const char *expr)
93 #ifdef HAVE_AUG_DEFVAR
98 r = aug_defvar (aug, name, expr);
100 reply_with_error ("Augeas defvar failed");
105 reply_with_error ("aug_defvar is not available");
110 guestfs_aug_defnode_ret *
111 do_aug_defnode (const char *name, const char *expr, const char *val)
113 #ifdef HAVE_AUG_DEFNODE
114 static guestfs_aug_defnode_ret r;
119 r.nrnodes = aug_defnode (aug, name, expr, val, &created);
120 if (r.nrnodes == -1) {
121 reply_with_error ("Augeas defnode failed");
127 reply_with_error ("aug_defvar is not available");
133 do_aug_get (const char *path)
135 const char *value = NULL;
141 r = aug_get (aug, path, &value);
143 reply_with_error ("no matching node");
147 reply_with_error ("Augeas get failed");
151 /* value can still be NULL here, eg. try with path == "/augeas".
152 * I don't understand this case, and it seems to contradict the
156 reply_with_error ("Augeas returned NULL match");
160 /* The value is an internal Augeas string, so we must copy it. GC FTW. */
163 reply_with_perror ("strdup");
167 return v; /* Caller frees. */
171 do_aug_set (const char *path, const char *val)
177 r = aug_set (aug, path, val);
179 reply_with_error ("Augeas set failed");
187 do_aug_insert (const char *path, const char *label, int before)
193 r = aug_insert (aug, path, label, before);
195 reply_with_error ("Augeas insert failed");
203 do_aug_rm (const char *path)
209 r = aug_rm (aug, path);
211 reply_with_error ("Augeas rm failed");
219 do_aug_mv (const char *src, const char *dest)
225 r = aug_mv (aug, src, dest);
227 reply_with_error ("Augeas mv failed");
235 do_aug_match (const char *path)
237 char **matches = NULL;
243 r = aug_match (aug, path, &matches);
245 reply_with_error ("Augeas match failed");
249 /* This returns an array of length r, which we must extend
250 * and add a terminating NULL.
252 vp = realloc (matches, sizeof (char *) * (r+1));
254 reply_with_perror ("realloc");
261 return matches; /* Caller frees. */
269 if (aug_save (aug) == -1) {
270 reply_with_error ("Augeas save failed");
283 if (aug_load (aug) == -1) {
284 reply_with_error ("Augeas load failed");
290 reply_with_error ("aug_load is not available");
295 /* Simpler version of aug-match, which also sorts the output. */
297 do_aug_ls (const char *path)
305 ABS_PATH (path, NULL);
310 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
311 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
316 /* we know path must be "/" because of ABS_PATH above */
317 matches = do_aug_match ("/");
319 len += 3; /* / * + terminating \0 */
322 reply_with_perror ("malloc");
326 snprintf (buf, len, "%s/*", path);
327 matches = do_aug_match (buf);
332 return NULL; /* do_aug_match has already sent the error */
334 sort_strings (matches, count_strings (matches));
335 return matches; /* Caller frees. */