diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-03-13 08:19:11 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-03-13 08:19:11 +0000 |
commit | 14df5fa5d1731a502332e9d4a41f54ee3d4a4dac (patch) | |
tree | 72313cef36867c63ae09e1d23bcd5d7c380fdf88 /daemon/mount.c | |
parent | d66dd2260c724bdfe57a8595aac37c8e9173cee5 (diff) | |
download | libguestfs-14df5fa5d1731a502332e9d4a41f54ee3d4a4dac.tar.gz libguestfs-14df5fa5d1731a502332e9d4a41f54ee3d4a4dac.tar.xz libguestfs-14df5fa5d1731a502332e9d4a41f54ee3d4a4dac.zip |
daemon: Implement a growable strings buffer type.
Previously a lot of daemon code used three variables (a string list,
'int size' and 'int alloc') to track growable strings buffers. This
commit implements a simple struct containing the same variables, but
using size_t instead of int:
struct stringsbuf {
char **argv;
size_t size;
size_t alloc;
};
Use it like this:
DECLARE_STRINGSBUF (ret);
//...
if (add_string (&ret, str) == -1)
return NULL;
//...
if (end_stringsbuf (&ret) == -1)
return NULL;
return ret.argv;
Diffstat (limited to 'daemon/mount.c')
-rw-r--r-- | daemon/mount.c | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/daemon/mount.c b/daemon/mount.c index 0c393f7a..5e74ce8f 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -224,8 +224,7 @@ mounts_or_mountpoints (int mp) { FILE *fp; struct mntent *m; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); size_t i; int r; @@ -241,42 +240,43 @@ mounts_or_mountpoints (int mp) while ((m = getmntent (fp)) != NULL) { /* Allow a mount directory like "/sysroot". */ if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) { - if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) { + if (add_string (&ret, m->mnt_fsname) == -1) { error: endmntent (fp); return NULL; } if (mp && - add_string (&ret, &size, &alloc, "/") == -1) + add_string (&ret, "/") == -1) goto error; } /* Or allow a mount directory like "/sysroot/...". */ if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') { - if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) + if (add_string (&ret, m->mnt_fsname) == -1) goto error; if (mp && - add_string (&ret, &size, &alloc, &m->mnt_dir[sysroot_len]) == -1) + add_string (&ret, &m->mnt_dir[sysroot_len]) == -1) goto error; } } endmntent (fp); - if (add_string (&ret, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&ret) == -1) return NULL; /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */ - for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) { - if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) { + for (i = 0; ret.argv[i] != NULL; i += mp ? 2 : 1) { + if (STRPREFIX (ret.argv[i], "/dev/mapper/") || + STRPREFIX (ret.argv[i], "/dev/dm-")) { char *canonical; - r = lv_canonical (ret[i], &canonical); + r = lv_canonical (ret.argv[i], &canonical); if (r == -1) { - free_strings (ret); + free_stringslen (ret.argv, ret.size); return NULL; } if (r == 1) { - free (ret[i]); - ret[i] = canonical; + free (ret.argv[i]); + ret.argv[i] = canonical; } /* Ignore the case where r == 0. This might happen where * eg. a LUKS /dev/mapper device is mounted, but that won't @@ -285,7 +285,7 @@ mounts_or_mountpoints (int mp) } } - return ret; + return ret.argv; } char ** @@ -325,8 +325,7 @@ do_umount_all (void) { FILE *fp; struct mntent *m; - char **mounts = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (mounts); char *err; int i, r; @@ -342,14 +341,14 @@ do_umount_all (void) while ((m = getmntent (fp)) != NULL) { /* Allow a mount directory like "/sysroot". */ if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) { - if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) { + if (add_string (&mounts, m->mnt_dir) == -1) { endmntent (fp); return -1; } } /* Or allow a mount directory like "/sysroot/...". */ if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') { - if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) { + if (add_string (&mounts, m->mnt_dir) == -1) { endmntent (fp); return -1; } @@ -358,21 +357,22 @@ do_umount_all (void) endmntent (fp); - qsort (mounts, size, sizeof (char *), compare_longest_first); + if (mounts.size > 0) + qsort (mounts.argv, mounts.size, sizeof (char *), compare_longest_first); /* Unmount them. */ - for (i = 0; i < size; ++i) { - r = command (NULL, &err, "umount", mounts[i], NULL); + for (i = 0; i < mounts.size; ++i) { + r = command (NULL, &err, "umount", mounts.argv[i], NULL); if (r == -1) { - reply_with_error ("umount: %s: %s", mounts[i], err); + reply_with_error ("umount: %s: %s", mounts.argv[i], err); free (err); - free_stringslen (mounts, size); + free_stringslen (mounts.argv, mounts.size); return -1; } free (err); } - free_stringslen (mounts, size); + free_stringslen (mounts.argv, mounts.size); return 0; } |