summaryrefslogtreecommitdiffstats
path: root/src/opk/write.c
blob: 12a36c624cda70b3a3b4597a925e35afd206c4ab (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
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opkg.
 *
 * opkg-opkg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opkg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opkg.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include "../defs.h"
#include "../dirent.h"
#include "../gzip.h"
#include "../i18n.h"
#include "../opk.h"
#include "../ustar.h"
#include "opk.h"

static int
_opkg_opk_opk_write_file(void *user_data, size_t size)
{
	struct opkg_opk_opk *opk = user_data;

	if (fwrite(opk->file_buffer, 1, sizeof(opk->file_buffer), opk->file) ==
			size) {
		return OPKG_OPK_OK;
	} else {
		return OPKG_OPK_ERROR;
	}
}

int
opkg_opk_opk_write(struct opkg_opk_opk *opk, const char *file_name)
{
	int                     ret;
	struct opkg_opk_dirent  dirent;
	char                   *buffer;
	size_t                  size;

	ret = OPKG_OPK_OK;

	/* Open outer archive. */
	opk->file = fopen(file_name, "wb");
	if (opk->file == NULL) {
		fprintf(stderr, _("Error: Failed to open file \"%s\"\n"),
				file_name);
		ret = OPKG_OPK_ERROR;
		goto out0;
	}

	/* Initialize outer gzip decompressor. */
	opk->outer_gzip = opkg_opk_gzip_init_write(&_opkg_opk_opk_write_file,
			opk);
	if (opk->outer_gzip == NULL) {
		fputs(_("Error: Failed to initialize\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out1;
	}
	opkg_opk_gzip_set_write_buffer(opk->outer_gzip, opk->file_buffer,
			sizeof(opk->file_buffer));

	/* Initialize outer ustar unarchiver. */
	opk->outer_ustar = opkg_opk_ustar_init(opk->outer_gzip);
	if (opk->outer_ustar == NULL) {
		fputs(_("Error: Failed to initialize\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out2;
	}

	/* Write version file. */
	dirent.name   = "debian-binary";
	dirent.parent = NULL;
	if (opkg_opk_ustar_write_header(opk->outer_ustar, &dirent, 0644,
				0, getpwuid(0)->pw_name,
				0, getgrgid(0)->gr_name,
				4, '-', NULL) != OPKG_OPK_OK) {
		fputs(_("Error: Failed to write header\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}
	opkg_opk_ustar_get_buffer(opk->outer_ustar, &buffer, &size);
	memcpy(buffer, "2.0\n", 4);
	if (opkg_opk_ustar_write_data(opk->outer_ustar, 4) != OPKG_OPK_OK) {
		fputs(_("Error: Failed to write data\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}

	/* TODO: control.tar.gz and data.tar.gz */

	/* Write trailer. */
	if (opkg_opk_ustar_write_trailer(opk->outer_ustar) != OPKG_OPK_OK) {
		fputs(_("Error: Failed to write trailer\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}

 out3:
	opkg_opk_ustar_free(opk->outer_ustar);
 out2:
	opkg_opk_gzip_free(opk->outer_gzip);
 out1:
	fclose(opk->file);
 out0:
	return ret;
}