stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/playlist.h"
|
|
|
|
#include "stream/stream.h"
|
2018-07-27 03:01:35 +00:00
|
|
|
#include "misc/natural_sort.h"
|
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
#include "demux.h"
|
|
|
|
|
|
|
|
#include "stream/stream_libarchive.h"
|
|
|
|
|
|
|
|
static int cmp_filename(const void *a, const void *b)
|
|
|
|
{
|
2018-07-27 03:01:35 +00:00
|
|
|
return mp_natural_sort_cmp(*(char **)a, *(char **)b);
|
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int open_file(struct demuxer *demuxer, enum demux_check check)
|
|
|
|
{
|
2016-12-04 22:15:31 +00:00
|
|
|
if (!demuxer->access_references)
|
|
|
|
return -1;
|
|
|
|
|
2015-08-17 21:59:44 +00:00
|
|
|
int flags = 0;
|
2015-08-24 20:21:36 +00:00
|
|
|
int probe_size = STREAM_BUFFER_SIZE;
|
|
|
|
if (check <= DEMUX_CHECK_REQUEST) {
|
2015-08-17 21:59:44 +00:00
|
|
|
flags |= MP_ARCHIVE_FLAG_UNSAFE;
|
2015-08-24 20:21:36 +00:00
|
|
|
probe_size *= 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
bstr probe = stream_peek(demuxer->stream, probe_size);
|
|
|
|
if (probe.len == 0)
|
|
|
|
return -1;
|
2019-06-19 14:48:46 +00:00
|
|
|
struct stream *probe_stream =
|
|
|
|
stream_memory_open(demuxer->global, probe.start, probe.len);
|
2015-08-24 20:21:36 +00:00
|
|
|
struct mp_archive *mpa = mp_archive_new(mp_null_log, probe_stream, flags);
|
|
|
|
bool ok = !!mpa;
|
|
|
|
free_stream(probe_stream);
|
|
|
|
mp_archive_free(mpa);
|
|
|
|
if (!ok)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
mpa = mp_archive_new(demuxer->log, demuxer->stream, flags);
|
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
if (!mpa)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct playlist *pl = talloc_zero(demuxer, struct playlist);
|
|
|
|
demuxer->playlist = pl;
|
|
|
|
|
|
|
|
// make it load archive://
|
|
|
|
pl->disable_safety = true;
|
|
|
|
|
|
|
|
char *prefix = mp_url_escape(mpa, demuxer->stream->url, "~|");
|
|
|
|
|
|
|
|
char **files = NULL;
|
|
|
|
int num_files = 0;
|
|
|
|
|
2016-07-18 10:44:56 +00:00
|
|
|
while (mp_archive_next_entry(mpa)) {
|
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
// stream_libarchive.c does the real work
|
2016-07-18 10:44:56 +00:00
|
|
|
char *f = talloc_asprintf(mpa, "archive://%s|%s", prefix,
|
|
|
|
mpa->entry_filename);
|
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
MP_TARRAY_APPEND(mpa, files, num_files, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files)
|
|
|
|
qsort(files, num_files, sizeof(files[0]), cmp_filename);
|
|
|
|
|
|
|
|
for (int n = 0; n < num_files; n++)
|
|
|
|
playlist_add_file(pl, files[n]);
|
|
|
|
|
|
|
|
demuxer->filetype = "archive";
|
|
|
|
demuxer->fully_read = true;
|
|
|
|
|
|
|
|
mp_archive_free(mpa);
|
demux: change hack for closing subtitle files early
Subtitles (and a few other file types, like playlists) are not streamed,
but fully read on opening. This means keeping the file handle or network
socket open is a waste of resources and could cause other weird
behavior. This is why there's a hack to close them after opening.
Change this hack to make the demuxer itself do this, which is less
weird. (Until recently, demuxer->stream ownership was more complex,
which is why it was done this way.)
There is some evil shit due to a huge ownership/lifetime mess of various
objects. Especially EDL (the currently only nested demuxer case)
requires being careful about mp_cancel and passing down stream pointers.
As one defensive programming measure, stop accessing the "stream"
variable in open_given_type(), even where it would still work. This
includes removing a redundant line of code, and removing the peak call,
which should not be needed anymore, as the remaining demuxers do this
mostly correctly.
2018-09-07 21:02:36 +00:00
|
|
|
demux_close_stream(demuxer);
|
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive.
libarchive supports a number of formats, including zip and (most of)
rar.
Unfortunately, seeking does not work too well. Most libarchive readers
do not support seeking, so it's emulated by skipping data until the
target position. On backwards seek, the file is reopened. This works
fine on a local machine (and if the file is not too large), but will
perform not so well over network connection.
This is disabled by default for now. One reason is that we try
libarchive on every file we open, before trying libavformat, and I'm not
sure if I trust libarchive that much yet. Another reason is that this
breaks multivolume rar support. While libarchive supports seeking in
rar, and (probably) supports multivolume archive, our support of
libarchive (probably) does not. I don't care about multivolume rar, but
vocal users do.
2015-08-16 22:55:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct demuxer_desc demuxer_desc_libarchive = {
|
|
|
|
.name = "libarchive",
|
|
|
|
.desc = "libarchive wrapper",
|
|
|
|
.open = open_file,
|
|
|
|
};
|