}
PAKFIRE_EXPORT char* pakfire_archive_extraction_path(PakfireArchive archive, const char* target) {
+ char prefix[PATH_MAX];
+
PakfireRepo repo = pakfire_repo_create(archive->pakfire, "dummy");
// Read package metadata
char* nevra = pakfire_package_get_nevra(pkg);
// Append package name and version to path
- char* prefix = pakfire_path_join(target, nevra);
+ int r = pakfire_path_join(prefix, sizeof(prefix), target, nevra);
// Cleanup
pakfire_package_unref(pkg);
pakfire_repo_unref(repo);
free(nevra);
- return prefix;
+ if (r < 0)
+ return NULL;
+
+ return strdup(prefix);
}
struct pakfire_archive_extractor {
struct pakfire_archive_extractor* extractor = (struct pakfire_archive_extractor*)data;
PakfireFile file = NULL;
- char* buffer;
+ char buffer[PATH_MAX];
int r = 1;
// Create a new file object if there is a filelist
// Prepend the prefix
if (extractor->prefix) {
- buffer = pakfire_path_join(extractor->prefix, path);
- if (!buffer)
+ r = pakfire_path_join(buffer, sizeof(buffer) - 1, extractor->prefix, path);
+ if (r < 0)
goto ERROR;
archive_entry_set_pathname(entry, buffer);
- free(buffer);
// Update hardlink destination
const char* link = archive_entry_hardlink(entry);
if (link) {
- buffer = pakfire_path_join(extractor->prefix, link);
- if (!buffer)
+ r = pakfire_path_join(buffer, sizeof(buffer) - 1, extractor->prefix, link);
+ if (r < 0)
goto ERROR;
archive_entry_set_hardlink(entry, buffer);
- free(buffer);
}
}
char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data) {
const char* root = pakfire_get_path(pakfire);
- // Write the scriptlet to disk
- char* path = pakfire_path_join(root, "tmp/.pakfire-script.XXXXXX");
+ char path[PATH_MAX];
int r;
+ // Write the scriptlet to disk
+ r = pakfire_path_join(path, sizeof(path) - 1, root, "tmp/.pakfire-script.XXXXXX");
+ if (r < 0)
+ goto out;
+
// Open a temporary file
int fd = mkstemp(path);
if (fd < 0) {
out:
// Remove script from disk
- unlink(path);
-
- // Cleanup
- free(path);
+ if (*path)
+ unlink(path);
return r;
}
char* pakfire_format_size(double size);
char* pakfire_format_date(time_t t);
-char* pakfire_path_join(const char* first, const char* second);
const char* pakfire_path_relpath(const char* root, const char* path);
int pakfire_path_isdir(const char* path);
char* pakfire_hexlify(const char* digest, const size_t length);
+int pakfire_path_join(char* dest, size_t length,
+ const char* first, const char* second);
+
int pakfire_mkdir(const char* path, mode_t mode);
FILE* pakfire_mktemp(char* path);
char* pakfire_mkdtemp(char* path);
#include <errno.h>
#include <gpgme.h>
+#include <linux/limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
// Set output to be ASCII armoured
gpgme_set_armor(ctx, 1);
+ char home[PATH_MAX];
+
// Use GPG
const char* path = pakfire_get_path(pakfire);
- char* home = pakfire_path_join(path, "etc/pakfire/gnupg");
+ int r = pakfire_path_join(home, sizeof(home) - 1, path, "etc/pakfire/gnupg");
+ if (r < 0)
+ goto FAIL;
// Check if gpg directories exist
if (pakfire_access(pakfire, home, NULL, R_OK) != 0) {
// Setup engine
error = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_OpenPGP, NULL, home);
- free(home);
if (gpg_err_code(error) != GPG_ERR_NO_ERROR)
goto FAIL;
pakfire_dirname;
pakfire_generate_uuid;
pakfire_path_isdir;
- pakfire_path_join;
pakfire_path_relpath;
pakfire_read_file_into_buffer;
pakfire_split_string;
}
static int pakfire_mount(Pakfire pakfire) {
+ char target[PATH_MAX];
int r;
for (const struct pakfire_mountpoint* mp = mountpoints; mp->source; mp++) {
DEBUG(pakfire, "Mounting /%s\n", mp->target);
- char* target = pakfire_path_join(pakfire->path, mp->target);
+ r = pakfire_path_join(target, sizeof(target) - 1, pakfire->path, mp->target);
+ if (r < 0)
+ return r;
RETRY:
// Perform mount()
r = pakfire_mkdir(target, S_IRUSR|S_IWUSR|S_IXUSR);
if (r) {
ERROR(pakfire, "Could not create %s\n", target);
- free(target);
return r;
}
}
ERROR(pakfire, "Could not mount /%s: %s\n", mp->target, strerror(errno));
- free(target);
return r;
}
-
- free(target);
}
return 0;
}
PAKFIRE_EXPORT char* pakfire_make_path(Pakfire pakfire, const char* path) {
+ char buffer[PATH_MAX];
+
// Make sure that path never starts with /
while (path && *path == '/')
path++;
- return pakfire_path_join(pakfire->path, path);
+ int r = pakfire_path_join(buffer, sizeof(buffer) - 1, pakfire->path, path);
+ if (r < 0)
+ return NULL;
+
+ return strdup(buffer);
}
PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags) {
#include <fcntl.h>
#include <ftw.h>
#include <libgen.h>
+#include <linux/limits.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
return pakfire_strftime("%Y-%m-%d", t);
}
-PAKFIRE_EXPORT char* pakfire_path_join(const char* first, const char* second) {
- char* buffer;
-
- if (!first)
- return strdup(second);
-
+PAKFIRE_EXPORT int pakfire_path_join(char* dest, size_t length,
+ const char* first, const char* second) {
if (!second)
- return strdup(first);
-
- if (*second == '/')
- return strdup(second);
+ return snprintf(dest, length, "%s", first);
- asprintf(&buffer, "%s/%s", first, second);
+ if (!first || *second == '/')
+ return snprintf(dest, length, "%s", second);
- return buffer;
+ return snprintf(dest, length, "%s/%s", first, second);
}
PAKFIRE_EXPORT const char* pakfire_path_relpath(const char* root, const char* path) {
}
PAKFIRE_EXPORT int pakfire_access(Pakfire pakfire, const char* dir, const char* file, int mode) {
- char* path = pakfire_path_join(dir, file);
+ char path[PATH_MAX];
- int r = access(path, mode);
+ int r = pakfire_path_join(path, sizeof(path) - 1, dir, file);
+ if (r < 0)
+ return r;
+
+ r = access(path, mode);
if (r) {
if (mode & R_OK)
DEBUG(pakfire, "%s does not exist\n", path);
}
- free(path);
-
return r;
}
#include "../testsuite.h"
-static const char* TEST_PKG1_PATH = "data/beep-1.3-2.ip3.x86_64.pfm";
-static const char* TEST_PKG1_FILE = "usr/bin/beep";
+#define TEST_PKG1_PATH "data/beep-1.3-2.ip3.x86_64.pfm"
+#define TEST_PKG1_FILE "usr/bin/beep"
static int test_open(const struct test* t) {
- char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH);
+ const char* path = TEST_SRC_PATH TEST_PKG1_PATH;
LOG("Trying to open %s\n", path);
// Open the archive
ASSERT(verify == PAKFIRE_ARCHIVE_VERIFY_OK);
pakfire_archive_unref(archive);
- free(path);
return EXIT_SUCCESS;
}
static int test_filelist(const struct test* t) {
- char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH);
+ const char* path = TEST_SRC_PATH TEST_PKG1_PATH;
// Open the archive
PakfireArchive archive;
ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path));
- // Free path
- free(path);
-
// Fetch the filelist
PakfireFilelist list = pakfire_archive_get_filelist(archive);
ASSERT(list);
}
static int test_extract(const struct test* t) {
- char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH);
+ const char* path = TEST_SRC_PATH TEST_PKG1_PATH;
PakfireArchive archive;
ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path));
- free(path);
// Extract the archive payload
int r = pakfire_archive_extract(archive, NULL, PAKFIRE_ARCHIVE_USE_PAYLOAD);
}
static int test_import(const struct test* t) {
- char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH);
+ const char* path = TEST_SRC_PATH TEST_PKG1_PATH;
PakfireArchive archive;
ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path));
- free(path);
PakfireRepo repo = pakfire_repo_create(t->pakfire, "tmp");
ASSERT(repo);
# #
#############################################################################*/
+#include <linux/limits.h>
#include <stdio.h>
+#include <string.h>
#include <pakfire/compress.h>
#include <pakfire/util.h>
static int read_test(const struct test* t,
FILE* (function)(FILE* f, const char* mode), const char* file) {
+ char path[PATH_MAX];
char buffer[1024];
- char* path = pakfire_path_join(TEST_SRC_PATH, file);
- ASSERT(path);
+ int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, file);
+ ASSERT(r >= 0);
FILE* f = fopen(path, "r");
ASSERT(f);
ASSERT(memcmp(buffer, TEST_DATA, sizeof(TEST_DATA) - 1) == 0);
fclose(f);
- free(path);
return EXIT_SUCCESS;
}
ssize_t packages = pakfire_db_packages(db);
ASSERT(packages == 0);
- char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep-1.3-2.ip3.x86_64.pfm");
+ const char* path = TEST_SRC_PATH "/data/beep-1.3-2.ip3.x86_64.pfm";
// Open archive
PakfireArchive archive;
pakfire_db_unref(db);
pakfire_package_unref(pkg);
pakfire_repo_unref(repo);
- free(path);
return 0;
}
#############################################################################*/
#include <glob.h>
+#include <linux/limits.h>
#include <stdlib.h>
#include <string.h>
};
static int load_macros(PakfireParser parser) {
- char* macros = pakfire_path_join(TEST_SRC_PATH, "../macros/*.macro");
- ASSERT(macros);
+ const char* macros = TEST_SRC_PATH "../macros/*.macro";
glob_t buffer;
int r = glob(macros, 0, NULL, &buffer);
ASSERT(r == 0);
}
- free(macros);
-
return 0;
}
static int test_parse(const struct test* t) {
const char** makefile = makefiles;
+ char path[PATH_MAX];
while (*makefile) {
- char* path = pakfire_path_join(TEST_SRC_PATH, *makefile);
+ int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *makefile);
+ ASSERT(r >= 0);
// Open file
FILE* f = fopen(path, "r");
PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
- int r = pakfire_parser_read(parser, f, NULL);
+ r = pakfire_parser_read(parser, f, NULL);
ASSERT(r == 0);
pakfire_parser_unref(parser);
- free(path);
fclose(f);
// Next file
return r;
// Read beep.nm
- char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep.nm");
- ASSERT(path);
+ const char* path = TEST_SRC_PATH "data/beep.nm";
r = pakfire_parser_read_file(parser, path, NULL);
ASSERT(r == 0);
pakfire_parser_unref(parser);
pakfire_package_unref(pkg);
pakfire_repo_unref(repo);
- free(path);
return EXIT_SUCCESS;
}
ASSERT(r == 0);
// Add a file to the package
- char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep-1.3-2.ip3.x86_64.pfm");
- ASSERT(path);
+ const char* path = TEST_SRC_PATH "data/beep-1.3-2.ip3.x86_64.pfm";
r = pakfire_packager_add(packager, path, NULL);
ASSERT(r == 0);
pakfire_packager_unref(packager);
pakfire_package_unref(pkg);
pakfire_repo_unref(repo);
- free(path);
return EXIT_SUCCESS;
}
# #
#############################################################################*/
+#include <linux/limits.h>
#include <stdlib.h>
#include <string.h>
static int test_parser_files(const struct test* t) {
const char** file = files;
+ char path[PATH_MAX];
while (*file) {
- char* path = pakfire_path_join(TEST_SRC_PATH, *file);
- ASSERT(path);
+ int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *file);
+ ASSERT(r >= 0);
// Create a new parser
PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
FILE* f = fopen(path, "r");
ASSERT(f);
- int r = pakfire_parser_read(parser, f, NULL);
+ r = pakfire_parser_read(parser, f, NULL);
if (r) {
fprintf(stderr, "Could not parse %s\n", path);
return EXIT_FAILURE;
fclose(f);
pakfire_parser_unref(parser);
- free(path);
// Next file
file++;
#include <pakfire/logging.h>
#include <pakfire/pakfire.h>
-const char* TEST_SRC_PATH = ABS_TOP_SRCDIR "/tests";
-
struct testsuite ts;
static int test_run(int i, struct test* t) {
#define MAX_TESTS 128
-extern const char* TEST_SRC_PATH;
+#define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/"
struct test {
const char* name;