X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=tools-util.h;h=e5edf98cf309bb59aedd31709e1ea707e30cf5dc;hb=4aefd5f903fe99bb34cbb2922d488e9a29e78ea9;hp=0b73b2832a26549fc8d2636b98351c2a6e54dae2;hpb=e004b95b88ae95cf7bb26bd7dc80c5dcf2b2664a;p=bcachefs-tools-debian diff --git a/tools-util.h b/tools-util.h index 0b73b28..e5edf98 100644 --- a/tools-util.h +++ b/tools-util.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -15,16 +16,19 @@ #include #include #include +#include #include "ccan/darray/darray.h" void die(const char *, ...); char *mprintf(const char *, ...); 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); struct stat xfstatat(int, const char *, int); struct stat xfstat(int); +struct stat xstat(const char *); #define xopenat(_dirfd, _path, ...) \ ({ \ @@ -37,10 +41,14 @@ struct stat xfstat(int); #define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__) #define xioctl(_fd, _nr, ...) \ -do { \ - if (ioctl((_fd), (_nr), ##__VA_ARGS__)) \ +({ \ + int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \ + if (_ret < 0) \ die(#_nr " ioctl error: %m"); \ -} while (0) + _ret; \ +}) + +int printf_pad(unsigned pad, const char * fmt, ...); enum units { BYTES, @@ -48,13 +56,13 @@ enum units { HUMAN_READABLE, }; -struct units_buf __pr_units(u64, enum units); +struct units_buf __pr_units(s64, enum units); struct units_buf { char b[20]; }; -#define pr_units(_v, _u) __pr_units(_v, _u).b +#define pr_units(_v, _u) &(__pr_units(_v, _u).b[0]) char *read_file_str(int, const char *); u64 read_file_u64(int, const char *); @@ -66,15 +74,6 @@ 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 *); - bool ask_yn(void); struct range { @@ -149,4 +148,25 @@ const char *strcmp_prefix(const char *, const char *); unsigned hatoi_validate(const char *, const char *); +u32 crc32c(u32, const void *, size_t); + +char *dev_to_name(dev_t); +char *dev_to_path(dev_t); +char *dev_to_mount(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; \ +}) + #endif /* _TOOLS_UTIL_H */