summaryrefslogtreecommitdiffstats
path: root/src/apk_io.h
blob: e95c8ed9e9120a3a31019118dde284468ae15793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* apk_io.h - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * 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.
 */

#ifndef APK_IO
#define APK_IO

#include <sys/types.h>
#include <openssl/evp.h>
#include <fcntl.h>
#include <time.h>

#include "apk_defines.h"
#include "apk_blob.h"
#include "apk_hash.h"

struct apk_id_cache {
	int root_fd;
	unsigned int genid;
	struct apk_hash uid_cache;
	struct apk_hash gid_cache;
};

struct apk_xattr {
	const char *name;
	apk_blob_t value;
};
APK_ARRAY(apk_xattr_array, struct apk_xattr);

struct apk_file_meta {
	time_t mtime, atime;
};
void apk_file_meta_to_fd(int fd, struct apk_file_meta *meta);

struct apk_file_info {
	char *name;
	char *link_target;
	char *uname;
	char *gname;
	off_t size;
	uid_t uid;
	gid_t gid;
	mode_t mode;
	time_t mtime;
	dev_t device;
	struct apk_checksum csum;
	struct apk_checksum xattr_csum;
	struct apk_xattr_array *xattrs;
};

struct apk_istream {
	void (*get_meta)(void *stream, struct apk_file_meta *meta);
	ssize_t (*read)(void *stream, void *ptr, size_t size);
	void (*close)(void *stream);
};

#define APK_BSTREAM_SINGLE_READ			0x0001
#define APK_BSTREAM_EOF				0x0002

struct apk_bstream {
	unsigned int flags;
	void (*get_meta)(void *stream, struct apk_file_meta *meta);
	apk_blob_t (*read)(void *stream, apk_blob_t token);
	void (*close)(void *stream, size_t *size);
};

struct apk_ostream {
	ssize_t (*write)(void *stream, const void *buf, size_t size);
	int (*close)(void *stream);
};

#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_bstream_gunzip_mpart(struct apk_bstream *,
					     apk_multipart_cb cb, void *ctx);
static inline struct apk_istream *apk_bstream_gunzip(struct apk_bstream *bs)
{
	return apk_bstream_gunzip_mpart(bs, NULL, NULL);
}

struct apk_ostream *apk_ostream_gzip(struct apk_ostream *);
struct apk_ostream *apk_ostream_counter(off_t *);

struct apk_istream *apk_istream_from_fd_pid(int fd, pid_t pid, int (*translate_status)(int));
struct apk_istream *apk_istream_from_file(int atfd, const char *file);
struct apk_istream *apk_istream_from_file_gz(int atfd, const char *file);
struct apk_istream *apk_istream_from_fd_url_if_modified(int atfd, const char *url, time_t since);
struct apk_istream *apk_istream_from_url_gz(const char *url);
ssize_t apk_istream_skip(struct apk_istream *istream, size_t size);

#define APK_SPLICE_ALL 0xffffffff
ssize_t apk_istream_splice(void *stream, int fd, size_t size,
			   apk_progress_cb cb, void *cb_ctx);

static inline struct apk_istream *apk_istream_from_fd(int fd)
{
	return apk_istream_from_fd_pid(fd, 0, NULL);
}
static inline struct apk_istream *apk_istream_from_url(const char *url)
{
	return apk_istream_from_fd_url_if_modified(AT_FDCWD, url, 0);
}
static inline struct apk_istream *apk_istream_from_fd_url(int atfd, const char *url)
{
	return apk_istream_from_fd_url_if_modified(atfd, url, 0);
}
static inline struct apk_istream *apk_istream_from_url_if_modified(const char *url, time_t since)
{
	return apk_istream_from_fd_url_if_modified(AT_FDCWD, url, since);
}

struct apk_bstream *apk_bstream_from_istream(struct apk_istream *istream);
struct apk_bstream *apk_bstream_from_fd_pid(int fd, pid_t pid, int (*translate_status)(int));
struct apk_bstream *apk_bstream_from_file(int atfd, const char *file);
struct apk_bstream *apk_bstream_from_fd_url_if_modified(int atfd, const char *url, time_t since);
struct apk_bstream *apk_bstream_tee(struct apk_bstream *from, int atfd, const char *to,
				    apk_progress_cb cb, void *cb_ctx);

static inline struct apk_bstream *apk_bstream_from_fd(int fd)
{
	return apk_bstream_from_fd_pid(fd, 0, NULL);
}

static inline struct apk_bstream *apk_bstream_from_url(const char *url)
{
	return apk_bstream_from_fd_url_if_modified(AT_FDCWD, url, 0);
}
static inline struct apk_bstream *apk_bstream_from_fd_url(int fd, const char *url)
{
	return apk_bstream_from_fd_url_if_modified(fd, url, 0);
}
static inline struct apk_bstream *apk_bstream_from_url_if_modified(const char *url, time_t since)
{
	return apk_bstream_from_fd_url_if_modified(AT_FDCWD, url, since);
}

struct apk_ostream *apk_ostream_to_fd(int fd);
struct apk_ostream *apk_ostream_to_file(int atfd, const char *file, const char *tmpfile, mode_t mode);
struct apk_ostream *apk_ostream_to_file_gz(int atfd, const char *file, const char *tmpfile, mode_t mode);
size_t apk_ostream_write_string(struct apk_ostream *ostream, const char *string);

apk_blob_t apk_blob_from_istream(struct apk_istream *istream, size_t size);
apk_blob_t apk_blob_from_file(int atfd, const char *file);

#define APK_BTF_ADD_EOL		0x00000001
int apk_blob_to_file(int atfd, const char *file, apk_blob_t b, unsigned int flags);

#define APK_FI_NOFOLLOW		0x80000000
#define APK_FI_XATTR_CSUM(x)	(((x) & 0xff) << 8)
#define APK_FI_CSUM(x)		(((x) & 0xff))
int apk_fileinfo_get(int atfd, const char *filename, unsigned int flags,
		     struct apk_file_info *fi);
void apk_fileinfo_hash_xattr(struct apk_file_info *fi);
void apk_fileinfo_free(struct apk_file_info *fi);

typedef int apk_dir_file_cb(void *ctx, int dirfd, const char *entry);
int apk_dir_foreach_file(int dirfd, apk_dir_file_cb cb, void *ctx);

const char *apk_url_local_file(const char *url);

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);
uid_t apk_resolve_uid(struct apk_id_cache *idc, const char *username, uid_t default_uid);
uid_t apk_resolve_gid(struct apk_id_cache *idc, const char *groupname, uid_t default_gid);

#endif