summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Woodward2010-06-09 00:24:07 -0400
committerJason Woodward2010-06-09 00:38:39 -0400
commit33eb4afdaaae102ce44232d28c227ba98318d5d3 (patch)
treeceb93c84dbafdac28af7f0063e5e30bb05bdca83
parent8f196b6232e9f4eba6fe340e25c11af917215d17 (diff)
downloadslapt-get-33eb4afdaaae102ce44232d28c227ba98318d5d3.tar.gz
Updated API, generalizing several types into slapt_list_t
Replaced slapt_pkg_action_args_t, struct slapt_exclude_list, and struct slapt_suggests with slapt_list_t, a generic string list type. Added support functions slapt_parse_delimited_list, slapt_init_list, slapt_add_list_item, slapt_remove_list_item, and slapt_free_list in support of slapt_list_t. Removed now unnecessary slapt_init_exclude_list, slapt_add_exclude, slapt_remove_exclude, slapt_free_exclude_list, slapt_init_pkg_action_args, slapt_add_pkg_action_args, and slapt_free_pkg_action_args. Added convenience function slapt_parse_delimited_list that can generate a slapt_list_t from the exclude list, suggestions, dependencies, and conflicts. This greatly simplified the exclude parsing and dependency parsing.
-rw-r--r--ChangeLog3
-rw-r--r--doc/libslapt.336
-rw-r--r--src/action.c42
-rw-r--r--src/action.h14
-rw-r--r--src/common.c105
-rw-r--r--src/common.h12
-rw-r--r--src/configuration.c125
-rw-r--r--src/configuration.h16
-rw-r--r--src/main.c20
-rw-r--r--src/package.c161
-rw-r--r--src/transaction.c74
-rw-r--r--src/transaction.h7
12 files changed, 211 insertions, 404 deletions
diff --git a/ChangeLog b/ChangeLog
index 6618043..b5b06ff 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
-0.10.2f June 07, 2010
+0.10.2f June 09, 2010
+ * Updated API, generalizing several types into slapt_list_t
* added reinstall notification
0.10.2e June 02, 2010
diff --git a/doc/libslapt.3 b/doc/libslapt.3
index 2de73e4..cb389f9 100644
--- a/doc/libslapt.3
+++ b/doc/libslapt.3
@@ -63,7 +63,7 @@ slapt-get stores the configuration information in a \fIslapt_rc_config\fP object
typedef struct {
char working_dir[WORKINGDIR_TOKEN_LEN];
struct slapt_source_list *sources;
- struct slapt_exclude_list *exclude_list;
+ slapt_list_t *exclude_list;
int (*progress_cb) (void *,double,double,double,double);
SLAPT_BOOL_T download_only;
SLAPT_BOOL_T dist_upgrade;
@@ -132,21 +132,6 @@ The \fIstruct slapt_source_list\fP stores the package sources. Sources can be a
.fi
.sp
.TP
-The \fIstruct slapt_exclude_list\fP stores the package excludes. Excludes can be added and removed with \fIslapt_add_exclude\fP() and \fIslapt_remove_exclude\fP(). Typically, \fIslapt_rc_config->excludes\fP is passed as the first argument, but it could be any pointer to a \fIstruct slapt_exclude_list\fP that is created by \fIslapt_init_exclude_list\fP().
-.sp
-.nf
- struct slapt_exclude_list {
- char **excludes;
- unsigned int count;
- };
-.sp
-.B "struct slapt_exclude_list *slapt_init_exclude_list(void);"
-.B "void slapt_add_exclude(struct slapt_exclude_list *list,const char *e);"
-.B "void slapt_remove_exclude(struct slapt_exclude_list *list,const char *e);"
-.B "void slapt_free_exclude_list(struct slapt_exclude_list *list);"
-.fi
-.sp
-.TP
In order to initialize the slapt_rc_config->working_dir directory, \fIslapt_working_dir_init\fP() must be called. It is a fatal error to call \fIslapt_working_dir_init\fP() with a directory that the current process does not have privileges to read, write, and execute.
.sp
.B "void slapt_working_dir_init(const slapt_rc_config *global_config);"
@@ -429,11 +414,6 @@ Progress callbacks make use of \fIstruct slapt_progress_data\fP.
Adding, removing, and upgrading packages can be wrapped in a \fIslapt_transaction_t\fP object. This structure will make sure that all packages are worked on in the specific order, downloaded, and handed off to the appropriate pkgtools command via the \fIslapt_handle_transaction\fP() function.
.sp
.nf
- struct slapt_suggests {
- char **pkgs;
- unsigned int count;
- };
-.sp
enum slapt_action {
USAGE = 0, UPDATE, INSTALL, REMOVE, SHOW, SEARCH, UPGRADE,
LIST, INSTALLED, CLEAN, SHOWVERSION, AUTOCLEAN, AVAILABLE
@@ -466,7 +446,7 @@ Adding, removing, and upgrading packages can be wrapped in a \fIslapt_transactio
struct slapt_pkg_upgrade_list *upgrade_pkgs;
struct slapt_pkg_list *remove_pkgs;
struct slapt_pkg_list *exclude_pkgs;
- struct slapt_suggests *suggests;
+ slapt_list_t *suggests;
struct slapt_pkg_err_list *conflict_err;
struct slapt_pkg_err_list *missing_err;
slapt_queue_t *queue;
@@ -613,6 +593,18 @@ Miscellaneous functionality within libslapt.
.B const char *slapt_priority_to_str(SLAPT_PRIORITY_T priority);
.sp
.B SLAPT_BOOL_T slapt_disk_space_check (const char *path,double space_needed);
+.sp
+ typedef struct {
+ char **items;
+ unsigned int count;
+ } slapt_list_t;
+.sp
+.B /* general list management */
+.B slapt_list_t *slapt_parse_delimited_list(char *line, char delim);
+.B slapt_list_t *slapt_init_list(void);
+.B void slapt_add_list_item(slapt_list_t *list,const char *item);
+.B void slapt_remove_list_item(slapt_list_t *list,const char *item);
+.B void slapt_free_list(slapt_list_t *list);
.fi
.sp
.SH LINKING WITH LIBSLAPT
diff --git a/src/action.c b/src/action.c
index c08e01d..2a7ad07 100644
--- a/src/action.c
+++ b/src/action.c
@@ -22,7 +22,7 @@ static int cmp_pkg_arch(const char *a,const char *b);
/* install pkg */
void slapt_pkg_action_install(const slapt_rc_config *global_config,
- const slapt_pkg_action_args_t *action_args)
+ const slapt_list_t *action_args)
{
unsigned int i;
slapt_transaction_t *tran = NULL;
@@ -46,7 +46,7 @@ void slapt_pkg_action_install(const slapt_rc_config *global_config,
}
for (i = 0; i < action_args->count; ++i) {
- char *arg = action_args->pkgs[i];
+ char *arg = action_args->items[i];
slapt_pkg_info_t *pkg = NULL;
slapt_pkg_info_t *installed_pkg = NULL;
@@ -226,7 +226,7 @@ void slapt_pkg_action_list(const int show)
/* remove/uninstall pkg */
void slapt_pkg_action_remove(const slapt_rc_config *global_config,
- const slapt_pkg_action_args_t *action_args)
+ const slapt_list_t *action_args)
{
unsigned int i;
struct slapt_pkg_list *installed_pkgs = NULL;
@@ -247,7 +247,7 @@ void slapt_pkg_action_remove(const slapt_rc_config *global_config,
for (i = 0; i < action_args->count; ++i) {
unsigned int c;
struct slapt_pkg_list *deps = NULL;
- char *arg = action_args->pkgs[i];
+ char *arg = action_args->items[i];
slapt_pkg_info_t *pkg = NULL;
/* Use regex to see if they specified a particular version */
@@ -726,40 +726,6 @@ void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
slapt_free_transaction(tran);
}
-slapt_pkg_action_args_t *slapt_init_pkg_action_args(int arg_count)
-{
- slapt_pkg_action_args_t *paa;
-
- paa = slapt_malloc( sizeof *paa );
- paa->pkgs = slapt_malloc( sizeof *paa->pkgs * arg_count );
- paa->count = 0;
-
- return paa;
-}
-
-slapt_pkg_action_args_t *slapt_add_pkg_action_args(slapt_pkg_action_args_t *p,
- const char *arg)
-{
- p->pkgs[p->count] = slapt_malloc(
- ( strlen(arg) + 1 ) * sizeof *p->pkgs[p->count]
- );
- memcpy(p->pkgs[p->count],arg,strlen(arg) + 1);
- ++p->count;
- return p;
-}
-
-void slapt_free_pkg_action_args(slapt_pkg_action_args_t *paa)
-{
- unsigned int i;
-
- for (i = 0; i < paa->count; ++i) {
- free(paa->pkgs[i]);
- }
-
- free(paa->pkgs);
- free(paa);
-}
-
static int cmp_pkg_arch(const char *a,const char *b)
{
int r = 0;
diff --git a/src/action.h b/src/action.h
index 8e5a270..63dd35d 100644
--- a/src/action.h
+++ b/src/action.h
@@ -1,19 +1,9 @@
-typedef struct {
- char **pkgs;
- unsigned int count;
-} slapt_pkg_action_args_t;
-
-slapt_pkg_action_args_t *slapt_init_pkg_action_args(int arg_count);
-slapt_pkg_action_args_t *slapt_add_pkg_action_args(slapt_pkg_action_args_t *p,
- const char *arg);
-void slapt_free_pkg_action_args(slapt_pkg_action_args_t *paa);
-
void slapt_pkg_action_install(const slapt_rc_config *global_config,
- const slapt_pkg_action_args_t *action_args);
+ const slapt_list_t *action_args);
void slapt_pkg_action_list(const int show);
void slapt_pkg_action_remove(const slapt_rc_config *global_config,
- const slapt_pkg_action_args_t *action_args);
+ const slapt_list_t *action_args);
void slapt_pkg_action_search(const char *pattern);
void slapt_pkg_action_show(const char *pkg_name);
void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config);
diff --git a/src/common.c b/src/common.c
index 9093875..23fda50 100644
--- a/src/common.c
+++ b/src/common.c
@@ -380,3 +380,108 @@ SLAPT_BOOL_T slapt_disk_space_check (const char *path,double space_needed)
return SLAPT_TRUE;
}
+slapt_list_t *slapt_init_list(void)
+{
+ slapt_list_t *list = slapt_malloc(sizeof *list);
+ list->items = slapt_malloc(sizeof *list->items);
+ list->count = 0;
+
+ return list;
+}
+
+void slapt_add_list_item(slapt_list_t *list,const char *item)
+{
+ char **realloc_tmp;
+
+ realloc_tmp =
+ realloc( list->items, sizeof *list->items * (list->count + 1) );
+
+ if ( realloc_tmp == NULL )
+ return;
+
+ list->items = realloc_tmp;
+ list->items[ list->count ] = strndup(item, strlen(item));
+ list->items[ list->count ][strlen(item)] = '\0';
+ ++list->count;
+
+}
+
+void slapt_remove_list_item(slapt_list_t *list, const char *item)
+{
+ unsigned int i = 0;
+ char *tmp = NULL;
+
+ while (i < list->count) {
+ if ( strcmp(item,list->items[i]) == 0 && tmp == NULL ) {
+ tmp = list->items[i];
+ }
+ if ( tmp != NULL && (i+1 < list->count) ) {
+ list->items[i] = list->items[i + 1];
+ }
+ ++i;
+ }
+ if ( tmp != NULL ) {
+ char **realloc_tmp;
+ int count = list->count - 1;
+ if ( count < 1 )
+ count = 1;
+
+ free(tmp);
+
+ realloc_tmp = realloc(list->items, sizeof *list->items * count);
+ if ( realloc_tmp != NULL ) {
+ list->items = realloc_tmp;
+ if (list->count > 0)
+ --list->count;
+ }
+
+ }
+}
+
+void slapt_free_list(slapt_list_t *list)
+{
+ unsigned int i;
+
+ for (i = 0; i < list->count; ++i) {
+ free(list->items[i]);
+ }
+ free(list->items);
+ free(list);
+}
+
+slapt_list_t *slapt_parse_delimited_list(char *line, char delim)
+{
+ slapt_list_t *list = slapt_init_list();
+ int count = 0, position = 0, len = strlen(line);
+
+ while (isspace(line[position]) != 0)
+ ++position;
+
+ while (position < len) {
+ char *start = line + position;
+ char *end = NULL, *ptr = NULL;
+ int start_len = strlen(start), end_len = 0;
+
+ if (strchr(start,delim) != NULL) {
+ if (line[position] == delim || isspace(line[position]) != 0) {
+ ++position;
+ continue;
+ }
+ end = strchr(start,delim);
+ }
+
+ if (end != NULL)
+ end_len = strlen(end);
+
+ ptr = strndup(start, start_len - end_len);
+
+ slapt_add_list_item(list, ptr);
+ free(ptr);
+ ++count;
+
+ position += start_len - end_len;
+ }
+
+ return list;
+}
+
diff --git a/src/common.h b/src/common.h
index 0390e45..a889a66 100644
--- a/src/common.h
+++ b/src/common.h
@@ -49,6 +49,11 @@ typedef struct {
int reg_return;
} slapt_regex_t;
+typedef struct {
+ char **items;
+ unsigned int count;
+} slapt_list_t;
+
FILE *slapt_open_file(const char *file_name,const char *mode);
slapt_regex_t *slapt_init_regex(const char *regex_string);
void slapt_execute_regex(slapt_regex_t *regex_t,const char *string);
@@ -73,3 +78,10 @@ const char *slapt_strerror(slapt_code_t code);
/* return human readable priority */
const char *slapt_priority_to_str(SLAPT_PRIORITY_T priority);
SLAPT_BOOL_T slapt_disk_space_check (const char *path,double space_needed);
+
+/* general list management */
+slapt_list_t *slapt_parse_delimited_list(char *line, char delim);
+slapt_list_t *slapt_init_list(void);
+void slapt_add_list_item(slapt_list_t *list,const char *item);
+void slapt_remove_list_item(slapt_list_t *list,const char *item);
+void slapt_free_list(slapt_list_t *list);
diff --git a/src/configuration.c b/src/configuration.c
index 1886d56..dd95b59 100644
--- a/src/configuration.c
+++ b/src/configuration.c
@@ -18,7 +18,7 @@
#include "main.h"
/* parse the exclude list */
-static struct slapt_exclude_list *parse_exclude(char *line);
+static slapt_list_t *parse_exclude(char *line);
slapt_rc_config *slapt_init_config(void)
@@ -44,7 +44,7 @@ slapt_rc_config *slapt_init_config(void)
global_config->progress_cb = NULL;
global_config->sources = slapt_init_source_list();
- global_config->exclude_list = slapt_init_exclude_list();
+ global_config->exclude_list = slapt_init_list();
global_config->retry = 1;
@@ -115,7 +115,7 @@ slapt_rc_config *slapt_read_rc_config(const char *file_name)
} else if ( (token_ptr = strstr(getline_buffer,SLAPT_EXCLUDE_TOKEN)) != NULL ) {
/* exclude list */
- slapt_free_exclude_list(global_config->exclude_list);
+ slapt_free_list(global_config->exclude_list);
global_config->exclude_list = parse_exclude(token_ptr);
}
@@ -179,126 +179,17 @@ void slapt_working_dir_init(const slapt_rc_config *global_config)
void slapt_free_rc_config(slapt_rc_config *global_config)
{
- slapt_free_exclude_list(global_config->exclude_list);
+ slapt_free_list(global_config->exclude_list);
slapt_free_source_list(global_config->sources);
free(global_config);
}
-static struct slapt_exclude_list *parse_exclude(char *line)
+static slapt_list_t *parse_exclude(char *line)
{
- struct slapt_exclude_list *list;
- unsigned int position = 0, len = 0;
-
- list = slapt_malloc( sizeof *list );
- list->excludes = slapt_malloc( sizeof *list->excludes );
- list->count = 0;
-
/* skip ahead past the = */
line = strchr(line,'=') + 1;
- len = strlen(line);
- while ( position < len ) {
-
- if ( strstr(line + position,",") == NULL ) {
- char *pointer = NULL;
-
- pointer = line + position;
-
- slapt_add_exclude(list,pointer);
-
- break;
- } else {
-
- if ( line[position] == ',' ) {
- ++position;
- continue;
- } else {
- char *buffer = NULL,*pointer = NULL;
-
- pointer = strchr(line + position,',');
- buffer = strndup(
- line + position,
- strlen(line + position) - strlen(pointer)
- );
-
- slapt_add_exclude(list,buffer);
- free(buffer);
- position += (strlen(line + position) - strlen(pointer) );
- }
- continue;
- }
- }
-
- return list;
-}
-
-struct slapt_exclude_list *slapt_init_exclude_list(void)
-{
- struct slapt_exclude_list *list = slapt_malloc(sizeof *list);
- list->excludes = slapt_malloc(sizeof *list->excludes);
- list->count = 0;
-
- return list;
-}
-
-void slapt_add_exclude(struct slapt_exclude_list *list,const char *e)
-{
- char **realloc_tmp;
-
- realloc_tmp =
- realloc( list->excludes, sizeof *list->excludes * (list->count + 1) );
-
- if ( realloc_tmp == NULL )
- return;
-
- list->excludes = realloc_tmp;
- list->excludes[ list->count ] = strndup(e, strlen(e));
- list->excludes[ list->count ][strlen(e)] = '\0';
- ++list->count;
-
-}
-
-void slapt_remove_exclude(struct slapt_exclude_list *list,const char *e)
-{
- unsigned int i = 0;
- char *tmp = NULL;
-
- while (i < list->count) {
- if ( strcmp(e,list->excludes[i]) == 0 && tmp == NULL ) {
- tmp = list->excludes[i];
- }
- if ( tmp != NULL && (i+1 < list->count) ) {
- list->excludes[i] = list->excludes[i + 1];
- }
- ++i;
- }
- if ( tmp != NULL ) {
- char **realloc_tmp;
- int count = list->count - 1;
- if ( count < 1 )
- count = 1;
-
- free(tmp);
-
- realloc_tmp = realloc(list->excludes, sizeof *list->excludes * count);
- if ( realloc_tmp != NULL ) {
- list->excludes = realloc_tmp;
- if (list->count > 0)
- --list->count;
- }
-
- }
-}
-
-void slapt_free_exclude_list(struct slapt_exclude_list *list)
-{
- unsigned int i;
-
- for (i = 0; i < list->count; ++i) {
- free(list->excludes[i]);
- }
- free(list->excludes);
- free(list);
+ return slapt_parse_delimited_list(line, ',');
}
struct slapt_source_list *slapt_init_source_list(void)
@@ -522,9 +413,9 @@ int slapt_write_rc_config(const slapt_rc_config *global_config, const char *loca
fprintf(rc,"%s",SLAPT_EXCLUDE_TOKEN);
for (i = 0;i < global_config->exclude_list->count;++i) {
if ( i+1 == global_config->exclude_list->count) {
- fprintf(rc,"%s",global_config->exclude_list->excludes[i]);
+ fprintf(rc,"%s",global_config->exclude_list->items[i]);
}else{
- fprintf(rc,"%s,",global_config->exclude_list->excludes[i]);
+ fprintf(rc,"%s,",global_config->exclude_list->items[i]);
}
}
fprintf(rc,"\n");
diff --git a/src/configuration.h b/src/configuration.h
index aee7560..1e601ce 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -6,11 +6,6 @@
#define SLAPT_EXCLUDE_TOKEN "EXCLUDE="
#define SLAPT_SOURCE_ATTRIBUTE_REGEX "(:[A-Z_,]+)$"
-struct slapt_exclude_list {
- char **excludes;
- unsigned int count;
-};
-
typedef struct {
char *url;
SLAPT_PRIORITY_T priority;
@@ -25,7 +20,7 @@ struct slapt_source_list {
typedef struct {
char working_dir[SLAPT_WORKINGDIR_TOKEN_LEN];
struct slapt_source_list *sources;
- struct slapt_exclude_list *exclude_list;
+ slapt_list_t *exclude_list;
int(*progress_cb)(void *,double,double,double,double);
SLAPT_BOOL_T download_only;
SLAPT_BOOL_T dist_upgrade;
@@ -55,15 +50,6 @@ void slapt_free_rc_config(slapt_rc_config *global_config);
/* check that working_dir exists or make it if permissions allow */
void slapt_working_dir_init(const slapt_rc_config *global_config);
-/*
- add an exclude expression to the exclude list.
- commonly called with global_config->exclude_list
-*/
-struct slapt_exclude_list *slapt_init_exclude_list(void);
-void slapt_add_exclude(struct slapt_exclude_list *list,const char *e);
-void slapt_remove_exclude(struct slapt_exclude_list *list,const char *e);
-void slapt_free_exclude_list(struct slapt_exclude_list *list);
-
/* create, destroy the source struct */
slapt_source_t *slapt_init_source(const char *s);
void slapt_free_source(slapt_source_t *src);
diff --git a/src/main.c b/src/main.c
index 6c054f4..6d9e562 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,7 +26,7 @@ extern int optind, opterr, optopt;
int main( int argc, char *argv[] )
{
slapt_rc_config *global_config, *initial_config; /* our config struct */
- slapt_pkg_action_args_t *paa;
+ slapt_list_t *paa;
int c = 0;
enum slapt_action do_action = 0;
@@ -318,13 +318,13 @@ int main( int argc, char *argv[] )
}
break;
case INSTALL:
- paa = slapt_init_pkg_action_args((argc - optind));
+ paa = slapt_init_list();
while (optind < argc) {
- slapt_add_pkg_action_args(paa,argv[optind]);
+ slapt_add_list_item(paa,argv[optind]);
++optind;
}
slapt_pkg_action_install( global_config, paa );
- slapt_free_pkg_action_args(paa);
+ slapt_free_list(paa);
break;
case INSTALL_DISK_SET:
{
@@ -352,28 +352,28 @@ int main( int argc, char *argv[] )
}
- paa = slapt_init_pkg_action_args(set_pkgs->pkg_count);
+ paa = slapt_init_list();
for (set_i = 0; set_i < set_pkgs->pkg_count; ++set_i) {
- slapt_add_pkg_action_args(paa,set_pkgs->pkgs[set_i]->name);
+ slapt_add_list_item(paa,set_pkgs->pkgs[set_i]->name);
}
slapt_free_pkg_list(set_pkgs);
slapt_free_pkg_list(avail_pkgs);
slapt_pkg_action_install( global_config, paa );
- slapt_free_pkg_action_args(paa);
+ slapt_free_list(paa);
}
break;
case REMOVE:
- paa = slapt_init_pkg_action_args((argc - optind));
+ paa = slapt_init_list();
while (optind < argc) {
- slapt_add_pkg_action_args(paa,argv[optind]);
+ slapt_add_list_item(paa,argv[optind]);
++optind;
}
slapt_pkg_action_remove( global_config, paa );
- slapt_free_pkg_action_args(paa);
+ slapt_free_list(paa);
break;
case SHOW:
while (optind < argc) {
diff --git a/src/package.c b/src/package.c
index 708dcdd..48cd57a 100644
--- a/src/package.c
+++ b/src/package.c
@@ -911,7 +911,7 @@ int slapt_is_excluded(const slapt_rc_config *global_config,
slapt_regex_t *exclude_reg = NULL;
/* return if its an exact match */
- if ((strncmp(global_config->exclude_list->excludes[i],
+ if ((strncmp(global_config->exclude_list->items[i],
pkg->name,strlen(pkg->name)) == 0))
return pkg_slapt_is_excluded;
@@ -920,8 +920,8 @@ int slapt_is_excluded(const slapt_rc_config *global_config,
from the exclude list
*/
if ((exclude_reg =
- slapt_init_regex(global_config->exclude_list->excludes[i])) == NULL) {
- fprintf(stderr,"\n\nugh %s\n\n",global_config->exclude_list->excludes[i]);
+ slapt_init_regex(global_config->exclude_list->items[i])) == NULL) {
+ fprintf(stderr,"\n\nugh %s\n\n",global_config->exclude_list->items[i]);
continue;
}
@@ -1320,9 +1320,8 @@ int slapt_get_pkg_dependencies(const slapt_rc_config *global_config,
struct slapt_pkg_err_list *conflict_err,
struct slapt_pkg_err_list *missing_err)
{
- int position = 0, len = 0;
- char *pointer = NULL;
- char *buffer = NULL;
+ unsigned int i = 0;
+ slapt_list_t *dep_parts = NULL;
/*
* don't go any further if the required member is empty
@@ -1345,68 +1344,28 @@ int slapt_get_pkg_dependencies(const slapt_rc_config *global_config,
missing_err = slapt_init_pkg_err_list();
/* parse dep line */
- len = strlen(pkg->required);
- while (position < len) {
+ dep_parts = slapt_parse_delimited_list(pkg->required, ',');
+ for (i = 0; i < dep_parts->count; i++) {
slapt_pkg_info_t *tmp_pkg = NULL;
- /* either the last or there was only one to begin with */
- if (strstr(pkg->required + position,",") == NULL) {
- pointer = pkg->required + position;
-
- /* parse the dep entry and try to lookup a package */
- if (strchr(pointer,'|') != NULL) {
- tmp_pkg = find_or_requirement(avail_pkgs,installed_pkgs,pointer);
- } else {
- tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,pointer);
- }
-
- if (tmp_pkg == NULL) {
- /* if we can't find a required dep, return -1 */
- slapt_add_pkg_err_to_list(missing_err,pkg->name,pointer);
- return -1;
- }
-
- position += strlen(pointer);
-
+ if (strchr(dep_parts->items[i],'|') != NULL) {
+ tmp_pkg = find_or_requirement(avail_pkgs,installed_pkgs,dep_parts->items[i]);
} else {
+ tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,dep_parts->items[i]);
+ }
- /* if we have a comma, skip it */
- if (pkg->required[position] == ',') {
- ++position;
- continue;
- }
-
- /* build the buffer to contain the dep entry */
- pointer = strchr(pkg->required + position,',');
- buffer = strndup(
- pkg->required + position,
- strlen(pkg->required + position) - strlen(pointer)
- );
-
- /* parse the dep entry and try to lookup a package */
- if (strchr(buffer,'|') != NULL) {
- tmp_pkg = find_or_requirement(avail_pkgs,installed_pkgs,buffer);
- } else {
- tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,buffer);
- }
-
- if (tmp_pkg == NULL) {
- /* if we can't find a required dep, stop */
- slapt_add_pkg_err_to_list(missing_err,pkg->name,buffer);
- free(buffer);
- return -1;
- }
-
- position += strlen(pkg->required + position) - strlen(pointer);
- free(buffer);
+ if (tmp_pkg == NULL) {
+ /* if we can't find a required dep, return -1 */
+ slapt_add_pkg_err_to_list(missing_err,pkg->name,dep_parts->items[i]);
+ slapt_free_list(dep_parts);
+ return -1;
}
/* if this pkg is excluded */
- if ((slapt_is_excluded(global_config,tmp_pkg) == 1) &&
- (global_config->ignore_dep == SLAPT_FALSE)) {
- if (slapt_get_exact_pkg(installed_pkgs,tmp_pkg->name,
- tmp_pkg->version) == NULL) {
+ if ((slapt_is_excluded(global_config,tmp_pkg) == 1) && (global_config->ignore_dep == SLAPT_FALSE)) {
+ if (slapt_get_exact_pkg(installed_pkgs,tmp_pkg->name, tmp_pkg->version) == NULL) {
slapt_add_pkg_err_to_list(conflict_err,pkg->name,tmp_pkg->name);
+ slapt_free_list(dep_parts);
return -1;
}
}
@@ -1423,20 +1382,27 @@ int slapt_get_pkg_dependencies(const slapt_rc_config *global_config,
global_config,avail_pkgs,installed_pkgs,tmp_pkg,
deps,conflict_err,missing_err
);
+
if (dep_check_return == -1 && global_config->ignore_dep == SLAPT_FALSE) {
+ slapt_free_list(dep_parts);
return -1;
} else {
/* now move the package to the end after it's dependencies */
slapt_pkg_info_t *tmp = NULL;
unsigned int i = 0;
+
while (i < deps->pkg_count) {
+
if (strcmp(deps->pkgs[i]->name,tmp_pkg->name) == 0 && tmp == NULL)
tmp = deps->pkgs[i];
+
/* move all subsequent packages up */
if (tmp != NULL && (i+1 < deps->pkg_count))
deps->pkgs[i] = deps->pkgs[i + 1];
+
++i;
}
+
/*
* now put the pkg we found at the end...
* note no resizing is necessary, we just moved the location
@@ -1447,8 +1413,9 @@ int slapt_get_pkg_dependencies(const slapt_rc_config *global_config,
} /* end already exists in dep check */
+ }
- }/* end while */
+ slapt_free_list(dep_parts);
return 0;
}
@@ -2455,74 +2422,28 @@ static slapt_pkg_info_t *find_or_requirement(struct slapt_pkg_list *avail_pkgs,
char *required_str)
{
slapt_pkg_info_t *pkg = NULL;
- int position = 0, len = 0;
-
- len = strlen(required_str);
- while (position < len) {
-
- if (strchr(required_str + position,'|') == NULL) {
- char *string = required_str + position;
- slapt_pkg_info_t *tmp_pkg = NULL;
-
- tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,string);
-
- if (tmp_pkg != NULL) {
- /* installed packages are preferred */
- if (slapt_get_exact_pkg(installed_pkgs,tmp_pkg->name,
- tmp_pkg->version) != NULL) {
- pkg = tmp_pkg;
- break;
- } else {
- /* otherwise remember the first package found and continue on */
- if (pkg == NULL) {
- pkg = tmp_pkg;
- }
- }
- }
-
- position += strlen(string);
- } else {
- char *string = NULL;
- int str_len = 0;
- char *next_token = NULL;
- slapt_pkg_info_t *tmp_pkg = NULL;
-
- if (required_str[position] == '|') {
- ++position;
- continue;
- }
-
- next_token = index(required_str + position,'|');
- str_len = strlen(required_str + position) - strlen(next_token);
-
- string = strndup(
- required_str + position,
- str_len
- );
+ unsigned int i = 0;
+ slapt_list_t *alternates = slapt_parse_delimited_list(required_str,'|');
- tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,string);
- free(string);
+ for (i = 0; i < alternates->count; i++) {
+ slapt_pkg_info_t *tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,alternates->items[i]);
- if (tmp_pkg != NULL) {
- /* installed packages are preferred */
- if (slapt_get_exact_pkg(installed_pkgs,tmp_pkg->name,
- tmp_pkg->version) != NULL) {
+ if (tmp_pkg != NULL) {
+ /* installed packages are preferred */
+ if (slapt_get_exact_pkg(installed_pkgs,tmp_pkg->name, tmp_pkg->version) != NULL) {
+ pkg = tmp_pkg;
+ break;
+ } else {
+ /* otherwise remember the first package found and continue on */
+ if (pkg == NULL) {
pkg = tmp_pkg;
- break;
- } else {
- /* otherwise remember the first package found and continue on */
- if (pkg == NULL) {
- pkg = tmp_pkg;
- }
}
}
-
- position += str_len;
}
- ++position;
}
+ slapt_free_list(alternates);
return pkg;
}
diff --git a/src/transaction.c b/src/transaction.c
index 130d07f..1115690 100644
--- a/src/transaction.c
+++ b/src/transaction.c
@@ -52,9 +52,7 @@ slapt_transaction_t *slapt_init_transaction(void)
tran->upgrade_pkgs->reinstall_count = 0;
- tran->suggests = slapt_malloc(sizeof *tran->suggests);
- tran->suggests->pkgs = slapt_malloc(sizeof *tran->suggests->pkgs);
- tran->suggests->count = 0;
+ tran->suggests = slapt_init_list();
tran->queue = slapt_queue_init();
@@ -121,7 +119,7 @@ int slapt_handle_transaction (const slapt_rc_config *global_config,
printf(" ");
for (i = 0; i < tran->suggests->count; ++i) {
- char *s = tran->suggests->pkgs[i];
+ char *s = tran->suggests->items[i];
/* don't show suggestion for something we already have
in the transaction */
@@ -736,11 +734,7 @@ void slapt_free_transaction(slapt_transaction_t *tran)
free(tran->exclude_pkgs->pkgs);
free(tran->exclude_pkgs);
- for (i = 0; i < tran->suggests->count; ++i) {
- free(tran->suggests->pkgs[i]);
- }
- free(tran->suggests->pkgs);
- free(tran->suggests);
+ slapt_free_list(tran->suggests);
queue_free(tran->queue);
@@ -936,72 +930,26 @@ struct slapt_pkg_list *slapt_is_conflicted(slapt_transaction_t *tran,
static void add_suggestion(slapt_transaction_t *tran, slapt_pkg_info_t *pkg)
{
- unsigned int position = 0,len = 0;
+ slapt_list_t *suggests = NULL;
+ unsigned int i = 0;
- if (pkg->suggests == NULL || (len = strlen(pkg->suggests)) == 0) {
+ if (pkg->suggests == NULL || strlen(pkg->suggests) == 0) {
return;
}
- while (isspace(pkg->suggests[position]) != 0)
- ++position;
-
- while (position < len) {
- int total_len = 0,rest_len = 0;
- char *p = NULL,*si = NULL,*tmp_suggests = NULL;
- char **tmp_realloc;
+ suggests = slapt_parse_delimited_list(pkg->suggests, ',');
- p = pkg->suggests + position;
- if (p == NULL)
- break;
-
- si = strpbrk(p," ,");
- if (si == NULL || strlen(si) <= 2) {
- total_len = strlen(p);
- rest_len = 0;
- tmp_suggests = strndup(p,strlen(p));
- } else {
- si = si + 1;
-
- total_len = strlen(p);
- rest_len = strlen(si);
-
- /* this will always encompass ending space, so we dont + 1 */
- tmp_suggests = slapt_malloc(sizeof *tmp_suggests * (total_len - rest_len));
- tmp_suggests = strncpy(tmp_suggests,p,(total_len - rest_len));
- tmp_suggests[total_len - rest_len - 1] = '\0';
- }
+ for (i = 0; i < suggests->count; i++) {
/* no need to add it if we already have it */
- if (slapt_search_transaction(tran,tmp_suggests) == 1) {
- free(tmp_suggests);
- position += (total_len - rest_len);
+ if (slapt_search_transaction(tran,suggests->items[i]) == 1)
continue;
- }
-
- tmp_realloc =
- realloc(tran->suggests->pkgs,
- sizeof *tran->suggests->pkgs * (tran->suggests->count + 1)
- );
-
- if (tmp_realloc != NULL) {
- tran->suggests->pkgs = tmp_realloc;
- tran->suggests->pkgs[tran->suggests->count] = slapt_malloc(
- sizeof *tran->suggests->pkgs[tran->suggests->count]
- * (strlen(tmp_suggests) + 1));
-
- tran->suggests->pkgs[tran->suggests->count] = strncpy(
- tran->suggests->pkgs[tran->suggests->count],
- tmp_suggests,strlen(tmp_suggests));
-
- tran->suggests->pkgs[tran->suggests->count][strlen(tmp_suggests)] = '\0';
- ++tran->suggests->count;
- }
- free(tmp_suggests);
+ slapt_add_list_item(tran->suggests, suggests->items[i]);
- position += (total_len - rest_len);
}
+ slapt_free_list(suggests);
}
int slapt_search_transaction_by_pkg(slapt_transaction_t *tran,
diff --git a/src/transaction.h b/src/transaction.h
index 11b37cd..d663994 100644
--- a/src/transaction.h
+++ b/src/transaction.h
@@ -5,11 +5,6 @@
*/
#define MAX_LINE_LEN 80
-struct slapt_suggests {
- char **pkgs;
- unsigned int count;
-};
-
typedef struct {
union { slapt_pkg_info_t *i; slapt_pkg_upgrade_t *u; } pkg;
unsigned int type; /* this is enum slapt_action defined in main.h */
@@ -25,7 +20,7 @@ typedef struct {
struct slapt_pkg_upgrade_list *upgrade_pkgs;
struct slapt_pkg_list *remove_pkgs;
struct slapt_pkg_list *exclude_pkgs;
- struct slapt_suggests *suggests;
+ slapt_list_t *suggests;
struct slapt_pkg_err_list *conflict_err;
struct slapt_pkg_err_list *missing_err;
slapt_queue_t *queue;