Whamcloud - gitweb
LU-13090 utils: fix lfs_migrate -p for file with pool
[fs/lustre-release.git] / libcfs / libcfs / libcfs_string.c
index 2a3009f..ee39d23 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2014, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -40,7 +36,9 @@
  * Author: Nathan Rutman <nathan.rutman@sun.com>
  */
 
+#include <linux/ctype.h>
 #include <libcfs/libcfs.h>
+#include <libcfs/libcfs_string.h>
 
 char *cfs_strrstr(const char *haystack, const char *needle)
 {
@@ -167,26 +165,6 @@ out:
 }
 EXPORT_SYMBOL(cfs_firststr);
 
-char *
-cfs_trimwhite(char *str)
-{
-       char *end;
-
-       while (isspace(*str))
-               str++;
-
-       end = str + strlen(str);
-       while (end > str) {
-               if (!isspace(end[-1]))
-                       break;
-               end--;
-       }
-
-       *end = 0;
-       return str;
-}
-EXPORT_SYMBOL(cfs_trimwhite);
-
 /**
  * Extracts tokens from strings.
  *
@@ -256,17 +234,47 @@ int
 cfs_str2num_check(char *str, int nob, unsigned *num,
                  unsigned min, unsigned max)
 {
-       char    *endp;
-
-       *num = simple_strtoul(str, &endp, 0);
-       if (endp == str)
-               return 0;
+       bool all_numbers = true;
+       char *endp, cache;
+       int len;
+       int rc;
+
+       endp = strim(str);
+       /**
+        * kstrouint can only handle strings composed
+        * of only numbers. We need to scan the string
+        * passed in for the first non-digit character
+        * and end the string at that location. If we
+        * don't find any non-digit character we still
+        * need to place a '\0' at position len since
+        * we are not interested in the rest of the
+        * string which is longer than len in size.
+        * After we are done the character at the
+        * position we placed '\0' must be restored.
+        */
+       len = min((int)strlen(endp), nob);
+       for (; endp < str + len; endp++) {
+               if (!isxdigit(*endp) && *endp != '-' &&
+                   *endp != '+') {
+                       all_numbers = false;
+                       break;
+               }
+       }
 
-       for (; endp < str + nob; endp++) {
-               if (!isspace(*endp))
-                       return 0;
+       /* Eat trailing space */
+       if (!all_numbers && isspace(*endp)) {
+               all_numbers = true;
+               endp--;
        }
 
+       cache = *endp;
+       *endp = '\0';
+
+       rc = kstrtouint(str, 0, num);
+       *endp = cache;
+       if (rc || !all_numbers)
+               return 0;
+
        return (*num >= min && *num <= max);
 }
 EXPORT_SYMBOL(cfs_str2num_check);
@@ -284,7 +292,7 @@ EXPORT_SYMBOL(cfs_str2num_check);
  * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
  * -ENOMEM will be returned.
  */
-int
+static int
 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
                     int bracketed, struct cfs_range_expr **expr)
 {
@@ -347,7 +355,6 @@ cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
        LIBCFS_FREE(re, sizeof(*re));
        return -EINVAL;
 }
-EXPORT_SYMBOL(cfs_range_expr_parse);
 
 /**
  * Print the range expression \a re into specified \a buffer.
@@ -485,6 +492,16 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
 }
 EXPORT_SYMBOL(cfs_expr_list_values);
 
+void
+cfs_expr_list_values_free(__u32 *values, int num)
+{
+       /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
+        * by OBD_FREE() if it's called by module other than libcfs & LNet,
+        * otherwise we will see fake memory leak */
+       LIBCFS_FREE(values, num * sizeof(values[0]));
+}
+EXPORT_SYMBOL(cfs_expr_list_values_free);
+
 /**
  * Frees cfs_range_expr structures of \a expr_list.
  *
@@ -589,65 +606,3 @@ cfs_expr_list_free_list(struct list_head *list)
        }
 }
 EXPORT_SYMBOL(cfs_expr_list_free_list);
-
-int
-cfs_ip_addr_parse(char *str, int len, struct list_head *list)
-{
-       struct cfs_expr_list    *el;
-       struct cfs_lstr         src;
-       int                     rc;
-       int                     i;
-
-       src.ls_str = str;
-       src.ls_len = len;
-       i = 0;
-
-       while (src.ls_str != NULL) {
-               struct cfs_lstr res;
-
-               if (!cfs_gettok(&src, '.', &res)) {
-                       rc = -EINVAL;
-                       goto out;
-               }
-
-               rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
-               if (rc != 0)
-                       goto out;
-
-               list_add_tail(&el->el_link, list);
-               i++;
-       }
-
-       if (i == 4)
-               return 0;
-
-       rc = -EINVAL;
- out:
-       cfs_expr_list_free_list(list);
-
-       return rc;
-}
-EXPORT_SYMBOL(cfs_ip_addr_parse);
-
-/**
- * Matches address (\a addr) against address set encoded in \a list.
- *
- * \retval 1 if \a addr matches
- * \retval 0 otherwise
- */
-int
-cfs_ip_addr_match(__u32 addr, struct list_head *list)
-{
-       struct cfs_expr_list *el;
-       int i = 0;
-
-       list_for_each_entry_reverse(el, list, el_link) {
-               if (!cfs_expr_list_match(addr & 0xff, el))
-                       return 0;
-               addr >>= 8;
-               i++;
-       }
-
-       return i == 4;
-}
-EXPORT_SYMBOL(cfs_ip_addr_match);