2011-02-05 16:27:13 +05:30
|
|
|
/*-
|
2014-10-17 13:40:16 +05:30
|
|
|
* Copyright (c) 2008-2014 Juan Romero Pardines.
|
2011-02-05 16:27:13 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2011-06-04 17:07:53 +05:30
|
|
|
|
2011-02-05 16:27:13 +05:30
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
char HIDDEN *
|
|
|
|
xbps_archive_get_file(struct archive *ar, struct archive_entry *entry)
|
2011-02-05 16:27:13 +05:30
|
|
|
{
|
2012-11-06 13:58:34 +05:30
|
|
|
size_t buflen;
|
2011-02-05 16:27:13 +05:30
|
|
|
ssize_t nbytes = -1;
|
2014-10-17 13:40:16 +05:30
|
|
|
char *buf;
|
2011-02-05 16:27:13 +05:30
|
|
|
|
|
|
|
assert(ar != NULL);
|
|
|
|
assert(entry != NULL);
|
|
|
|
|
|
|
|
buflen = (size_t)archive_entry_size(entry);
|
2014-11-17 20:15:46 +05:30
|
|
|
buf = malloc(buflen+1);
|
2011-02-05 16:27:13 +05:30
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
nbytes = archive_read_data(ar, buf, buflen);
|
|
|
|
if ((size_t)nbytes != buflen) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-17 20:15:46 +05:30
|
|
|
buf[buflen] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
xbps_dictionary_t HIDDEN
|
2019-10-02 00:48:25 +05:30
|
|
|
xbps_archive_get_dictionary(struct archive *ar, struct archive_entry *entry, char **bytes)
|
2014-11-17 20:15:46 +05:30
|
|
|
{
|
|
|
|
xbps_dictionary_t d = NULL;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
if ((buf = xbps_archive_get_file(ar, entry)) == NULL)
|
|
|
|
return NULL;
|
2011-02-05 16:27:13 +05:30
|
|
|
|
2012-11-11 21:27:23 +05:30
|
|
|
/* If blob is already a dictionary we are done */
|
2013-06-20 13:56:12 +05:30
|
|
|
d = xbps_dictionary_internalize(buf);
|
2019-10-02 00:48:25 +05:30
|
|
|
if (bytes == NULL) {
|
|
|
|
free(buf);
|
|
|
|
} else {
|
|
|
|
*bytes = buf;
|
|
|
|
}
|
2014-11-17 20:15:46 +05:30
|
|
|
return d;
|
2011-02-05 16:27:13 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct archive_entry *entry;
|
|
|
|
|
|
|
|
assert(ar);
|
|
|
|
assert(buf);
|
|
|
|
assert(fname);
|
|
|
|
assert(uname);
|
|
|
|
assert(gname);
|
|
|
|
|
|
|
|
entry = archive_entry_new();
|
2019-10-26 02:30:35 +05:30
|
|
|
if (entry == NULL)
|
|
|
|
return archive_errno(ar);
|
2013-06-10 13:58:39 +05:30
|
|
|
|
|
|
|
archive_entry_set_filetype(entry, AE_IFREG);
|
|
|
|
archive_entry_set_perm(entry, mode);
|
|
|
|
archive_entry_set_uname(entry, uname);
|
|
|
|
archive_entry_set_gname(entry, gname);
|
|
|
|
archive_entry_set_pathname(entry, fname);
|
|
|
|
archive_entry_set_size(entry, buflen);
|
|
|
|
|
2013-08-29 15:27:57 +05:30
|
|
|
if (archive_write_header(ar, entry) != ARCHIVE_OK) {
|
|
|
|
archive_entry_free(entry);
|
2013-06-10 13:58:39 +05:30
|
|
|
return archive_errno(ar);
|
2013-08-29 15:27:57 +05:30
|
|
|
}
|
|
|
|
if (archive_write_data(ar, buf, buflen) != ARCHIVE_OK) {
|
|
|
|
archive_entry_free(entry);
|
2013-06-10 13:58:39 +05:30
|
|
|
return archive_errno(ar);
|
2013-08-29 15:27:57 +05:30
|
|
|
}
|
2019-10-26 02:30:35 +05:30
|
|
|
if (archive_write_finish_entry(ar) != ARCHIVE_OK) {
|
|
|
|
archive_entry_free(entry);
|
|
|
|
return archive_errno(ar);
|
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
archive_entry_free(entry);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|