From: Michael Tremer Date: Fri, 20 Aug 2021 10:59:32 +0000 (+0000) Subject: repo: Implement adding more files to repositories X-Git-Tag: 0.9.28~979 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0c3dce19c931f85641cd95ff01018a007e05d918;p=pakfire.git repo: Implement adding more files to repositories Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index f3675e44a..f6784db9e 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -1052,21 +1052,69 @@ static PyObject* Pakfire_open(PakfireObject* self, PyObject* args) { } static PyObject* Pakfire_repo_compose(PakfireObject* self, PyObject* args, PyObject* kwargs) { - char* kwlist[] = { "path", NULL }; + char* kwlist[] = { "path", "files", NULL }; const char* path = NULL; + PyObject* list = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &path)) + PyObject* ret = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO", kwlist, &path, &list)) + return NULL; + + // List must be a sequence + if (!PySequence_Check(list)) { + PyErr_SetString(PyExc_ValueError, "Expected a sequence."); return NULL; + } const int flags = 0; - int r = pakfire_repo_compose(self->pakfire, path, flags); + // How many new files do we have? + ssize_t num_files = PySequence_Length(list); + if (num_files < 0) + return NULL; + + // Allocate files array + const char** files = calloc(num_files + 1, sizeof(*files)); + if (!files) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + + for (int i = 0; i < num_files; i++) { + PyObject* file = PySequence_GetItem(list, i); + if (!file) + goto ERROR; + + // Check if file is a Unicode object + if (!PyUnicode_Check(file)) { + PyErr_SetString(PyExc_ValueError, "Expected a string."); + goto ERROR; + } + + // Add pointer to string to files array + files[i] = PyUnicode_AsUTF8(file); + if (!files[i]) + goto ERROR; + + Py_DECREF(file); + } + + int r = pakfire_repo_compose(self->pakfire, path, flags, files); if (r) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } - Py_RETURN_NONE; + // Return None on success + ret = Py_None; + Py_INCREF(ret); + +ERROR: + if (files) + free(files); + + return ret; } static struct PyMethodDef Pakfire_methods[] = { diff --git a/src/libpakfire/include/pakfire/repo.h b/src/libpakfire/include/pakfire/repo.h index c7700f277..0972d200d 100644 --- a/src/libpakfire/include/pakfire/repo.h +++ b/src/libpakfire/include/pakfire/repo.h @@ -84,7 +84,8 @@ int pakfire_repo_refresh(struct pakfire_repo* repo, int force); // Compose -int pakfire_repo_compose(struct pakfire* pakfire, const char* path, int flags); +int pakfire_repo_compose(struct pakfire* pakfire, const char* path, int flags, + const char** files); #ifdef PAKFIRE_PRIVATE diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index fa46606fb..d07d5931e 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -1103,7 +1103,8 @@ static int pakfire_repo_write_metadata(struct pakfire_repo* repo) { return 0; } -PAKFIRE_EXPORT int pakfire_repo_compose(struct pakfire* pakfire, const char* path, int flags) { +PAKFIRE_EXPORT int pakfire_repo_compose(struct pakfire* pakfire, const char* path, + int flags, const char** files) { struct pakfire_repo* repo = NULL; int r; @@ -1113,6 +1114,14 @@ PAKFIRE_EXPORT int pakfire_repo_compose(struct pakfire* pakfire, const char* pat return 1; } + size_t num_files = 0; + + // Count files + if (files) { + for (const char** file = files; *file; file++) + num_files++; + } + char baseurl[PATH_MAX]; // Prefix path with file:// to form baseurl @@ -1141,7 +1150,64 @@ PAKFIRE_EXPORT int pakfire_repo_compose(struct pakfire* pakfire, const char* pat goto ERROR; } - // XXX Add more files + struct pakfire_archive* archive = NULL; + struct pakfire_package* package = NULL; + char destination_path[PATH_MAX]; + + DEBUG(pakfire, "Adding %zu file(s) to the repository\n", num_files); + + // Add more files + if (files) { + for (const char** file = files; *file; file++) { + DEBUG(pakfire, "Adding %s to repository...\n", *file); + + // Open source archive + r = pakfire_archive_open(&archive, pakfire, *file); + if (r) { + ERROR(pakfire, "Could not open %s: %m\n", *file); + goto OUT; + } + + // Add package metadata + r = pakfire_repo_add_archive(repo, archive, &package); + if (r) { + ERROR(pakfire, "Could not add %s to the repository: %m\n", *file); + goto OUT; + } + + const char* filename = pakfire_package_get_filename(package); + + // Make new path + r = pakfire_path_join(destination_path, path, filename); + if (r < 0) + goto OUT; + + // Copying archive to destination + r = pakfire_archive_copy(archive, destination_path); + if (r) { + ERROR(pakfire, "Could not copy archive to %s: %m\n", destination_path); + goto OUT; + } + + const char* repo_path = pakfire_path_relpath(path, destination_path); + + // Update package location relative to the repository directory + pakfire_package_set_path(package, repo_path); + +OUT: + if (archive) { + pakfire_archive_unref(archive); + archive = NULL; + } + if (package) { + pakfire_package_unref(package); + package = NULL; + } + + if (r) + goto ERROR; + } + } // Write metadata to disk r = pakfire_repo_write_metadata(repo); diff --git a/src/scripts/pakfire-builder.in b/src/scripts/pakfire-builder.in index c5419e349..80b4e1fad 100644 --- a/src/scripts/pakfire-builder.in +++ b/src/scripts/pakfire-builder.in @@ -137,8 +137,8 @@ class Cli(object): repo_compose.add_argument("path", help=_("The path to the repository"), ) - repo_compose.add_argument("archive", nargs="+", - help=_("Archives to be added to this repository") + repo_compose.add_argument("file", nargs="+", + help=_("Files to be added to this repository") ) repo_compose.add_argument("--key", help=_("Key used to sign archives"), @@ -294,7 +294,7 @@ class Cli(object): Composes a repository """ p = self.pakfire(ns) - p.repo_compose(ns.path) + p.repo_compose(ns.path, files=ns.file) def _shell(self, ns): """