Blame libarchive/archive_entry_linkify.3

Packit 08bd4c
.\" Copyright (c) 2010 Joerg Sonnenberger
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
.Dd February 2, 2012
Packit 08bd4c
.Dt ARCHIVE_ENTRY_LINKIFY 3
Packit 08bd4c
.Os
Packit 08bd4c
.Sh NAME
Packit 08bd4c
.Nm archive_entry_linkresolver ,
Packit 08bd4c
.Nm archive_entry_linkresolver_new ,
Packit 08bd4c
.Nm archive_entry_linkresolver_set_strategy ,
Packit 08bd4c
.Nm archive_entry_linkresolver_free ,
Packit 08bd4c
.Nm archive_entry_linkify
Packit 08bd4c
.Nd hardlink resolver functions
Packit 08bd4c
.Sh LIBRARY
Packit 08bd4c
Streaming Archive Library (libarchive, -larchive)
Packit 08bd4c
.Sh SYNOPSIS
Packit 08bd4c
.In archive_entry.h
Packit 08bd4c
.Ft struct archive_entry_linkresolver *
Packit 08bd4c
.Fn archive_entry_linkresolver_new void
Packit 08bd4c
.Ft void
Packit 08bd4c
.Fo archive_entry_linkresolver_set_strategy
Packit 08bd4c
.Fa "struct archive_entry_linkresolver *resolver"
Packit 08bd4c
.Fa "int format"
Packit 08bd4c
.Fc
Packit 08bd4c
.Ft void
Packit 08bd4c
.Fo archive_entry_linkresolver_free
Packit 08bd4c
.Fa "struct archive_entry_linkresolver *resolver"
Packit 08bd4c
.Fc
Packit 08bd4c
.Ft void
Packit 08bd4c
.Fo archive_entry_linkify
Packit 08bd4c
.Fa "struct archive_entry_linkresolver *resolver"
Packit 08bd4c
.Fa "struct archive_entry **entry"
Packit 08bd4c
.Fa "struct archive_entry **sparse"
Packit 08bd4c
.Fc
Packit 08bd4c
.Sh DESCRIPTION
Packit 08bd4c
Programs that want to create archives have to deal with hardlinks.
Packit 08bd4c
Hardlinks are handled in different ways by the archive formats.
Packit 08bd4c
The basic strategies are:
Packit 08bd4c
.Bl -enum
Packit 08bd4c
.It
Packit 08bd4c
Ignore hardlinks and store the body for each reference (old cpio, zip).
Packit 08bd4c
.It
Packit 08bd4c
Store the body the first time an inode is seen (ustar, pax).
Packit 08bd4c
.It
Packit 08bd4c
Store the body the last time an inode is seen (new cpio).
Packit 08bd4c
.El
Packit 08bd4c
.Pp
Packit 08bd4c
The
Packit 08bd4c
.Nm
Packit 08bd4c
functions help by providing a unified interface and handling the complexity
Packit 08bd4c
behind the scene.
Packit 08bd4c
.Pp
Packit 08bd4c
The
Packit 08bd4c
.Nm
Packit 08bd4c
functions assume that
Packit 08bd4c
.Vt archive_entry
Packit 08bd4c
instances have valid nlinks, inode and device values.
Packit 08bd4c
The inode and device value is used to match entries.
Packit 08bd4c
The nlinks value is used to determined if all references have been found and
Packit 08bd4c
if the internal references can be recycled.
Packit 08bd4c
.Pp
Packit 08bd4c
The
Packit 08bd4c
.Fn archive_entry_linkresolver_new
Packit 08bd4c
function allocates a new link resolver.
Packit 08bd4c
The instance can be freed using
Packit 08bd4c
.Fn archive_entry_linkresolver_free .
Packit 08bd4c
All deferred entries are flushed and the internal storage is freed.
Packit 08bd4c
.Pp
Packit 08bd4c
The
Packit 08bd4c
.Fn archive_entry_linkresolver_set_strategy
Packit 08bd4c
function selects the optimal hardlink strategy for the given format.
Packit 08bd4c
The format code can be obtained from
Packit 08bd4c
.Xr archive_format 3 .
Packit 08bd4c
The function can be called more than once, but it is recommended to
Packit 08bd4c
flush all deferred entries first.
Packit 08bd4c
.Pp
Packit 08bd4c
The
Packit 08bd4c
.Fn archive_entry_linkify
Packit 08bd4c
function is the core of
Packit 08bd4c
.Nm .
Packit 08bd4c
The
Packit 08bd4c
.Fn entry
Packit 08bd4c
argument points to the
Packit 08bd4c
.Vt archive_entry
Packit 08bd4c
that should be written.
Packit 08bd4c
Depending on the strategy one of the following actions is taken:
Packit 08bd4c
.Bl -enum
Packit 08bd4c
.It
Packit 08bd4c
For the simple archive formats
Packit 08bd4c
.Va *entry
Packit 08bd4c
is left unmodified and
Packit 08bd4c
.Va *sparse
Packit 08bd4c
is set to
Packit 08bd4c
.Dv NULL .
Packit 08bd4c
.It
Packit 08bd4c
For tar like archive formats,
Packit 08bd4c
.Va *sparse
Packit 08bd4c
is set to
Packit 08bd4c
.Dv NULL .
Packit 08bd4c
If
Packit 08bd4c
.Va *entry
Packit 08bd4c
is
Packit 08bd4c
.Dv NULL ,
Packit 08bd4c
no action is taken.
Packit 08bd4c
If the hardlink count of
Packit 08bd4c
.Va *entry
Packit 08bd4c
is larger than 1 and the file type is a regular file or symbolic link,
Packit 08bd4c
the internal list is searched for a matching inode.
Packit 08bd4c
If such an inode is found, the link count is decremented and the file size
Packit 08bd4c
of
Packit 08bd4c
.Va *entry
Packit 08bd4c
is set to 0 to notify that no body should be written.
Packit 08bd4c
If no such inode is found, a copy of the entry is added to the internal cache
Packit 08bd4c
with a link count reduced by one.
Packit 08bd4c
.It
Packit 08bd4c
For new cpio like archive formats a value for
Packit 08bd4c
.Va *entry
Packit 08bd4c
of
Packit 08bd4c
.Dv NULL
Packit 08bd4c
is used to flush deferred entries.
Packit 08bd4c
In that case
Packit 08bd4c
.Va *entry
Packit 08bd4c
is set to an arbitrary deferred entry and the entry itself is removed from the
Packit 08bd4c
internal list.
Packit 08bd4c
If the internal list is empty,
Packit 08bd4c
.Va *entry
Packit 08bd4c
is set to
Packit 08bd4c
.Dv NULL .
Packit 08bd4c
In either case,
Packit 08bd4c
.Va *sparse
Packit 08bd4c
is set to
Packit 08bd4c
.Dv NULL
Packit 08bd4c
and the function returns.
Packit 08bd4c
If the hardlink count of
Packit 08bd4c
.Va *entry
Packit 08bd4c
is one or the file type is a directory or device,
Packit 08bd4c
.Va *sparse
Packit 08bd4c
is set to
Packit 08bd4c
.Dv NULL
Packit 08bd4c
and no further action is taken.
Packit 08bd4c
Otherwise, the internal list is searched for a matching inode.
Packit 08bd4c
If such an inode is not found, the entry is added to the internal list,
Packit 08bd4c
both
Packit 08bd4c
.Va *entry
Packit 08bd4c
and
Packit 08bd4c
.Va *sparse
Packit 08bd4c
are set to
Packit 08bd4c
.Dv NULL
Packit 08bd4c
and the function returns.
Packit 08bd4c
If such an inode is found, the link count is decremented.
Packit 08bd4c
If it remains larger than one, the existing entry on the internal list
Packit 08bd4c
is swapped with
Packit 08bd4c
.Va *entry
Packit 08bd4c
after retaining the link count.
Packit 08bd4c
The existing entry is returned in
Packit 08bd4c
.Va *entry .
Packit 08bd4c
If the link count reached one, the new entry is also removed from the
Packit 08bd4c
internal list and returned in
Packit 08bd4c
.Va *sparse .
Packit 08bd4c
Otherwise
Packit 08bd4c
.Va *sparse
Packit 08bd4c
is set to
Packit 08bd4c
.Dv NULL .
Packit 08bd4c
.El
Packit 08bd4c
.Pp
Packit 08bd4c
The general usage is therefore:
Packit 08bd4c
.Bl -enum
Packit 08bd4c
.It
Packit 08bd4c
For each new archive entry, call
Packit 08bd4c
.Fn archive_entry_linkify .
Packit 08bd4c
.It
Packit 08bd4c
Keep in mind that the entries returned may have a size of 0 now.
Packit 08bd4c
.It
Packit 08bd4c
If
Packit 08bd4c
.Va *entry
Packit 08bd4c
is not
Packit 08bd4c
.Dv NULL ,
Packit 08bd4c
archive it.
Packit 08bd4c
.It
Packit 08bd4c
If
Packit 08bd4c
.Va *sparse
Packit 08bd4c
is not
Packit 08bd4c
.Dv NULL ,
Packit 08bd4c
archive it.
Packit 08bd4c
.It
Packit 08bd4c
After all entries have been written to disk, call
Packit 08bd4c
.Fn archive_entry_linkify
Packit 08bd4c
with
Packit 08bd4c
.Va *entry
Packit 08bd4c
set to
Packit 08bd4c
.Dv NULL
Packit 08bd4c
and archive the returned entry as long as it is not
Packit 08bd4c
.Dv NULL .
Packit 08bd4c
.El
Packit 08bd4c
.Sh RETURN VALUES
Packit 08bd4c
.Fn archive_entry_linkresolver_new
Packit 08bd4c
returns
Packit 08bd4c
.Dv NULL
Packit 08bd4c
on
Packit 08bd4c
.Xr malloc 3
Packit 08bd4c
failures.
Packit 08bd4c
.Sh SEE ALSO
Packit 08bd4c
.Xr archive_entry 3