]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - tools-util.c
New upstream snapshot
[bcachefs-tools-debian] / tools-util.c
index 3cc0de444cf95b32d2def2e16c476c6bd726bcb6..9491779baffb59e5c83f8d3e9fde10285b1bc8be 100644 (file)
@@ -94,12 +94,12 @@ void xpread(int fd, void *buf, size_t count, off_t offset)
        }
 }
 
-void xpwrite(int fd, const void *buf, size_t count, off_t offset)
+void xpwrite(int fd, const void *buf, size_t count, off_t offset, const char *msg)
 {
        ssize_t r = pwrite(fd, buf, count, offset);
 
        if (r != count)
-               die("write error (ret %zi err %m)", r);
+               die("error writing %s (ret %zi err %m)", msg, r);
 }
 
 struct stat xfstatat(int dirfd, const char *path, int flags)
@@ -211,7 +211,7 @@ u64 read_file_u64(int dirfd, const char *path)
 {
        char *buf = read_file_str(dirfd, path);
        u64 v;
-       if (kstrtou64(buf, 10, &v))
+       if (bch2_strtou64_h(buf, &v))
                die("read_file_u64: error parsing %s (got %s)", path, buf);
        free(buf);
        return v;
@@ -242,17 +242,17 @@ u64 get_size(const char *path, int fd)
        return ret;
 }
 
-/* Returns blocksize in units of 512 byte sectors: */
+/* Returns blocksize, in bytes: */
 unsigned get_blocksize(const char *path, int fd)
 {
        struct stat statbuf = xfstat(fd);
 
        if (!S_ISBLK(statbuf.st_mode))
-               return statbuf.st_blksize >> 9;
+               return statbuf.st_blksize;
 
        unsigned ret;
        xioctl(fd, BLKPBSZGET, &ret);
-       return ret >> 9;
+       return ret;
 }
 
 /* Open a block device, do magic blkid stuff to probe for existing filesystems: */
@@ -262,7 +262,9 @@ int open_for_format(const char *dev, bool force)
        const char *fs_type = NULL, *fs_label = NULL;
        size_t fs_type_len, fs_label_len;
 
-       int fd = xopen(dev, O_RDWR|O_EXCL);
+       int fd = open(dev, O_RDWR|O_EXCL);
+       if (fd < 0)
+               die("Error opening device to format %s: %m", dev);
 
        if (force)
                return fd;
@@ -401,24 +403,6 @@ char *strcmp_prefix(char *a, const char *a_prefix)
        return *a_prefix ? NULL : a;
 }
 
-unsigned hatoi_validate(const char *s, const char *msg)
-{
-       u64 v;
-
-       if (bch2_strtoull_h(s, &v))
-               die("bad %s %s", msg, s);
-
-       v /= 512;
-
-       if (v > USHRT_MAX)
-               die("%s too large\n", msg);
-
-       if (!v)
-               die("%s too small\n", msg);
-
-       return v;
-}
-
 /* crc32c */
 
 static u32 crc32c_default(u32 crc, const void *buf, size_t size)