11 #include <sys/types.h>
14 #include <linux/bug.h>
15 #include <linux/byteorder.h>
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/uuid.h>
21 #include "ccan/darray/darray.h"
23 void die(const char *, ...);
24 char *mprintf(const char *, ...)
25 __attribute__ ((format (printf, 1, 2)));
26 void *xcalloc(size_t, size_t);
27 void *xmalloc(size_t);
28 void *xrealloc(void *, size_t);
29 void xpread(int, void *, size_t, off_t);
30 void xpwrite(int, const void *, size_t, off_t);
31 struct stat xfstatat(int, const char *, int);
32 struct stat xfstat(int);
33 struct stat xstat(const char *);
35 #define xopenat(_dirfd, _path, ...) \
37 int _fd = openat((_dirfd), (_path), __VA_ARGS__); \
39 die("Error opening %s: %m", (_path)); \
43 #define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__)
45 #define xioctl(_fd, _nr, ...) \
47 int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \
49 die(#_nr " ioctl error: %m"); \
53 int printf_pad(unsigned pad, const char * fmt, ...);
61 struct units_buf __pr_units(s64, enum units);
67 #define pr_units(_v, _u) &(__pr_units(_v, _u).b[0])
69 char *read_file_str(int, const char *);
70 u64 read_file_u64(int, const char *);
72 ssize_t read_string_list_or_die(const char *, const char * const[],
75 u64 get_size(const char *, int);
76 unsigned get_blocksize(const char *, int);
77 int open_for_format(const char *, bool);
86 typedef darray(struct range) ranges;
88 static inline void range_add(ranges *data, u64 offset, u64 size)
90 darray_append(*data, (struct range) {
96 void ranges_sort_merge(ranges *);
97 void ranges_roundup(ranges *, unsigned);
98 void ranges_rounddown(ranges *, unsigned);
106 static inline struct range hole_iter_next(struct hole_iter *iter)
109 .start = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
110 .end = iter->idx < iter->r.size
111 ? iter->r.item[iter->idx].start : iter->end,
114 BUG_ON(r.start > r.end);
120 #define for_each_hole(_iter, _ranges, _end, _i) \
121 for (_iter = (struct hole_iter) { .r = _ranges, .end = _end }; \
122 (_iter.idx <= _iter.r.size && \
123 (_i = hole_iter_next(&_iter), true));)
125 #include <linux/fiemap.h>
129 struct fiemap_extent fe[1024];
134 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
136 memset(iter, 0, sizeof(*iter));
138 iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
139 iter->f.fm_length = FIEMAP_MAX_OFFSET;
143 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
145 #define fiemap_for_each(fd, iter, extent) \
146 for (fiemap_iter_init(&iter, fd); \
147 (extent = fiemap_iter_next(&iter)).fe_length;)
149 char *strcmp_prefix(char *, const char *);
151 unsigned hatoi_validate(const char *, const char *);
153 u32 crc32c(u32, const void *, size_t);
155 char *dev_to_name(dev_t);
156 char *dev_to_path(dev_t);
157 struct mntent *dev_to_mount(char *);
158 bool dev_mounted_rw(char *);
160 #define args_shift(_nr) \
162 unsigned _n = min((_nr), argc); \
169 char *_ret = argc ? argv[0] : NULL; \
175 #endif /* _TOOLS_UTIL_H */