2008-11-07 15:11:08 +00:00
|
|
|
/* apk_io.h - Alpine Package Keeper (APK)
|
|
|
|
*
|
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.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2008-11-07 15:11:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef APK_IO
|
|
|
|
#define APK_IO
|
|
|
|
|
2013-06-12 15:43:56 +00:00
|
|
|
#include <sys/types.h>
|
2013-06-18 07:52:23 +00:00
|
|
|
#include <fcntl.h>
|
2014-10-08 08:13:21 +00:00
|
|
|
#include <time.h>
|
2009-07-14 16:14:05 +00:00
|
|
|
|
2008-11-07 15:11:08 +00:00
|
|
|
#include "apk_defines.h"
|
|
|
|
#include "apk_blob.h"
|
2020-05-19 08:39:21 +00:00
|
|
|
#include "apk_atom.h"
|
2021-06-09 15:21:40 +00:00
|
|
|
#include "apk_crypto.h"
|
2010-10-08 12:36:54 +00:00
|
|
|
|
2021-06-21 09:55:32 +00:00
|
|
|
ssize_t apk_write_fully(int fd, const void *ptr, size_t size);
|
|
|
|
|
2021-06-07 16:49:15 +00:00
|
|
|
struct apk_id_hash {
|
|
|
|
int empty;
|
|
|
|
struct hlist_head by_id[16], by_name[16];
|
|
|
|
};
|
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
struct apk_id_cache {
|
|
|
|
int root_fd;
|
2021-06-07 16:49:15 +00:00
|
|
|
struct apk_id_hash uid_cache;
|
|
|
|
struct apk_id_hash gid_cache;
|
2010-10-08 12:36:54 +00:00
|
|
|
};
|
2008-11-07 15:11:08 +00:00
|
|
|
|
2015-03-10 12:38:06 +00:00
|
|
|
struct apk_xattr {
|
|
|
|
const char *name;
|
|
|
|
apk_blob_t value;
|
|
|
|
};
|
|
|
|
APK_ARRAY(apk_xattr_array, struct apk_xattr);
|
|
|
|
|
2015-11-09 10:47:23 +00:00
|
|
|
struct apk_file_meta {
|
|
|
|
time_t mtime, atime;
|
|
|
|
};
|
|
|
|
|
2008-11-14 12:26:59 +00:00
|
|
|
struct apk_file_info {
|
2021-06-09 20:12:52 +00:00
|
|
|
const char *name;
|
|
|
|
const char *link_target;
|
|
|
|
const char *uname;
|
|
|
|
const char *gname;
|
2008-11-14 12:26:59 +00:00
|
|
|
off_t size;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
mode_t mode;
|
|
|
|
time_t mtime;
|
|
|
|
dev_t device;
|
2021-06-09 15:21:40 +00:00
|
|
|
struct apk_digest digest;
|
|
|
|
struct apk_digest xattr_digest;
|
2015-03-10 12:38:06 +00:00
|
|
|
struct apk_xattr_array *xattrs;
|
2008-11-14 12:26:59 +00:00
|
|
|
};
|
|
|
|
|
2020-01-10 09:02:48 +00:00
|
|
|
extern size_t apk_io_bufsize;
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_istream;
|
|
|
|
struct apk_ostream;
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
struct apk_istream_ops {
|
2019-12-18 08:00:29 +00:00
|
|
|
void (*get_meta)(struct apk_istream *is, struct apk_file_meta *meta);
|
|
|
|
ssize_t (*read)(struct apk_istream *is, void *ptr, size_t size);
|
2021-01-07 16:21:36 +00:00
|
|
|
int (*close)(struct apk_istream *is);
|
2008-11-07 15:11:08 +00:00
|
|
|
};
|
|
|
|
|
2020-01-10 09:02:48 +00:00
|
|
|
#define APK_ISTREAM_SINGLE_READ 0x0001
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
struct apk_istream {
|
2020-01-10 09:02:48 +00:00
|
|
|
uint8_t *ptr, *end, *buf;
|
|
|
|
size_t buf_size;
|
|
|
|
int err;
|
|
|
|
unsigned int flags;
|
2017-06-21 13:07:58 +00:00
|
|
|
const struct apk_istream_ops *ops;
|
|
|
|
};
|
|
|
|
|
2021-06-09 20:12:52 +00:00
|
|
|
typedef int (*apk_archive_entry_parser)(void *ctx,
|
|
|
|
const struct apk_file_info *ae,
|
|
|
|
struct apk_istream *istream);
|
|
|
|
|
2019-12-18 10:58:32 +00:00
|
|
|
#define APK_IO_ALL ((size_t)-1)
|
|
|
|
|
2020-10-05 12:59:00 +00:00
|
|
|
#define APK_ISTREAM_FORCE_REFRESH ((time_t) -1)
|
|
|
|
|
2021-07-16 16:01:41 +00:00
|
|
|
struct apk_istream *apk_istream_from_blob(struct apk_istream *, apk_blob_t);
|
|
|
|
struct apk_istream *__apk_istream_from_file(int atfd, const char *file, int try_mmap);
|
|
|
|
static inline struct apk_istream *apk_istream_from_file(int atfd, const char *file) { return __apk_istream_from_file(atfd, file, 0); }
|
|
|
|
static inline struct apk_istream *apk_istream_from_file_mmap(int atfd, const char *file) { return __apk_istream_from_file(atfd, file, 1); }
|
2020-01-10 09:02:48 +00:00
|
|
|
struct apk_istream *apk_istream_from_fd(int fd);
|
|
|
|
struct apk_istream *apk_istream_from_fd_url_if_modified(int atfd, const char *url, time_t since);
|
2021-07-17 20:21:16 +00:00
|
|
|
static inline int apk_istream_error(struct apk_istream *is, int err) { if (is->err >= 0 && err) is->err = err; return is->err < 0 ? is->err : 0; }
|
2021-07-16 16:01:41 +00:00
|
|
|
apk_blob_t apk_istream_mmap(struct apk_istream *is);
|
2021-07-17 20:21:16 +00:00
|
|
|
ssize_t apk_istream_read_max(struct apk_istream *is, void *ptr, size_t size);
|
|
|
|
int apk_istream_read(struct apk_istream *is, void *ptr, size_t size);
|
2021-07-16 13:31:59 +00:00
|
|
|
void *apk_istream_peek(struct apk_istream *is, size_t len);
|
2021-06-09 20:12:52 +00:00
|
|
|
void *apk_istream_get(struct apk_istream *is, size_t len);
|
2021-07-17 12:43:08 +00:00
|
|
|
int apk_istream_get_max(struct apk_istream *is, size_t size, apk_blob_t *data);
|
|
|
|
int apk_istream_get_delim(struct apk_istream *is, apk_blob_t token, apk_blob_t *data);
|
|
|
|
static inline int apk_istream_get_all(struct apk_istream *is, apk_blob_t *data) { return apk_istream_get_max(is, APK_IO_ALL, data); }
|
2019-12-18 10:58:32 +00:00
|
|
|
ssize_t apk_stream_copy(struct apk_istream *is, struct apk_ostream *os, size_t size,
|
2021-06-09 15:21:40 +00:00
|
|
|
apk_progress_cb cb, void *cb_ctx, struct apk_digest_ctx *dctx);
|
2020-01-10 09:02:48 +00:00
|
|
|
|
2020-10-05 12:59:00 +00:00
|
|
|
static inline struct apk_istream *apk_istream_from_url(const char *url, time_t since)
|
2020-01-10 09:02:48 +00:00
|
|
|
{
|
2020-10-05 12:59:00 +00:00
|
|
|
return apk_istream_from_fd_url_if_modified(AT_FDCWD, url, since);
|
2020-01-10 09:02:48 +00:00
|
|
|
}
|
2020-10-05 12:59:00 +00:00
|
|
|
static inline struct apk_istream *apk_istream_from_fd_url(int atfd, const char *url, time_t since)
|
2020-01-10 09:02:48 +00:00
|
|
|
{
|
2020-10-05 12:59:00 +00:00
|
|
|
return apk_istream_from_fd_url_if_modified(atfd, url, since);
|
2020-01-10 09:02:48 +00:00
|
|
|
}
|
|
|
|
static inline void apk_istream_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
|
|
|
|
{
|
|
|
|
is->ops->get_meta(is, meta);
|
|
|
|
}
|
2021-01-07 16:21:36 +00:00
|
|
|
static inline int apk_istream_close(struct apk_istream *is)
|
2020-01-10 09:02:48 +00:00
|
|
|
{
|
2021-01-07 16:21:36 +00:00
|
|
|
return is->ops->close(is);
|
2020-01-10 09:02:48 +00:00
|
|
|
}
|
2021-07-17 14:57:07 +00:00
|
|
|
static inline int apk_istream_close_error(struct apk_istream *is, int r)
|
|
|
|
{
|
|
|
|
if (r < 0) apk_istream_error(is, r);
|
|
|
|
return apk_istream_close(is);
|
|
|
|
}
|
2020-01-10 09:02:48 +00:00
|
|
|
|
2020-01-11 01:23:22 +00:00
|
|
|
struct apk_segment_istream {
|
|
|
|
struct apk_istream is;
|
|
|
|
struct apk_istream *pis;
|
|
|
|
size_t bytes_left;
|
|
|
|
time_t mtime;
|
|
|
|
};
|
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);
|
2021-07-17 14:57:07 +00:00
|
|
|
|
2021-07-30 13:38:53 +00:00
|
|
|
struct apk_digest_istream {
|
|
|
|
struct apk_istream is;
|
|
|
|
struct apk_istream *pis;
|
|
|
|
struct apk_digest *digest;
|
|
|
|
struct apk_digest_ctx dctx;
|
2021-11-05 11:20:19 +00:00
|
|
|
off_t size_left;
|
2021-07-30 13:38:53 +00:00
|
|
|
};
|
2021-11-05 11:20:19 +00:00
|
|
|
struct apk_istream *apk_istream_verify(struct apk_digest_istream *dis, struct apk_istream *is, off_t size, struct apk_digest *d);
|
2021-07-30 13:38:53 +00:00
|
|
|
|
2021-07-17 14:57:07 +00:00
|
|
|
#define APK_ISTREAM_TEE_COPY_META 1
|
|
|
|
#define APK_ISTREAM_TEE_OPTIONAL 2
|
|
|
|
|
|
|
|
struct apk_istream *apk_istream_tee(struct apk_istream *from, struct apk_ostream *to, int copy_meta,
|
2020-01-11 07:16:38 +00:00
|
|
|
apk_progress_cb cb, void *cb_ctx);
|
2020-01-11 01:23:22 +00:00
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
struct apk_ostream_ops {
|
2021-07-17 14:57:07 +00:00
|
|
|
void (*set_meta)(struct apk_ostream *os, struct apk_file_meta *meta);
|
2021-07-17 16:17:46 +00:00
|
|
|
int (*write)(struct apk_ostream *os, const void *buf, size_t size);
|
2019-12-18 08:00:29 +00:00
|
|
|
int (*close)(struct apk_ostream *os);
|
2008-11-28 14:28:54 +00:00
|
|
|
};
|
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
struct apk_ostream {
|
|
|
|
const struct apk_ostream_ops *ops;
|
2020-09-30 11:11:37 +00:00
|
|
|
int rc;
|
2017-06-21 13:07:58 +00:00
|
|
|
};
|
|
|
|
|
2009-07-16 12:16:05 +00:00
|
|
|
struct apk_ostream *apk_ostream_counter(off_t *);
|
2008-11-28 14:28:54 +00:00
|
|
|
struct apk_ostream *apk_ostream_to_fd(int fd);
|
2020-10-02 12:25:12 +00:00
|
|
|
struct apk_ostream *apk_ostream_to_file(int atfd, const char *file, mode_t mode);
|
2021-07-17 16:17:46 +00:00
|
|
|
ssize_t apk_ostream_write_string(struct apk_ostream *os, const char *string);
|
2021-07-17 14:57:07 +00:00
|
|
|
void apk_ostream_copy_meta(struct apk_ostream *os, struct apk_istream *is);
|
2021-06-07 16:49:15 +00:00
|
|
|
static inline int apk_ostream_error(struct apk_ostream *os) { return os->rc; }
|
2020-10-05 15:52:51 +00:00
|
|
|
static inline int apk_ostream_cancel(struct apk_ostream *os, int rc) { if (!os->rc) os->rc = rc; return rc; }
|
2021-07-17 16:17:46 +00:00
|
|
|
static inline int apk_ostream_write(struct apk_ostream *os, const void *buf, size_t size) {
|
2017-06-21 13:07:58 +00:00
|
|
|
return os->ops->write(os, buf, size);
|
|
|
|
}
|
|
|
|
static inline int apk_ostream_close(struct apk_ostream *os)
|
|
|
|
{
|
2020-09-30 11:11:37 +00:00
|
|
|
int rc = os->rc;
|
|
|
|
return os->ops->close(os) ?: rc;
|
2017-06-21 13:07:58 +00:00
|
|
|
}
|
2008-11-27 19:06:45 +00:00
|
|
|
|
2008-11-07 15:11:08 +00:00
|
|
|
apk_blob_t apk_blob_from_istream(struct apk_istream *istream, size_t size);
|
2009-07-31 13:08:09 +00:00
|
|
|
apk_blob_t apk_blob_from_file(int atfd, const char *file);
|
2008-11-07 15:11:08 +00:00
|
|
|
|
2012-02-23 15:04:51 +00:00
|
|
|
#define APK_BTF_ADD_EOL 0x00000001
|
|
|
|
int apk_blob_to_file(int atfd, const char *file, apk_blob_t b, unsigned int flags);
|
2012-02-22 06:45:40 +00:00
|
|
|
|
2009-08-05 10:13:52 +00:00
|
|
|
#define APK_FI_NOFOLLOW 0x80000000
|
2021-06-09 15:21:40 +00:00
|
|
|
#define APK_FI_XATTR_DIGEST(x) (((x) & 0xff) << 8)
|
2021-06-09 20:12:52 +00:00
|
|
|
#define APK_FI_XATTR_CSUM(x) APK_FI_XATTR_DIGEST(apk_digest_alg_by_len(x))
|
2021-06-09 15:21:40 +00:00
|
|
|
#define APK_FI_DIGEST(x) (((x) & 0xff))
|
2021-06-09 20:12:52 +00:00
|
|
|
#define APK_FI_CSUM(x) APK_FI_DIGEST(apk_digest_alg_by_len(x))
|
2015-03-10 13:46:05 +00:00
|
|
|
int apk_fileinfo_get(int atfd, const char *filename, unsigned int flags,
|
2020-05-19 08:39:21 +00:00
|
|
|
struct apk_file_info *fi, struct apk_atom_pool *atoms);
|
2021-06-09 15:21:40 +00:00
|
|
|
void apk_fileinfo_hash_xattr(struct apk_file_info *fi, uint8_t alg);
|
2015-03-10 13:46:05 +00:00
|
|
|
void apk_fileinfo_free(struct apk_file_info *fi);
|
2012-02-23 15:04:51 +00:00
|
|
|
|
|
|
|
typedef int apk_dir_file_cb(void *ctx, int dirfd, const char *entry);
|
2012-02-22 06:45:40 +00:00
|
|
|
int apk_dir_foreach_file(int dirfd, apk_dir_file_cb cb, void *ctx);
|
2012-02-23 15:04:51 +00:00
|
|
|
|
2009-04-16 17:05:22 +00:00
|
|
|
const char *apk_url_local_file(const char *url);
|
2009-04-15 06:56:09 +00:00
|
|
|
|
2010-10-08 12:36:54 +00:00
|
|
|
void apk_id_cache_init(struct apk_id_cache *idc, int root_fd);
|
|
|
|
void apk_id_cache_free(struct apk_id_cache *idc);
|
|
|
|
void apk_id_cache_reset(struct apk_id_cache *idc);
|
2021-06-07 16:49:15 +00:00
|
|
|
uid_t apk_id_cache_resolve_uid(struct apk_id_cache *idc, apk_blob_t username, uid_t default_uid);
|
|
|
|
gid_t apk_id_cache_resolve_gid(struct apk_id_cache *idc, apk_blob_t groupname, gid_t default_gid);
|
|
|
|
apk_blob_t apk_id_cache_resolve_user(struct apk_id_cache *idc, uid_t uid);
|
|
|
|
apk_blob_t apk_id_cache_resolve_group(struct apk_id_cache *idc, gid_t gid);
|
2010-06-12 10:43:29 +00:00
|
|
|
|
2021-07-16 13:31:59 +00:00
|
|
|
// Gzip support
|
|
|
|
|
|
|
|
#define APK_MPART_DATA 1 /* data processed so far */
|
|
|
|
#define APK_MPART_BOUNDARY 2 /* final part of data, before boundary */
|
|
|
|
#define APK_MPART_END 3 /* signals end of stream */
|
|
|
|
|
|
|
|
typedef int (*apk_multipart_cb)(void *ctx, int part, apk_blob_t data);
|
|
|
|
|
|
|
|
struct apk_istream *apk_istream_zlib(struct apk_istream *, int,
|
|
|
|
apk_multipart_cb cb, void *ctx);
|
|
|
|
static inline struct apk_istream *apk_istream_gunzip_mpart(struct apk_istream *is,
|
|
|
|
apk_multipart_cb cb, void *ctx) {
|
|
|
|
return apk_istream_zlib(is, 0, cb, ctx);
|
|
|
|
}
|
|
|
|
static inline struct apk_istream *apk_istream_gunzip(struct apk_istream *is) {
|
|
|
|
return apk_istream_zlib(is, 0, NULL, NULL);
|
|
|
|
}
|
|
|
|
static inline struct apk_istream *apk_istream_deflate(struct apk_istream *is) {
|
|
|
|
return apk_istream_zlib(is, 1, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct apk_ostream *apk_ostream_zlib(struct apk_ostream *, int);
|
|
|
|
static inline struct apk_ostream *apk_ostream_gzip(struct apk_ostream *os) {
|
|
|
|
return apk_ostream_zlib(os, 0);
|
|
|
|
}
|
|
|
|
static inline struct apk_ostream *apk_ostream_deflate(struct apk_ostream *os) {
|
|
|
|
return apk_ostream_zlib(os, 1);
|
|
|
|
}
|
|
|
|
|
2008-11-07 15:11:08 +00:00
|
|
|
#endif
|