ARCHIVE_READ_OPEN(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

✨ ARCHIVE_READ_OPEN ✨

okay bestie, so this one lives in the Library Functions Manual β€” it's the libarchive streaming-archive reading chapter of your life πŸ’… (that's the Streaming Archive Library, a.k.a. libarchive, and you link it with -larchive, iykyk).


🌸 SYNOPSIS 🌸

like, first of all, include the thing:

#include <archive.h>

and then your options are giving very much pick your fighter:

int
archive_read_open(struct archive *, void *client_data,
    archive_open_callback *, archive_read_callback *,
    archive_close_callback *);

int
archive_read_open2(struct archive *, void *client_data,
    archive_open_callback *, archive_read_callback *,
    archive_skip_callback *, archive_close_callback *);

int
archive_read_open_FILE(struct archive *, FILE *file);

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

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

int
archive_read_open_memory(struct archive *, const void *buff,
    size_t size);

πŸ’– DESCRIPTION πŸ’–

okay so here's the whole vibe β€” each function, one by one, no cap:

  • archive_read_open() β€” literally the same as archive_read_open2(), except the skip callback is assumed to be NULL. that's it. that's the whole difference 🫢
  • archive_read_open2() β€” this is the moment you freeze the settings, open the archive, and get ready to read entries ✨. it's the most generic version of this call, and it accepts four callback functions. most clients will want archive_read_open_filename(), archive_read_open_FILE(), archive_read_open_fd(), or archive_read_open_memory() instead, bestie. the library invokes the client-provided functions to obtain raw bytes from the archive.
  • archive_read_open_FILE() β€” like archive_read_open(), except it accepts a FILE * pointer. ⚠️ this one should not be used with tape drives or other devices that require strict I/O blocking. (not me warning you early, love that for you)
  • archive_read_open_fd() β€” like archive_read_open(), except it accepts a file descriptor and block size rather than a set of function pointers. note that the file descriptor will not be automatically closed at end-of-archive. this function is safe for use with tape drives or other blocked devices πŸ”₯
  • archive_read_open_file() β€” this is a deprecated synonym for archive_read_open_filename(). she's iconic but she's retired, mwah πŸ’…
  • archive_read_open_filename() β€” like archive_read_open(), except it accepts a simple filename and a block size. a NULL filename represents standard input πŸ‘‘. this function is safe for use with tape drives or other blocked devices.
  • archive_read_open_memory() β€” like archive_read_open(), except it accepts a pointer and size of a block of memory containing the archive data. memory archive era 🧚

a complete description of the struct archive and struct archive_entry objects can be found in the overview manual page for libarchive(3). go read it, it's giving main character energy.


πŸ¦‹ CLIENT CALLBACKS πŸ¦‹

the callback functions must match these prototypes, fr fr:

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

typedef la_int64_t archive_skip_callback(struct archive *,
void *client_data, off_t request)

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

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

and now the tea on each one β˜•

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 error code and message and return ARCHIVE_FATAL. that's the whole assignment, slay πŸ’€

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 return -1.

the skip callback is invoked when the library wants to ignore a block of data. the return value is the number of bytes actually skipped, which may differ from the request. if the callback cannot skip data, it should return zero. if the skip callback is not provided (the function pointer is NULL), the library will invoke the read function instead and simply discard the result. a skip callback can provide significant performance gains when reading uncompressed archives from slow disk drives or other media that can skip quickly πŸ”₯πŸ”₯ this is the glow-up you didn't know you needed.

the close callback is invoked by archive_close when the archive processing 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 return ARCHIVE_FATAL. clean exit, periodt ✨


πŸ’œ RETURN VALUES πŸ’œ

these functions return ARCHIVE_OK on success, or ARCHIVE_FATAL. that's the vibe check β€” either we ate or we didn't πŸ™ˆ


🌈 ERRORS 🌈

detailed error codes and textual descriptions are available from the archive_errno() and archive_error_string() functions. so like, when something goes wrong, that's where you look πŸ‘€


⭐ SEE ALSO ⭐

tar(1), archive_read(3), archive_read_data(3), archive_read_filter(3), archive_read_format(3), archive_read_set_options(3), archive_util(3), libarchive(3), tar(5)

go read them all, bestie, you'll be so educated πŸŽ€


ngl the whole page is basically "here's six ways to open a streaming archive and here's what your callbacks gotta do" β€” pick the one that matches how you're getting your bytes and you're golden πŸ’–

that's the whole scroll 🫢 this page came from Debian, dated February 2, 2012 β€” yes, 2012, she's been serving libarchive knowledge since forever. mwah πŸ’…βœ¨

πŸ“¦ where this page comes from