From fd1eb7a72977f5bd054e25a864207786512e26af Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 29 Sep 2011 17:48:03 +0200 Subject: libmount: add function to parse offsets/sizes --- libmount/src/context_umount.c | 16 +++++----------- libmount/src/mountP.h | 2 ++ libmount/src/utils.c | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) (limited to 'libmount') diff --git a/libmount/src/context_umount.c b/libmount/src/context_umount.c index bc08affc0..e271845e2 100644 --- a/libmount/src/context_umount.c +++ b/libmount/src/context_umount.c @@ -127,17 +127,11 @@ static int mnt_loopdev_associated_fs(const char *devname, struct libmnt_fs *fs) /* check for offset option in @fs */ optstr = (char *) mnt_fs_get_user_options(fs); - if (optstr && !mnt_optstr_get_option(optstr, "offset=", &val, &valsz)) { - int rc; - - val = strndup(val, valsz); - if (!val) - return 0; - rc = strtosize(val, &offset); - free(val); - if (rc) - return 0; - } + + if (optstr && + mnt_optstr_get_option(optstr, "offset", &val, &valsz) == 0 && + mnt_parse_offset(val, valsz, &offset) != 0) + return 0; /* TODO: * if (mnt_loopdev_associated_file(devname, src, offset)) diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 644e8e89b..92d0b26a5 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -118,6 +118,8 @@ extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]); extern int endswith(const char *s, const char *sx); extern int startswith(const char *s, const char *sx); +extern int mnt_parse_offset(const char *str, size_t len, uintmax_t *res); + extern int mnt_chdir_to_parent(const char *target, char **filename); extern char *mnt_get_username(const uid_t uid); extern int mnt_get_uid(const char *username, uid_t *uid); diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 7f2397ad1..f3a174ff1 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -53,6 +53,24 @@ int startswith(const char *s, const char *sx) return !strncmp(s, sx, off); } +int mnt_parse_offset(const char *str, size_t len, uintmax_t *res) +{ + char *p; + int rc = 0; + + if (!str && !*str) + return -EINVAL; + + p = strndup(str, len); + if (!p) + return -errno; + + if (strtosize(p, res)) + rc = -EINVAL; + free(p); + return rc; +} + /* returns basename and keeps dirname in the @path, if @path is "/" (root) * then returns empty string */ static char *stripoff_last_component(char *path) -- cgit v1.2.3-55-g7522