2008-11-07 15:11:08 +00:00
|
|
|
/* io.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2008-11-07 15:11:08 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2009-06-11 10:03:10 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
2008-11-07 15:11:08 +00:00
|
|
|
* under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation. See http://www.gnu.org/ for details.
|
|
|
|
*/
|
|
|
|
|
2009-07-22 11:56:27 +00:00
|
|
|
#include <errno.h>
|
2008-11-07 15:11:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2015-03-11 14:10:33 +00:00
|
|
|
#include <endian.h>
|
2008-11-07 15:11:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <malloc.h>
|
2012-02-22 06:45:40 +00:00
|
|
|
#include <dirent.h>
|
2015-04-22 06:45:01 +00:00
|
|
|
#include <stdint.h>
|
2008-11-07 17:06:58 +00:00
|
|
|
#include <sys/mman.h>
|
2010-12-09 08:47:09 +00:00
|
|
|
#include <sys/wait.h>
|
2010-08-30 13:04:25 +00:00
|
|
|
#include <sys/stat.h>
|
2015-04-08 07:25:44 +00:00
|
|
|
#include <sys/xattr.h>
|
2010-06-12 10:43:29 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2008-11-07 15:11:08 +00:00
|
|
|
|
|
|
|
#include "apk_defines.h"
|
|
|
|
#include "apk_io.h"
|
2010-06-12 10:43:29 +00:00
|
|
|
#include "apk_hash.h"
|
2018-10-26 05:21:52 +00:00
|
|
|
#include "apk_openssl.h"
|
2008-11-07 15:11:08 +00:00
|
|
|
|
2013-07-04 07:36:47 +00:00
|
|
|
#if defined(__GLIBC__) || defined(__UCLIBC__)
|
|
|
|
#define HAVE_FGETPWENT_R
|
|
|
|
#define HAVE_FGETGRENT_R
|
|
|
|
#endif
|
|
|
|
|
2020-01-11 01:23:22 +00:00
|
|
|
size_t apk_io_bufsize = 8*1024;
|
2020-01-10 09:02:48 +00:00
|
|
|
|
2015-11-09 10:47:23 +00:00
|
|
|
static void apk_file_meta_from_fd(int fd, struct apk_file_meta *meta)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) == 0) {
|
|
|
|
meta->mtime = st.st_mtime;
|
|
|
|
meta->atime = st.st_atime;
|
|
|
|
} else {
|
|
|
|
memset(meta, 0, sizeof(*meta));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void apk_file_meta_to_fd(int fd, struct apk_file_meta *meta)
|
|
|
|
{
|
|
|
|
struct timespec times[2] = {
|
|
|
|
{ .tv_sec = meta->atime, .tv_nsec = meta->atime ? 0 : UTIME_OMIT },
|
|
|
|
{ .tv_sec = meta->mtime, .tv_nsec = meta->mtime ? 0 : UTIME_OMIT }
|
|
|
|
};
|
|
|
|
futimens(fd, times);
|
|
|
|
}
|
|
|
|
|
2020-01-10 09:02:48 +00:00
|
|
|
ssize_t apk_istream_read(struct apk_istream *is, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
ssize_t left = size, r = 0;
|
|
|
|
|
|
|
|
while (left) {
|
|
|
|
if (is->ptr != is->end) {
|
2020-01-11 07:58:41 +00:00
|
|
|
r = min(left, is->end - is->ptr);
|
2020-01-10 09:02:48 +00:00
|
|
|
if (ptr) {
|
|
|
|
memcpy(ptr, is->ptr, r);
|
|
|
|
ptr += r;
|
|
|
|
}
|
|
|
|
is->ptr += r;
|
|
|
|
left -= r;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is->err) break;
|
|
|
|
|
|
|
|
if (ptr && left > is->buf_size/4) {
|
|
|
|
r = is->ops->read(is, ptr, left);
|
|
|
|
if (r <= 0) break;
|
|
|
|
left -= r;
|
|
|
|
ptr += r;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = is->ops->read(is, is->buf, is->buf_size);
|
|
|
|
if (r <= 0) break;
|
|
|
|
|
|
|
|
is->ptr = is->buf;
|
|
|
|
is->end = is->buf + r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0) return r;
|
|
|
|
if (size && left == size && !is->err) is->err = 1;
|
|
|
|
if (size == left) return is->err < 0 ? is->err : 0;
|
|
|
|
return size - left;
|
|
|
|
}
|
|
|
|
|
2020-01-11 01:23:22 +00:00
|
|
|
static int __apk_istream_fill(struct apk_istream *is)
|
|
|
|
{
|
|
|
|
ssize_t sz;
|
|
|
|
|
|
|
|
if (is->err) return is->err;
|
|
|
|
|
|
|
|
if (is->ptr != is->buf) {
|
|
|
|
sz = is->end - is->ptr;
|
|
|
|
memmove(is->buf, is->ptr, sz);
|
|
|
|
is->ptr = is->buf;
|
|
|
|
is->end = is->buf + sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz = is->ops->read(is, is->end, is->buf + is->buf_size - is->end);
|
|
|
|
if (sz <= 0) {
|
|
|
|
is->err = sz ?: 1;
|
|
|
|
return is->err;
|
|
|
|
}
|
|
|
|
is->end += sz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
apk_blob_t apk_istream_get(struct apk_istream *is, size_t len)
|
|
|
|
{
|
|
|
|
apk_blob_t ret = APK_BLOB_NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (is->end - is->ptr >= len) {
|
|
|
|
ret = APK_BLOB_PTR_LEN((char*)is->ptr, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (is->err>0 || is->end-is->ptr == is->buf_size) {
|
|
|
|
ret = APK_BLOB_PTR_LEN((char*)is->ptr, is->end - is->ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!__apk_istream_fill(is));
|
|
|
|
|
|
|
|
if (!APK_BLOB_IS_NULL(ret)) {
|
|
|
|
is->ptr = (uint8_t*)ret.ptr + ret.len;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (struct apk_blob) { .len = is->err < 0 ? is->err : 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
apk_blob_t apk_istream_get_all(struct apk_istream *is)
|
|
|
|
{
|
|
|
|
if (is->ptr == is->end)
|
|
|
|
__apk_istream_fill(is);
|
|
|
|
|
|
|
|
if (is->ptr != is->end) {
|
|
|
|
apk_blob_t ret = APK_BLOB_PTR_LEN((char*)is->ptr, is->end - is->ptr);
|
|
|
|
is->ptr = is->end = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (struct apk_blob) { .len = is->err < 0 ? is->err : 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
apk_blob_t apk_istream_get_delim(struct apk_istream *is, apk_blob_t token)
|
|
|
|
{
|
|
|
|
apk_blob_t ret = APK_BLOB_NULL, left = APK_BLOB_NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (apk_blob_split(APK_BLOB_PTR_LEN((char*)is->ptr, is->end - is->ptr), token, &ret, &left))
|
|
|
|
break;
|
|
|
|
if (is->end - is->ptr == is->buf_size) {
|
|
|
|
is->err = -ENOBUFS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!__apk_istream_fill(is));
|
|
|
|
|
|
|
|
/* Last segment before end-of-file. Return also zero length non-null
|
|
|
|
* blob if eof comes immediately after the delimiter. */
|
|
|
|
if (is->ptr && is->err > 0)
|
|
|
|
ret = APK_BLOB_PTR_LEN((char*)is->ptr, is->end - is->ptr);
|
|
|
|
|
|
|
|
if (!APK_BLOB_IS_NULL(ret)) {
|
|
|
|
is->ptr = (uint8_t*)left.ptr;
|
|
|
|
is->end = (uint8_t*)left.ptr + left.len;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return (struct apk_blob) { .len = is->err < 0 ? is->err : 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
static void segment_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
|
|
|
|
{
|
|
|
|
struct apk_segment_istream *sis = container_of(is, struct apk_segment_istream, is);
|
|
|
|
*meta = (struct apk_file_meta) {
|
|
|
|
.atime = sis->mtime,
|
|
|
|
.mtime = sis->mtime,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t segment_read(struct apk_istream *is, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
struct apk_segment_istream *sis = container_of(is, struct apk_segment_istream, is);
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
if (size > sis->bytes_left) size = sis->bytes_left;
|
|
|
|
if (size == 0) return 0;
|
|
|
|
|
|
|
|
r = sis->pis->ops->read(sis->pis, ptr, size);
|
|
|
|
if (r <= 0) {
|
|
|
|
/* If inner stream returned zero (end-of-stream), we
|
|
|
|
* are getting short read, because tar header indicated
|
|
|
|
* more was to be expected. */
|
|
|
|
if (r == 0) r = -ECONNABORTED;
|
|
|
|
} else {
|
|
|
|
sis->bytes_left -= r;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void segment_close(struct apk_istream *is)
|
|
|
|
{
|
|
|
|
struct apk_segment_istream *sis = container_of(is, struct apk_segment_istream, is);
|
|
|
|
|
|
|
|
if (sis->bytes_left) {
|
|
|
|
apk_istream_read(sis->pis, NULL, sis->bytes_left);
|
|
|
|
sis->bytes_left = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct apk_istream_ops segment_istream_ops = {
|
|
|
|
.get_meta = segment_get_meta,
|
|
|
|
.read = segment_read,
|
|
|
|
.close = segment_close,
|
|
|
|
};
|
|
|
|
|
2020-01-11 07:16:38 +00:00
|
|
|
struct apk_istream *apk_istream_segment(struct apk_segment_istream *sis, struct apk_istream *is, size_t len, time_t mtime)
|
2020-01-11 01:23:22 +00:00
|
|
|
{
|
|
|
|
*sis = (struct apk_segment_istream) {
|
|
|
|
.is.ops = &segment_istream_ops,
|
|
|
|
.is.buf = is->buf,
|
|
|
|
.is.buf_size = is->buf_size,
|
|
|
|
.is.ptr = is->ptr,
|
|
|
|
.is.end = is->end,
|
|
|
|
.pis = is,
|
|
|
|
.bytes_left = len,
|
|
|
|
.mtime = mtime,
|
|
|
|
};
|
|
|
|
if (sis->is.end - sis->is.ptr > len) {
|
|
|
|
sis->is.end = sis->is.ptr + len;
|
|
|
|
is->ptr += len;
|
|
|
|
} else {
|
|
|
|
is->ptr = is->end = 0;
|
|
|
|
}
|
|
|
|
sis->bytes_left -= sis->is.end - sis->is.ptr;
|
2020-01-11 07:16:38 +00:00
|
|
|
return &sis->is;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct apk_tee_istream {
|
|
|
|
struct apk_istream is;
|
|
|
|
struct apk_istream *inner_is;
|
|
|
|
int fd, copy_meta;
|
|
|
|
size_t size;
|
|
|
|
apk_progress_cb cb;
|
|
|
|
void *cb_ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void tee_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
|
|
|
|
{
|
|
|
|
struct apk_tee_istream *tee = container_of(is, struct apk_tee_istream, is);
|
|
|
|
apk_istream_get_meta(tee->inner_is, meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t __tee_write(struct apk_tee_istream *tee, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
ssize_t w = write(tee->fd, ptr, size);
|
|
|
|
if (size != w) {
|
|
|
|
if (w < 0) return w;
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
tee->size += size;
|
|
|
|
if (tee->cb) tee->cb(tee->cb_ctx, tee->size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t tee_read(struct apk_istream *is, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
struct apk_tee_istream *tee = container_of(is, struct apk_tee_istream, is);
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
r = tee->inner_is->ops->read(tee->inner_is, ptr, size);
|
|
|
|
if (r <= 0) return r;
|
|
|
|
|
|
|
|
return __tee_write(tee, ptr, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tee_close(struct apk_istream *is)
|
|
|
|
{
|
|
|
|
struct apk_tee_istream *tee = container_of(is, struct apk_tee_istream, is);
|
|
|
|
struct apk_file_meta meta;
|
|
|
|
|
|
|
|
if (tee->copy_meta) {
|
|
|
|
apk_istream_get_meta(tee->inner_is, &meta);
|
|
|
|
apk_file_meta_to_fd(tee->fd, &meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
apk_istream_close(tee->inner_is);
|
|
|
|
close(tee->fd);
|
|
|
|
free(tee);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct apk_istream_ops tee_istream_ops = {
|
|
|
|
.get_meta = tee_get_meta,
|
|
|
|
.read = tee_read,
|
|
|
|
.close = tee_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct apk_istream *apk_istream_tee(struct apk_istream *from, int atfd, const char *to, int copy_meta, apk_progress_cb cb, void *cb_ctx)
|
|
|
|
{
|
|
|
|
struct apk_tee_istream *tee;
|
|
|
|
int fd, r;
|
|
|
|
|
|
|
|
if (IS_ERR_OR_NULL(from)) return ERR_CAST(from);
|
|
|
|
|
|
|
|
fd = openat(atfd, to, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
|
|
|
if (fd < 0) {
|
|
|
|
r = -errno;
|
|
|
|
goto err_is;
|
|
|
|
}
|
|
|
|
|
|
|
|
tee = malloc(sizeof *tee);
|
|
|
|
if (!tee) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto err_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
*tee = (struct apk_tee_istream) {
|
|
|
|
.is.ops = &tee_istream_ops,
|
|
|
|
.is.buf = from->buf,
|
|
|
|
.is.buf_size = from->buf_size,
|
|
|
|
.is.ptr = from->ptr,
|
|
|
|
.is.end = from->end,
|
|
|
|
.inner_is = from,
|
|
|
|
.fd = fd,
|
|
|
|
.copy_meta = copy_meta,
|
|
|
|
.cb = cb,
|
|
|
|
.cb_ctx = cb_ctx,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (from->ptr != from->end) {
|
|
|
|
r = __tee_write(tee, from->ptr, from->end - from->ptr);
|
|
|
|
if (r < 0) goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tee->is;
|
|
|
|
err_free:
|
|
|
|
free(tee);
|
|
|
|
err_fd:
|
|
|
|
close(fd);
|
|
|
|
err_is:
|
|
|
|
apk_istream_close(from);
|
|
|
|
return ERR_PTR(r);
|
2020-01-11 01:23:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 07:38:06 +00:00
|
|
|
struct apk_mmap_istream {
|
|
|
|
struct apk_istream is;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mmap_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
|
|
|
|
{
|
|
|
|
struct apk_mmap_istream *mis = container_of(is, struct apk_mmap_istream, is);
|
|
|
|
return apk_file_meta_from_fd(mis->fd, meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t mmap_read(struct apk_istream *is, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mmap_close(struct apk_istream *is)
|
|
|
|
{
|
|
|
|
struct apk_mmap_istream *mis = container_of(is, struct apk_mmap_istream, is);
|
|
|
|
|
|
|
|
munmap(mis->is.buf, mis->is.buf_size);
|
|
|
|
close(mis->fd);
|
|
|
|
free(mis);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct apk_istream_ops mmap_istream_ops = {
|
|
|
|
.get_meta = mmap_get_meta,
|
|
|
|
.read = mmap_read,
|
|
|
|
.close = mmap_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct apk_istream *apk_mmap_istream_from_fd(int fd)
|
|
|
|
{
|
|
|
|
struct apk_mmap_istream *mis;
|
|
|
|
struct stat st;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0) return ERR_PTR(-errno);
|
|
|
|
|
|
|
|
ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (ptr == MAP_FAILED) return ERR_PTR(-errno);
|
|
|
|
|
|
|
|
mis = malloc(sizeof *mis);
|
|
|
|
if (mis == NULL) {
|
|
|
|
munmap(ptr, st.st_size);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
*mis = (struct apk_mmap_istream) {
|
|
|
|
.is.flags = APK_ISTREAM_SINGLE_READ,
|
|
|
|
.is.err = 1,
|
|
|
|
.is.ops = &mmap_istream_ops,
|
|
|
|
.is.buf = ptr,
|
|
|
|
.is.buf_size = st.st_size,
|
|
|
|
.is.ptr = ptr,
|
|
|
|
.is.end = ptr + st.st_size,
|
|
|
|
.fd = fd,
|
|
|
|
};
|
|
|
|
return &mis->is;
|
|
|
|
}
|
|
|
|
|
2008-11-07 15:11:08 +00:00
|
|
|
struct apk_fd_istream {
|
|
|
|
struct apk_istream is;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static void fdi_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
|
2015-11-09 10:47:23 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fd_istream *fis = container_of(is, struct apk_fd_istream, is);
|
2015-11-09 10:47:23 +00:00
|
|
|
apk_file_meta_from_fd(fis->fd, meta);
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static ssize_t fdi_read(struct apk_istream *is, void *ptr, size_t size)
|
2008-11-07 15:11:08 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fd_istream *fis = container_of(is, struct apk_fd_istream, is);
|
2020-01-10 09:02:48 +00:00
|
|
|
ssize_t r;
|
2008-11-07 15:11:08 +00:00
|
|
|
|
2020-01-10 09:02:48 +00:00
|
|
|
r = read(fis->fd, ptr, size);
|
|
|
|
if (r < 0) return -errno;
|
|
|
|
return r;
|
2008-11-07 15:11:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static void fdi_close(struct apk_istream *is)
|
2008-11-07 15:11:08 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fd_istream *fis = container_of(is, struct apk_fd_istream, is);
|
2008-11-07 15:11:08 +00:00
|
|
|
|
|
|
|
close(fis->fd);
|
|
|
|
free(fis);
|
|
|
|
}
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
static const struct apk_istream_ops fd_istream_ops = {
|
|
|
|
.get_meta = fdi_get_meta,
|
|
|
|
.read = fdi_read,
|
|
|
|
.close = fdi_close,
|
|
|
|
};
|
|
|
|
|
2020-01-05 23:14:28 +00:00
|
|
|
struct apk_istream *apk_istream_from_fd(int fd)
|
2008-11-07 15:11:08 +00:00
|
|
|
{
|
|
|
|
struct apk_fd_istream *fis;
|
|
|
|
|
2015-03-10 11:04:14 +00:00
|
|
|
if (fd < 0) return ERR_PTR(-EBADF);
|
2008-11-28 13:03:27 +00:00
|
|
|
|
2020-01-10 09:02:48 +00:00
|
|
|
fis = malloc(sizeof(*fis) + apk_io_bufsize);
|
2009-08-12 08:05:09 +00:00
|
|
|
if (fis == NULL) {
|
|
|
|
close(fd);
|
2015-03-10 11:04:14 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2009-08-12 08:05:09 +00:00
|
|
|
}
|
2008-11-07 15:11:08 +00:00
|
|
|
|
|
|
|
*fis = (struct apk_fd_istream) {
|
2017-06-21 13:07:58 +00:00
|
|
|
.is.ops = &fd_istream_ops,
|
2020-01-10 09:02:48 +00:00
|
|
|
.is.buf = (uint8_t *)(fis + 1),
|
|
|
|
.is.buf_size = apk_io_bufsize,
|
2008-11-07 15:11:08 +00:00
|
|
|
.fd = fd,
|
|
|
|
};
|
|
|
|
|
|
|
|
return &fis->is;
|
|
|
|
}
|
|
|
|
|
2009-07-31 13:08:09 +00:00
|
|
|
struct apk_istream *apk_istream_from_file(int atfd, const char *file)
|
2008-11-07 15:11:08 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-06-11 10:42:21 +00:00
|
|
|
fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
|
2015-03-10 11:04:14 +00:00
|
|
|
if (fd < 0) return ERR_PTR(-errno);
|
2008-11-07 15:11:08 +00:00
|
|
|
|
2020-01-11 07:38:06 +00:00
|
|
|
if (0) {
|
|
|
|
struct apk_istream *is = apk_mmap_istream_from_fd(fd);
|
|
|
|
if (!IS_ERR_OR_NULL(is)) return is;
|
|
|
|
}
|
2008-11-07 15:11:08 +00:00
|
|
|
return apk_istream_from_fd(fd);
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
ssize_t apk_istream_splice(struct apk_istream *is, int fd, size_t size,
|
2017-10-12 10:35:46 +00:00
|
|
|
apk_progress_cb cb, void *cb_ctx)
|
2008-11-07 15:11:08 +00:00
|
|
|
{
|
2009-07-30 06:11:14 +00:00
|
|
|
static void *splice_buffer = NULL;
|
2009-07-31 07:50:15 +00:00
|
|
|
unsigned char *buf, *mmapbase = MAP_FAILED;
|
2017-08-22 14:28:21 +00:00
|
|
|
size_t bufsz, done = 0, togo;
|
|
|
|
ssize_t r;
|
2008-11-07 17:06:58 +00:00
|
|
|
|
|
|
|
bufsz = size;
|
2009-07-30 06:07:35 +00:00
|
|
|
if (size > 128 * 1024) {
|
2015-09-03 10:15:18 +00:00
|
|
|
if (size != APK_SPLICE_ALL) {
|
|
|
|
r = posix_fallocate(fd, 0, size);
|
|
|
|
if (r == 0)
|
|
|
|
mmapbase = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
2015-09-11 14:16:26 +00:00
|
|
|
else if (r == EBADF || r == EFBIG || r == ENOSPC || r == EIO)
|
2015-09-03 10:15:18 +00:00
|
|
|
return -r;
|
|
|
|
}
|
2020-01-11 07:58:41 +00:00
|
|
|
bufsz = min(bufsz, 2*1024*1024);
|
2009-07-31 07:50:15 +00:00
|
|
|
buf = mmapbase;
|
2009-07-29 16:33:52 +00:00
|
|
|
}
|
2009-07-31 07:50:15 +00:00
|
|
|
if (mmapbase == MAP_FAILED) {
|
2017-10-12 10:35:46 +00:00
|
|
|
if (!splice_buffer) splice_buffer = malloc(256*1024);
|
2009-07-30 06:11:14 +00:00
|
|
|
buf = splice_buffer;
|
2017-10-12 10:35:46 +00:00
|
|
|
if (!buf) return -ENOMEM;
|
2020-01-11 07:58:41 +00:00
|
|
|
bufsz = min(bufsz, 256*1024);
|
2009-07-29 16:33:52 +00:00
|
|
|
}
|
2008-11-07 15:11:08 +00:00
|
|
|
|
|
|
|
while (done < size) {
|
2017-10-12 10:35:46 +00:00
|
|
|
if (cb != NULL) cb(cb_ctx, done);
|
2009-01-07 19:45:11 +00:00
|
|
|
|
2020-01-11 07:58:41 +00:00
|
|
|
togo = min(size - done, bufsz);
|
2017-06-21 13:07:58 +00:00
|
|
|
r = apk_istream_read(is, buf, togo);
|
2017-10-12 10:35:46 +00:00
|
|
|
if (r <= 0) {
|
|
|
|
if (r) goto err;
|
|
|
|
if (size != APK_SPLICE_ALL && done != size) {
|
|
|
|
r = -EBADMSG;
|
|
|
|
goto err;
|
|
|
|
}
|
2013-06-17 10:51:49 +00:00
|
|
|
break;
|
2017-10-12 10:35:46 +00:00
|
|
|
}
|
2009-07-29 16:33:52 +00:00
|
|
|
|
2009-07-31 07:50:15 +00:00
|
|
|
if (mmapbase == MAP_FAILED) {
|
2009-07-29 16:33:52 +00:00
|
|
|
if (write(fd, buf, r) != r) {
|
|
|
|
if (r < 0)
|
|
|
|
r = -errno;
|
|
|
|
goto err;
|
|
|
|
}
|
2009-07-31 07:50:15 +00:00
|
|
|
} else
|
|
|
|
buf += r;
|
2009-07-29 16:33:52 +00:00
|
|
|
|
2008-11-07 15:11:08 +00:00
|
|
|
done += r;
|
|
|
|
}
|
2008-11-07 17:06:58 +00:00
|
|
|
r = done;
|
|
|
|
err:
|
2009-07-31 07:50:15 +00:00
|
|
|
if (mmapbase != MAP_FAILED)
|
|
|
|
munmap(mmapbase, size);
|
2008-11-07 17:06:58 +00:00
|
|
|
return r;
|
2008-11-07 15:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
apk_blob_t apk_blob_from_istream(struct apk_istream *is, size_t size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
2017-08-22 14:28:21 +00:00
|
|
|
ssize_t rsize;
|
2008-11-07 15:11:08 +00:00
|
|
|
|
|
|
|
ptr = malloc(size);
|
|
|
|
if (ptr == NULL)
|
|
|
|
return APK_BLOB_NULL;
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
rsize = apk_istream_read(is, ptr, size);
|
2008-11-07 15:11:08 +00:00
|
|
|
if (rsize < 0) {
|
|
|
|
free(ptr);
|
|
|
|
return APK_BLOB_NULL;
|
|
|
|
}
|
|
|
|
if (rsize != size)
|
|
|
|
ptr = realloc(ptr, rsize);
|
|
|
|
|
|
|
|
return APK_BLOB_PTR_LEN(ptr, rsize);
|
|
|
|
}
|
|
|
|
|
2009-07-31 13:08:09 +00:00
|
|
|
apk_blob_t apk_blob_from_file(int atfd, const char *file)
|
2008-11-27 14:59:04 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
char *buf;
|
|
|
|
|
2010-06-11 10:42:21 +00:00
|
|
|
fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
|
2008-11-27 14:59:04 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return APK_BLOB_NULL;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
goto err_fd;
|
|
|
|
|
|
|
|
buf = malloc(st.st_size);
|
|
|
|
if (buf == NULL)
|
|
|
|
goto err_fd;
|
|
|
|
|
|
|
|
if (read(fd, buf, st.st_size) != st.st_size)
|
|
|
|
goto err_read;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return APK_BLOB_PTR_LEN(buf, st.st_size);
|
|
|
|
err_read:
|
|
|
|
free(buf);
|
|
|
|
err_fd:
|
|
|
|
close(fd);
|
|
|
|
return APK_BLOB_NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-23 15:04:51 +00:00
|
|
|
int apk_blob_to_file(int atfd, const char *file, apk_blob_t b, unsigned int flags)
|
|
|
|
{
|
|
|
|
int fd, r, len;
|
|
|
|
|
|
|
|
fd = openat(atfd, file, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
len = b.len;
|
|
|
|
r = write(fd, b.ptr, len);
|
|
|
|
if ((r == len) &&
|
|
|
|
(flags & APK_BTF_ADD_EOL) && (b.len == 0 || b.ptr[b.len-1] != '\n')) {
|
|
|
|
len = 1;
|
|
|
|
r = write(fd, "\n", len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
r = -errno;
|
|
|
|
else if (r != len)
|
|
|
|
r = -ENOSPC;
|
|
|
|
else
|
|
|
|
r = 0;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (r != 0)
|
|
|
|
unlinkat(atfd, file, 0);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:10:33 +00:00
|
|
|
static int cmp_xattr(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
const struct apk_xattr *d1 = p1, *d2 = p2;
|
|
|
|
return strcmp(d1->name, d2->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hash_len_data(EVP_MD_CTX *ctx, uint32_t len, const void *ptr)
|
|
|
|
{
|
|
|
|
uint32_t belen = htobe32(len);
|
|
|
|
EVP_DigestUpdate(ctx, &belen, sizeof(belen));
|
|
|
|
EVP_DigestUpdate(ctx, ptr, len);
|
|
|
|
}
|
|
|
|
|
2015-04-08 07:25:44 +00:00
|
|
|
void apk_fileinfo_hash_xattr_array(struct apk_xattr_array *xattrs, const EVP_MD *md, struct apk_checksum *csum)
|
2015-03-11 14:10:33 +00:00
|
|
|
{
|
|
|
|
struct apk_xattr *xattr;
|
2018-10-26 05:21:52 +00:00
|
|
|
EVP_MD_CTX *mdctx;
|
2015-03-11 14:10:33 +00:00
|
|
|
|
2018-10-26 05:21:52 +00:00
|
|
|
if (!xattrs || xattrs->num == 0) goto err;
|
|
|
|
mdctx = EVP_MD_CTX_new();
|
|
|
|
if (!mdctx) goto err;
|
2015-03-11 14:10:33 +00:00
|
|
|
|
2015-04-08 07:25:44 +00:00
|
|
|
qsort(xattrs->item, xattrs->num, sizeof(xattrs->item[0]), cmp_xattr);
|
2015-03-11 14:10:33 +00:00
|
|
|
|
2020-01-24 14:56:53 +00:00
|
|
|
EVP_DigestInit_ex(mdctx, md, NULL);
|
2015-04-08 07:25:44 +00:00
|
|
|
foreach_array_item(xattr, xattrs) {
|
2018-10-26 05:21:52 +00:00
|
|
|
hash_len_data(mdctx, strlen(xattr->name), xattr->name);
|
|
|
|
hash_len_data(mdctx, xattr->value.len, xattr->value.ptr);
|
2015-03-11 14:10:33 +00:00
|
|
|
}
|
2018-10-26 05:21:52 +00:00
|
|
|
csum->type = EVP_MD_CTX_size(mdctx);
|
|
|
|
EVP_DigestFinal_ex(mdctx, csum->data, NULL);
|
|
|
|
EVP_MD_CTX_free(mdctx);
|
|
|
|
return;
|
|
|
|
err:
|
|
|
|
csum->type = APK_CHECKSUM_NONE;
|
2015-04-08 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void apk_fileinfo_hash_xattr(struct apk_file_info *fi)
|
|
|
|
{
|
|
|
|
apk_fileinfo_hash_xattr_array(fi->xattrs, apk_checksum_default(), &fi->xattr_csum);
|
2015-03-11 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 13:46:05 +00:00
|
|
|
int apk_fileinfo_get(int atfd, const char *filename, unsigned int flags,
|
|
|
|
struct apk_file_info *fi)
|
2008-11-14 12:26:59 +00:00
|
|
|
{
|
2009-07-31 13:08:09 +00:00
|
|
|
struct stat64 st;
|
2015-04-08 07:25:44 +00:00
|
|
|
unsigned int checksum = flags & 0xff;
|
|
|
|
unsigned int xattr_checksum = (flags >> 8) & 0xff;
|
|
|
|
int atflags = 0;
|
2008-11-14 12:26:59 +00:00
|
|
|
|
2009-08-05 10:13:52 +00:00
|
|
|
if (flags & APK_FI_NOFOLLOW)
|
|
|
|
atflags |= AT_SYMLINK_NOFOLLOW;
|
|
|
|
|
|
|
|
if (fstatat64(atfd, filename, &st, atflags) != 0)
|
2009-07-22 11:56:27 +00:00
|
|
|
return -errno;
|
2008-11-14 12:26:59 +00:00
|
|
|
|
|
|
|
*fi = (struct apk_file_info) {
|
|
|
|
.size = st.st_size,
|
|
|
|
.uid = st.st_uid,
|
|
|
|
.gid = st.st_gid,
|
|
|
|
.mode = st.st_mode,
|
|
|
|
.mtime = st.st_mtime,
|
|
|
|
.device = st.st_dev,
|
|
|
|
};
|
|
|
|
|
2015-04-08 07:25:44 +00:00
|
|
|
if (xattr_checksum != APK_CHECKSUM_NONE) {
|
|
|
|
ssize_t len, vlen;
|
|
|
|
int fd, i, r;
|
|
|
|
char val[1024], buf[1024];
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
fd = openat(atfd, filename, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
len = flistxattr(fd, buf, sizeof(buf));
|
|
|
|
if (len > 0) {
|
|
|
|
struct apk_xattr_array *xattrs = NULL;
|
|
|
|
apk_xattr_array_init(&xattrs);
|
|
|
|
for (i = 0; i < len; i += strlen(&buf[i]) + 1) {
|
|
|
|
vlen = fgetxattr(fd, &buf[i], val, sizeof(val));
|
|
|
|
if (vlen < 0) {
|
|
|
|
r = errno;
|
|
|
|
if (r == ENODATA) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*apk_xattr_array_add(&xattrs) = (struct apk_xattr) {
|
|
|
|
.name = &buf[i],
|
|
|
|
.value = *apk_blob_atomize_dup(APK_BLOB_PTR_LEN(val, vlen)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
apk_fileinfo_hash_xattr_array(xattrs, apk_checksum_evp(xattr_checksum), &fi->xattr_csum);
|
|
|
|
apk_xattr_array_free(&xattrs);
|
2015-04-08 08:16:46 +00:00
|
|
|
} else if (r < 0) r = errno;
|
2015-04-08 07:25:44 +00:00
|
|
|
close(fd);
|
|
|
|
} else r = errno;
|
|
|
|
|
|
|
|
if (r && r != ENOTSUP) return -r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checksum == APK_CHECKSUM_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode))
|
2009-07-14 16:14:05 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-04-08 07:25:44 +00:00
|
|
|
/* Checksum file content */
|
2009-08-11 16:02:22 +00:00
|
|
|
if ((flags & APK_FI_NOFOLLOW) && S_ISLNK(st.st_mode)) {
|
|
|
|
char *target = alloca(st.st_size);
|
|
|
|
if (target == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (readlinkat(atfd, filename, target, st.st_size) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
EVP_Digest(target, st.st_size, fi->csum.data, NULL,
|
|
|
|
apk_checksum_evp(checksum), NULL);
|
|
|
|
fi->csum.type = checksum;
|
2015-04-08 07:25:44 +00:00
|
|
|
} else {
|
2020-01-11 01:23:22 +00:00
|
|
|
struct apk_istream *is = apk_istream_from_file(atfd, filename);
|
|
|
|
if (!IS_ERR_OR_NULL(is)) {
|
2018-10-26 05:21:52 +00:00
|
|
|
EVP_MD_CTX *mdctx;
|
2015-04-08 07:25:44 +00:00
|
|
|
apk_blob_t blob;
|
|
|
|
|
2018-10-26 05:21:52 +00:00
|
|
|
mdctx = EVP_MD_CTX_new();
|
|
|
|
if (mdctx) {
|
|
|
|
EVP_DigestInit_ex(mdctx, apk_checksum_evp(checksum), NULL);
|
2020-01-11 01:23:22 +00:00
|
|
|
if (is->flags & APK_ISTREAM_SINGLE_READ)
|
2018-10-26 05:21:52 +00:00
|
|
|
EVP_MD_CTX_set_flags(mdctx, EVP_MD_CTX_FLAG_ONESHOT);
|
2020-01-11 01:23:22 +00:00
|
|
|
while (!APK_BLOB_IS_NULL(blob = apk_istream_get_all(is)))
|
2018-10-26 05:21:52 +00:00
|
|
|
EVP_DigestUpdate(mdctx, (void*) blob.ptr, blob.len);
|
|
|
|
fi->csum.type = EVP_MD_CTX_size(mdctx);
|
|
|
|
EVP_DigestFinal_ex(mdctx, fi->csum.data, NULL);
|
|
|
|
EVP_MD_CTX_free(mdctx);
|
|
|
|
}
|
2020-01-11 01:23:22 +00:00
|
|
|
apk_istream_close(is);
|
2015-04-08 07:25:44 +00:00
|
|
|
}
|
2009-07-13 17:37:03 +00:00
|
|
|
}
|
2008-11-14 12:26:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-11-27 19:06:45 +00:00
|
|
|
|
2015-03-10 13:46:05 +00:00
|
|
|
void apk_fileinfo_free(struct apk_file_info *fi)
|
2015-03-10 12:38:06 +00:00
|
|
|
{
|
|
|
|
apk_xattr_array_free(&fi->xattrs);
|
|
|
|
}
|
|
|
|
|
2012-02-22 06:45:40 +00:00
|
|
|
int apk_dir_foreach_file(int dirfd, apk_dir_file_cb cb, void *ctx)
|
|
|
|
{
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *dir;
|
2017-02-02 23:37:23 +00:00
|
|
|
int ret = 0;
|
2012-02-22 06:45:40 +00:00
|
|
|
|
|
|
|
if (dirfd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
dir = fdopendir(dirfd);
|
|
|
|
if (dir == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* We get called here with dup():ed fd. Since they all refer to
|
|
|
|
* same object, we need to rewind so subsequent calls work. */
|
|
|
|
rewinddir(dir);
|
|
|
|
|
|
|
|
while ((de = readdir(dir)) != NULL) {
|
2012-02-23 13:05:06 +00:00
|
|
|
if (de->d_name[0] == '.') {
|
|
|
|
if (de->d_name[1] == 0 ||
|
|
|
|
(de->d_name[1] == '.' && de->d_name[2] == 0))
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-02 23:37:23 +00:00
|
|
|
ret = cb(ctx, dirfd, de->d_name);
|
|
|
|
if (ret) break;
|
2012-02-22 06:45:40 +00:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2017-02-02 23:37:23 +00:00
|
|
|
return ret;
|
2012-02-22 06:45:40 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 13:08:09 +00:00
|
|
|
struct apk_istream *apk_istream_from_file_gz(int atfd, const char *file)
|
2008-11-27 19:06:45 +00:00
|
|
|
{
|
2020-01-11 07:16:38 +00:00
|
|
|
return apk_istream_gunzip(apk_istream_from_file(atfd, file));
|
2008-11-28 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct apk_fd_ostream {
|
|
|
|
struct apk_ostream os;
|
2009-08-12 08:05:09 +00:00
|
|
|
int fd, rc;
|
|
|
|
|
|
|
|
const char *file, *tmpfile;
|
|
|
|
int atfd;
|
|
|
|
|
2009-03-17 12:17:35 +00:00
|
|
|
size_t bytes;
|
|
|
|
char buffer[1024];
|
2008-11-28 14:28:54 +00:00
|
|
|
};
|
|
|
|
|
2009-07-22 11:56:27 +00:00
|
|
|
static ssize_t safe_write(int fd, const void *ptr, size_t size)
|
2008-11-28 14:28:54 +00:00
|
|
|
{
|
2009-07-22 11:56:27 +00:00
|
|
|
ssize_t i = 0, r;
|
2008-11-28 14:28:54 +00:00
|
|
|
|
|
|
|
while (i < size) {
|
2009-03-17 12:17:35 +00:00
|
|
|
r = write(fd, ptr + i, size - i);
|
2008-11-28 14:28:54 +00:00
|
|
|
if (r < 0)
|
2009-07-22 11:56:27 +00:00
|
|
|
return -errno;
|
2008-11-28 14:28:54 +00:00
|
|
|
if (r == 0)
|
|
|
|
return i;
|
|
|
|
i += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-07-22 11:56:27 +00:00
|
|
|
static ssize_t fdo_flush(struct apk_fd_ostream *fos)
|
2009-03-17 12:17:35 +00:00
|
|
|
{
|
2009-07-22 11:56:27 +00:00
|
|
|
ssize_t r;
|
|
|
|
|
2009-03-17 12:17:35 +00:00
|
|
|
if (fos->bytes == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-08-12 08:05:09 +00:00
|
|
|
if ((r = safe_write(fos->fd, fos->buffer, fos->bytes)) != fos->bytes) {
|
|
|
|
fos->rc = r < 0 ? r : -EIO;
|
2009-07-22 11:56:27 +00:00
|
|
|
return r;
|
2009-08-12 08:05:09 +00:00
|
|
|
}
|
2009-03-17 12:17:35 +00:00
|
|
|
|
|
|
|
fos->bytes = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static ssize_t fdo_write(struct apk_ostream *os, const void *ptr, size_t size)
|
2009-03-17 12:17:35 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fd_ostream *fos = container_of(os, struct apk_fd_ostream, os);
|
2009-07-22 11:56:27 +00:00
|
|
|
ssize_t r;
|
2009-03-17 12:17:35 +00:00
|
|
|
|
|
|
|
if (size + fos->bytes >= sizeof(fos->buffer)) {
|
2009-07-22 11:56:27 +00:00
|
|
|
r = fdo_flush(fos);
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
2009-08-12 08:05:09 +00:00
|
|
|
if (size >= sizeof(fos->buffer) / 2) {
|
|
|
|
r = safe_write(fos->fd, ptr, size);
|
|
|
|
if (r != size)
|
|
|
|
fos->rc = r < 0 ? r : -EIO;
|
|
|
|
return r;
|
|
|
|
}
|
2009-03-17 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&fos->buffer[fos->bytes], ptr, size);
|
|
|
|
fos->bytes += size;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static int fdo_close(struct apk_ostream *os)
|
2008-11-28 14:28:54 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fd_ostream *fos = container_of(os, struct apk_fd_ostream, os);
|
2015-05-19 18:15:15 +00:00
|
|
|
int rc;
|
2008-11-28 14:28:54 +00:00
|
|
|
|
2009-03-17 12:17:35 +00:00
|
|
|
fdo_flush(fos);
|
2015-05-19 18:15:15 +00:00
|
|
|
rc = fos->rc;
|
|
|
|
|
2009-08-12 08:05:09 +00:00
|
|
|
if (fos->fd > STDERR_FILENO &&
|
|
|
|
close(fos->fd) < 0)
|
|
|
|
rc = -errno;
|
|
|
|
|
|
|
|
if (fos->tmpfile != NULL) {
|
|
|
|
if (rc == 0)
|
|
|
|
renameat(fos->atfd, fos->tmpfile,
|
|
|
|
fos->atfd, fos->file);
|
|
|
|
else
|
|
|
|
unlinkat(fos->atfd, fos->tmpfile, 0);
|
|
|
|
}
|
|
|
|
|
2008-11-28 14:28:54 +00:00
|
|
|
free(fos);
|
2009-08-12 08:05:09 +00:00
|
|
|
|
|
|
|
return rc;
|
2008-11-28 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
static const struct apk_ostream_ops fd_ostream_ops = {
|
|
|
|
.write = fdo_write,
|
|
|
|
.close = fdo_close,
|
|
|
|
};
|
|
|
|
|
2008-11-28 14:28:54 +00:00
|
|
|
struct apk_ostream *apk_ostream_to_fd(int fd)
|
|
|
|
{
|
|
|
|
struct apk_fd_ostream *fos;
|
|
|
|
|
2015-03-10 11:15:58 +00:00
|
|
|
if (fd < 0) return ERR_PTR(-EBADF);
|
2008-11-28 14:28:54 +00:00
|
|
|
|
|
|
|
fos = malloc(sizeof(struct apk_fd_ostream));
|
2009-08-12 08:05:09 +00:00
|
|
|
if (fos == NULL) {
|
|
|
|
close(fd);
|
2015-03-10 11:15:58 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2009-08-12 08:05:09 +00:00
|
|
|
}
|
2008-11-28 14:28:54 +00:00
|
|
|
|
|
|
|
*fos = (struct apk_fd_ostream) {
|
2017-06-21 13:07:58 +00:00
|
|
|
.os.ops = &fd_ostream_ops,
|
2008-11-28 14:28:54 +00:00
|
|
|
.fd = fd,
|
|
|
|
};
|
|
|
|
|
|
|
|
return &fos->os;
|
|
|
|
}
|
|
|
|
|
2009-08-12 08:05:09 +00:00
|
|
|
struct apk_ostream *apk_ostream_to_file(int atfd,
|
|
|
|
const char *file,
|
|
|
|
const char *tmpfile,
|
|
|
|
mode_t mode)
|
2008-11-28 14:28:54 +00:00
|
|
|
{
|
2009-08-12 08:05:09 +00:00
|
|
|
struct apk_ostream *os;
|
2008-11-28 14:28:54 +00:00
|
|
|
int fd;
|
|
|
|
|
2010-06-11 10:42:21 +00:00
|
|
|
fd = openat(atfd, tmpfile ?: file, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, mode);
|
2015-03-10 11:15:58 +00:00
|
|
|
if (fd < 0) return ERR_PTR(-errno);
|
2008-11-28 14:28:54 +00:00
|
|
|
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2009-08-12 08:05:09 +00:00
|
|
|
os = apk_ostream_to_fd(fd);
|
2015-03-10 11:15:58 +00:00
|
|
|
if (IS_ERR_OR_NULL(os)) return ERR_CAST(os);
|
2009-08-12 08:05:09 +00:00
|
|
|
|
|
|
|
if (tmpfile != NULL) {
|
|
|
|
struct apk_fd_ostream *fos =
|
|
|
|
container_of(os, struct apk_fd_ostream, os);
|
|
|
|
fos->file = file;
|
|
|
|
fos->tmpfile = tmpfile;
|
|
|
|
fos->atfd = atfd;
|
|
|
|
}
|
|
|
|
return os;
|
2008-11-27 19:06:45 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 12:16:05 +00:00
|
|
|
struct apk_counter_ostream {
|
|
|
|
struct apk_ostream os;
|
|
|
|
off_t *counter;
|
|
|
|
};
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static ssize_t co_write(struct apk_ostream *os, const void *ptr, size_t size)
|
2009-07-16 12:16:05 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_counter_ostream *cos = container_of(os, struct apk_counter_ostream, os);
|
2009-07-16 12:16:05 +00:00
|
|
|
|
|
|
|
*cos->counter += size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static int co_close(struct apk_ostream *os)
|
2009-07-16 12:16:05 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_counter_ostream *cos = container_of(os, struct apk_counter_ostream, os);
|
2009-07-16 12:16:05 +00:00
|
|
|
|
|
|
|
free(cos);
|
2009-08-12 08:05:09 +00:00
|
|
|
return 0;
|
2009-07-16 12:16:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
static const struct apk_ostream_ops counter_ostream_ops = {
|
|
|
|
.write = co_write,
|
|
|
|
.close = co_close,
|
|
|
|
};
|
|
|
|
|
2009-07-16 12:16:05 +00:00
|
|
|
struct apk_ostream *apk_ostream_counter(off_t *counter)
|
|
|
|
{
|
|
|
|
struct apk_counter_ostream *cos;
|
|
|
|
|
|
|
|
cos = malloc(sizeof(struct apk_counter_ostream));
|
|
|
|
if (cos == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*cos = (struct apk_counter_ostream) {
|
2017-06-21 13:07:58 +00:00
|
|
|
.os.ops = &counter_ostream_ops,
|
2009-07-16 12:16:05 +00:00
|
|
|
.counter = counter,
|
|
|
|
};
|
|
|
|
|
|
|
|
return &cos->os;
|
|
|
|
}
|
|
|
|
|
2009-04-16 14:05:27 +00:00
|
|
|
size_t apk_ostream_write_string(struct apk_ostream *os, const char *string)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(string);
|
2017-06-21 13:07:58 +00:00
|
|
|
if (apk_ostream_write(os, string, len) != len)
|
2009-04-16 14:05:27 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2010-06-12 10:43:29 +00:00
|
|
|
|
|
|
|
struct cache_item {
|
|
|
|
apk_hash_node hash_node;
|
|
|
|
unsigned int genid;
|
|
|
|
union {
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
};
|
|
|
|
unsigned short len;
|
|
|
|
char name[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static apk_blob_t cache_item_get_key(apk_hash_item item)
|
|
|
|
{
|
|
|
|
struct cache_item *ci = (struct cache_item *) item;
|
|
|
|
return APK_BLOB_PTR_LEN(ci->name, ci->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct apk_hash_ops id_hash_ops = {
|
|
|
|
.node_offset = offsetof(struct cache_item, hash_node),
|
|
|
|
.get_key = cache_item_get_key,
|
|
|
|
.hash_key = apk_blob_hash,
|
|
|
|
.compare = apk_blob_compare,
|
|
|
|
.delete_item = (apk_hash_delete_f) free,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cache_item *resolve_cache_item(struct apk_hash *hash, apk_blob_t name)
|
|
|
|
{
|
|
|
|
struct cache_item *ci;
|
|
|
|
unsigned long h;
|
|
|
|
|
|
|
|
h = id_hash_ops.hash_key(name);
|
|
|
|
ci = (struct cache_item *) apk_hash_get_hashed(hash, name, h);
|
|
|
|
if (ci != NULL)
|
|
|
|
return ci;
|
|
|
|
|
|
|
|
ci = calloc(1, sizeof(struct cache_item) + name.len);
|
|
|
|
if (ci == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ci->len = name.len;
|
|
|
|
memcpy(ci->name, name.ptr, name.len);
|
|
|
|
apk_hash_insert_hashed(hash, ci, h);
|
|
|
|
|
|
|
|
return ci;
|
|
|
|
}
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
void apk_id_cache_init(struct apk_id_cache *idc, int root_fd)
|
2010-06-12 10:43:29 +00:00
|
|
|
{
|
2010-10-08 12:36:54 +00:00
|
|
|
idc->root_fd = root_fd;
|
|
|
|
idc->genid = 1;
|
|
|
|
apk_hash_init(&idc->uid_cache, &id_hash_ops, 256);
|
|
|
|
apk_hash_init(&idc->gid_cache, &id_hash_ops, 256);
|
2010-06-12 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
void apk_id_cache_free(struct apk_id_cache *idc)
|
2010-06-12 10:43:29 +00:00
|
|
|
{
|
2010-10-08 12:36:54 +00:00
|
|
|
apk_hash_free(&idc->uid_cache);
|
|
|
|
apk_hash_free(&idc->gid_cache);
|
2010-06-12 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
void apk_id_cache_reset(struct apk_id_cache *idc)
|
2010-06-12 10:43:29 +00:00
|
|
|
{
|
2010-10-08 12:36:54 +00:00
|
|
|
idc->genid++;
|
|
|
|
if (idc->genid == 0)
|
|
|
|
idc->genid = 1;
|
2010-06-12 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
uid_t apk_resolve_uid(struct apk_id_cache *idc, const char *username, uid_t default_uid)
|
2010-06-12 10:43:29 +00:00
|
|
|
{
|
2013-07-04 07:36:47 +00:00
|
|
|
#ifdef HAVE_FGETPWENT_R
|
2010-06-12 10:43:29 +00:00
|
|
|
char buf[1024];
|
2013-07-04 07:36:47 +00:00
|
|
|
struct passwd pwent;
|
|
|
|
#endif
|
|
|
|
struct cache_item *ci;
|
|
|
|
struct passwd *pwd;
|
2010-10-08 12:36:54 +00:00
|
|
|
FILE *in;
|
2010-06-12 10:43:29 +00:00
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
ci = resolve_cache_item(&idc->uid_cache, APK_BLOB_STR(username));
|
2010-06-12 10:43:29 +00:00
|
|
|
if (ci == NULL)
|
|
|
|
return default_uid;
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
if (ci->genid != idc->genid) {
|
|
|
|
ci->genid = idc->genid;
|
|
|
|
ci->uid = -1;
|
|
|
|
|
|
|
|
in = fdopen(openat(idc->root_fd, "etc/passwd", O_RDONLY|O_CLOEXEC), "r");
|
|
|
|
if (in != NULL) {
|
|
|
|
do {
|
2013-07-04 07:36:47 +00:00
|
|
|
#ifdef HAVE_FGETPWENT_R
|
2010-10-08 12:36:54 +00:00
|
|
|
fgetpwent_r(in, &pwent, buf, sizeof(buf), &pwd);
|
2013-07-04 07:36:47 +00:00
|
|
|
#else
|
|
|
|
pwd = fgetpwent(in);
|
|
|
|
#endif
|
2010-10-08 12:36:54 +00:00
|
|
|
if (pwd == NULL)
|
|
|
|
break;
|
|
|
|
if (strcmp(pwd->pw_name, username) == 0) {
|
|
|
|
ci->uid = pwd->pw_uid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
fclose(in);
|
|
|
|
}
|
2010-06-12 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->uid != -1)
|
|
|
|
return ci->uid;
|
|
|
|
|
|
|
|
return default_uid;
|
|
|
|
}
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
uid_t apk_resolve_gid(struct apk_id_cache *idc, const char *groupname, uid_t default_gid)
|
2010-06-12 10:43:29 +00:00
|
|
|
{
|
2013-07-04 07:36:47 +00:00
|
|
|
#ifdef HAVE_FGETGRENT_R
|
2010-06-12 10:43:29 +00:00
|
|
|
char buf[1024];
|
2013-07-04 07:36:47 +00:00
|
|
|
struct group grent;
|
|
|
|
#endif
|
|
|
|
struct cache_item *ci;
|
|
|
|
struct group *grp;
|
2010-10-08 12:36:54 +00:00
|
|
|
FILE *in;
|
2010-06-12 10:43:29 +00:00
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
ci = resolve_cache_item(&idc->gid_cache, APK_BLOB_STR(groupname));
|
2010-06-12 10:43:29 +00:00
|
|
|
if (ci == NULL)
|
|
|
|
return default_gid;
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
if (ci->genid != idc->genid) {
|
|
|
|
ci->genid = idc->genid;
|
|
|
|
ci->gid = -1;
|
|
|
|
|
2010-10-08 13:09:03 +00:00
|
|
|
in = fdopen(openat(idc->root_fd, "etc/group", O_RDONLY|O_CLOEXEC), "r");
|
2010-10-08 12:36:54 +00:00
|
|
|
if (in != NULL) {
|
|
|
|
do {
|
2013-07-04 07:36:47 +00:00
|
|
|
#ifdef HAVE_FGETGRENT_R
|
2010-10-08 12:36:54 +00:00
|
|
|
fgetgrent_r(in, &grent, buf, sizeof(buf), &grp);
|
2013-07-04 07:36:47 +00:00
|
|
|
#else
|
|
|
|
grp = fgetgrent(in);
|
|
|
|
#endif
|
2010-10-08 12:36:54 +00:00
|
|
|
if (grp == NULL)
|
|
|
|
break;
|
|
|
|
if (strcmp(grp->gr_name, groupname) == 0) {
|
|
|
|
ci->gid = grp->gr_gid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
fclose(in);
|
|
|
|
}
|
2010-06-12 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->gid != -1)
|
|
|
|
return ci->gid;
|
|
|
|
|
|
|
|
return default_gid;
|
|
|
|
}
|