aboutsummaryrefslogtreecommitdiffstats
path: root/src/input_archive.c
blob: d687d9623af22526779583bc9fc58add61de69d6 (plain) (blame)
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
/* the Music Player Daemon (MPD)
 * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com>
 * This project's homepage is: http://www.musicpd.org
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "archive_api.h"
#include "archive_list.h"
#include "input_archive.h"
#include "input_stream.h"
#include "gcc.h"
#include "log.h"
#include "ls.h"

#include <stdbool.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <glib.h>

typedef struct {
	const struct archive_plugin *aplugin;
	const struct input_plugin *iplugin;
	struct archive_file *file;
} archive_context;

/**
 * select correct archive plugin to handle the input stream
 * may allow stacking of archive plugins. for example for handling
 * tar.gz a gzip handler opens file (through inputfile stream)
 * then it opens a tar handler and sets gzip inputstream as
 * parent_stream so tar plugin fetches file data from gzip
 * plugin and gzip fetches file from disk
 */
static bool
input_archive_open(struct input_stream *is, const char *pathname)
{
	archive_context *arch_ctx;
	const struct archive_plugin *arplug;
	char *archive, *filename, *suffix, *pname;
	bool opened;

	if (pathname[0] != '/')
		return false;

	pname = g_strdup(pathname);
	// archive_lookup will modify pname when true is returned
	if (!archive_lookup(pname, &archive, &filename, &suffix)) {
		g_debug("not an archive, lookup %s failed\n", pname);
		g_free(pname);
		return false;
	}

	//check which archive plugin to use (by ext)
	arplug = archive_plugin_from_suffix(suffix);
	if (!arplug) {
		g_warning("can't handle archive %s\n",archive);
		g_free(pname);
		return false;
	}

	arch_ctx = (archive_context *) g_malloc(sizeof(archive_context));

	//setup archive plugin pointer
	arch_ctx->aplugin = arplug;
	//open archive file
	arch_ctx->file = arplug->open(archive);
	//setup fileops
	arplug->setup_stream(arch_ctx->file, is);
	//setup input plugin backup
	arch_ctx->iplugin = is->plugin;
	is->plugin = &input_plugin_archive;

	//internal handle
	is->data = arch_ctx;

	//open archive
	opened = arch_ctx->iplugin->open(is, filename);

	if (!opened) {
		g_warning("open inarchive file %s failed\n\n",filename);
	} else {
		is->ready = true;
	}
	g_free(pname);
	return opened;
}

static void
input_archive_close(struct input_stream *is)
{
	archive_context *arch_ctx = (archive_context *)is->data;
	//close archive infile ops
	arch_ctx->iplugin->close(is);
	//close archive
	arch_ctx->aplugin->close(arch_ctx->file);
	//free private data
	g_free(arch_ctx);
}

static bool
input_archive_seek(struct input_stream *is, off_t offset, int whence)
{
	archive_context *arch_ctx = (archive_context *)is->data;
	return arch_ctx->iplugin->seek(is, offset, whence);
}

static size_t
input_archive_read(struct input_stream *is, void *ptr, size_t size)
{
	archive_context *arch_ctx = (archive_context *)is->data;
	assert(ptr != NULL);
	assert(size > 0);
	return arch_ctx->iplugin->read(is, ptr, size);
}

static bool
input_archive_eof(struct input_stream *is)
{
	archive_context *arch_ctx = (archive_context *)is->data;
	return arch_ctx->iplugin->eof(is);
}

static int
input_archive_buffer(struct input_stream *is)
{
	archive_context *arch_ctx = (archive_context *)is->data;
	return arch_ctx->iplugin->buffer(is);
}

const struct input_plugin input_plugin_archive = {
	.open = input_archive_open,
	.close = input_archive_close,
	.buffer = input_archive_buffer,
	.read = input_archive_read,
	.eof = input_archive_eof,
	.seek = input_archive_seek,
};