const char* arch = NULL;
int offline = 0;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|zp", kwlist, &path, &arch, &offline))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzp", kwlist, &path, &arch, &offline))
return -1;
// Create a new Pakfire instance
PyErr_SetString(PyExc_ValueError, "Invalid architecture or path");
break;
- // path does not exist
- case ENOENT:
- PyErr_Format(PyExc_FileNotFoundError,
- "%s does not exist or is not a directory", path);
- break;
-
// Anything else
default:
PyErr_SetFromErrno(PyExc_OSError);
#include <ctype.h>
#include <errno.h>
-#include <ftw.h>
#include <linux/limits.h>
#include <stddef.h>
#include <stdio.h>
int activated;
int nrefs;
+
+ int destroy_on_free;
};
static int log_priority(const char* priority) {
static void pakfire_free(Pakfire pakfire) {
DEBUG(pakfire, "Releasing Pakfire at %p\n", pakfire);
+ if (pakfire->destroy_on_free) {
+ DEBUG(pakfire, "Destroying %s\n", pakfire->path);
+
+ pakfire_rmtree(pakfire->path, 0);
+ }
+
pakfire_repo_free_all(pakfire);
if (pakfire->pool)
}
PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char* arch) {
- int r;
+ char tempdir[PATH_MAX] = PAKFIRE_PRIVATE_DIR "/tmp/XXXXXX";
+ int r = 1;
// Default to the native architecture
if (!arch)
}
// Path must be absolute
- if (!pakfire_string_startswith(path, "/")) {
+ if (path && !pakfire_string_startswith(path, "/")) {
errno = EINVAL;
return 1;
}
- // Check if path exists
- if (!pakfire_path_isdir(path)) {
- errno = ENOENT;
- return 1;
- }
-
// Check if we are running as root
uid_t uid = getuid();
if (uid != 0) {
p->nrefs = 1;
+ // Generate a random path if none is set
+ if (!path) {
+ path = pakfire_mkdtemp(tempdir);
+ if (!path)
+ goto ERROR;
+
+ // Destroy everything when done
+ p->destroy_on_free = 1;
+ }
+
// Set path
snprintf(p->path, sizeof(p->path) - 1, "%s", path);
return 0;
}
-static int _unlink(const char* path, const struct stat* stat, int typeflag, struct FTW* ftwbuf) {
- return remove(path);
-}
-
PAKFIRE_EXPORT int pakfire_cache_destroy(Pakfire pakfire, const char* path) {
char cache_path[PATH_MAX];
pakfire_make_cache_path(pakfire, cache_path, sizeof(cache_path) - 1, "%s", path);
// Completely delete the tree of files
- int r = nftw(cache_path, _unlink, 64, FTW_DEPTH|FTW_PHYS);
-
- // It is okay if the path doesn't exist
- if (r < 0 && errno == ENOENT)
- r = 0;
-
- return r;
+ return pakfire_rmtree(cache_path, 0);
}
PAKFIRE_EXPORT pakfire_log_function_t pakfire_log_get_function(Pakfire pakfire) {
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
+#include <ftw.h>
#include <libgen.h>
#include <math.h>
#include <stddef.h>
return fdopen(fd, "w+");
}
+char* pakfire_mkdtemp(char* path) {
+ int r = pakfire_mkparentdir(path);
+ if (r)
+ return NULL;
+
+ return mkdtemp(path);
+}
+
+static int _unlink(const char* path, const struct stat* stat, int typeflag, struct FTW* ftwbuf) {
+ return remove(path);
+}
+
+int pakfire_rmtree(const char* path, int flags) {
+ int r = nftw(path, _unlink, 64, flags|FTW_DEPTH|FTW_PHYS);
+
+ // Ignore if path didn't exist
+ if (r < 0 && errno == ENOENT)
+ r = 0;
+
+ return r;
+}
+
// JSON Stuff
static struct json_object* pakfire_json_parse(Pakfire pakfire, FILE* f) {
from .i18n import _
class Cli(object):
+ default_path = "/"
+
def __init__(self):
self.ui = ui.cli.CliUI()
help=_("Run pakfire in offline mode."))
def pakfire(self, ns):
- p = base.Pakfire(path=ns.root, offline=ns.offline)
+ p = base.Pakfire(
+ path=ns.root if "root" in ns else self.default_path,
+ offline=ns.offline,
+ )
# Disable repositories.
for repo_name in ns.disable_repo:
class CliBuilder(Cli):
+ default_path = None
+
def parse_cli(self):
parser = argparse.ArgumentParser(
description = _("Pakfire builder command line interface"),