return 0;
}
-int pakfire_make_ramdisk(struct pakfire* pakfire, char* path, const char* args) {
- int r;
-
- // Create a new temporary directory
- char* p = pakfire_mkdtemp(path);
- if (!p)
- return -errno;
-
- // Mount the ramdisk
- r = pakfire_mount(pakfire, "pakfire_ramdisk", p, "tmpfs", 0, args);
- if (r) {
- PAKFIRE_ERROR(pakfire, "Could not mount ramdisk at %s (%s): %s\n",
- p, args, strerror(errno));
- return r;
- }
-
- PAKFIRE_DEBUG(pakfire, "Ramdisk mounted at %s (%s)\n", p, args);
-
- return 0;
-}
-
int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int flags) {
struct stat st;
char mountpoint[PATH_MAX];
int flags;
+ // Keep some internal state
+ enum pakfire_internal_flags {
+ PAKFIRE_HAS_PATH = (1 << 0),
+ PAKFIRE_DESTROY_ON_FREE = (1 << 1),
+ } internal_flags;
+
// Lock
FILE* lock;
struct pakfire_snapshot* snapshot;
// States
- unsigned int destroy_on_free:1;
unsigned int pool_ready:1;
unsigned int in_free:1;
};
if (pakfire->snapshot)
pakfire_snapshot_unref(pakfire->snapshot);
- if (pakfire->destroy_on_free && *pakfire->path) {
+ if (pakfire->internal_flags & PAKFIRE_DESTROY_ON_FREE) {
CTX_DEBUG(pakfire->ctx, "Destroying %s\n", pakfire->path);
// Umount the ramdisk
PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, struct pakfire_ctx* ctx,
const char* path, const char* arch, FILE* conf, int flags) {
struct pakfire* p = NULL;
- char tempdir[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
char private_dir[PATH_MAX];
- int r = 1;
+ int r;
// Default to the native architecture
if (!arch)
arch = pakfire_arch_native();
+ // Check path
+ if (path) {
+ // Check that we don't have path set when using snapshots
+ if (flags & PAKFIRE_USE_SNAPSHOT) {
+ CTX_ERROR(ctx, "Cannot use path with snapshots\n");
+ r = -EINVAL;
+ goto ERROR;
+ }
+
+ // Path must be absolute
+ if (!pakfire_string_startswith(path, "/")) {
+ CTX_ERROR(ctx, "Invalid path: %s\n", path);
+ r = -EINVAL;
+ goto ERROR;
+ }
+ }
+
// Allocate a new object
p = calloc(1, sizeof(*p));
if (!p)
goto ERROR;
}
- // Path must be absolute
- if (path && !pakfire_string_startswith(path, "/")) {
- CTX_ERROR(p->ctx, "Invalid path: %s\n", path);
- r = -EINVAL;
- goto ERROR;
- }
-
- // If using a snapshot, path cannot be set
+ // Are we using a snapshot?
if (p->flags & PAKFIRE_USE_SNAPSHOT) {
- if (path) {
- CTX_ERROR(p->ctx, "Cannot use snapshots with path\n");
- r = -EINVAL;
- goto ERROR;
- }
-
// Find the most recent snapshot
r = pakfire_snapshot_find(&p->snapshot, p);
if (r < 0) {
CTX_ERROR(p->ctx, "Could not find a snapshot: %s\n", strerror(-r));
goto ERROR;
}
+ }
+
+ // Store if we have a path
+ if (path) {
+ p->internal_flags |= PAKFIRE_HAS_PATH;
+
+ // Otherwise we create our own temporary path
+ } else {
+ // Template when running Pakfire in a temporary environment
+ char tmppath[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX";
+
+ // Create a new temporary directory
+ path = pakfire_mkdtemp(tmppath);
+ if (!path)
+ return -errno;
+
+ // Destroy the temporary directory afterwards
+ p->internal_flags |= PAKFIRE_DESTROY_ON_FREE;
// Mount the snapshot
if (p->snapshot) {
CTX_ERROR(p->ctx, "Could not mount snapshot: %s\n", strerror(-r));
goto ERROR;
}
- }
- }
- // Create a ramdisk if no path was given
- if (!path) {
- r = pakfire_make_ramdisk(p, tempdir, NULL);
- if (r)
- goto ERROR;
-
- // Use the ramdisk as path
- path = tempdir;
-
- // Destroy everything when done
- p->destroy_on_free = 1;
+ // Mount a tmpfs
+ } else {
+ r = mount("pakfire_root", path, "tmpfs", 0, NULL);
+ if (r) {
+ CTX_ERROR(p->ctx, "Could not mount tmpfs: %m\n");
+ r = -errno;
+ goto ERROR;
+ }
+ }
}
- // Set path
+ // Store the path
r = pakfire_string_set(p->path, path);
if (r < 0)
goto ERROR;