]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - tools-util.h
Merge pull request #206 from kode54/fix-fsck-service
[bcachefs-tools-debian] / tools-util.h
index d1122f5d1556d34970d5704c06dedb8eb832b583..563313845df3fe609ecb5b0742ea0364c8ef6e8c 100644 (file)
@@ -18,6 +18,8 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/uuid.h>
+#include "libbcachefs/bcachefs.h"
+#include "libbcachefs/bbpos.h"
 #include "libbcachefs/darray.h"
 
 #define noreturn __attribute__((noreturn))
@@ -26,15 +28,42 @@ void die(const char *, ...)
        __attribute__ ((format (printf, 1, 2))) noreturn;
 char *mprintf(const char *, ...)
        __attribute__ ((format (printf, 1, 2)));
-void *xcalloc(size_t, size_t);
-void *xmalloc(size_t);
-void *xrealloc(void *, size_t);
 void xpread(int, void *, size_t, off_t);
 void xpwrite(int, const void *, size_t, off_t, const char *);
 struct stat xfstatat(int, const char *, int);
 struct stat xfstat(int);
 struct stat xstat(const char *);
 
+static inline void *xmalloc(size_t size)
+{
+       void *p = malloc(size);
+
+       if (!p)
+               die("insufficient memory");
+
+       memset(p, 0, size);
+       return p;
+}
+
+static inline void *xcalloc(size_t count, size_t size)
+{
+       void *p = calloc(count, size);
+
+       if (!p)
+               die("insufficient memory");
+
+       return p;
+}
+
+static inline void *xrealloc(void *p, size_t size)
+{
+       p = realloc(p, size);
+       if (!p)
+               die("insufficient memory");
+
+       return p;
+}
+
 #define xopenat(_dirfd, _path, ...)                                    \
 ({                                                                     \
        int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
@@ -60,9 +89,10 @@ u64 read_file_u64(int, const char *);
 ssize_t read_string_list_or_die(const char *, const char * const[],
                                const char *);
 
-u64 get_size(const char *, int);
-unsigned get_blocksize(const char *, int);
-int open_for_format(const char *, bool);
+u64 get_size(int);
+unsigned get_blocksize(int);
+struct dev_opts;
+int open_for_format(struct dev_opts *, bool);
 
 bool ask_yn(void);
 
@@ -113,8 +143,7 @@ static inline struct range hole_iter_next(struct hole_iter *iter)
 #include <linux/fiemap.h>
 
 struct fiemap_iter {
-       struct fiemap           f;
-       struct fiemap_extent    fe[1024];
+       struct fiemap           *f;
        unsigned                idx;
        int                     fd;
 };
@@ -123,11 +152,20 @@ static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
 {
        memset(iter, 0, sizeof(*iter));
 
-       iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
-       iter->f.fm_length       = FIEMAP_MAX_OFFSET;
+       iter->f = xmalloc(sizeof(struct fiemap) +
+                         sizeof(struct fiemap_extent) * 1024);
+
+       iter->f->fm_extent_count        = 1024;
+       iter->f->fm_length      = FIEMAP_MAX_OFFSET;
        iter->fd                = fd;
 }
 
+static inline void fiemap_iter_exit(struct fiemap_iter *iter)
+{
+       free(iter->f);
+       memset(iter, 0, sizeof(*iter));
+}
+
 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
 
 #define fiemap_for_each(fd, iter, extent)                              \
@@ -159,5 +197,8 @@ do {                                                                        \
 })
 
 struct bpos bpos_parse(char *);
+struct bbpos bbpos_parse(char *);
+
+darray_str get_or_split_cmdline_devs(int argc, char *argv[]);
 
 #endif /* _TOOLS_UTIL_H */