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