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