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 #define noreturn __attribute__((noreturn))
25 void die(const char *, ...) noreturn;
26 char *mprintf(const char *, ...)
27 __attribute__ ((format (printf, 1, 2)));
28 void *xcalloc(size_t, size_t);
29 void *xmalloc(size_t);
30 void *xrealloc(void *, size_t);
31 void xpread(int, void *, size_t, off_t);
32 void xpwrite(int, const void *, size_t, off_t);
33 struct stat xfstatat(int, const char *, int);
34 struct stat xfstat(int);
35 struct stat xstat(const char *);
37 #define xopenat(_dirfd, _path, ...) \
39 int _fd = openat((_dirfd), (_path), __VA_ARGS__); \
41 die("Error opening %s: %m", (_path)); \
45 #define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__)
47 #define xioctl(_fd, _nr, ...) \
49 int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \
51 die(#_nr " ioctl error: %m"); \
55 int printf_pad(unsigned pad, const char * fmt, ...);
63 struct units_buf __pr_units(s64, enum units);
69 #define pr_units(_v, _u) &(__pr_units(_v, _u).b[0])
71 char *read_file_str(int, const char *);
72 u64 read_file_u64(int, const char *);
74 ssize_t read_string_list_or_die(const char *, const char * const[],
77 u64 get_size(const char *, int);
78 unsigned get_blocksize(const char *, int);
79 int open_for_format(const char *, bool);
88 typedef darray(struct range) ranges;
90 static inline void range_add(ranges *data, u64 offset, u64 size)
92 darray_append(*data, (struct range) {
98 void ranges_sort_merge(ranges *);
99 void ranges_roundup(ranges *, unsigned);
100 void ranges_rounddown(ranges *, unsigned);
108 static inline struct range hole_iter_next(struct hole_iter *iter)
111 .start = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
112 .end = iter->idx < iter->r.size
113 ? iter->r.item[iter->idx].start : iter->end,
116 BUG_ON(r.start > r.end);
122 #define for_each_hole(_iter, _ranges, _end, _i) \
123 for (_iter = (struct hole_iter) { .r = _ranges, .end = _end }; \
124 (_iter.idx <= _iter.r.size && \
125 (_i = hole_iter_next(&_iter), true));)
127 #include <linux/fiemap.h>
131 struct fiemap_extent fe[1024];
136 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
138 memset(iter, 0, sizeof(*iter));
140 iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
141 iter->f.fm_length = FIEMAP_MAX_OFFSET;
145 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
147 #define fiemap_for_each(fd, iter, extent) \
148 for (fiemap_iter_init(&iter, fd); \
149 (extent = fiemap_iter_next(&iter)).fe_length;)
151 char *strcmp_prefix(char *, const char *);
153 unsigned hatoi_validate(const char *, const char *);
155 u32 crc32c(u32, const void *, size_t);
157 char *dev_to_name(dev_t);
158 char *dev_to_path(dev_t);
159 struct mntent *dev_to_mount(char *);
160 int dev_mounted(char *);
162 #define args_shift(_nr) \
164 unsigned _n = min((_nr), argc); \
171 char *_ret = argc ? argv[0] : NULL; \
177 struct bpos bpos_parse(char *);
179 #endif /* _TOOLS_UTIL_H */