bcachefs-tools/tools-util.h

176 lines
3.7 KiB
C
Raw Normal View History

2017-01-08 12:13:18 +03:00
#ifndef _TOOLS_UTIL_H
#define _TOOLS_UTIL_H
2017-02-02 06:16:42 +03:00
#include <errno.h>
#include <mntent.h>
2017-01-08 12:13:18 +03:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
2017-12-28 09:01:16 +03:00
#include <sys/ioctl.h>
2017-03-01 13:45:15 +03:00
#include <sys/stat.h>
2017-01-08 12:13:18 +03:00
#include <sys/types.h>
2017-02-02 06:16:42 +03:00
#include <unistd.h>
2017-01-08 12:13:18 +03:00
2017-03-01 13:45:15 +03:00
#include <linux/bug.h>
2017-01-08 12:13:18 +03:00
#include <linux/byteorder.h>
#include <linux/kernel.h>
#include <linux/log2.h>
#include <linux/string.h>
#include <linux/types.h>
2017-12-23 08:50:55 +03:00
#include <linux/uuid.h>
2017-03-01 13:45:15 +03:00
#include "ccan/darray/darray.h"
2017-01-08 12:13:18 +03:00
2017-05-05 11:27:01 +03:00
void die(const char *, ...);
2018-05-04 21:27:13 +03:00
char *mprintf(const char *, ...)
__attribute__ ((format (printf, 1, 2)));
2017-05-05 11:27:01 +03:00
void *xcalloc(size_t, size_t);
void *xmalloc(size_t);
2017-12-23 08:50:55 +03:00
void *xrealloc(void *, size_t);
2017-05-05 11:27:01 +03:00
void xpread(int, void *, size_t, off_t);
void xpwrite(int, const void *, size_t, off_t);
struct stat xfstatat(int, const char *, int);
struct stat xfstat(int);
2018-02-08 23:30:19 +03:00
struct stat xstat(const char *);
2017-02-02 06:16:42 +03:00
2017-03-01 13:45:15 +03:00
#define xopenat(_dirfd, _path, ...) \
({ \
int _fd = openat((_dirfd), (_path), __VA_ARGS__); \
if (_fd < 0) \
2017-04-15 07:40:50 +03:00
die("Error opening %s: %m", (_path)); \
2017-03-01 13:45:15 +03:00
_fd; \
})
#define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__)
#define xioctl(_fd, _nr, ...) \
2017-12-28 09:01:16 +03:00
({ \
int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \
if (_ret < 0) \
2017-04-15 07:40:50 +03:00
die(#_nr " ioctl error: %m"); \
2017-12-28 09:01:16 +03:00
_ret; \
})
2017-03-01 13:45:15 +03:00
2017-12-30 05:14:51 +03:00
int printf_pad(unsigned pad, const char * fmt, ...);
2017-01-08 12:13:18 +03:00
enum units {
BYTES,
SECTORS,
HUMAN_READABLE,
};
2017-12-23 08:50:55 +03:00
struct units_buf __pr_units(s64, enum units);
2017-01-08 12:13:18 +03:00
struct units_buf {
char b[20];
};
2017-08-18 00:11:06 +03:00
#define pr_units(_v, _u) &(__pr_units(_v, _u).b[0])
2017-01-08 12:13:18 +03:00
char *read_file_str(int, const char *);
u64 read_file_u64(int, const char *);
ssize_t read_string_list_or_die(const char *, const char * const[],
const char *);
u64 get_size(const char *, int);
unsigned get_blocksize(const char *, int);
2017-03-09 21:13:45 +03:00
int open_for_format(const char *, bool);
2017-01-08 12:13:18 +03:00
2017-02-02 06:16:42 +03:00
bool ask_yn(void);
2017-01-08 12:13:18 +03:00
2017-03-01 13:45:15 +03:00
struct range {
u64 start;
u64 end;
};
typedef darray(struct range) ranges;
static inline void range_add(ranges *data, u64 offset, u64 size)
{
darray_append(*data, (struct range) {
.start = offset,
.end = offset + size
});
}
void ranges_sort_merge(ranges *);
void ranges_roundup(ranges *, unsigned);
void ranges_rounddown(ranges *, unsigned);
struct hole_iter {
ranges r;
size_t idx;
u64 end;
};
static inline struct range hole_iter_next(struct hole_iter *iter)
{
struct range r = {
.start = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
.end = iter->idx < iter->r.size
? iter->r.item[iter->idx].start : iter->end,
};
BUG_ON(r.start > r.end);
iter->idx++;
return r;
}
#define for_each_hole(_iter, _ranges, _end, _i) \
for (_iter = (struct hole_iter) { .r = _ranges, .end = _end }; \
(_iter.idx <= _iter.r.size && \
(_i = hole_iter_next(&_iter), true));)
#include <linux/fiemap.h>
struct fiemap_iter {
struct fiemap f;
struct fiemap_extent fe[1024];
unsigned idx;
int fd;
};
static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
{
memset(iter, 0, sizeof(*iter));
iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
iter->f.fm_length = FIEMAP_MAX_OFFSET;
iter->fd = fd;
}
struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
#define fiemap_for_each(fd, iter, extent) \
for (fiemap_iter_init(&iter, fd); \
(extent = fiemap_iter_next(&iter)).fe_length;)
2018-12-19 23:23:59 +03:00
char *strcmp_prefix(char *, const char *);
2017-03-01 13:45:15 +03:00
2017-03-09 21:13:45 +03:00
unsigned hatoi_validate(const char *, const char *);
2017-05-17 08:16:31 +03:00
u32 crc32c(u32, const void *, size_t);
2017-12-28 09:01:16 +03:00
char *dev_to_name(dev_t);
2017-12-23 08:50:55 +03:00
char *dev_to_path(dev_t);
struct mntent *dev_to_mount(char *);
bool dev_mounted_rw(char *);
2017-12-23 08:50:55 +03:00
2017-12-30 05:14:51 +03:00
#define args_shift(_nr) \
do { \
2018-02-08 23:30:19 +03:00
unsigned _n = min((_nr), argc); \
argc -= _n; \
argv += _n; \
2017-12-30 05:14:51 +03:00
} while (0)
#define arg_pop() \
({ \
char *_ret = argc ? argv[0] : NULL; \
if (_ret) \
args_shift(1); \
_ret; \
})
2017-01-08 12:13:18 +03:00
#endif /* _TOOLS_UTIL_H */