]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add function to parse offsets/sizes
authorKarel Zak <kzak@redhat.com>
Thu, 29 Sep 2011 15:48:03 +0000 (17:48 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 29 Sep 2011 15:48:03 +0000 (17:48 +0200)
libmount/src/context_umount.c
libmount/src/mountP.h
libmount/src/utils.c

index bc08affc063456cf99cba89f3d30f3eb281b4638..e271845e2fd5154ebd9aa11e64ee9d176ca88154 100644 (file)
@@ -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))
index 644e8e89bbd81d0c677a0040f173f8d4ade24867..92d0b26a50a0c3ccf8b6f0c011a1a84461655ef3 100644 (file)
@@ -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);
index 7f2397ad121469b49b07205a1ed0dd263f782560..f3a174ff1a7bd42fa9fec1049463a47a8cf25961 100644 (file)
@@ -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)