X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbasic%2Fio-util.h;h=4684ed3bfc782919b71600521ba7a66f4b04043d;hp=cd2aa75ad2c6859cde3a9b8f2a58fca1acb4cb6e;hb=5091db1fa99acdbc3d9acb485c1c70d8bfb636db;hpb=da2587d5154e11d4e643e326793f3ce2cc48dee6 diff --git a/src/basic/io-util.h b/src/basic/io-util.h index cd2aa75ad..4684ed3bf 100644 --- a/src/basic/io-util.h +++ b/src/basic/io-util.h @@ -1,5 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - #pragma once /*** @@ -22,9 +20,12 @@ ***/ #include +#include +#include #include #include +#include "macro.h" #include "time-util.h" int flush_fd(int fd); @@ -45,7 +46,7 @@ ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length); char *_s = (char *)(s); \ _i->iov_base = _s; \ _i->iov_len = strlen(_s); \ - } while(false) + } while (false) static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) { unsigned j; @@ -74,3 +75,21 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) { return k; } + +static inline bool FILE_SIZE_VALID(uint64_t l) { + /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than + * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */ + + return (l >> 63) == 0; +} + +static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) { + + /* Same as above, but allows one extra value: -1 as indication for infinity. */ + + if (l == (uint64_t) -1) + return true; + + return FILE_SIZE_VALID(l); + +}