"Fossies" - the Fresh Open Source Software Archive

Member "btrfs-progs-v5.4/utils-lib.c" (3 Dec 2019, 1572 Bytes) of package /linux/misc/btrfs-progs-v5.4.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils-lib.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: v5.1.1_vs_v5.2.

    1 #include "kerncompat.h"
    2 #include "common/utils.h"
    3 #include <unistd.h>
    4 #include <stdlib.h>
    5 #include <limits.h>
    6 #include <sys/ioctl.h>
    7 #include <ioctl.h>
    8 
    9 #if BTRFS_FLAT_INCLUDES
   10 #include "ctree.h"
   11 #else
   12 #include <btrfs/ctree.h>
   13 #endif /* BTRFS_FLAT_INCLUDES */
   14 
   15 /*
   16  * This function should be only used when parsing command arg, it won't return
   17  * error to its caller and rather exit directly just like usage().
   18  */
   19 u64 arg_strtou64(const char *str)
   20 {
   21     u64 value;
   22     char *ptr_parse_end = NULL;
   23 
   24     value = strtoull(str, &ptr_parse_end, 0);
   25     if (ptr_parse_end && *ptr_parse_end != '\0') {
   26         fprintf(stderr, "ERROR: %s is not a valid numeric value.\n",
   27             str);
   28         exit(1);
   29     }
   30 
   31     /*
   32      * if we pass a negative number to strtoull, it will return an
   33      * unexpected number to us, so let's do the check ourselves.
   34      */
   35     if (str[0] == '-') {
   36         fprintf(stderr, "ERROR: %s: negative value is invalid.\n",
   37             str);
   38         exit(1);
   39     }
   40     if (value == ULLONG_MAX) {
   41         fprintf(stderr, "ERROR: %s is too large.\n", str);
   42         exit(1);
   43     }
   44     return value;
   45 }
   46 
   47 /*
   48  * For a given:
   49  * - file or directory return the containing tree root id
   50  * - subvolume return its own tree id
   51  * - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is
   52  *   undefined and function returns -1
   53  */
   54 int lookup_path_rootid(int fd, u64 *rootid)
   55 {
   56     struct btrfs_ioctl_ino_lookup_args args;
   57     int ret;
   58 
   59     memset(&args, 0, sizeof(args));
   60     args.treeid = 0;
   61     args.objectid = BTRFS_FIRST_FREE_OBJECTID;
   62 
   63     ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
   64     if (ret < 0)
   65         return -errno;
   66 
   67     *rootid = args.treeid;
   68 
   69     return 0;
   70 }
   71