]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
cmd_list improvements; use %m
[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/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include <linux/bug.h>
13 #include <linux/byteorder.h>
14 #include <linux/kernel.h>
15 #include <linux/log2.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include "ccan/darray/darray.h"
19
20 #define die(arg, ...)                                                   \
21 do {                                                                    \
22         fprintf(stderr, arg "\n", ##__VA_ARGS__);                       \
23         exit(EXIT_FAILURE);                                             \
24 } while (0)
25
26 #define mprintf(...)                                                    \
27 ({                                                                      \
28         char *_str;                                                     \
29         int ret = asprintf(&_str, __VA_ARGS__);                         \
30         if (ret < 0)                                                    \
31                 die("insufficient memory");                             \
32         _str;                                                           \
33 })
34
35 static inline void *xcalloc(size_t count, size_t size)
36 {
37         void *p = calloc(count, size);
38
39         if (!p)
40                 die("insufficient memory");
41
42         return p;
43 }
44
45 static inline void *xmalloc(size_t size)
46 {
47         void *p = malloc(size);
48
49         if (!p)
50                 die("insufficient memory");
51
52         memset(p, 0, size);
53         return p;
54 }
55
56 static inline void xpread(int fd, void *buf, size_t count, off_t offset)
57 {
58         ssize_t r = pread(fd, buf, count, offset);
59
60         if (r != count)
61                 die("read error (ret %zi)", r);
62 }
63
64 static inline void xpwrite(int fd, const void *buf, size_t count, off_t offset)
65 {
66         ssize_t r = pwrite(fd, buf, count, offset);
67
68         if (r != count)
69                 die("write error (ret %zi err %m)", r);
70 }
71
72 #define xopenat(_dirfd, _path, ...)                                     \
73 ({                                                                      \
74         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
75         if (_fd < 0)                                                    \
76                 die("Error opening %s: %m", (_path));                   \
77         _fd;                                                            \
78 })
79
80 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
81
82 static inline struct stat xfstatat(int dirfd, const char *path, int flags)
83 {
84         struct stat stat;
85         if (fstatat(dirfd, path, &stat, flags))
86                 die("stat error: %m");
87         return stat;
88 }
89
90 static inline struct stat xfstat(int fd)
91 {
92         struct stat stat;
93         if (fstat(fd, &stat))
94                 die("stat error: %m");
95         return stat;
96 }
97
98 #define xioctl(_fd, _nr, ...)                                           \
99 do {                                                                    \
100         if (ioctl((_fd), (_nr), ##__VA_ARGS__))                         \
101                 die(#_nr " ioctl error: %m");                           \
102 } while (0)
103
104 enum units {
105         BYTES,
106         SECTORS,
107         HUMAN_READABLE,
108 };
109
110 struct units_buf __pr_units(u64, enum units);
111
112 struct units_buf {
113         char    b[20];
114 };
115
116 #define pr_units(_v, _u)        __pr_units(_v, _u).b
117
118 char *read_file_str(int, const char *);
119 u64 read_file_u64(int, const char *);
120
121 ssize_t read_string_list_or_die(const char *, const char * const[],
122                                 const char *);
123
124 u64 get_size(const char *, int);
125 unsigned get_blocksize(const char *, int);
126 int open_for_format(const char *, bool);
127
128 int bcachectl_open(void);
129
130 struct bcache_handle {
131         int     ioctl_fd;
132         int     sysfs_fd;
133 };
134
135 struct bcache_handle bcache_fs_open(const char *);
136
137 bool ask_yn(void);
138
139 struct range {
140         u64             start;
141         u64             end;
142 };
143
144 typedef darray(struct range) ranges;
145
146 static inline void range_add(ranges *data, u64 offset, u64 size)
147 {
148         darray_append(*data, (struct range) {
149                 .start = offset,
150                 .end = offset + size
151         });
152 }
153
154 void ranges_sort_merge(ranges *);
155 void ranges_roundup(ranges *, unsigned);
156 void ranges_rounddown(ranges *, unsigned);
157
158 struct hole_iter {
159         ranges          r;
160         size_t          idx;
161         u64             end;
162 };
163
164 static inline struct range hole_iter_next(struct hole_iter *iter)
165 {
166         struct range r = {
167                 .start  = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
168                 .end    = iter->idx < iter->r.size
169                         ? iter->r.item[iter->idx].start : iter->end,
170         };
171
172         BUG_ON(r.start > r.end);
173
174         iter->idx++;
175         return r;
176 }
177
178 #define for_each_hole(_iter, _ranges, _end, _i)                         \
179         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
180              (_iter.idx <= _iter.r.size &&                              \
181               (_i = hole_iter_next(&_iter), true));)
182
183 #include <linux/fiemap.h>
184
185 struct fiemap_iter {
186         struct fiemap           f;
187         struct fiemap_extent    fe[1024];
188         unsigned                idx;
189         int                     fd;
190 };
191
192 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
193 {
194         memset(iter, 0, sizeof(*iter));
195
196         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
197         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
198         iter->fd                = fd;
199 }
200
201 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
202
203 #define fiemap_for_each(fd, iter, extent)                               \
204         for (fiemap_iter_init(&iter, fd);                               \
205              (extent = fiemap_iter_next(&iter)).fe_length;)
206
207 const char *strcmp_prefix(const char *, const char *);
208
209 unsigned hatoi_validate(const char *, const char *);
210
211 #endif /* _TOOLS_UTIL_H */