/* Extra room for the ending NUL-byte, necessary even when the buffer has
 * SOL_BUFFER_FLAGS_NO_NUL_BYTE because vsnprintf always write the '\0'. */
SOL_API int
sol_buffer_append_vprintf(struct sol_buffer *buf, const char *fmt, va_list args)
{
    va_list args_copy;
    size_t space;
    char *p;
    ssize_t done;
    int r;

    SOL_NULL_CHECK(buf, -EINVAL);
    SOL_NULL_CHECK(fmt, -EINVAL);

    /* We need to copy args since we first try to do one vsnprintf()
     * to the existing space and if it doesn't fit we do a second
     * one. The second one would fail since args may be changed by
     * vsnprintf(), so the copy.
     */
    va_copy(args_copy, args);

    space = buf->capacity - buf->used;
    p = sol_buffer_at_end(buf);
    done = vsnprintf(p, space, fmt, args_copy);
    if (done < 0) {
        r = -errno;
        goto end;
    } else if ((size_t)done >= space) {
        size_t new_size;

        r = sol_util_size_add(buf->used, done, &new_size);
        if (r < 0)
            goto end;

        /* Extra room for the ending NUL-byte. */
        if (new_size >= SIZE_MAX - 1) {
            r = -EOVERFLOW;
            goto end;
        }
        r = sol_buffer_ensure(buf, new_size + 1);
        if (r < 0)
            goto end;

        space = buf->capacity - buf->used;
        p = sol_buffer_at_end(buf);
        done = vsnprintf(p, space, fmt, args);
        if (done < 0) {
            r = -errno;
            goto end;
        }
    }

    buf->used += done;
    r = 0;

end:
    va_end(args_copy);
    return r;
}
Exemple #2
0
SOL_API ssize_t
sol_util_fill_buffer(const int fd, struct sol_buffer *buffer, const size_t size)
{
    size_t bytes_read = 0, s;
    unsigned int retry = 0;
    ssize_t ret;

    SOL_NULL_CHECK(buffer, -EINVAL);

    if (sol_util_size_add(buffer->used, size, &s) < 0)
        return -EOVERFLOW;

    ret = sol_buffer_ensure(buffer, s);
    if (ret < 0)
        return ret;

    do {
        ret = read(fd, (char *)buffer->data + buffer->used + bytes_read,
            size - bytes_read);
        if (ret < 0) {
            retry++;
            if (retry >= SOL_UTIL_MAX_READ_ATTEMPTS) {
                if (errno == EINTR || errno == EAGAIN) {
                    ret = 0; /* if we exceed maximum attempts, don't return error */
                }
                break;
            }

            if (errno == EINTR || errno == EAGAIN)
                continue;
            else
                break;
        } else if (ret == 0) {
            retry++;
            if (retry >= SOL_UTIL_MAX_READ_ATTEMPTS)
                break;
            continue;
        }

        retry = 0; //We only count consecutive failures
        bytes_read += (size_t)ret;
    } while (bytes_read < size);

    buffer->used += bytes_read;

    if (ret >= 0)
        ret = bytes_read;

    if (SOL_BUFFER_NEEDS_NUL_BYTE(buffer)) {
        int err;

        err = sol_buffer_ensure_nul_byte(buffer);
        SOL_INT_CHECK(err, < 0, err);
    }

    return ret;
}
Exemple #3
0
ssize_t
sol_util_fill_buffer(const int fd, struct sol_buffer *buffer, const size_t size)
{
    size_t bytes_read = 0, s;
    unsigned int retry = 0;
    ssize_t ret;

    SOL_NULL_CHECK(buffer, -EINVAL);

    if (sol_util_size_add(buffer->used, size, &s) < 0)
        return -EOVERFLOW;

    ret = sol_buffer_ensure(buffer, s);
    if (ret < 0)
        return ret;

    do {
        ret = read(fd, (char *)buffer->data + buffer->used + bytes_read,
            size - bytes_read);
        if (ret < 0) {
            retry++;
            if (retry >= SOL_UTIL_MAX_READ_ATTEMPTS)
                break;

            if (errno == EINTR || errno == EAGAIN) {
                continue;
            } else
                break;
        }

        retry = 0; //We only count consecutive failures
        bytes_read += (size_t)ret;
    } while (ret && bytes_read < size);

    buffer->used += bytes_read;

    if (ret > 0)
        ret = bytes_read;

    if (!(buffer->flags & SOL_BUFFER_FLAGS_NO_NUL_BYTE)) {
        if (buffer->used == buffer->capacity)
            SOL_WRN("sol_buffer %p asks for terminating NUL byte, but doesn't have space for it",
                buffer);
        else
            *((char *)buffer->data + buffer->used) = '\0';
    }

    return ret;
}
Exemple #4
0
SOL_API int
sol_buffer_expand(struct sol_buffer *buf, size_t bytes)
{
    size_t new_size, available;
    int err;

    SOL_NULL_CHECK(buf, -EINVAL);

    available = buf->capacity - buf->used;

    if (available >= bytes)
        return 0;

    err = sol_util_size_add(buf->capacity, bytes - available, &new_size);
    SOL_INT_CHECK(err, < 0, err);
    err = sol_buffer_ensure(buf, new_size);
    SOL_INT_CHECK(err, < 0, err);

    return 0;
}
SOL_API int
sol_buffer_insert_slice(struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice)
{
    const size_t nul_size = nul_byte_size(buf);
    char *p;
    size_t new_size;
    int err;

    SOL_NULL_CHECK(buf, -EINVAL);
    SOL_INT_CHECK(pos, > buf->used, -EINVAL);

    if (pos == buf->used)
        return sol_buffer_append_slice(buf, slice);

    err = sol_util_size_add(buf->used, slice.len, &new_size);
    if (err < 0)
        return err;

    /* Extra room for the ending NUL-byte. */
    if (new_size >= SIZE_MAX - nul_size)
        return -EOVERFLOW;
    err = sol_buffer_ensure(buf, new_size + nul_size);
    if (err < 0)
        return err;

    p = sol_buffer_at(buf, pos);
    memmove(p + slice.len, p, buf->used - pos);
    memcpy(p, slice.data, slice.len);
    buf->used += slice.len;

    if (nul_size) {
        p = sol_buffer_at_end(buf);
        p[0] = '\0';
    }

    return 0;
}