XBPS Library API 20240111
The X Binary Package System
archive.c
1/*-
2 * Copyright (c) 2008-2014 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <stdio.h>
27#include <stdbool.h>
28#include <stdlib.h>
29#include <string.h>
30#include <errno.h>
31
32#include <archive.h>
33#include <archive_entry.h>
34
35#include "xbps_api_impl.h"
36
37char HIDDEN *
38xbps_archive_get_file(struct archive *ar, struct archive_entry *entry)
39{
40 size_t buflen;
41 ssize_t nbytes = -1;
42 char *buf;
43
44 assert(ar != NULL);
45 assert(entry != NULL);
46
47 buflen = (size_t)archive_entry_size(entry);
48 buf = malloc(buflen+1);
49 if (buf == NULL)
50 return NULL;
51
52 nbytes = archive_read_data(ar, buf, buflen);
53 if ((size_t)nbytes != buflen) {
54 free(buf);
55 return NULL;
56 }
57 buf[buflen] = '\0';
58 return buf;
59}
60
61xbps_dictionary_t HIDDEN
62xbps_archive_get_dictionary(struct archive *ar, struct archive_entry *entry)
63{
64 xbps_dictionary_t d = NULL;
65 char *buf;
66
67 if ((buf = xbps_archive_get_file(ar, entry)) == NULL)
68 return NULL;
69
70 /* If blob is already a dictionary we are done */
71 d = xbps_dictionary_internalize(buf);
72 free(buf);
73 return d;
74}
75
76int
77xbps_archive_append_buf(struct archive *ar, const void *buf, const size_t buflen,
78 const char *fname, const mode_t mode, const char *uname, const char *gname)
79{
80 struct archive_entry *entry;
81
82 assert(ar);
83 assert(buf);
84 assert(fname);
85 assert(uname);
86 assert(gname);
87
88 entry = archive_entry_new();
89 if (entry == NULL)
90 return archive_errno(ar);
91
92 archive_entry_set_filetype(entry, AE_IFREG);
93 archive_entry_set_perm(entry, mode);
94 archive_entry_set_uname(entry, uname);
95 archive_entry_set_gname(entry, gname);
96 archive_entry_set_pathname(entry, fname);
97 archive_entry_set_size(entry, buflen);
98
99 if (archive_write_header(ar, entry) != ARCHIVE_OK) {
100 archive_entry_free(entry);
101 return archive_errno(ar);
102 }
103 if (archive_write_data(ar, buf, buflen) != ARCHIVE_OK) {
104 archive_entry_free(entry);
105 return archive_errno(ar);
106 }
107 if (archive_write_finish_entry(ar) != ARCHIVE_OK) {
108 archive_entry_free(entry);
109 return archive_errno(ar);
110 }
111 archive_entry_free(entry);
112
113 return 0;
114}
int xbps_archive_append_buf(struct archive *ar, const void *buf, const size_t buflen, const char *fname, const mode_t mode, const char *uname, const char *gname)
Definition archive.c:77