}
PAKFIRE_EXPORT int pakfire_archive_read(struct pakfire_archive* archive, const char* filename,
- char** data, size_t* data_size) {
+ char** data, size_t* size) {
struct archive* a = NULL;
struct archive_entry* entry = NULL;
int r = 0;
+ if (!filename || !data || !size) {
+ errno = EINVAL;
+ return 1;
+ }
+
// Strip leading / from filenames, because the payload does
// not have leading slashes.
while (*filename == '/')
goto ERROR;
r = pakfire_archive_copy_data_to_buffer(archive->pakfire, payload, entry,
- data, data_size);
+ data, size);
ERROR:
if (payload)
return r;
}
+static int test_read(const struct test* t) {
+ struct pakfire_archive* archive = NULL;
+ char* data = NULL;
+ size_t length = 0;
+ int r = EXIT_FAILURE;
+
+ const char beep[] = {
+ 0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ };
+
+ // Open the archive
+ ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, TEST_SRC_PATH TEST_PKG1_PATH));
+
+ // Read a file
+ ASSERT_SUCCESS(pakfire_archive_read(archive, "/usr/bin/beep", &data, &length));
+
+ // Check filesize
+ ASSERT(length == 14552);
+
+ // Check the first couple of bytes
+ ASSERT_COMPARE(data, beep, sizeof(beep));
+
+ if (data) {
+ free(data);
+ data = NULL;
+ }
+
+ // Try to access a file that does not exist
+ ASSERT_ERRNO(pakfire_archive_read(archive, "/does/not/exist", &data, &length), ENOENT);
+
+ // Some invalid calls
+ ASSERT_ERRNO(pakfire_archive_read(archive, NULL, &data, &length), EINVAL);
+ ASSERT_ERRNO(pakfire_archive_read(archive, "/does/not/exist", NULL, &length), EINVAL);
+ ASSERT_ERRNO(pakfire_archive_read(archive, "/does/not/exist", &data, NULL), EINVAL);
+
+ // Everything passed
+ r = EXIT_SUCCESS;
+
+FAIL:
+ if (archive)
+ pakfire_archive_unref(archive);
+ if (data)
+ free(data);
+
+ return r;
+}
+
static int test_filelist(const struct test* t) {
const char* path = TEST_SRC_PATH TEST_PKG1_PATH;
int r = EXIT_FAILURE;
testsuite_add_test(test_open);
testsuite_add_test(test_open_directory);
testsuite_add_test(test_get);
+ testsuite_add_test(test_read);
testsuite_add_test(test_filelist);
testsuite_add_test(test_extract);
testsuite_add_test(test_import);
#define ASSERT_SUCCESS(expr) \
do { \
if ((expr)) { \
- LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \
- __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ LOG_ERROR("Failed assertion: %s (errno = %s) at %s:%d %s\n", \
+ #expr, strerror(errno), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
goto FAIL; \
} \
} while (0)
} \
} while (0)
+#define ASSERT_COMPARE(value1, value2, length) \
+ do { \
+ if (!value1) { \
+ LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
+ #value1, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ goto FAIL; \
+ } \
+ if (!value2) { \
+ LOG_ERROR("Failed assertion: %s is NULL at %s:%d %s\n", \
+ #value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ goto FAIL; \
+ } \
+ if (!length) { \
+ LOG_ERROR("Failed assertion: %s is zero at %s:%d %s\n", \
+ #length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ goto FAIL; \
+ } \
+ if (memcmp(value1, value2, length) != 0) { \
+ LOG_ERROR("Failed assertion: %s != %s (length = %zu) at %s:%d %s\n", \
+ #value1, #value2, length, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+ goto FAIL; \
+ } \
+ } while (0)
+
// Helper functions
FILE* test_mktemp(char** path);
char* test_mkdtemp();