Blame libarchive/archive_read_disk.3

Packit 08bd4c
.\" Copyright (c) 2003-2009 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 April 3, 2017
Packit 08bd4c
.Dt ARCHIVE_READ_DISK 3
Packit 08bd4c
.Os
Packit 08bd4c
.Sh NAME
Packit 08bd4c
.Nm archive_read_disk_new ,
Packit 08bd4c
.Nm archive_read_disk_set_behavior ,
Packit 08bd4c
.Nm archive_read_disk_set_symlink_logical ,
Packit 08bd4c
.Nm archive_read_disk_set_symlink_physical ,
Packit 08bd4c
.Nm archive_read_disk_set_symlink_hybrid ,
Packit 08bd4c
.Nm archive_read_disk_entry_from_file ,
Packit 08bd4c
.Nm archive_read_disk_gname ,
Packit 08bd4c
.Nm archive_read_disk_uname ,
Packit 08bd4c
.Nm archive_read_disk_set_uname_lookup ,
Packit 08bd4c
.Nm archive_read_disk_set_gname_lookup ,
Packit 08bd4c
.Nm archive_read_disk_set_standard_lookup
Packit 08bd4c
.Nd functions for reading objects from disk
Packit 08bd4c
.Sh LIBRARY
Packit 08bd4c
Streaming Archive Library (libarchive, -larchive)
Packit 08bd4c
.Sh SYNOPSIS
Packit 08bd4c
.In archive.h
Packit 08bd4c
.Ft struct archive *
Packit 08bd4c
.Fn archive_read_disk_new "void"
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fn archive_read_disk_set_behavior "struct archive *" "int"
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fn archive_read_disk_set_symlink_logical "struct archive *"
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fn archive_read_disk_set_symlink_physical "struct archive *"
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fn archive_read_disk_set_symlink_hybrid "struct archive *"
Packit 08bd4c
.Ft const char *
Packit 08bd4c
.Fn archive_read_disk_gname "struct archive *" "gid_t"
Packit 08bd4c
.Ft const char *
Packit 08bd4c
.Fn archive_read_disk_uname "struct archive *" "uid_t"
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fo archive_read_disk_set_gname_lookup
Packit 08bd4c
.Fa "struct archive *"
Packit 08bd4c
.Fa "void *"
Packit 08bd4c
.Fa "const char *(*lookup)(void *, gid_t)"
Packit 08bd4c
.Fa "void (*cleanup)(void *)"
Packit 08bd4c
.Fc
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fo archive_read_disk_set_uname_lookup
Packit 08bd4c
.Fa "struct archive *"
Packit 08bd4c
.Fa "void *"
Packit 08bd4c
.Fa "const char *(*lookup)(void *, uid_t)"
Packit 08bd4c
.Fa "void (*cleanup)(void *)"
Packit 08bd4c
.Fc
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fn archive_read_disk_set_standard_lookup "struct archive *"
Packit 08bd4c
.Ft int
Packit 08bd4c
.Fo archive_read_disk_entry_from_file
Packit 08bd4c
.Fa "struct archive *"
Packit 08bd4c
.Fa "struct archive_entry *"
Packit 08bd4c
.Fa "int fd"
Packit 08bd4c
.Fa "const struct stat *"
Packit 08bd4c
.Fc
Packit 08bd4c
.Sh DESCRIPTION
Packit 08bd4c
These functions provide an API for reading information about
Packit 08bd4c
objects on disk.
Packit 08bd4c
In particular, they provide an interface for populating
Packit 08bd4c
.Tn struct archive_entry
Packit 08bd4c
objects.
Packit 08bd4c
.Bl -tag -width indent
Packit 08bd4c
.It Fn archive_read_disk_new
Packit 08bd4c
Allocates and initializes a
Packit 08bd4c
.Tn struct archive
Packit 08bd4c
object suitable for reading object information from disk.
Packit 08bd4c
.It Fn archive_read_disk_set_behavior
Packit 08bd4c
Configures various behavior options when reading entries from disk.
Packit 08bd4c
The flags field consists of a bitwise OR of one or more of the
Packit 08bd4c
following values:
Packit 08bd4c
.Bl -tag -compact -width "indent"
Packit 08bd4c
.It Cm ARCHIVE_READDISK_HONOR_NODUMP
Packit 08bd4c
Skip files and directories with the nodump file attribute (file flag) set.
Packit 08bd4c
By default, the nodump file atrribute is ignored.
Packit 08bd4c
.It Cm ARCHIVE_READDISK_MAC_COPYFILE
Packit 08bd4c
Mac OS X specific. Read metadata (ACLs and extended attributes) with
Packit 08bd4c
.Xr copyfile 3 .
Packit 08bd4c
By default, metadata is read using
Packit 08bd4c
.Xr copyfile 3 .
Packit 08bd4c
.It Cm ARCHIVE_READDISK_NO_ACL
Packit 08bd4c
Do not read Access Control Lists.
Packit 08bd4c
By default, ACLs are read from disk.
Packit 08bd4c
.It Cm ARCHIVE_READDISK_NO_FFLAGS
Packit 08bd4c
Do not read file attributes (file flags).
Packit 08bd4c
By default, file attributes are read from disk.
Packit 08bd4c
See
Packit 08bd4c
.Xr chattr 1
Packit 08bd4c
.Pq Linux
Packit 08bd4c
or
Packit 08bd4c
.Xr chflags 1
Packit 08bd4c
.Pq FreeBSD, Mac OS X
Packit 08bd4c
for more information on file attributes.
Packit 08bd4c
.It Cm ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS
Packit 08bd4c
Do not traverse mount points.
Packit 08bd4c
By defaut, moint points are traversed.
Packit 08bd4c
.It Cm ARCHIVE_READDISK_NO_XATTR
Packit 08bd4c
Do not read extended file attributes (xattrs).
Packit 08bd4c
By default, extended file attributes are read from disk.
Packit 08bd4c
See
Packit 08bd4c
.Xr xattr 7
Packit 08bd4c
.Pq Linux ,
Packit 08bd4c
.Xr xattr 2
Packit 08bd4c
.Pq Mac OS X ,
Packit 08bd4c
or
Packit 08bd4c
.Xr getextattr 8
Packit 08bd4c
.Pq FreeBSD
Packit 08bd4c
for more information on extended file attributes.
Packit 08bd4c
.It Cm ARCHIVE_READDISK_RESTORE_ATIME
Packit 08bd4c
Restore access time of traversed files.
Packit 08bd4c
By default, access time of traversed files is not restored.
Packit 08bd4c
.El
Packit 08bd4c
.It Xo
Packit 08bd4c
.Fn archive_read_disk_set_symlink_logical ,
Packit 08bd4c
.Fn archive_read_disk_set_symlink_physical ,
Packit 08bd4c
.Fn archive_read_disk_set_symlink_hybrid
Packit 08bd4c
.Xc
Packit 08bd4c
This sets the mode used for handling symbolic links.
Packit 08bd4c
The
Packit 08bd4c
.Dq logical
Packit 08bd4c
mode follows all symbolic links.
Packit 08bd4c
The
Packit 08bd4c
.Dq physical
Packit 08bd4c
mode does not follow any symbolic links.
Packit 08bd4c
The
Packit 08bd4c
.Dq hybrid
Packit 08bd4c
mode currently behaves identically to the
Packit 08bd4c
.Dq logical
Packit 08bd4c
mode.
Packit 08bd4c
.It Xo
Packit 08bd4c
.Fn archive_read_disk_gname ,
Packit 08bd4c
.Fn archive_read_disk_uname
Packit 08bd4c
.Xc
Packit 08bd4c
Returns a user or group name given a gid or uid value.
Packit 08bd4c
By default, these always return a NULL string.
Packit 08bd4c
.It Xo
Packit 08bd4c
.Fn archive_read_disk_set_gname_lookup ,
Packit 08bd4c
.Fn archive_read_disk_set_uname_lookup
Packit 08bd4c
.Xc
Packit 08bd4c
These allow you to override the functions used for
Packit 08bd4c
user and group name lookups.
Packit 08bd4c
You may also provide a
Packit 08bd4c
.Tn void *
Packit 08bd4c
pointer to a private data structure and a cleanup function for
Packit 08bd4c
that data.
Packit 08bd4c
The cleanup function will be invoked when the
Packit 08bd4c
.Tn struct archive
Packit 08bd4c
object is destroyed or when new lookup functions are registered.
Packit 08bd4c
.It Fn archive_read_disk_set_standard_lookup
Packit 08bd4c
This convenience function installs a standard set of user
Packit 08bd4c
and group name lookup functions.
Packit 08bd4c
These functions use
Packit 08bd4c
.Xr getpwuid 3
Packit 08bd4c
and
Packit 08bd4c
.Xr getgrgid 3
Packit 08bd4c
to convert ids to names, defaulting to NULL if the names cannot
Packit 08bd4c
be looked up.
Packit 08bd4c
These functions also implement a simple memory cache to reduce
Packit 08bd4c
the number of calls to
Packit 08bd4c
.Xr getpwuid 3
Packit 08bd4c
and
Packit 08bd4c
.Xr getgrgid 3 .
Packit 08bd4c
.It Fn archive_read_disk_entry_from_file
Packit 08bd4c
Populates a
Packit 08bd4c
.Tn struct archive_entry
Packit 08bd4c
object with information about a particular file.
Packit 08bd4c
The
Packit 08bd4c
.Tn archive_entry
Packit 08bd4c
object must have already been created with
Packit 08bd4c
.Xr archive_entry_new 3
Packit 08bd4c
and at least one of the source path or path fields must already be set.
Packit 08bd4c
(If both are set, the source path will be used.)
Packit 08bd4c
.Pp
Packit 08bd4c
Information is read from disk using the path name from the
Packit 08bd4c
.Tn struct archive_entry
Packit 08bd4c
object.
Packit 08bd4c
If a file descriptor is provided, some information will be obtained using
Packit 08bd4c
that file descriptor, on platforms that support the appropriate
Packit 08bd4c
system calls.
Packit 08bd4c
.Pp
Packit 08bd4c
If a pointer to a
Packit 08bd4c
.Tn struct stat
Packit 08bd4c
is provided, information from that structure will be used instead
Packit 08bd4c
of reading from the disk where appropriate.
Packit 08bd4c
This can provide performance benefits in scenarios where
Packit 08bd4c
.Tn struct stat
Packit 08bd4c
information has already been read from the disk as a side effect
Packit 08bd4c
of some other operation.
Packit 08bd4c
(For example, directory traversal libraries often provide this information.)
Packit 08bd4c
.Pp
Packit 08bd4c
Where necessary, user and group ids are converted to user and group names
Packit 08bd4c
using the currently registered lookup functions above.
Packit 08bd4c
This affects the file ownership fields and ACL values in the
Packit 08bd4c
.Tn struct archive_entry
Packit 08bd4c
object.
Packit 08bd4c
.El
Packit 08bd4c
More information about the
Packit 08bd4c
.Va struct archive
Packit 08bd4c
object and the overall design of the library can be found in the
Packit 08bd4c
.Xr libarchive 3
Packit 08bd4c
overview.
Packit 08bd4c
.Sh EXAMPLE
Packit 08bd4c
The following illustrates basic usage of the library by
Packit 08bd4c
showing how to use it to copy an item on disk into an archive.
Packit 08bd4c
.Bd -literal -offset indent
Packit 08bd4c
void
Packit 08bd4c
file_to_archive(struct archive *a, const char *name)
Packit 08bd4c
{
Packit 08bd4c
  char buff[8192];
Packit 08bd4c
  size_t bytes_read;
Packit 08bd4c
  struct archive *ard;
Packit 08bd4c
  struct archive_entry *entry;
Packit 08bd4c
  int fd;
Packit 08bd4c
Packit 08bd4c
  ard = archive_read_disk_new();
Packit 08bd4c
  archive_read_disk_set_standard_lookup(ard);
Packit 08bd4c
  entry = archive_entry_new();
Packit 08bd4c
  fd = open(name, O_RDONLY);
Packit 08bd4c
  if (fd < 0)
Packit 08bd4c
     return;
Packit 08bd4c
  archive_entry_copy_pathname(entry, name);
Packit 08bd4c
  archive_read_disk_entry_from_file(ard, entry, fd, NULL);
Packit 08bd4c
  archive_write_header(a, entry);
Packit 08bd4c
  while ((bytes_read = read(fd, buff, sizeof(buff))) > 0)
Packit 08bd4c
    archive_write_data(a, buff, bytes_read);
Packit 08bd4c
  archive_write_finish_entry(a);
Packit 08bd4c
  archive_read_free(ard);
Packit 08bd4c
  archive_entry_free(entry);
Packit 08bd4c
}
Packit 08bd4c
.Ed
Packit 08bd4c
.Sh RETURN VALUES
Packit 08bd4c
Most functions return
Packit 08bd4c
.Cm ARCHIVE_OK
Packit 08bd4c
(zero) on success, or one of several negative
Packit 08bd4c
error codes for errors.
Packit 08bd4c
Specific error codes include:
Packit 08bd4c
.Cm ARCHIVE_RETRY
Packit 08bd4c
for operations that might succeed if retried,
Packit 08bd4c
.Cm ARCHIVE_WARN
Packit 08bd4c
for unusual conditions that do not prevent further operations, and
Packit 08bd4c
.Cm ARCHIVE_FATAL
Packit 08bd4c
for serious errors that make remaining operations impossible.
Packit 08bd4c
.Pp
Packit 08bd4c
.Fn archive_read_disk_new
Packit 08bd4c
returns a pointer to a newly-allocated
Packit 08bd4c
.Tn struct archive
Packit 08bd4c
object or NULL if the allocation failed for any reason.
Packit 08bd4c
.Pp
Packit 08bd4c
.Fn archive_read_disk_gname
Packit 08bd4c
and
Packit 08bd4c
.Fn archive_read_disk_uname
Packit 08bd4c
return
Packit 08bd4c
.Tn const char *
Packit 08bd4c
pointers to the textual name or NULL if the lookup failed for any reason.
Packit 08bd4c
The returned pointer points to internal storage that
Packit 08bd4c
may be reused on the next call to either of these functions;
Packit 08bd4c
callers should copy the string if they need to continue accessing it.
Packit 08bd4c
.\"
Packit 08bd4c
.Sh ERRORS
Packit 08bd4c
Detailed error codes and textual descriptions are available from the
Packit 08bd4c
.Fn archive_errno
Packit 08bd4c
and
Packit 08bd4c
.Fn archive_error_string
Packit 08bd4c
functions.
Packit 08bd4c
.\"
Packit 08bd4c
.Sh SEE ALSO
Packit 08bd4c
.Xr archive_read 3 ,
Packit 08bd4c
.Xr archive_util 3 ,
Packit 08bd4c
.Xr archive_write 3 ,
Packit 08bd4c
.Xr archive_write_disk 3 ,
Packit 08bd4c
.Xr tar 1 ,
Packit 08bd4c
.Xr libarchive 3
Packit 08bd4c
.Sh HISTORY
Packit 08bd4c
The
Packit 08bd4c
.Nm libarchive
Packit 08bd4c
library first appeared in
Packit 08bd4c
.Fx 5.3 .
Packit 08bd4c
The
Packit 08bd4c
.Nm archive_read_disk
Packit 08bd4c
interface was added to
Packit 08bd4c
.Nm libarchive 2.6
Packit 08bd4c
and first appeared in
Packit 08bd4c
.Fx 8.0 .
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@FreeBSD.org .
Packit 08bd4c
.Sh BUGS
Packit 08bd4c
The
Packit 08bd4c
.Dq standard
Packit 08bd4c
user name and group name lookup functions are not the defaults because
Packit 08bd4c
.Xr getgrgid 3
Packit 08bd4c
and
Packit 08bd4c
.Xr getpwuid 3
Packit 08bd4c
are sometimes too large for particular applications.
Packit 08bd4c
The current design allows the application author to use a more
Packit 08bd4c
compact implementation when appropriate.
Packit 08bd4c
.Pp
Packit 08bd4c
The full list of metadata read from disk by
Packit 08bd4c
.Fn archive_read_disk_entry_from_file
Packit 08bd4c
is necessarily system-dependent.
Packit 08bd4c
.Pp
Packit 08bd4c
The
Packit 08bd4c
.Fn archive_read_disk_entry_from_file
Packit 08bd4c
function reads as much information as it can from disk.
Packit 08bd4c
Some method should be provided to limit this so that clients who
Packit 08bd4c
do not need ACLs, for instance, can avoid the extra work needed
Packit 08bd4c
to look up such information.
Packit 08bd4c
.Pp
Packit 08bd4c
This API should provide a set of methods for walking a directory tree.
Packit 08bd4c
That would make it a direct parallel of the
Packit 08bd4c
.Xr archive_read 3
Packit 08bd4c
API.
Packit 08bd4c
When such methods are implemented, the
Packit 08bd4c
.Dq hybrid
Packit 08bd4c
symbolic link mode will make sense.