From 9fc030ed9bffec9f9715595dd5a205a353912d3c Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Wed, 2 Dec 2020 13:08:58 +0100 Subject: Update xlosetup's 'lib' and 'libsmartcol' from util-linux 2.36.1 --- src/utils/include/all-io.h | 38 +++++++++++++++++++++++++++++-- src/utils/include/buffer.h | 28 +++++++++++++++++++++++ src/utils/include/fileutils.h | 4 ++++ src/utils/include/jsonwrt.h | 44 ++++++++++++++++++++++++++++++++++++ src/utils/include/loopdev.h | 1 + src/utils/include/procutils.h | 3 +++ src/utils/include/pt-gpt-partnames.h | 14 ++++++++++-- src/utils/include/randutils.h | 2 +- src/utils/include/strutils.h | 38 +++++++++++++++++++++++++++++-- src/utils/include/swapheader.h | 23 ------------------- src/utils/include/swapprober.h | 9 -------- 11 files changed, 165 insertions(+), 39 deletions(-) create mode 100644 src/utils/include/buffer.h create mode 100644 src/utils/include/jsonwrt.h delete mode 100644 src/utils/include/swapheader.h delete mode 100644 src/utils/include/swapprober.h (limited to 'src/utils/include') diff --git a/src/utils/include/all-io.h b/src/utils/include/all-io.h index 8ffa9cf..5ed2d11 100644 --- a/src/utils/include/all-io.h +++ b/src/utils/include/all-io.h @@ -12,6 +12,10 @@ #include #include #include +#include +#ifdef HAVE_SYS_SENDFILE_H +# include +#endif #include "c.h" @@ -63,13 +67,15 @@ static inline ssize_t read_all(int fd, char *buf, size_t count) memset(buf, 0, count); while (count > 0) { ret = read(fd, buf, count); - if (ret <= 0) { - if (ret < 0 && (errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { + if (ret < 0) { + if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { xusleep(250000); continue; } return c ? c : -1; } + if (ret == 0) + return c; tries = 0; count -= ret; buf += ret; @@ -78,4 +84,32 @@ static inline ssize_t read_all(int fd, char *buf, size_t count) return c; } +static inline ssize_t sendfile_all(int out, int in, off_t *off, size_t count) +{ +#if defined(HAVE_SENDFILE) && defined(__linux__) + ssize_t ret; + ssize_t c = 0; + int tries = 0; + while (count) { + ret = sendfile(out, in, off, count); + if (ret < 0) { + if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { + xusleep(250000); + continue; + } + return c ? c : -1; + } + if (ret == 0) + return c; + tries = 0; + count -= ret; + c += ret; + } + return c; +#else + errno = ENOSYS; + return -1; +#endif +} + #endif /* UTIL_LINUX_ALL_IO_H */ diff --git a/src/utils/include/buffer.h b/src/utils/include/buffer.h new file mode 100644 index 0000000..5bc7037 --- /dev/null +++ b/src/utils/include/buffer.h @@ -0,0 +1,28 @@ +#ifndef UTIL_LINUX_BUFFER +#define UTIL_LINUX_BUFFER + +#include "c.h" + +struct ul_buffer { + char *begin; /* begin of the data */ + char *end; /* current end of data */ + + size_t sz; /* allocated space for data */ + size_t chunksize; +}; + +#define UL_INIT_BUFFER { .begin = NULL } + +void ul_buffer_reset_data(struct ul_buffer *buf); +void ul_buffer_free_data(struct ul_buffer *buf); +int ul_buffer_is_empty(struct ul_buffer *buf); +void ul_buffer_set_chunksize(struct ul_buffer *buf, size_t sz); +void ul_buffer_refer_string(struct ul_buffer *buf, char *str); +int ul_buffer_alloc_data(struct ul_buffer *buf, size_t sz); +int ul_buffer_append_data(struct ul_buffer *buf, const char *data, size_t sz); +int ul_buffer_append_string(struct ul_buffer *buf, const char *str); +int ul_buffer_append_ntimes(struct ul_buffer *buf, size_t n, const char *str); +int ul_buffer_set_data(struct ul_buffer *buf, const char *data, size_t sz); +char *ul_buffer_get_data(struct ul_buffer *buf); + +#endif /* UTIL_LINUX_BUFFER */ diff --git a/src/utils/include/fileutils.h b/src/utils/include/fileutils.h index 479ad15..618bf39 100644 --- a/src/utils/include/fileutils.h +++ b/src/utils/include/fileutils.h @@ -74,4 +74,8 @@ static inline struct dirent *xreaddir(DIR *dp) extern void close_all_fds(const int exclude[], size_t exsz); +#define UL_COPY_READ_ERROR (-1) +#define UL_COPY_WRITE_ERROR (-2) +int ul_copy_file(int from, int to); + #endif /* UTIL_LINUX_FILEUTILS */ diff --git a/src/utils/include/jsonwrt.h b/src/utils/include/jsonwrt.h new file mode 100644 index 0000000..04ef49e --- /dev/null +++ b/src/utils/include/jsonwrt.h @@ -0,0 +1,44 @@ +#ifndef UTIL_LINUX_JSONWRT_H +#define UTIL_LINUX_JSONWRT_H + +enum { + UL_JSON_OBJECT, + UL_JSON_ARRAY, + UL_JSON_VALUE +}; + +struct ul_jsonwrt { + FILE *out; + int indent; + + unsigned int postponed_break :1; +}; + +void ul_jsonwrt_init(struct ul_jsonwrt *fmt, FILE *out, int indent); +void ul_jsonwrt_indent(struct ul_jsonwrt *fmt); +void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type); +void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type, int islast); + +#define ul_jsonwrt_root_open(_f) ul_jsonwrt_open(_f, NULL, UL_JSON_OBJECT) +#define ul_jsonwrt_root_close(_f) ul_jsonwrt_close(_f, UL_JSON_OBJECT, 1) + +#define ul_jsonwrt_array_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_ARRAY) +#define ul_jsonwrt_array_close(_f, _l) ul_jsonwrt_close(_f, UL_JSON_ARRAY, _l) + +#define ul_jsonwrt_object_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_OBJECT) +#define ul_jsonwrt_object_close(_f, _l) ul_jsonwrt_close(_f, UL_JSON_OBJECT, _l) + +#define ul_jsonwrt_value_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_VALUE) +#define ul_jsonwrt_value_close(_f, _l) ul_jsonwrt_close(_f, UL_JSON_VALUE, _l) + + +void ul_jsonwrt_value_raw(struct ul_jsonwrt *fmt, + const char *name, const char *data, int islast); +void ul_jsonwrt_value_s(struct ul_jsonwrt *fmt, + const char *name, const char *data, int islast); +void ul_jsonwrt_value_u64(struct ul_jsonwrt *fmt, + const char *name, uint64_t data, int islast); +void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt, + const char *name, int data, int islast); + +#endif /* UTIL_LINUX_JSONWRT_H */ diff --git a/src/utils/include/loopdev.h b/src/utils/include/loopdev.h index 9fa0688..0184ffe 100644 --- a/src/utils/include/loopdev.h +++ b/src/utils/include/loopdev.h @@ -156,6 +156,7 @@ extern int is_loopdev(const char *device); extern int loopdev_is_autoclear(const char *device); extern char *loopdev_get_backing_file(const char *device); +extern int loopdev_has_backing_file(const char *device); extern int loopdev_is_used(const char *device, const char *filename, uint64_t offset, uint64_t sizelimit, int flags); extern char *loopdev_find_by_backing_file(const char *filename, diff --git a/src/utils/include/procutils.h b/src/utils/include/procutils.h index 9f8dd76..c9f5bb5 100644 --- a/src/utils/include/procutils.h +++ b/src/utils/include/procutils.h @@ -2,6 +2,7 @@ #define UTIL_LINUX_PROCUTILS #include +#include struct proc_tasks { DIR *dir; @@ -31,4 +32,6 @@ extern int proc_next_pid(struct proc_processes *ps, pid_t *pid); extern char *proc_get_command(pid_t pid); extern char *proc_get_command_name(pid_t pid); +extern int proc_is_procfs(int fd); + #endif /* UTIL_LINUX_PROCUTILS */ diff --git a/src/utils/include/pt-gpt-partnames.h b/src/utils/include/pt-gpt-partnames.h index 604f2c6..6c54a71 100644 --- a/src/utils/include/pt-gpt-partnames.h +++ b/src/utils/include/pt-gpt-partnames.h @@ -50,8 +50,8 @@ DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")), DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")), DEF_GUID("3B8F8425-20E0-4F3B-907F-1A25A76F98E8", N_("Linux server data")), DEF_GUID("44479540-F297-41B2-9AF7-D131D5F0458A", N_("Linux root (x86)")), -DEF_GUID("69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", N_("Linux root (ARM)")), DEF_GUID("4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", N_("Linux root (x86-64)")), +DEF_GUID("69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", N_("Linux root (ARM)")), DEF_GUID("B921B045-1DF0-41C3-AF44-4C6F280D3FAE", N_("Linux root (ARM-64)")), DEF_GUID("993D8D3D-F80E-4225-855A-9DAF8ED7EA97", N_("Linux root (IA-64)")), DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")), @@ -60,11 +60,21 @@ DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")), DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")), DEF_GUID("4D21B016-B534-45C2-A9FB-5C16E091FD2D", N_("Linux variable data")), DEF_GUID("7EC6F557-3BC5-4ACA-B293-16EF5DF639D1", N_("Linux temporary data")), +DEF_GUID("75250D76-8CC6-458E-BD66-BD47CC81A812", N_("Linux /usr (x86)")), +DEF_GUID("8484680C-9521-48C6-9C11-B0720656F69E", N_("Linux /usr (x86-64)")), +DEF_GUID("7D0359A3-02B3-4F0A-865C-654403E70625", N_("Linux /usr (ARM)")), +DEF_GUID("B0E01050-EE5F-4390-949A-9101B17104E9", N_("Linux /usr (ARM-64)")), +DEF_GUID("4301D2A6-4E3B-4B2A-BB94-9E0B2C4225EA", N_("Linux /usr (IA-64)")), DEF_GUID("D13C5D3B-B5D1-422A-B29F-9454FDC89D76", N_("Linux root verity (x86)")), -DEF_GUID("7386CDF2-203C-47A9-A498-F2ECCE45A2D6", N_("Linux root verity (ARM)")), DEF_GUID("2C7357ED-EBD2-46D9-AEC1-23D437EC2BF5", N_("Linux root verity (x86-64)")), +DEF_GUID("7386CDF2-203C-47A9-A498-F2ECCE45A2D6", N_("Linux root verity (ARM)")), DEF_GUID("DF3300CE-D69F-4C92-978C-9BFB0F38D820", N_("Linux root verity (ARM-64)")), DEF_GUID("86ED10D5-B607-45BB-8957-D350F23D0571", N_("Linux root verity (IA-64)")), +DEF_GUID("8F461B0D-14EE-4E81-9AA9-049B6FB97ABD", N_("Linux /usr verity (x86)")), +DEF_GUID("77FF5F63-E7B6-4633-ACF4-1565B864C0E6", N_("Linux /usr verity (x86-64)")), +DEF_GUID("C215D751-7BCD-4649-BE90-6627490A4C05", N_("Linux /usr verity (ARM)")), +DEF_GUID("6E11A4E7-FBCA-4DED-B9E9-E1A512BB664E", N_("Linux /usr verity (ARM-64)")), +DEF_GUID("6A491E03-3BE7-4545-8E38-83320E0EA880", N_("Linux /usr verity (IA-64)")), /* ... too crazy, ignore for now: DEF_GUID("7FFEC5C9-2D00-49B7-8941-3EA10A5586B7", N_("Linux plain dm-crypt")), DEF_GUID("CA7D7CCB-63ED-4C53-861C-1742536059CC", N_("Linux LUKS")), diff --git a/src/utils/include/randutils.h b/src/utils/include/randutils.h index 86e35f3..690bf5e 100644 --- a/src/utils/include/randutils.h +++ b/src/utils/include/randutils.h @@ -11,7 +11,7 @@ extern int rand_get_number(int low_n, int high_n); /* /dev/urandom based with fallback to rand() */ extern int random_get_fd(void); -extern void random_get_bytes(void *buf, size_t nbytes); +extern int ul_random_get_bytes(void *buf, size_t nbytes); extern const char *random_tell_source(void); #endif diff --git a/src/utils/include/strutils.h b/src/utils/include/strutils.h index 4b3182f..09cc35e 100644 --- a/src/utils/include/strutils.h +++ b/src/utils/include/strutils.h @@ -9,6 +9,8 @@ #include #include +#include "c.h" + /* initialize a custom exit code for all *_or_err functions */ extern void strutils_set_exitcode(int exit_code); @@ -61,8 +63,13 @@ extern char *strnchr(const char *s, size_t maxlen, int c); /* caller guarantees n > 0 */ static inline void xstrncpy(char *dest, const char *src, size_t n) { - strncpy(dest, src, n-1); - dest[n-1] = 0; + size_t len = src ? strlen(src) : 0; + + if (!len) + return; + len = min(len, n - 1); + memcpy(dest, src, len); + dest[len] = 0; } /* This is like strncpy(), but based on memcpy(), so compilers and static @@ -303,6 +310,33 @@ static inline size_t ltrim_whitespace(unsigned char *str) return len; } +/* Removes left-hand, right-hand and repeating whitespaces. + */ +static inline size_t normalize_whitespace(unsigned char *str) +{ + size_t i, x, sz = strlen((char *) str); + int nsp = 0, intext = 0; + + if (!sz) + return 0; + + for (i = 0, x = 0; i < sz; ) { + if (isspace(str[i])) + nsp++; + else + nsp = 0, intext = 1; + + if (nsp > 1 || (nsp && !intext)) + i++; + else + str[x++] = str[i++]; + } + if (nsp) /* tailing space */ + x--; + str[x] = '\0'; + return x; +} + static inline void strrep(char *s, int find, int replace) { while (s && *s && (s = strchr(s, find)) != NULL) diff --git a/src/utils/include/swapheader.h b/src/utils/include/swapheader.h deleted file mode 100644 index 3fce0d0..0000000 --- a/src/utils/include/swapheader.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _SWAPHEADER_H -#define _SWAPHEADER_H - -#define SWAP_VERSION 1 -#define SWAP_UUID_LENGTH 16 -#define SWAP_LABEL_LENGTH 16 -#define SWAP_SIGNATURE "SWAPSPACE2" -#define SWAP_SIGNATURE_SZ (sizeof(SWAP_SIGNATURE) - 1) - -#include - -struct swap_header_v1_2 { - char bootbits[1024]; /* Space for disklabel etc. */ - uint32_t version; - uint32_t last_page; - uint32_t nr_badpages; - unsigned char uuid[SWAP_UUID_LENGTH]; - char volume_name[SWAP_LABEL_LENGTH]; - uint32_t padding[117]; - uint32_t badpages[1]; -}; - -#endif /* _SWAPHEADER_H */ diff --git a/src/utils/include/swapprober.h b/src/utils/include/swapprober.h deleted file mode 100644 index 5107700..0000000 --- a/src/utils/include/swapprober.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef UTIL_LINUX_SWAP_PROBER_H -#define UTIL_LINUX_SWAP_PROBER_H - -#include - -blkid_probe get_swap_prober(const char *devname); - -#endif /* UTIL_LINUX_SWAP_PROBER_H */ - -- cgit v1.2.3-55-g7522