]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
show-super can now print more stuff
[bcachefs-tools-debian] / tools-util.h
1 #ifndef _TOOLS_UTIL_H
2 #define _TOOLS_UTIL_H
3
4 #include <errno.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/ioctl.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
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"
21
22 void die(const char *, ...);
23 char *mprintf(const char *, ...);
24 void *xcalloc(size_t, size_t);
25 void *xmalloc(size_t);
26 void *xrealloc(void *, size_t);
27 void xpread(int, void *, size_t, off_t);
28 void xpwrite(int, const void *, size_t, off_t);
29 struct stat xfstatat(int, const char *, int);
30 struct stat xfstat(int);
31
32 #define xopenat(_dirfd, _path, ...)                                     \
33 ({                                                                      \
34         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
35         if (_fd < 0)                                                    \
36                 die("Error opening %s: %m", (_path));                   \
37         _fd;                                                            \
38 })
39
40 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
41
42 #define xioctl(_fd, _nr, ...)                                           \
43 ({                                                                      \
44         int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__);                  \
45         if (_ret < 0)                                                   \
46                 die(#_nr " ioctl error: %m");                           \
47         _ret;                                                           \
48 })
49
50 int printf_pad(unsigned pad, const char * fmt, ...);
51
52 enum units {
53         BYTES,
54         SECTORS,
55         HUMAN_READABLE,
56 };
57
58 struct units_buf __pr_units(s64, enum units);
59
60 struct units_buf {
61         char    b[20];
62 };
63
64 #define pr_units(_v, _u)        &(__pr_units(_v, _u).b[0])
65
66 char *read_file_str(int, const char *);
67 u64 read_file_u64(int, const char *);
68
69 ssize_t read_string_list_or_die(const char *, const char * const[],
70                                 const char *);
71
72 u64 get_size(const char *, int);
73 unsigned get_blocksize(const char *, int);
74 int open_for_format(const char *, bool);
75
76 bool ask_yn(void);
77
78 struct range {
79         u64             start;
80         u64             end;
81 };
82
83 typedef darray(struct range) ranges;
84
85 static inline void range_add(ranges *data, u64 offset, u64 size)
86 {
87         darray_append(*data, (struct range) {
88                 .start = offset,
89                 .end = offset + size
90         });
91 }
92
93 void ranges_sort_merge(ranges *);
94 void ranges_roundup(ranges *, unsigned);
95 void ranges_rounddown(ranges *, unsigned);
96
97 struct hole_iter {
98         ranges          r;
99         size_t          idx;
100         u64             end;
101 };
102
103 static inline struct range hole_iter_next(struct hole_iter *iter)
104 {
105         struct range r = {
106                 .start  = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
107                 .end    = iter->idx < iter->r.size
108                         ? iter->r.item[iter->idx].start : iter->end,
109         };
110
111         BUG_ON(r.start > r.end);
112
113         iter->idx++;
114         return r;
115 }
116
117 #define for_each_hole(_iter, _ranges, _end, _i)                         \
118         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
119              (_iter.idx <= _iter.r.size &&                              \
120               (_i = hole_iter_next(&_iter), true));)
121
122 #include <linux/fiemap.h>
123
124 struct fiemap_iter {
125         struct fiemap           f;
126         struct fiemap_extent    fe[1024];
127         unsigned                idx;
128         int                     fd;
129 };
130
131 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
132 {
133         memset(iter, 0, sizeof(*iter));
134
135         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
136         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
137         iter->fd                = fd;
138 }
139
140 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
141
142 #define fiemap_for_each(fd, iter, extent)                               \
143         for (fiemap_iter_init(&iter, fd);                               \
144              (extent = fiemap_iter_next(&iter)).fe_length;)
145
146 const char *strcmp_prefix(const char *, const char *);
147
148 unsigned hatoi_validate(const char *, const char *);
149
150 u32 crc32c(u32, const void *, size_t);
151
152 char *dev_to_name(dev_t);
153 char *dev_to_path(dev_t);
154 char *dev_to_mount(char *);
155
156 #define args_shift(_nr)                                                 \
157 do {                                                                    \
158         argc -= (_nr);                                                  \
159         argv += (_nr);                                                  \
160 } while (0)
161
162 #define arg_pop()                                                       \
163 ({                                                                      \
164         char *_ret = argc ? argv[0] : NULL;                             \
165         if (_ret)                                                       \
166                 args_shift(1);                                          \
167         _ret;                                                           \
168 })
169
170 #endif /* _TOOLS_UTIL_H */