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) + 8;
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)
97 r = aug_defvar (aug, name, expr);
99 reply_with_error ("Augeas defvar failed");
105 guestfs_aug_defnode_ret *
106 do_aug_defnode (const char *name, const char *expr, const char *val)
108 static guestfs_aug_defnode_ret r;
113 r.nrnodes = aug_defnode (aug, name, expr, val, &created);
114 if (r.nrnodes == -1) {
115 reply_with_error ("Augeas defnode failed");
123 do_aug_get (const char *path)
125 const char *value = NULL;
131 r = aug_get (aug, path, &value);
133 reply_with_error ("no matching node");
137 reply_with_error ("Augeas get failed");
141 /* value can still be NULL here, eg. try with path == "/augeas".
142 * I don't understand this case, and it seems to contradict the
146 reply_with_error ("Augeas returned NULL match");
150 /* The value is an internal Augeas string, so we must copy it. GC FTW. */
153 reply_with_perror ("strdup");
157 return v; /* Caller frees. */
161 do_aug_set (const char *path, const char *val)
167 r = aug_set (aug, path, val);
169 reply_with_error ("Augeas set failed");
177 do_aug_insert (const char *path, const char *label, int before)
183 r = aug_insert (aug, path, label, before);
185 reply_with_error ("Augeas insert failed");
193 do_aug_rm (const char *path)
199 r = aug_rm (aug, path);
201 reply_with_error ("Augeas rm failed");
209 do_aug_mv (const char *src, const char *dest)
215 r = aug_mv (aug, src, dest);
217 reply_with_error ("Augeas mv failed");
225 do_aug_match (const char *path)
227 char **matches = NULL;
233 r = aug_match (aug, path, &matches);
235 reply_with_error ("Augeas match failed");
239 /* This returns an array of length r, which we must extend
240 * and add a terminating NULL.
242 vp = realloc (matches, sizeof (char *) * (r+1));
244 reply_with_perror ("realloc");
251 return matches; /* Caller frees. */
259 if (aug_save (aug) == -1) {
260 reply_with_error ("Augeas save failed");
272 if (aug_load (aug) == -1) {
273 reply_with_error ("Augeas load failed");
280 /* Simpler version of aug-match, which also sorts the output. */
282 do_aug_ls (const char *path)
290 ABS_PATH (path, NULL);
295 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
296 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
301 /* we know path must be "/" because of ABS_PATH above */
302 matches = do_aug_match ("/");
304 len += 3; /* / * + terminating \0 */
307 reply_with_perror ("malloc");
311 snprintf (buf, len, "%s/*", path);
312 matches = do_aug_match (buf);
317 return NULL; /* do_aug_match has already sent the error */
319 sort_strings (matches, count_strings (matches));
320 return matches; /* Caller frees. */