tRefactor code in the main() - pm - barely a pack manager
HTML git clone git://z3bra.org/pm
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 9971c62e398790939966c62bb1af1fa98fc5c4c2
DIR parent a96b471ef2c41c2d2a6d2763b8b869561b54c00a
HTML Author: z3bra <willyatmailoodotorg>
Date: Tue, 19 Apr 2016 12:59:55 +0200
Refactor code in the main()
install() and update() are now wrappers for pack_install(), and take a
char *path as argument instead of the pack structure.
This brings consistency in the code (as with delete()) and provide a
cleaner interface to deal with inline arguments or stdin.
In the meantime, use of the "const" keyword has been reworked to provide
ttype checks.
Diffstat:
M pm.c | 121 ++++++++++++++++++-------------
1 file changed, 70 insertions(+), 51 deletions(-)
---
DIR diff --git a/pm.c b/pm.c
t@@ -54,18 +54,22 @@ char *slurp(int fd);
struct pack *pack_load(char *path);
void pack_free(struct pack *p);
+int pack_install(const char *rootfs, const char *datadir, struct pack *p);
-int inspect_collision(char *rootfs, struct pack *p);
-int inspect_system(int fd, char *datadir);
-int inspect_files(int fd, char *datadir, char *packname);
-int inspect(char *datadir, char *packname);
+int inspect_collision(const char *rootfs, struct pack *p);
+int inspect_system(int fd, const char *datadir);
+int inspect_files(int fd, const char *datadir, const char *packname);
+int inspect(const char *datadir, const char *packname);
-int write_metadata(char *datadir, struct pack *pack);
+int write_metadata(const char *datadir, struct pack *pack);
int write_entry(struct archive *a, struct archive *w);
-int unpack(char *rootfs, char *datadir, struct pack *p);
-int install(char *rootfs, char *datadir, struct pack *p);
+int unpack(const char *rootfs, const char *datadir, struct pack *p);
int delete_node(char *path);
int delete_content(char *map, size_t size);
+
+/* action wrappers around CLI arguments */
+int install(const char *rootfs, const char *datadir, char *path);
+int update(const char *rootfs, const char *datadir, char *path);
int delete(const char *rootfs, const char *datadir, const char *name);
int verbose = 0;
t@@ -170,7 +174,7 @@ slurp(int fd)
* Check for collisions between the filesystem and the tarball
*/
int
-inspect_collision(char *rootfs, struct pack *p)
+inspect_collision(const char *rootfs, struct pack *p)
{
int r = 0;
char cwd[PATH_MAX] = "";
t@@ -208,7 +212,7 @@ inspect_collision(char *rootfs, struct pack *p)
* Write files installed by a pack to a file descriptor
*/
int
-inspect_files(int fd, char *datadir, char *packname)
+inspect_files(int fd, const char *datadir, const char *packname)
{
int meta;
char tmp[PATH_MAX] = "";
t@@ -233,7 +237,7 @@ inspect_files(int fd, char *datadir, char *packname)
* Write packs installed in datadir to a file descriptor
*/
int
-inspect_system(int fd, char *datadir)
+inspect_system(int fd, const char *datadir)
{
DIR *d;
struct dirent *p;
t@@ -270,7 +274,7 @@ inspect_system(int fd, char *datadir)
* files, or listing packs actually installed
*/
int
-inspect(char *datadir, char *packname)
+inspect(const char *datadir, const char *packname)
{
/* name is NULL, list packs installed */
if (!packname)
t@@ -286,7 +290,7 @@ inspect(char *datadir, char *packname)
* + datadir/packname/version - version of the pack installed
*/
int
-write_metadata(char *datadir, struct pack *p)
+write_metadata(const char *datadir, struct pack *p)
{
int fd, r;
struct stat st;
t@@ -348,7 +352,7 @@ write_entry(struct archive *a, struct archive *w)
* Extract a tarball to the given directory
*/
int
-unpack(char *rootfs, char *datadir, struct pack *p)
+unpack(const char *rootfs, const char *datadir, struct pack *p)
{
int r, fd;
struct archive *a;
t@@ -418,7 +422,7 @@ unpack(char *rootfs, char *datadir, struct pack *p)
* If overwrite is set to 1, it will overwrite all files
*/
int
-install(char *rootfs, char *datadir, struct pack *p)
+pack_install(const char *rootfs, const char *datadir, struct pack *p)
{
int r;
char tmp[PATH_MAX] = "";
t@@ -601,6 +605,48 @@ delete(const char *rootfs, const char *datadir, const char *packname)
/*
+ * Install a pack from the given path. This wraps load/install of a pack
+ */
+int
+install(const char *rootfs, const char *datadir, char *path)
+{
+ int r = 0;
+ struct pack *p = NULL;
+
+ if ((p = pack_load(path)) == NULL)
+ return ERR_PACK_LOAD;
+
+ r += pack_install(rootfs, datadir, p);
+ pack_free(p);
+
+ return r;
+}
+
+
+/*
+ * Update a pack. This should be as easy as delete/install.
+ * Deletion is required in case the file structure changes
+ */
+int
+update(const char *rootfs, const char *datadir, char *path)
+{
+ int r = 0;
+ struct pack *p = NULL;
+
+ if ((p = pack_load(path)) == NULL)
+ return ERR_PACK_LOAD;
+
+ if (delete(rootfs, datadir, p->name) != 0)
+ return ERR_DELETE;
+
+ r += pack_install(rootfs, datadir, p);
+ pack_free(p);
+
+ return r;
+}
+
+
+/*
* Load a pack from a tarball and return a pack structure
*/
struct pack *
t@@ -676,7 +722,6 @@ main (int argc, char **argv)
{
int r = 0;
char *n = NULL, *argv0;
- struct pack *p = NULL;
uint8_t action = ACTION_DEFAULT;
char rootfs[PATH_MAX] = "";
char datadir[PATH_MAX] = "";
t@@ -713,51 +758,25 @@ main (int argc, char **argv)
switch (action) {
case ACTION_INSTALL:
- /* black magic to read from stdin if no arguments given */
- while ((argc>0 && (n = *argv++) != NULL) || (n=slurp(0)) != NULL) {
- if ((p = pack_load(n))) {
- r += install(rootfs, datadir, p);
- pack_free(p);
- }
- /* slurp() allocates memory that must be freed */
- if (argc == 0) {
- free(n);
- }
- }
- break;
-
case ACTION_UPDATE:
- /* black magic to read from stdin if no arguments given */
- while ((argc>0 && (n = *argv++) != NULL) || (n=slurp(0)) != NULL) {
- if ((p = pack_load(n))) {
- if (delete(rootfs, datadir, p->name) == 0)
- r += install(rootfs, datadir, p);
- pack_free(p);
- }
- /* slurp() allocates memory that must be freed */
- if (argc == 0) {
- free(n);
- }
- }
- break;
-
case ACTION_DELETE:
- /* black magic to read from stdin if no arguments given */
- while ((argc>0 && (n = *argv++) != NULL) || (n=slurp(0)) != NULL) {
- r += delete(rootfs, datadir, n);
+ /* These action require a read from stdin if no arg is given */
+ while ((argc>0 && (n=strdup(*argv++))!=NULL) || (n=slurp(0))!=NULL) {
- /* slurp() allocates memory that must be freed */
- if (argc == 0) {
- free(n);
- }
+ if (action == ACTION_INSTALL)
+ r += install(rootfs, datadir, n);
+ if (action == ACTION_UPDATE)
+ r += update(rootfs, datadir, n);
+ if (action == ACTION_DELETE)
+ r += delete(rootfs, datadir, n);
+
+ free(n);
}
break;
-
case ACTION_INSPECT:
if (inspect(datadir, n) != 0)
return ERR_INSPECT;
break;
-
default:
usage(argv0);
return ERR_INVALID_ACTION;