]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
Delete some dead code
[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 "libbcachefs/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 char *read_file_str(int, const char *);
57 u64 read_file_u64(int, const char *);
58
59 ssize_t read_string_list_or_die(const char *, const char * const[],
60                                 const char *);
61
62 u64 get_size(const char *, int);
63 unsigned get_blocksize(const char *, int);
64 int open_for_format(const char *, bool);
65
66 bool ask_yn(void);
67
68 struct range {
69         u64             start;
70         u64             end;
71 };
72
73 typedef DARRAY(struct range) ranges;
74
75 static inline void range_add(ranges *data, u64 offset, u64 size)
76 {
77         darray_push(data, ((struct range) {
78                 .start = offset,
79                 .end = offset + size
80         }));
81 }
82
83 void ranges_sort_merge(ranges *);
84 void ranges_roundup(ranges *, unsigned);
85 void ranges_rounddown(ranges *, unsigned);
86
87 struct hole_iter {
88         ranges          r;
89         size_t          idx;
90         u64             end;
91 };
92
93 static inline struct range hole_iter_next(struct hole_iter *iter)
94 {
95         struct range r = {
96                 .start  = iter->idx ? iter->r.data[iter->idx - 1].end : 0,
97                 .end    = iter->idx < iter->r.size
98                         ? iter->r.data[iter->idx].start : iter->end,
99         };
100
101         BUG_ON(r.start > r.end);
102
103         iter->idx++;
104         return r;
105 }
106
107 #define for_each_hole(_iter, _ranges, _end, _i)                         \
108         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
109              (_iter.idx <= _iter.r.size &&                              \
110               (_i = hole_iter_next(&_iter), true));)
111
112 #include <linux/fiemap.h>
113
114 struct fiemap_iter {
115         struct fiemap           f;
116         struct fiemap_extent    fe[1024];
117         unsigned                idx;
118         int                     fd;
119 };
120
121 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
122 {
123         memset(iter, 0, sizeof(*iter));
124
125         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
126         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
127         iter->fd                = fd;
128 }
129
130 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
131
132 #define fiemap_for_each(fd, iter, extent)                               \
133         for (fiemap_iter_init(&iter, fd);                               \
134              (extent = fiemap_iter_next(&iter)).fe_length;)
135
136 char *strcmp_prefix(char *, const char *);
137
138 u32 crc32c(u32, const void *, size_t);
139
140 char *dev_to_name(dev_t);
141 char *dev_to_path(dev_t);
142 struct mntent *dev_to_mount(char *);
143 int dev_mounted(char *);
144
145 #define args_shift(_nr)                                                 \
146 do {                                                                    \
147         unsigned _n = min((_nr), argc);                                 \
148         argc -= _n;                                                     \
149         argv += _n;                                                     \
150 } while (0)
151
152 #define arg_pop()                                                       \
153 ({                                                                      \
154         char *_ret = argc ? argv[0] : NULL;                             \
155         if (_ret)                                                       \
156                 args_shift(1);                                          \
157         _ret;                                                           \
158 })
159
160 struct bpos bpos_parse(char *);
161
162 #endif /* _TOOLS_UTIL_H */