#summary ARCHIVE_ENTRY_LINKIFY 3 manual page
== NAME ==
*archive_entry_linkresolver*,
*archive_entry_linkresolver_new*,
*archive_entry_linkresolver_set_strategy*,
*archive_entry_linkresolver_free*,
*archive_entry_linkify*
- hardlink resolver functions
== LIBRARY ==
Lb libarchive
== SYNOPSIS ==
*#include <archive_entry.h>*
<br>
*struct archive_entry_linkresolver `*`*
<br>
*archive_entry_linkresolver_new*(_void_);
<br>
*void*
<br>
*archive_entry_linkresolver_set_strategy*(_struct archive_entry_linkresolver `*`resolver_, _int format_);
<br>
*void*
<br>
*archive_entry_linkresolver_free*(_struct archive_entry_linkresolver `*`resolver_);
<br>
*void*
<br>
*archive_entry_linkify*(_struct archive_entry_linkresolver `*`resolver_, _struct archive_entry `*``*`entry_, _struct archive_entry `*``*`sparse_);
== DESCRIPTION ==
Programs that want to create archives have to deal with hardlinks.
Hardlinks are handled in different ways by the archive formats.
The basic strategies are:
<ol>
<li>
Ignore hardlinks and store the body for each reference (old cpio, zip).
</li><li>
Store the body the first time an inode is seen (ustar, pax).
</li><li>
Store the body the last time an inode is seen (new cpio).
</li></ol>

The
*archive_entry_linkresolver*
functions help by providing a unified interface and handling the complexity
behind the scene.

The
*archive_entry_linkresolver*
functions assume that
_archive_entry_
instances have valid nlinks, inode and device values.
The inode and device value is used to match entries.
The nlinks value is used to determined if all references have been found and
if the internal references can be recycled.

The
*archive_entry_linkresolver_new*()
function allocates a new link resolver.
The instance can be freed using
*archive_entry_linkresolver_free*().
All deferred entries are flushed and the internal storage is freed.

The
*archive_entry_linkresolver_set_strategy*()
function selects the optimal hardlink strategy for the given format.
The format code can be obtained from
*archive_format*(3).
The function can be called more than once, but it is recommended to
flush all deferred entries first.

The
*archive_entry_linkify*()
function is the core of
*archive_entry_linkresolver*.
The
*entry*()
argument points to the
_archive_entry_
that should be written.
Depending on the strategy one of the following actions is taken:
<ol>
<li>
For the simple archive formats
_*entry_
is left unmodified and
_*sparse_
is set to
NULL.
</li><li>
For tar like archive formats,
_*sparse_
is set to
NULL.
If
_*entry_
is
NULL,
no action is taken.
If the hardlink count of
_*entry_
is larger than 1 and the file type is a regular file or symbolic link,
the internal list is searched for a matching inode.
If such an inode is found, the link count is decremented and the file size
of
_*entry_
is set to 0 to notify that no body should be written.
If no such inode is found, a copy of the entry is added to the internal cache
with a link count reduced by one.
</li><li>
For new cpio like archive formats a value for
_*entry_
of
NULL
is used to flush deferred entries.
In that case
_*entry_
is set to an arbitrary deferred entry and the entry itself is removed from the
internal list.
If the internal list is empty,
_*entry_
is set to
NULL.
In either case,
_*sparse_
is set to
NULL
and the function returns.
If the hardlink count of
_*entry_
is one or the file type is a directory or device,
_*sparse_
is set to
NULL
and no further action is taken.
Otherwise, the internal list is searched for a matching inode.
If such an inode is not found, the entry is added to the internal list,
both
_*entry_
and
_*sparse_
are set to
NULL
and the function returns.
If such an inode is found, the link count is decremented.
If it remains larger than one, the existing entry on the internal list
is swapped with
_*entry_
after retaining the link count.
The existing entry is returned in
_*entry_.
If the link count reached one, the new entry is also removed from the
internal list and returned in
_*sparse_.
Otherwise
_*sparse_
is set to
NULL.
</li></ol>

The general usage is therefore:
<ol>
<li>
For each new archive entry, call
*archive_entry_linkify*().
</li><li>
Keep in mind that the entries returned may have a size of 0 now.
</li><li>
If
_*entry_
is not
NULL,
archive it.
</li><li>
If
_*sparse_
is not
NULL,
archive it.
</li><li>
After all entries have been written to disk, call
*archive_entry_linkify*()
with
_*entry_
set to
NULL
and archive the returned entry as long as it is not
NULL.
</li></ol>
== RETURN VALUES ==
*archive_entry_linkresolver_new*()
returns
NULL
on
*malloc*(3)
failures.
== SEE ALSO ==
*archive_entry*(3)
