10 #include <sys/types.h>
13 #include <linux/bug.h>
14 #include <linux/byteorder.h>
15 #include <linux/kernel.h>
16 #include <linux/log2.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/uuid.h>
20 #include "ccan/darray/darray.h"
22 void die(const char *, ...);
23 char *mprintf(const char *, ...)
24 __attribute__ ((format (printf, 1, 2)));
25 void *xcalloc(size_t, size_t);
26 void *xmalloc(size_t);
27 void *xrealloc(void *, size_t);
28 void xpread(int, void *, size_t, off_t);
29 void xpwrite(int, const void *, size_t, off_t);
30 struct stat xfstatat(int, const char *, int);
31 struct stat xfstat(int);
32 struct stat xstat(const char *);
34 #define xopenat(_dirfd, _path, ...) \
36 int _fd = openat((_dirfd), (_path), __VA_ARGS__); \
38 die("Error opening %s: %m", (_path)); \
42 #define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__)
44 #define xioctl(_fd, _nr, ...) \
46 int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \
48 die(#_nr " ioctl error: %m"); \
52 int printf_pad(unsigned pad, const char * fmt, ...);
60 struct units_buf __pr_units(s64, enum units);
66 #define pr_units(_v, _u) &(__pr_units(_v, _u).b[0])
68 char *read_file_str(int, const char *);
69 u64 read_file_u64(int, const char *);
71 ssize_t read_string_list_or_die(const char *, const char * const[],
74 u64 get_size(const char *, int);
75 unsigned get_blocksize(const char *, int);
76 int open_for_format(const char *, bool);
85 typedef darray(struct range) ranges;
87 static inline void range_add(ranges *data, u64 offset, u64 size)
89 darray_append(*data, (struct range) {
95 void ranges_sort_merge(ranges *);
96 void ranges_roundup(ranges *, unsigned);
97 void ranges_rounddown(ranges *, unsigned);
105 static inline struct range hole_iter_next(struct hole_iter *iter)
108 .start = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
109 .end = iter->idx < iter->r.size
110 ? iter->r.item[iter->idx].start : iter->end,
113 BUG_ON(r.start > r.end);
119 #define for_each_hole(_iter, _ranges, _end, _i) \
120 for (_iter = (struct hole_iter) { .r = _ranges, .end = _end }; \
121 (_iter.idx <= _iter.r.size && \
122 (_i = hole_iter_next(&_iter), true));)
124 #include <linux/fiemap.h>
128 struct fiemap_extent fe[1024];
133 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
135 memset(iter, 0, sizeof(*iter));
137 iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
138 iter->f.fm_length = FIEMAP_MAX_OFFSET;
142 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
144 #define fiemap_for_each(fd, iter, extent) \
145 for (fiemap_iter_init(&iter, fd); \
146 (extent = fiemap_iter_next(&iter)).fe_length;)
148 char *strcmp_prefix(char *, const char *);
150 unsigned hatoi_validate(const char *, const char *);
152 u32 crc32c(u32, const void *, size_t);
154 char *dev_to_name(dev_t);
155 char *dev_to_path(dev_t);
156 char *dev_to_mount(char *);
158 #define args_shift(_nr) \
160 unsigned _n = min((_nr), argc); \
167 char *_ret = argc ? argv[0] : NULL; \
173 #endif /* _TOOLS_UTIL_H */