summaryrefslogtreecommitdiff
path: root/package/cfgfs/src/ft_pack.c
blob: 5d04f8b5c374b58b29c459e9e47e563d69703b0a (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
/* $MirOS: contrib/hosted/fwcf/ft_pack.c,v 1.7 2006/09/23 23:21:04 tg Exp $ */

/*-
 * Copyright (c) 2006
 *	Thorsten Glaser <tg@mirbsd.de>
 *
 * Licensee is hereby permitted to deal in this work without restric-
 * tion, including unlimited rights to use, publicly perform, modify,
 * merge, distribute, sell, give away or sublicence, provided all co-
 * pyright notices above, these terms and the disclaimer are retained
 * in all redistributions or reproduced in accompanying documentation
 * or other materials provided with binary redistributions.
 *
 * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
 * express, or implied, to the maximum extent permitted by applicable
 * law, without malicious intent or gross negligence; in no event may
 * licensor, an author or contributor be held liable for any indirect
 * or other damage, or direct damage except proven a consequence of a
 * direct error of said person and intended use of this work, loss or
 * other issues arising in any way out of its use, even if advised of
 * the possibility of such damage or existence of a defect.
 */

#include <sys/param.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "defs.h"
#include "fts_subs.h"
#include "pack.h"

__RCSID("$MirOS: contrib/hosted/fwcf/ft_pack.c,v 1.7 2006/09/23 23:21:04 tg Exp $");

char *
ft_pack(ftsf_entry *e)
{
	char f_header[4096], *hdrptr = f_header;
	size_t hdrleft = sizeof (f_header), k;
	char *f_data = NULL, *rv;
	off_t e_size;

	if (e == NULL)
		return (NULL);
	if ((e->etype != FTSF_FILE) && (e->etype != FTSF_SYMLINK) &&
	    (e->etype != FTSF_DIR))
		return (NULL);

	e_size = e->statp->st_size;

	if ((k = strlcpy(hdrptr, e->pathname, hdrleft)) >= hdrleft)
		return (NULL);
	hdrptr += ++k;
	hdrleft -= k;

	if (e->etype == FTSF_SYMLINK) {
		STOREB(0x03);
		e->statp->st_mtime = 0;
		e->statp->st_mode = 0;
	} else if (e->etype == FTSF_DIR) {
		STOREB(0x05);
		e_size = 0;
	}

	if (e->statp->st_mtime) {
		STOREB(0x10);
		STORED(e->statp->st_mtime);
	}

	if (e->statp->st_gid > 0xFF) {
		STOREB('G');
		STORED(e->statp->st_gid);
	} else if (e->statp->st_gid) {
		STOREB('g');
		STOREB(e->statp->st_gid);
	}

	if (e->statp->st_mode > 0xFFFF) {
		STOREB('M');
		STORED(e->statp->st_mode);
	} else if (e->statp->st_mode) {
		STOREB('m');
		STOREW(e->statp->st_mode);
	}

	if (e->statp->st_uid > 0xFF) {
		STOREB('U');
		STORED(e->statp->st_uid);
	} else if (e->statp->st_uid) {
		STOREB('u');
		STOREB(e->statp->st_uid);
	}

	/* e_size is zero for everything except files and symlinks */
	if (e_size > 0xFF) {
		STOREB('S');
		STORET(e_size);
	} else if (e_size) {
		STOREB('s');
		STOREB(e_size);
	}

	STOREB(0);

	if (e_size) {
		if ((f_data = malloc(e_size)) == NULL)
			return (NULL);
		if (asprintf(&rv, "%s/%s", ftsf_prefix, e->pathname) == -1)
			return (NULL);
		if (e->etype == FTSF_SYMLINK) {
			if (readlink(rv, f_data, e_size) != e_size)
				return (NULL);
		} else {
			int fd;

			if ((fd = open(rv, O_RDONLY, 0)) < 0)
				return (NULL);
			if (read(fd, f_data, e_size) != e_size)
				return (NULL);
			close(fd);
		}
		free(rv);
	}
	k = sizeof (size_t) + (hdrptr - f_header) + e_size;
	if ((rv = malloc(k)) == NULL)
		return (NULL);
	*(size_t *)rv = k;
	memcpy(rv + sizeof (size_t), f_header, hdrptr - f_header);
	memcpy(rv + sizeof (size_t) + (hdrptr - f_header), f_data, e_size);
	return (rv);
}