Blame libarchive/archive_read.3

Packit 08bd4c
.\" Copyright (c) 2003-2007 Tim Kientzle
Packit 08bd4c
.\" All rights reserved.
Packit 08bd4c
.\"
Packit 08bd4c
.\" Redistribution and use in source and binary forms, with or without
Packit 08bd4c
.\" modification, are permitted provided that the following conditions
Packit 08bd4c
.\" are met:
Packit 08bd4c
.\" 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
.\"    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
.\"    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
.\"    documentation and/or other materials provided with the distribution.
Packit 08bd4c
.\"
Packit 08bd4c
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Packit 08bd4c
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 08bd4c
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 08bd4c
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit 08bd4c
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 08bd4c
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 08bd4c
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 08bd4c
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 08bd4c
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 08bd4c
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 08bd4c
.\" SUCH DAMAGE.
Packit 08bd4c
.\"
Packit 08bd4c
.\" $FreeBSD$
Packit 08bd4c
.\"
Packit 08bd4c
.Dd February 2, 2012
Packit 08bd4c
.Dt ARCHIVE_READ 3
Packit 08bd4c
.Os
Packit 08bd4c
.Sh NAME
Packit 08bd4c
.Nm archive_read
Packit 08bd4c
.Nd functions for reading streaming archives
Packit 08bd4c
.Sh LIBRARY
Packit 08bd4c
Streaming Archive Library (libarchive, -larchive)
Packit 08bd4c
.Sh SYNOPSIS
Packit 08bd4c
.In archive.h
Packit 08bd4c
.Sh DESCRIPTION
Packit 08bd4c
These functions provide a complete API for reading streaming archives.
Packit 08bd4c
The general process is to first create the
Packit 08bd4c
.Tn struct archive
Packit 08bd4c
object, set options, initialize the reader, iterate over the archive
Packit 08bd4c
headers and associated data, then close the archive and release all
Packit 08bd4c
resources.
Packit 08bd4c
.\"
Packit 08bd4c
.Ss Create archive object
Packit 08bd4c
See
Packit 08bd4c
.Xr archive_read_new 3 .
Packit 08bd4c
.Pp
Packit 08bd4c
To read an archive, you must first obtain an initialized
Packit 08bd4c
.Tn struct archive
Packit 08bd4c
object from
Packit 08bd4c
.Fn archive_read_new .
Packit 08bd4c
.\"
Packit 08bd4c
.Ss Enable filters and formats
Packit 08bd4c
See
Packit 08bd4c
.Xr archive_read_filter 3
Packit 08bd4c
and
Packit 08bd4c
.Xr archive_read_format 3 .
Packit 08bd4c
.Pp
Packit 08bd4c
You can then modify this object for the desired operations with the
Packit 08bd4c
various
Packit 08bd4c
.Fn archive_read_set_XXX
Packit 08bd4c
and
Packit 08bd4c
.Fn archive_read_support_XXX
Packit 08bd4c
functions.
Packit 08bd4c
In particular, you will need to invoke appropriate
Packit 08bd4c
.Fn archive_read_support_XXX
Packit 08bd4c
functions to enable the corresponding compression and format
Packit 08bd4c
support.
Packit 08bd4c
Note that these latter functions perform two distinct operations:
Packit 08bd4c
they cause the corresponding support code to be linked into your
Packit 08bd4c
program, and they enable the corresponding auto-detect code.
Packit 08bd4c
Unless you have specific constraints, you will generally want
Packit 08bd4c
to invoke
Packit 08bd4c
.Fn archive_read_support_filter_all
Packit 08bd4c
and
Packit 08bd4c
.Fn archive_read_support_format_all
Packit 08bd4c
to enable auto-detect for all formats and compression types
Packit 08bd4c
currently supported by the library.
Packit 08bd4c
.\"
Packit 08bd4c
.Ss Set options
Packit 08bd4c
See
Packit 08bd4c
.Xr archive_read_set_options 3 .
Packit 08bd4c
.\"
Packit 08bd4c
.Ss Open archive
Packit 08bd4c
See
Packit 08bd4c
.Xr archive_read_open 3 .
Packit 08bd4c
.Pp
Packit 08bd4c
Once you have prepared the
Packit 08bd4c
.Tn struct archive
Packit 08bd4c
object, you call
Packit 08bd4c
.Fn archive_read_open
Packit 08bd4c
to actually open the archive and prepare it for reading.
Packit 08bd4c
There are several variants of this function;
Packit 08bd4c
the most basic expects you to provide pointers to several
Packit 08bd4c
functions that can provide blocks of bytes from the archive.
Packit 08bd4c
There are convenience forms that allow you to
Packit 08bd4c
specify a filename, file descriptor,
Packit 08bd4c
.Ft "FILE *"
Packit 08bd4c
object, or a block of memory from which to read the archive data.
Packit 08bd4c
Note that the core library makes no assumptions about the
Packit 08bd4c
size of the blocks read;
Packit 08bd4c
callback functions are free to read whatever block size is
Packit 08bd4c
most appropriate for the medium.
Packit 08bd4c
.\"
Packit 08bd4c
.Ss Consume archive
Packit 08bd4c
See
Packit 08bd4c
.Xr archive_read_header 3 ,
Packit 08bd4c
.Xr archive_read_data 3
Packit 08bd4c
and
Packit 08bd4c
.Xr archive_read_extract 3 .
Packit 08bd4c
.Pp
Packit 08bd4c
Each archive entry consists of a header followed by a certain
Packit 08bd4c
amount of data.
Packit 08bd4c
You can obtain the next header with
Packit 08bd4c
.Fn archive_read_next_header ,
Packit 08bd4c
which returns a pointer to an
Packit 08bd4c
.Tn struct archive_entry
Packit 08bd4c
structure with information about the current archive element.
Packit 08bd4c
If the entry is a regular file, then the header will be followed
Packit 08bd4c
by the file data.
Packit 08bd4c
You can use
Packit 08bd4c
.Fn archive_read_data
Packit 08bd4c
(which works much like the
Packit 08bd4c
.Xr read 2
Packit 08bd4c
system call)
Packit 08bd4c
to read this data from the archive, or
Packit 08bd4c
.Fn archive_read_data_block
Packit 08bd4c
which provides a slightly more efficient interface.
Packit 08bd4c
You may prefer to use the higher-level
Packit 08bd4c
.Fn archive_read_data_skip ,
Packit 08bd4c
which reads and discards the data for this entry,
Packit 08bd4c
.Fn archive_read_data_into_fd ,
Packit 08bd4c
which copies the data to the provided file descriptor, or
Packit 08bd4c
.Fn archive_read_extract ,
Packit 08bd4c
which recreates the specified entry on disk and copies data
Packit 08bd4c
from the archive.
Packit 08bd4c
In particular, note that
Packit 08bd4c
.Fn archive_read_extract
Packit 08bd4c
uses the
Packit 08bd4c
.Tn struct archive_entry
Packit 08bd4c
structure that you provide it, which may differ from the
Packit 08bd4c
entry just read from the archive.
Packit 08bd4c
In particular, many applications will want to override the
Packit 08bd4c
pathname, file permissions, or ownership.
Packit 08bd4c
.\"
Packit 08bd4c
.Ss Release resources
Packit 08bd4c
See
Packit 08bd4c
.Xr archive_read_free 3 .
Packit 08bd4c
.Pp
Packit 08bd4c
Once you have finished reading data from the archive, you
Packit 08bd4c
should call
Packit 08bd4c
.Fn archive_read_close
Packit 08bd4c
to close the archive, then call
Packit 08bd4c
.Fn archive_read_free
Packit 08bd4c
to release all resources, including all memory allocated by the library.
Packit 08bd4c
.\"
Packit 08bd4c
.Sh EXAMPLE
Packit 08bd4c
The following illustrates basic usage of the library.
Packit 08bd4c
In this example,
Packit 08bd4c
the callback functions are simply wrappers around the standard
Packit 08bd4c
.Xr open 2 ,
Packit 08bd4c
.Xr read 2 ,
Packit 08bd4c
and
Packit 08bd4c
.Xr close 2
Packit 08bd4c
system calls.
Packit 08bd4c
.Bd -literal -offset indent
Packit 08bd4c
void
Packit 08bd4c
list_archive(const char *name)
Packit 08bd4c
{
Packit 08bd4c
  struct mydata *mydata;
Packit 08bd4c
  struct archive *a;
Packit 08bd4c
  struct archive_entry *entry;
Packit 08bd4c
Packit 08bd4c
  mydata = malloc(sizeof(struct mydata));
Packit 08bd4c
  a = archive_read_new();
Packit 08bd4c
  mydata->name = name;
Packit 08bd4c
  archive_read_support_filter_all(a);
Packit 08bd4c
  archive_read_support_format_all(a);
Packit 08bd4c
  archive_read_open(a, mydata, myopen, myread, myclose);
Packit 08bd4c
  while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
Packit 08bd4c
    printf("%s\en",archive_entry_pathname(entry));
Packit 08bd4c
    archive_read_data_skip(a);
Packit 08bd4c
  }
Packit 08bd4c
  archive_read_free(a);
Packit 08bd4c
  free(mydata);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
la_ssize_t
Packit 08bd4c
myread(struct archive *a, void *client_data, const void **buff)
Packit 08bd4c
{
Packit 08bd4c
  struct mydata *mydata = client_data;
Packit 08bd4c
Packit 08bd4c
  *buff = mydata->buff;
Packit 08bd4c
  return (read(mydata->fd, mydata->buff, 10240));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
int
Packit 08bd4c
myopen(struct archive *a, void *client_data)
Packit 08bd4c
{
Packit 08bd4c
  struct mydata *mydata = client_data;
Packit 08bd4c
Packit 08bd4c
  mydata->fd = open(mydata->name, O_RDONLY);
Packit 08bd4c
  return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
int
Packit 08bd4c
myclose(struct archive *a, void *client_data)
Packit 08bd4c
{
Packit 08bd4c
  struct mydata *mydata = client_data;
Packit 08bd4c
Packit 08bd4c
  if (mydata->fd > 0)
Packit 08bd4c
    close(mydata->fd);
Packit 08bd4c
  return (ARCHIVE_OK);
Packit 08bd4c
}
Packit 08bd4c
.Ed
Packit 08bd4c
.\" .Sh ERRORS
Packit 08bd4c
.Sh SEE ALSO
Packit 08bd4c
.Xr tar 1 ,
Packit 08bd4c
.Xr libarchive 3 ,
Packit 08bd4c
.Xr archive_read_new 3 ,
Packit 08bd4c
.Xr archive_read_data 3 ,
Packit 08bd4c
.Xr archive_read_extract 3 ,
Packit 08bd4c
.Xr archive_read_filter 3 ,
Packit 08bd4c
.Xr archive_read_format 3 ,
Packit 08bd4c
.Xr archive_read_header 3 ,
Packit 08bd4c
.Xr archive_read_open 3 ,
Packit 08bd4c
.Xr archive_read_set_options 3 ,
Packit 08bd4c
.Xr archive_util 3 ,
Packit 08bd4c
.Xr tar 5
Packit 08bd4c
.Sh HISTORY
Packit 08bd4c
The
Packit 08bd4c
.Nm libarchive
Packit 08bd4c
library first appeared in
Packit 08bd4c
.Fx 5.3 .
Packit 08bd4c
.Sh AUTHORS
Packit 08bd4c
.An -nosplit
Packit 08bd4c
The
Packit 08bd4c
.Nm libarchive
Packit 08bd4c
library was written by
Packit 08bd4c
.An Tim Kientzle Aq kientzle@acm.org .
Packit 08bd4c
.Sh BUGS
Packit 08bd4c
Many traditional archiver programs treat
Packit 08bd4c
empty files as valid empty archives.
Packit 08bd4c
For example, many implementations of
Packit 08bd4c
.Xr tar 1
Packit 08bd4c
allow you to append entries to an empty file.
Packit 08bd4c
Of course, it is impossible to determine the format of an empty file
Packit 08bd4c
by inspecting the contents, so this library treats empty files as
Packit 08bd4c
having a special
Packit 08bd4c
.Dq empty
Packit 08bd4c
format.