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)
65 buf = sysroot_path (root);
67 reply_with_perror ("malloc");
71 aug = aug_init (buf, NULL, flags);
75 reply_with_error ("Augeas initialization failed");
81 reply_with_error ("%s is not available", __func__);
97 reply_with_error ("%s is not available", __func__);
103 do_aug_defvar (char *name, char *expr)
105 #ifdef HAVE_AUG_DEFVAR
110 r = aug_defvar (aug, name, expr);
112 reply_with_error ("Augeas defvar failed");
117 reply_with_error ("%s is not available", __func__);
122 guestfs_int_int_bool *
123 do_aug_defnode (char *name, char *expr, char *val)
125 #ifdef HAVE_AUG_DEFNODE
126 static guestfs_int_int_bool r;
131 r.i = aug_defnode (aug, name, expr, val, &created);
133 reply_with_error ("Augeas defnode failed");
139 reply_with_error ("%s is not available", __func__);
145 do_aug_get (char *path)
148 const char *value = NULL;
154 r = aug_get (aug, path, &value);
156 reply_with_error ("no matching node");
160 reply_with_error ("Augeas get failed");
164 /* value can still be NULL here, eg. try with path == "/augeas".
165 * I don't understand this case, and it seems to contradict the
169 reply_with_error ("Augeas returned NULL match");
173 /* The value is an internal Augeas string, so we must copy it. GC FTW. */
176 reply_with_perror ("strdup");
180 return v; /* Caller frees. */
182 reply_with_error ("%s is not available", __func__);
188 do_aug_set (char *path, char *val)
195 r = aug_set (aug, path, val);
197 reply_with_error ("Augeas set failed");
203 reply_with_error ("%s is not available", __func__);
209 do_aug_insert (char *path, char *label, int before)
216 r = aug_insert (aug, path, label, before);
218 reply_with_error ("Augeas insert failed");
224 reply_with_error ("%s is not available", __func__);
230 do_aug_rm (char *path)
237 r = aug_rm (aug, path);
239 reply_with_error ("Augeas rm failed");
245 reply_with_error ("%s is not available", __func__);
251 do_aug_mv (char *src, char *dest)
258 r = aug_mv (aug, src, dest);
260 reply_with_error ("Augeas mv failed");
266 reply_with_error ("%s is not available", __func__);
272 do_aug_match (char *path)
275 char **matches = NULL;
281 r = aug_match (aug, path, &matches);
283 reply_with_error ("Augeas match failed");
287 /* This returns an array of length r, which we must extend
288 * and add a terminating NULL.
290 vp = realloc (matches, sizeof (char *) * (r+1));
292 reply_with_perror ("realloc");
299 return matches; /* Caller frees. */
301 reply_with_error ("%s is not available", __func__);
312 if (aug_save (aug) == -1) {
313 reply_with_error ("Augeas save failed");
319 reply_with_error ("%s is not available", __func__);
330 if (aug_load (aug) == -1) {
331 reply_with_error ("Augeas load failed");
337 reply_with_error ("%s is not available", __func__);
342 /* Simpler version of aug-match, which also sorts the output. */
344 do_aug_ls (char *path)
353 ABS_PATH (path, NULL);
358 (path[len-1] == '/' || path[len-1] == ']' || path[len-1] == '*')) {
359 reply_with_error ("don't use aug-ls with a path that ends with / ] *");
364 /* we know path must be "/" because of ABS_PATH above */
365 matches = do_aug_match ("/");
367 len += 3; /* / * + terminating \0 */
370 reply_with_perror ("malloc");
374 snprintf (buf, len, "%s/*", path);
375 matches = do_aug_match (buf);
380 return NULL; /* do_aug_match has already sent the error */
382 sort_strings (matches, count_strings (matches));
383 return matches; /* Caller frees. */
385 reply_with_error ("%s is not available", __func__);