Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
archive_read(3)		 BSD Library Functions Manual	       archive_read(3)

NAME
     archive_read_new, archive_read_set_bytes_per_block,
     archive_read_support_compression_all,
     archive_read_support_compression_bzip2,
     archive_read_support_compression_compress,
     archive_read_support_compression_gzip,
     archive_read_support_compression_none, archive_read_support_format_all,
     archive_read_support_format_cpio, archive_read_support_format_iso9660,
     archive_read_support_format_tar, archive_read_support_format_zip,
     archive_read_open,	archive_read_open_fd, archive_read_open_file,
     archive_read_next_header, archive_read_data, archive_read_data_block,
     archive_read_data_skip, archive_read_data_into_buffer,
     archive_read_data_into_fd,	archive_read_extract,
     archive_read_extract_set_progress_callback, archive_read_close,
     archive_read_finish -- functions for reading streaming archives

SYNOPSIS
     #include <archive.h>

     struct archive *
     archive_read_new(void);

     int
     archive_read_set_bytes_per_block(struct archive *,	int);

     int
     archive_read_support_compression_all(struct archive *);

     int
     archive_read_support_compression_bzip2(struct archive *);

     int
     archive_read_support_compression_compress(struct archive *);

     int
     archive_read_support_compression_gzip(struct archive *);

     int
     archive_read_support_compression_none(struct archive *);

     int
     archive_read_support_format_all(struct archive *);

     int
     archive_read_support_format_cpio(struct archive *);

     int
     archive_read_support_format_iso9660(struct	archive	*);

     int
     archive_read_support_format_tar(struct archive *);

     int
     archive_read_support_format_zip(struct archive *);

     int
     archive_read_open(struct archive *, void *client_data,
	 archive_open_archive_callback *, archive_read_archive_callback	*,
	 archive_close_archive_callback	*);

     int
     archive_read_open_fd(struct archive *, int	fd, size_t block_size);

     int
     archive_read_open_file(struct archive *, const char *filename,
	 size_t	block_size);

     int
     archive_read_next_header(struct archive *,	struct archive_entry **);

     ssize_t
     archive_read_data(struct archive *, void *buff, size_t len);

     int
     archive_read_data_block(struct archive *, const void **buff, size_t *len,
	 off_t *offset);

     int
     archive_read_data_skip(struct archive *);

     int
     archive_read_data_into_buffer(struct archive *, void *);

     int
     archive_read_data_into_fd(struct archive *, int fd);

     int
     archive_read_extract(struct archive *, struct archive_entry *,
	 int flags);

     void
     archive_read_extract_set_progress_callback(struct archive *,
	 void (*func)(void *), void *user_data);

     int
     archive_read_close(struct archive *);

     void
     archive_read_finish(struct	archive	*);

DESCRIPTION
     These functions provide a complete	API for	reading	streaming archives.
     The general process is to first create the	struct archive object, set op-
     tions, initialize the reader, iterate over	the archive headers and	asso-
     ciated data, then close the archive and release all resources.  The fol-
     lowing summary describes the functions in approximately the order they
     would be used:
     archive_read_new()
	     Allocates and initializes a struct	archive	object suitable	for
	     reading from an archive.
     archive_read_set_bytes_per_block()
	     Sets the block size used for reading the archive data.  This con-
	     trols the size that will be used when invoking the	read callback
	     function.	The default is 20 records or 10240 bytes for tar for-
	     mats.
     archive_read_support_compression_all(),
	     archive_read_support_compression_bzip2(),
	     archive_read_support_compression_compress(),
	     archive_read_support_compression_gzip(),
	     archive_read_support_compression_none()
	     Enables auto-detection code and decompression support for the
	     specified compression.  Note that "none" is always	enabled	by de-
	     fault.  For convenience, archive_read_support_compression_all()
	     enables all available decompression code.
     archive_read_support_format_all(),	archive_read_support_format_cpio(),
	     archive_read_support_format_iso9660(),
	     archive_read_support_format_tar,()
	     archive_read_support_format_zip()
	     Enables support---including auto-detection	code---for the speci-
	     fied archive format.  For example,
	     archive_read_support_format_tar() enables support for a variety
	     of	standard tar formats, old-style	tar, ustar, pax	interchange
	     format, and many common variants.	For convenience,
	     archive_read_support_format_all() enables support for all avail-
	     able formats.  Note that there is no default.
     archive_read_open()
	     Freeze the	settings, open the archive, and	prepare	for reading
	     entries.  This is the most	generic	version	of this	call, which
	     accepts three callback functions.	Most clients will want to use
	     archive_read_open_file() or archive_read_open_fd()	instead.  The
	     library invokes the client-provided functions to obtain raw bytes
	     from the archive.	Note: The API permits a	decompression method
	     to	fork and invoke	the callbacks from another process.  Although
	     none of the current decompression methods use this	technique, fu-
	     ture decompression	methods	may utilize this technique.  If	the
	     decompressor forks, it will ensure	that the open and close	call-
	     backs are invoked within the same process as the read callback.
	     In	particular, clients should not attempt to use shared variables
	     to	communicate between the	open/read/close	callbacks and the
	     mainline code.
     archive_read_open_fd()
	     Like archive_read_open(), except that it accepts a	file descrip-
	     tor and block size	rather than a trio of function pointers.  Note
	     that the file descriptor will not be automatically	closed at end-
	     of-archive.
     archive_read_open_file()
	     Like archive_read_open(), except that it accepts a	simple file-
	     name and a	block size.  A NULL filename represents	standard in-
	     put.
     archive_read_next_header()
	     Read the header for the next entry	and return a pointer to	a
	     struct archive_entry.
     archive_read_data()
	     Read data associated with the header just read.  Internally, this
	     is	a convenience function that calls archive_read_data_block()
	     and fills any gaps	with nulls so that callers see a single	con-
	     tinuous stream of data.
     archive_read_data_block()
	     Return the	next available block of	data for this entry.  Unlike
	     archive_read_data(), the archive_read_data_block()	function
	     avoids copying data and allows you	to correctly handle sparse
	     files, as supported by some archive formats.  The library gauran-
	     tees that offsets will increase and that blocks will not overlap.
	     Note that the blocks returned from	this function can be much
	     larger than the block size	read from disk,	due to compression and
	     internal buffer optimizations.
     archive_read_data_skip()
	     A convenience function that repeatedly calls
	     archive_read_data_block() to skip all of the data for this	ar-
	     chive entry.
     archive_read_data_into_buffer()
	     A convenience function that repeatedly calls
	     archive_read_data_block() to copy the entire entry	into the
	     client-supplied buffer.  Note that	the client is responsible for
	     sizing the	buffer appropriately.
     archive_read_data_into_fd()
	     A convenience function that repeatedly calls
	     archive_read_data_block() to copy the entire entry	to the pro-
	     vided file	descriptor.
     archive_read_extract()
	     A convenience function that recreates the specified object	on
	     disk and reads the	entry data into	that object.  The filename,
	     permissions, and other critical information are taken from	the
	     provided archive_entry object.  The flags argument	modifies how
	     the object	is recreated.  It consists of a	bitwise	OR of one or
	     more of the following values:
	     ARCHIVE_EXTRACT_OWNER
		     The user and group	IDs should be set on the restored
		     file.  By default,	the user and group IDs are not re-
		     stored.
	     ARCHIVE_EXTRACT_PERM
		     The permissions (mode bits) should	be restored for	all
		     objects.  By default, permissions are only	restored for
		     regular files.
	     ARCHIVE_EXTRACT_TIME
		     The timestamps (mtime, ctime, and atime) should be	re-
		     stored.  By default, they are ignored.  Note that restor-
		     ing of atime is not currently supported.
	     ARCHIVE_EXTRACT_NO_OVERWRITE
		     Existing files on disk will not be	overwritten.  By de-
		     fault, existing regular files are truncated and overwrit-
		     ten; existing directories will have their permissions up-
		     dated; other pre-existing objects are unlinked and	recre-
		     ated from scratch.
	     ARCHIVE_EXTRACT_UNLINK
		     Existing files on disk will be unlinked and recreated
		     from scratch.  By default,	existing files are truncated
		     and rewritten, but	the file is not	recreated.  In partic-
		     ular, the default behavior	does not break existing	hard
		     links.
	     ARCHIVE_EXTRACT_ACL
		     Attempt to	restore	ACLs.  By default, extended ACLs are
		     ignored.
	     ARCHIVE_EXTRACT_FFLAGS
		     Attempt to	restore	extended file flags.  By default, file
		     flags are ignored.
	     Note that not all attributes are set immediately; some attributes
	     are cached	in memory and written to disk only when	the archive is
	     closed.  (For example, read-only directories are initially	cre-
	     ated writable so that files within	those directories can be re-
	     stored.  The final	permissions are	set when the archive is
	     closed.)
     archive_read_extract_set_progress_callback()
	     Sets a pointer to a user-defined callback that can	be used	for
	     updating progress displays	during extraction.  The	progress func-
	     tion will be invoked during the extraction	of large regular
	     files.  The progress function will	be invoked with	the pointer
	     provided to this call.  Generally,	the data pointed to should in-
	     clude a reference to the archive object and the archive_entry ob-
	     ject so that various statistics can be retrieved for the progress
	     display.
     archive_read_close()
	     Complete the archive and invoke the close callback.
     archive_read_finish()
	     Invokes archive_read_close() if it	was not	invoked	maually, then
	     release all resources.

     Note that the library determines most of the relevant information about
     the archive by inspection.	 In particular,	it automatically detects
     gzip(1) or	bzip2(1) compression and transparently performs	the appropri-
     ate decompression.	 It also automatically detects the archive format.

     A complete	description of the struct archive and struct archive_entry ob-
     jects can be found	in the overview	manual page for	libarchive(3).

CLIENT CALLBACKS
     The callback functions must match the following prototypes:

	   typedef ssize_t archive_read_callback(struct	archive	*, void
	   *client_data, const void **buffer)

	   typedef int archive_open_callback(struct archive *, void
	   *client_data)

	   typedef int archive_close_callback(struct archive *,	void
	   *client_data)

     The open callback is invoked by archive_open().  It should	return
     ARCHIVE_OK	if the underlying file or data source is successfully opened.
     If	the open fails,	it should call archive_set_error() to register an er-
     ror code and message and return ARCHIVE_FATAL.

     The read callback is invoked whenever the library requires	raw bytes from
     the archive.  The read callback should read data into a buffer, set the
     const void	**buffer argument to point to the available data, and return a
     count of the number of bytes available.  The library will invoke the read
     callback again only after it has consumed this data.  The library imposes
     no	constraints on the size	of the data blocks returned.  On end-of-file,
     the read callback should return zero.  On error, the read callback	should
     invoke archive_set_error()	to register an error code and message and re-
     turn -1.

     The close callback	is invoked by archive_close when the archive process-
     ing is complete.  The callback should return ARCHIVE_OK on	success.  On
     failure, the callback should invoke archive_set_error() to	register an
     error code	and message and	regurn ARCHIVE_FATAL.

EXAMPLE
     The following illustrates basic usage of the library.  In this example,
     the callback functions are	simply wrappers	around the standard open(2),
     read(2), and close(2) system calls.

	   void
	   list_archive(const char *name)
	   {
	     struct mydata *mydata;
	     struct archive *a;
	     struct archive_entry *entry;

	     mydata = malloc(sizeof(struct mydata));
	     a = archive_read_new();
	     mydata->name = name;
	     archive_read_support_compression_all(a);
	     archive_read_support_format_all(a);
	     archive_read_open(a, mydata, myopen, myread, myclose);
	     while (archive_read_next_header(a,	&entry)	== ARCHIVE_OK) {
	       printf("%s\n",archive_entry_pathname(entry));
	       archive_read_data_skip(a);
	     }
	     archive_read_finish(a);
	     free(mydata);
	   }

	   ssize_t
	   myread(struct archive *a, void *client_data,	const void **buff)
	   {
	     struct mydata *mydata = client_data;

	     *buff = mydata->buff;
	     return (read(mydata->fd, mydata->buff, 10240));
	   }

	   int
	   myopen(struct archive *a, void *client_data)
	   {
	     struct mydata *mydata = client_data;

	     mydata->fd	= open(mydata->name, O_RDONLY);
	     return (mydata->fd	>= 0);
	   }

	   int
	   myclose(struct archive *a, void *client_data)
	   {
	     struct mydata *mydata = client_data;

	     if	(mydata->fd > 0)
	       close(mydata->fd);
	     return (0);
	   }

RETURN VALUES
     Most functions return zero	on success, non-zero on	error.	The possible
     return codes include: ARCHIVE_OK (the operation succeeded), ARCHIVE_WARN
     (the operation succeeded but a non-critical error was encountered),
     ARCHIVE_EOF (end-of-archive was encountered), ARCHIVE_RETRY (the opera-
     tion failed but can be retried), and ARCHIVE_FATAL	(there was a fatal er-
     ror; the archive should be	closed immediately).  Detailed error codes and
     textual descriptions are available	from the archive_errno() and
     archive_error_string() functions.

     archive_read_new()	returns	a pointer to a freshly allocated struct
     archive object.  It returns NULL on error.

     archive_read_data() returns a count of bytes actually read	or zero	at the
     end of the	entry.	On error, a value of ARCHIVE_FATAL, ARCHIVE_WARN, or
     ARCHIVE_RETRY is returned and an error code and textual description can
     be	retrieved from the archive_errno() and archive_error_string() func-
     tions.

     The library expects the client callbacks to behave	similarly.  If there
     is	an error, you can use archive_set_error() to set an appropriate	error
     code and description, then	return one of the non-zero values above.
     (Note that	the value eventually returned to the client may	not be the
     same; many	errors that are	not critical at	the level of basic I/O can
     prevent the archive from being properly read, thus	most I/O errors	even-
     tually cause ARCHIVE_FATAL	to be returned.)

SEE ALSO
     tar(1), archive(3), archive_util(3), tar(5)

HISTORY
     The libarchive library first appeared in FreeBSD 5.3.

AUTHORS
     The libarchive library was	written	by Tim Kientzle	<kientzle@acm.org>.

BUGS
     Directories are actually extracted	in two distinct	phases.	 Directories
     are created during	archive_read_extract(),	but final permissions are not
     set until archive_read_close().  This separation is necessary to cor-
     rectly handle borderline cases such as a non-writable directory contain-
     ing files,	but can	cause unexpected results.  In particular, directory
     permissions are not fully restored	until the archive is closed.  If you
     use chdir(2) to change the	current	directory between calls	to
     archive_read_extract() or before calling archive_read_close(), you	may
     confuse the permission-setting logic with the result that directory per-
     missions are restored incorrectly.

BSD				January	8, 2005				   BSD

NAME | SYNOPSIS | DESCRIPTION | CLIENT CALLBACKS | EXAMPLE | RETURN VALUES | SEE ALSO | HISTORY | AUTHORS | BUGS

Want to link to this manual page? Use this URL:
<https://www.freebsd.org/cgi/man.cgi?query=archive_read&sektion=3&manpath=FreeBSD+5.4-RELEASE>

home | help