]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/blkdev: add get string property
authorSage Weil <sage@redhat.com>
Fri, 24 Mar 2017 00:40:48 +0000 (19:40 -0500)
committerSage Weil <sage@redhat.com>
Wed, 29 Mar 2017 17:23:59 +0000 (13:23 -0400)
Signed-off-by: Sage Weil <sage@redhat.com>
src/common/blkdev.cc
src/common/blkdev.h

index 23818fcf52bf29936493d39c6acd8aa41b1dda43..5842063b3c4efe26b57c58ba84df675044efc041 100644 (file)
@@ -126,12 +126,14 @@ int get_block_device_base(const char *dev, char *out, size_t out_len)
 }
 
 /**
- * get a block device property
+ * get a block device property as a string
  *
- * return the value (we assume it is positive)
+ * store property in *val, up to maxlen chars
+ * return 0 on success
  * return negative error on error
  */
-int64_t get_block_device_int_property(const char *devname, const char *property)
+int64_t get_block_device_string_property(const char *devname, const char *property,
+                                        char *val, size_t maxlen)
 {
   char basename[PATH_MAX], filename[PATH_MAX];
   int64_t r;
@@ -148,26 +150,46 @@ int64_t get_block_device_int_property(const char *devname, const char *property)
     return -errno;
   }
 
-  char buff[256] = {0};
-  if (fgets(buff, sizeof(buff) - 1, fp)) {
-    // strip newline etc
-    for (char *p = buff; *p; ++p) {
-      if (!isdigit(*p)) {
-       *p = 0;
-       break;
-      }
-    }
-    char *endptr = 0;
-    r = strtoll(buff, &endptr, 10);
-    if (endptr != buff + strlen(buff))
-      r = -EINVAL;
+  int r = 0;
+  if (fgets(val, maxlen - 1, fp)) {
+    // truncate at newline
+    char *p = val;
+    while (*p && *p != '\n')
+      ++p;
+    *p = 0;
   } else {
-    r = 0;
+    r = -EINVAL;
   }
   fclose(fp);
   return r;
 }
 
+/**
+ * get a block device property
+ *
+ * return the value (we assume it is positive)
+ * return negative error on error
+ */
+int64_t get_block_device_int_property(const char *devname, const char *property)
+{
+  char buff[256] = {0};
+  int r = get_block_device_string_property(devname, property, buff, sizeof(buff));
+  if (r < 0)
+    return r;
+  // take only digits
+  for (char *p = buff; *p; ++p) {
+    if (!isdigit(*p)) {
+      *p = 0;
+      break;
+    }
+  }
+  char *endptr = 0;
+  r = strtoll(buff, &endptr, 10);
+  if (endptr != buff + strlen(buff))
+    r = -EINVAL;
+  return r;
+}
+
 bool block_device_support_discard(const char *devname)
 {
   return get_block_device_int_property(devname, "queue/discard_granularity") > 0;
index b0459b750df78c67025cef665c1cd7c41cd1afb1..1c8f8da2733d89955cacf2b40e01f7ba7dc9d3e9 100644 (file)
@@ -7,6 +7,8 @@ extern void set_block_device_sandbox_dir(const char *dir);
 extern int get_block_device_base(const char *dev, char *out, size_t out_len);
 extern int get_block_device_size(int fd, int64_t *psize);
 extern int64_t get_block_device_int_property(const char *devname, const char *property);
+extern int64_t get_block_device_string_property(const char *devname, const char *property,
+                                               char *val, size_t maxlen);
 extern bool block_device_support_discard(const char *devname);
 extern bool block_device_is_rotational(const char *devname);
 extern int block_device_discard(int fd, int64_t offset, int64_t len);