]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
Fix format args to die()
[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 *, ...) __attribute__((format(printf, 1, 2))) 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 *);
36
37 #define xopenat(_dirfd, _path, ...)                                     \
38 ({                                                                      \
39         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
40         if (_fd < 0)                                                    \
41                 die("Error opening %s: %m", (_path));                   \
42         _fd;                                                            \
43 })
44
45 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
46
47 #define xioctl(_fd, _nr, ...)                                           \
48 ({                                                                      \
49         int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__);                  \
50         if (_ret < 0)                                                   \
51                 die(#_nr " ioctl error: %m");                           \
52         _ret;                                                           \
53 })
54
55 int printf_pad(unsigned pad, const char * fmt, ...);
56
57 enum units {
58         BYTES,
59         SECTORS,
60         HUMAN_READABLE,
61 };
62
63 struct units_buf __pr_units(s64, enum units);
64
65 struct units_buf {
66         char    b[20];
67 };
68
69 #define pr_units(_v, _u)        &(__pr_units(_v, _u).b[0])
70
71 char *read_file_str(int, const char *);
72 u64 read_file_u64(int, const char *);
73
74 ssize_t read_string_list_or_die(const char *, const char * const[],
75                                 const char *);
76
77 u64 get_size(const char *, int);
78 unsigned get_blocksize(const char *, int);
79 int open_for_format(const char *, bool);
80
81 bool ask_yn(void);
82
83 struct range {
84         u64             start;
85         u64             end;
86 };
87
88 typedef darray(struct range) ranges;
89
90 static inline void range_add(ranges *data, u64 offset, u64 size)
91 {
92         darray_append(*data, (struct range) {
93                 .start = offset,
94                 .end = offset + size
95         });
96 }
97
98 void ranges_sort_merge(ranges *);
99 void ranges_roundup(ranges *, unsigned);
100 void ranges_rounddown(ranges *, unsigned);
101
102 struct hole_iter {
103         ranges          r;
104         size_t          idx;
105         u64             end;
106 };
107
108 static inline struct range hole_iter_next(struct hole_iter *iter)
109 {
110         struct range r = {
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,
114         };
115
116         BUG_ON(r.start > r.end);
117
118         iter->idx++;
119         return r;
120 }
121
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));)
126
127 #include <linux/fiemap.h>
128
129 struct fiemap_iter {
130         struct fiemap           f;
131         struct fiemap_extent    fe[1024];
132         unsigned                idx;
133         int                     fd;
134 };
135
136 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
137 {
138         memset(iter, 0, sizeof(*iter));
139
140         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
141         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
142         iter->fd                = fd;
143 }
144
145 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
146
147 #define fiemap_for_each(fd, iter, extent)                               \
148         for (fiemap_iter_init(&iter, fd);                               \
149              (extent = fiemap_iter_next(&iter)).fe_length;)
150
151 char *strcmp_prefix(char *, const char *);
152
153 unsigned hatoi_validate(const char *, const char *);
154
155 u32 crc32c(u32, const void *, size_t);
156
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 *);
161
162 #define args_shift(_nr)                                                 \
163 do {                                                                    \
164         unsigned _n = min((_nr), argc);                                 \
165         argc -= _n;                                                     \
166         argv += _n;                                                     \
167 } while (0)
168
169 #define arg_pop()                                                       \
170 ({                                                                      \
171         char *_ret = argc ? argv[0] : NULL;                             \
172         if (_ret)                                                       \
173                 args_shift(1);                                          \
174         _ret;                                                           \
175 })
176
177 struct bpos bpos_parse(char *);
178
179 #endif /* _TOOLS_UTIL_H */