X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=tools-util.h;h=563313845df3fe609ecb5b0742ea0364c8ef6e8c;hb=249bf7b9d446092e1b744c366b7e8c4563a437f0;hp=732c5108272cf3877ce5e7ebee28b3ed1bb643fd;hpb=e394bd4ba3934cea237ad699cae9fe86396d6f15;p=bcachefs-tools-debian diff --git a/tools-util.h b/tools-util.h index 732c510..5633138 100644 --- a/tools-util.h +++ b/tools-util.h @@ -2,9 +2,11 @@ #define _TOOLS_UTIL_H #include +#include #include #include #include +#include #include #include #include @@ -15,124 +17,82 @@ #include #include #include -#include "ccan/darray/darray.h" +#include +#include "libbcachefs/bcachefs.h" +#include "libbcachefs/bbpos.h" +#include "libbcachefs/darray.h" + +#define noreturn __attribute__((noreturn)) + +void die(const char *, ...) + __attribute__ ((format (printf, 1, 2))) noreturn; +char *mprintf(const char *, ...) + __attribute__ ((format (printf, 1, 2))); +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 *); -#define die(arg, ...) \ -do { \ - fprintf(stderr, arg "\n", ##__VA_ARGS__); \ - exit(EXIT_FAILURE); \ -} while (0) - -#define mprintf(...) \ -({ \ - char *_str; \ - int ret = asprintf(&_str, __VA_ARGS__); \ - if (ret < 0) \ - die("insufficient memory"); \ - _str; \ -}) - -static inline void *xcalloc(size_t count, size_t size) +static inline void *xmalloc(size_t size) { - void *p = calloc(count, size); + void *p = malloc(size); if (!p) die("insufficient memory"); + memset(p, 0, size); return p; } -static inline void *xmalloc(size_t size) +static inline void *xcalloc(size_t count, size_t size) { - void *p = malloc(size); + void *p = calloc(count, size); if (!p) die("insufficient memory"); - memset(p, 0, size); return p; } -static inline void xpread(int fd, void *buf, size_t count, off_t offset) +static inline void *xrealloc(void *p, size_t size) { - ssize_t r = pread(fd, buf, count, offset); - - if (r != count) - die("read error (ret %zi)", r); -} - -static inline void xpwrite(int fd, const void *buf, size_t count, off_t offset) -{ - ssize_t r = pwrite(fd, buf, count, offset); + p = realloc(p, size); + if (!p) + die("insufficient memory"); - if (r != count) - die("write error (ret %zi err %s)", r, strerror(errno)); + return p; } #define xopenat(_dirfd, _path, ...) \ ({ \ int _fd = openat((_dirfd), (_path), __VA_ARGS__); \ if (_fd < 0) \ - die("Error opening %s: %s", (_path), strerror(errno)); \ + die("Error opening %s: %m", (_path)); \ _fd; \ }) #define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__) -static inline struct stat xfstatat(int dirfd, const char *path, int flags) -{ - struct stat stat; - if (fstatat(dirfd, path, &stat, flags)) - die("stat error: %s", strerror(errno)); - return stat; -} - -static inline struct stat xfstat(int fd) -{ - struct stat stat; - if (fstat(fd, &stat)) - die("stat error: %s", strerror(errno)); - return stat; -} - #define xioctl(_fd, _nr, ...) \ -do { \ - if (ioctl((_fd), (_nr), ##__VA_ARGS__)) \ - die(#_nr " ioctl error: %s", strerror(errno)); \ -} while (0) - -enum units { - BYTES, - SECTORS, - HUMAN_READABLE, -}; - -struct units_buf __pr_units(u64, enum units); - -struct units_buf { - char b[20]; -}; - -#define pr_units(_v, _u) __pr_units(_v, _u).b +({ \ + int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \ + if (_ret < 0) \ + die(#_nr " ioctl error: %m"); \ + _ret; \ +}) +void write_file_str(int, const char *, const char *); char *read_file_str(int, const char *); 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); - -int bcachectl_open(void); - -struct bcache_handle { - int ioctl_fd; - int sysfs_fd; -}; - -struct bcache_handle bcache_fs_open(const char *); +u64 get_size(int); +unsigned get_blocksize(int); +struct dev_opts; +int open_for_format(struct dev_opts *, bool); bool ask_yn(void); @@ -141,14 +101,14 @@ struct range { u64 end; }; -typedef darray(struct range) ranges; +typedef DARRAY(struct range) ranges; static inline void range_add(ranges *data, u64 offset, u64 size) { - darray_append(*data, (struct range) { + darray_push(data, ((struct range) { .start = offset, .end = offset + size - }); + })); } void ranges_sort_merge(ranges *); @@ -164,9 +124,9 @@ struct hole_iter { static inline struct range hole_iter_next(struct hole_iter *iter) { struct range r = { - .start = iter->idx ? iter->r.item[iter->idx - 1].end : 0, - .end = iter->idx < iter->r.size - ? iter->r.item[iter->idx].start : iter->end, + .start = iter->idx ? iter->r.data[iter->idx - 1].end : 0, + .end = iter->idx < iter->r.nr + ? iter->r.data[iter->idx].start : iter->end, }; BUG_ON(r.start > r.end); @@ -177,14 +137,13 @@ static inline struct range hole_iter_next(struct hole_iter *iter) #define for_each_hole(_iter, _ranges, _end, _i) \ for (_iter = (struct hole_iter) { .r = _ranges, .end = _end }; \ - (_iter.idx <= _iter.r.size && \ + (_iter.idx <= _iter.r.nr && \ (_i = hole_iter_next(&_iter), true));) #include struct fiemap_iter { - struct fiemap f; - struct fiemap_extent fe[1024]; + struct fiemap *f; unsigned idx; int fd; }; @@ -193,19 +152,53 @@ 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) \ for (fiemap_iter_init(&iter, fd); \ (extent = fiemap_iter_next(&iter)).fe_length;) -const char *strcmp_prefix(const char *, const char *); +char *strcmp_prefix(char *, const char *); + +u32 crc32c(u32, const void *, size_t); + +char *dev_to_name(dev_t); +char *dev_to_path(dev_t); +struct mntent *dev_to_mount(char *); +int dev_mounted(char *); + +#define args_shift(_nr) \ +do { \ + unsigned _n = min((_nr), argc); \ + argc -= _n; \ + argv += _n; \ +} while (0) + +#define arg_pop() \ +({ \ + char *_ret = argc ? argv[0] : NULL; \ + if (_ret) \ + args_shift(1); \ + _ret; \ +}) + +struct bpos bpos_parse(char *); +struct bbpos bbpos_parse(char *); -unsigned hatoi_validate(const char *, const char *); +darray_str get_or_split_cmdline_devs(int argc, char *argv[]); #endif /* _TOOLS_UTIL_H */