Use dynamic string allocation in package structures

This also affects all structures with static strings, such as depmiss,
conflict, etc. This should help a lot with memory usage, and hopefully make
things a bit more "idiot proof".

Currently our pactest pass/fail rate is identical before and after this
patch. This is not to say it is a perfect patch- I have yet to pull valgrind
out. However, this should be quite safe to use in all situations from here
on out, and we can start plugging the memleaks.

Original-work-by: Aaron Griffin <aaronmgriffin@gmail.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-01-11 00:01:58 -06:00
parent 0a65de10b1
commit ccc1c73152
14 changed files with 288 additions and 203 deletions

View file

@ -143,7 +143,8 @@ int _alpm_add_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
if(data) { if(data) {
*data = lp; *data = lp;
} else { } else {
FREELIST(lp); alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
alpm_list_free(lp);
} }
RET_ERR(PM_ERR_UNSATISFIED_DEPS, -1); RET_ERR(PM_ERR_UNSATISFIED_DEPS, -1);
} }
@ -193,7 +194,8 @@ int _alpm_add_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
if(data) { if(data) {
*data = lp; *data = lp;
} else { } else {
FREELIST(lp); alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_fileconflict_free);
alpm_list_free(lp);
} }
RET_ERR(PM_ERR_FILE_CONFLICTS, -1); RET_ERR(PM_ERR_FILE_CONFLICTS, -1);
} }

View file

@ -106,7 +106,7 @@ void _alpm_db_rewind(pmdb_t *db)
rewinddir(db->handle); rewinddir(db->handle);
} }
static int _alpm_db_splitname(const char *target, char *name, char *version) static int splitname(const char *target, pmpkg_t *pkg)
{ {
/* the format of a db entry is as follows: /* the format of a db entry is as follows:
* package-version-rel/ * package-version-rel/
@ -115,10 +115,10 @@ static int _alpm_db_splitname(const char *target, char *name, char *version)
*/ */
char *tmp, *p, *q; char *tmp, *p, *q;
if(target == NULL) { if(target == NULL || pkg == NULL) {
return(-1); return(-1);
} }
tmp = strdup(target); STRDUP(tmp, target, RET_ERR(PM_ERR_MEMORY, -1));
p = tmp + strlen(tmp); p = tmp + strlen(tmp);
/* do the magic parsing- find the beginning of the version string /* do the magic parsing- find the beginning of the version string
@ -130,14 +130,16 @@ static int _alpm_db_splitname(const char *target, char *name, char *version)
} }
/* copy into fields and return */ /* copy into fields and return */
if(version) { if(pkg->version) {
strncpy(version, p+1, PKG_VERSION_LEN); FREE(pkg->version);
} }
STRDUP(pkg->version, p+1, RET_ERR(PM_ERR_MEMORY, -1));
/* insert a terminator at the end of the name (on hyphen)- then copy it */ /* insert a terminator at the end of the name (on hyphen)- then copy it */
*p = '\0'; *p = '\0';
if(name) { if(pkg->name) {
strncpy(name, tmp, PKG_NAME_LEN); FREE(pkg->name);
} }
STRDUP(pkg->name, tmp, RET_ERR(PM_ERR_MEMORY, -1));
free(tmp); free(tmp);
return(0); return(0);
@ -148,7 +150,6 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
struct dirent *ent = NULL; struct dirent *ent = NULL;
struct stat sbuf; struct stat sbuf;
char path[PATH_MAX]; char path[PATH_MAX];
char name[PKG_FULLNAME_LEN];
char *ptr = NULL; char *ptr = NULL;
int found = 0; int found = 0;
pmpkg_t *pkg = NULL; pmpkg_t *pkg = NULL;
@ -168,7 +169,9 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
/* search for a specific package (by name only) */ /* search for a specific package (by name only) */
rewinddir(db->handle); rewinddir(db->handle);
while(!found && (ent = readdir(db->handle)) != NULL) { while(!found && (ent = readdir(db->handle)) != NULL) {
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { char *name;
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue; continue;
} }
/* stat the entry, make sure it's a directory */ /* stat the entry, make sure it's a directory */
@ -176,7 +179,9 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
if(stat(path, &sbuf) || !S_ISDIR(sbuf.st_mode)) { if(stat(path, &sbuf) || !S_ISDIR(sbuf.st_mode)) {
continue; continue;
} }
strncpy(name, ent->d_name, PKG_FULLNAME_LEN);
STRDUP(name, ent->d_name, return(NULL));
/* truncate the string at the second-to-last hyphen, */ /* truncate the string at the second-to-last hyphen, */
/* which will give us the package name */ /* which will give us the package name */
if((ptr = rindex(name, '-'))) { if((ptr = rindex(name, '-'))) {
@ -185,10 +190,12 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
if((ptr = rindex(name, '-'))) { if((ptr = rindex(name, '-'))) {
*ptr = '\0'; *ptr = '\0';
} }
if(!strcmp(name, target)) { if(strcmp(name, target) == 0) {
found = 1; found = 1;
} }
FREE(name);
} }
if(!found) { if(!found) {
return(NULL); return(NULL);
} }
@ -199,7 +206,7 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
if(ent == NULL) { if(ent == NULL) {
return(NULL); return(NULL);
} }
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
isdir = 0; isdir = 0;
continue; continue;
} }
@ -217,7 +224,7 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
return(NULL); return(NULL);
} }
/* split the db entry name */ /* split the db entry name */
if(_alpm_db_splitname(ent->d_name, pkg->name, pkg->version) != 0) { if(splitname(ent->d_name, pkg) != 0) {
_alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"), _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
ent->d_name); ent->d_name);
alpm_pkg_free(pkg); alpm_pkg_free(pkg);
@ -251,7 +258,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
RET_ERR(PM_ERR_DB_NULL, -1); RET_ERR(PM_ERR_DB_NULL, -1);
} }
if(info == NULL || info->name[0] == 0 || info->version[0] == 0) { if(info == NULL || info->name == NULL || info->version == NULL) {
_alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_db_read, skipping\n"); _alpm_log(PM_LOG_DEBUG, "invalid package entry provided to _alpm_db_read, skipping\n");
return(-1); return(-1);
} }
@ -296,122 +303,117 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
break; break;
} }
_alpm_strtrim(line); _alpm_strtrim(line);
if(!strcmp(line, "%FILENAME%")) { if(strcmp(line, "%FILENAME%") == 0) {
/* filename is _new_ - it provides the real name of the package, on the if(fgets(line, 512, fp) == NULL) {
* server, to allow for us to not tie the name of the actual file to the
* data of the package
*/
if(fgets(info->filename, sizeof(info->filename), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(info->filename); STRDUP(info->filename, _alpm_strtrim(line), goto error);
} else if(!strcmp(line, "%DESC%")) { } else if(strcmp(line, "%DESC%") == 0) {
if(fgets(info->desc, sizeof(info->desc), fp) == NULL) { if(fgets(line, 512, fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(info->desc); STRDUP(info->desc, _alpm_strtrim(line), goto error);
} else if(!strcmp(line, "%GROUPS%")) { } else if(strcmp(line, "%GROUPS%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->groups = alpm_list_add(info->groups, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->groups = alpm_list_add(info->groups, linedup);
} }
} else if(!strcmp(line, "%URL%")) { } else if(strcmp(line, "%URL%") == 0) {
if(fgets(info->url, sizeof(info->url), fp) == NULL) { if(fgets(line, 512, fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(info->url); STRDUP(info->url, _alpm_strtrim(line), goto error);
} else if(!strcmp(line, "%LICENSE%")) { } else if(strcmp(line, "%LICENSE%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->licenses = alpm_list_add(info->licenses, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->licenses = alpm_list_add(info->licenses, linedup);
} }
} else if(!strcmp(line, "%ARCH%")) { } else if(strcmp(line, "%ARCH%") == 0) {
if(fgets(info->arch, sizeof(info->arch), fp) == NULL) { if(fgets(line, 512, fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(info->arch); STRDUP(info->arch, _alpm_strtrim(line), goto error);
} else if(!strcmp(line, "%BUILDDATE%")) { } else if(strcmp(line, "%BUILDDATE%") == 0) {
char tmp[32]; if(fgets(line, 512, fp) == NULL) {
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(tmp); _alpm_strtrim(line);
char first = tolower(tmp[0]); char first = tolower(line[0]);
if(first > 'a' && first < 'z') { if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; //initialize to null incase of failure struct tm tmp_tm = {0}; //initialize to null incase of failure
setlocale(LC_TIME, "C"); setlocale(LC_TIME, "C");
strptime(tmp, "%a %b %e %H:%M:%S %Y", &tmp_tm); strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
info->builddate = mktime(&tmp_tm); info->builddate = mktime(&tmp_tm);
setlocale(LC_TIME, ""); setlocale(LC_TIME, "");
} else { } else {
info->builddate = atol(tmp); info->builddate = atol(line);
} }
} else if(!strcmp(line, "%INSTALLDATE%")) { } else if(strcmp(line, "%INSTALLDATE%") == 0) {
char tmp[32]; if(fgets(line, 512, fp) == NULL) {
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(tmp); _alpm_strtrim(line);
char first = tolower(tmp[0]); char first = tolower(line[0]);
if(first > 'a' && first < 'z') { if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; //initialize to null incase of failure struct tm tmp_tm = {0}; //initialize to null incase of failure
setlocale(LC_TIME, "C"); setlocale(LC_TIME, "C");
strptime(tmp, "%a %b %e %H:%M:%S %Y", &tmp_tm); strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
info->installdate = mktime(&tmp_tm); info->installdate = mktime(&tmp_tm);
setlocale(LC_TIME, ""); setlocale(LC_TIME, "");
} else { } else {
info->installdate = atol(tmp); info->installdate = atol(line);
} }
} else if(!strcmp(line, "%PACKAGER%")) { } else if(strcmp(line, "%PACKAGER%") == 0) {
if(fgets(info->packager, sizeof(info->packager), fp) == NULL) { if(fgets(line, 512, fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(info->packager); STRDUP(info->packager, _alpm_strtrim(line), goto error);
} else if(!strcmp(line, "%REASON%")) { } else if(strcmp(line, "%REASON%") == 0) {
char tmp[32]; if(fgets(line, 512, fp) == NULL) {
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(tmp); info->reason = atol(_alpm_strtrim(line));
info->reason = atol(tmp); } else if(strcmp(line, "%SIZE%") == 0 || strcmp(line, "%CSIZE%") == 0) {
} else if(!strcmp(line, "%SIZE%") || !strcmp(line, "%CSIZE%")) {
/* NOTE: the CSIZE and SIZE fields both share the "size" field /* NOTE: the CSIZE and SIZE fields both share the "size" field
* in the pkginfo_t struct. This can be done b/c CSIZE * in the pkginfo_t struct. This can be done b/c CSIZE
* is currently only used in sync databases, and SIZE is * is currently only used in sync databases, and SIZE is
* only used in local databases. * only used in local databases.
*/ */
char tmp[32]; if(fgets(line, 512, fp) == NULL) {
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(tmp); info->size = atol(_alpm_strtrim(line));
info->size = atol(tmp);
/* also store this value to isize if isize is unset */ /* also store this value to isize if isize is unset */
if(info->isize == 0) { if(info->isize == 0) {
info->isize = atol(tmp); info->isize = info->size;
} }
} else if(!strcmp(line, "%ISIZE%")) { } else if(strcmp(line, "%ISIZE%") == 0) {
/* ISIZE (installed size) tag only appears in sync repositories, /* ISIZE (installed size) tag only appears in sync repositories,
* not the local one. */ * not the local one. */
char tmp[32]; if(fgets(line, 512, fp) == NULL) {
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(tmp); info->isize = atol(_alpm_strtrim(line));
info->isize = atol(tmp); } else if(strcmp(line, "%MD5SUM%") == 0) {
} else if(!strcmp(line, "%MD5SUM%")) {
/* MD5SUM tag only appears in sync repositories, /* MD5SUM tag only appears in sync repositories,
* not the local one. */ * not the local one. */
if(fgets(info->md5sum, sizeof(info->md5sum), fp) == NULL) { if(fgets(line, 512, fp) == NULL) {
goto error; goto error;
} }
} else if(!strcmp(line, "%REPLACES%")) { STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%REPLACES%") == 0) {
/* the REPLACES tag is special -- it only appears in sync repositories, /* the REPLACES tag is special -- it only appears in sync repositories,
* not the local one. */ * not the local one. */
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->replaces = alpm_list_add(info->replaces, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->replaces = alpm_list_add(info->replaces, linedup);
} }
} else if(!strcmp(line, "%FORCE%")) { } else if(strcmp(line, "%FORCE%") == 0) {
/* FORCE tag only appears in sync repositories, /* FORCE tag only appears in sync repositories,
* not the local one. */ * not the local one. */
info->force = 1; info->force = 1;
@ -430,13 +432,17 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
} }
while(fgets(line, 256, fp)) { while(fgets(line, 256, fp)) {
_alpm_strtrim(line); _alpm_strtrim(line);
if(!strcmp(line, "%FILES%")) { if(strcmp(line, "%FILES%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->files = alpm_list_add(info->files, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->files = alpm_list_add(info->files, linedup);
} }
} else if(!strcmp(line, "%BACKUP%")) { } else if(strcmp(line, "%BACKUP%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->backup = alpm_list_add(info->backup, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->backup = alpm_list_add(info->backup, linedup);
} }
} }
} }
@ -454,33 +460,39 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
while(!feof(fp)) { while(!feof(fp)) {
fgets(line, 255, fp); fgets(line, 255, fp);
_alpm_strtrim(line); _alpm_strtrim(line);
if(!strcmp(line, "%DEPENDS%")) { if(strcmp(line, "%DEPENDS%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
pmdepend_t *dep = alpm_splitdep(line); pmdepend_t *dep = alpm_splitdep(_alpm_strtrim(line));
info->depends = alpm_list_add(info->depends, dep); info->depends = alpm_list_add(info->depends, dep);
} }
} else if(!strcmp(line, "%OPTDEPENDS%")) { } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->optdepends = alpm_list_add(info->optdepends, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->optdepends = alpm_list_add(info->optdepends, linedup);
} }
} else if(!strcmp(line, "%CONFLICTS%")) { } else if(strcmp(line, "%CONFLICTS%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->conflicts = alpm_list_add(info->conflicts, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->conflicts = alpm_list_add(info->conflicts, linedup);
} }
} else if(!strcmp(line, "%PROVIDES%")) { } else if(strcmp(line, "%PROVIDES%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->provides = alpm_list_add(info->provides, strdup(line)); char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->provides = alpm_list_add(info->provides, linedup);
} }
} }
/* TODO: we were going to move these things here, but it should wait. /* TODO: we were going to move these things here, but it should wait.
* A better change would be to figure out how to restructure the DB. */ * A better change would be to figure out how to restructure the DB. */
/* else if(!strcmp(line, "%REPLACES%")) { /* else if(strcmp(line, "%REPLACES%") == 0) {
* the REPLACES tag is special -- it only appears in sync repositories, * the REPLACES tag is special -- it only appears in sync repositories,
* not the local one. * * not the local one. *
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->replaces = alpm_list_add(info->replaces, strdup(line)); info->replaces = alpm_list_add(info->replaces, strdup(line));
} }
} else if(!strcmp(line, "%FORCE%")) { } else if(strcmp(line, "%FORCE%") == 0) {
* FORCE tag only appears in sync repositories, * FORCE tag only appears in sync repositories,
* not the local one. * * not the local one. *
info->force = 1; info->force = 1;
@ -497,7 +509,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
while(!feof(fp)) { while(!feof(fp)) {
fgets(line, 255, fp); fgets(line, 255, fp);
_alpm_strtrim(line); _alpm_strtrim(line);
if(!strcmp(line, "%DELTAS%")) { if(strcmp(line, "%DELTAS%") == 0) {
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
info->deltas = alpm_list_add(info->deltas, _alpm_delta_parse(line)); info->deltas = alpm_list_add(info->deltas, _alpm_delta_parse(line));
} }
@ -565,7 +577,7 @@ int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
} }
fprintf(fp, "%%NAME%%\n%s\n\n" fprintf(fp, "%%NAME%%\n%s\n\n"
"%%VERSION%%\n%s\n\n", info->name, info->version); "%%VERSION%%\n%s\n\n", info->name, info->version);
if(info->desc[0]) { if(info->desc) {
fprintf(fp, "%%DESC%%\n" fprintf(fp, "%%DESC%%\n"
"%s\n\n", info->desc); "%s\n\n", info->desc);
} }
@ -577,7 +589,7 @@ int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
fprintf(fp, "\n"); fprintf(fp, "\n");
} }
if(local) { if(local) {
if(info->url[0]) { if(info->url) {
fprintf(fp, "%%URL%%\n" fprintf(fp, "%%URL%%\n"
"%s\n\n", info->url); "%s\n\n", info->url);
} }
@ -588,7 +600,7 @@ int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
} }
fprintf(fp, "\n"); fprintf(fp, "\n");
} }
if(info->arch[0]) { if(info->arch) {
fprintf(fp, "%%ARCH%%\n" fprintf(fp, "%%ARCH%%\n"
"%s\n\n", info->arch); "%s\n\n", info->arch);
} }
@ -600,7 +612,7 @@ int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
fprintf(fp, "%%INSTALLDATE%%\n" fprintf(fp, "%%INSTALLDATE%%\n"
"%ju\n\n", (uintmax_t)info->installdate); "%ju\n\n", (uintmax_t)info->installdate);
} }
if(info->packager[0]) { if(info->packager) {
fprintf(fp, "%%PACKAGER%%\n" fprintf(fp, "%%PACKAGER%%\n"
"%s\n\n", info->packager); "%s\n\n", info->packager);
} }

View file

@ -49,12 +49,19 @@ pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2)
MALLOC(conflict, sizeof(pmconflict_t), RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(conflict, sizeof(pmconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
strncpy(conflict->package1, package1, PKG_NAME_LEN); STRDUP(conflict->package1, package1, RET_ERR(PM_ERR_MEMORY, NULL));
strncpy(conflict->package2, package2, PKG_NAME_LEN); STRDUP(conflict->package2, package2, RET_ERR(PM_ERR_MEMORY, NULL));
return(conflict); return(conflict);
} }
void _alpm_conflict_free(pmconflict_t *conflict)
{
FREE(conflict->package2);
FREE(conflict->package1);
FREE(conflict);
}
int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack) int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack)
{ {
alpm_list_t *i; alpm_list_t *i;
@ -110,7 +117,7 @@ static void add_conflict(alpm_list_t **baddeps, const char *pkg1,
if(conflict && !_alpm_conflict_isin(conflict, *baddeps)) { if(conflict && !_alpm_conflict_isin(conflict, *baddeps)) {
*baddeps = alpm_list_add(*baddeps, conflict); *baddeps = alpm_list_add(*baddeps, conflict);
} else { } else {
FREE(conflict); _alpm_conflict_free(conflict);
} }
} }
@ -293,15 +300,15 @@ static alpm_list_t *add_fileconflict(alpm_list_t *conflicts,
const char* name1, const char* name2) const char* name1, const char* name2)
{ {
pmfileconflict_t *conflict; pmfileconflict_t *conflict;
MALLOC(conflict, sizeof(pmfileconflict_t), return(conflicts)); MALLOC(conflict, sizeof(pmfileconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
conflict->type = type; conflict->type = type;
strncpy(conflict->target, name1, PKG_NAME_LEN); STRDUP(conflict->target, name1, RET_ERR(PM_ERR_MEMORY, NULL));
strncpy(conflict->file, filestr, CONFLICT_FILE_LEN); STRDUP(conflict->file, filestr, RET_ERR(PM_ERR_MEMORY, NULL));
if(name2) { if(name2) {
strncpy(conflict->ctarget, name2, PKG_NAME_LEN); STRDUP(conflict->ctarget, name2, RET_ERR(PM_ERR_MEMORY, NULL));
} else { } else {
conflict->ctarget[0] = '\0'; conflict->ctarget = "";
} }
conflicts = alpm_list_add(conflicts, conflict); conflicts = alpm_list_add(conflicts, conflict);
@ -311,6 +318,16 @@ static alpm_list_t *add_fileconflict(alpm_list_t *conflicts,
return(conflicts); return(conflicts);
} }
void _alpm_fileconflict_free(pmfileconflict_t *conflict)
{
if(strlen(conflict->ctarget) > 0) {
FREE(conflict->ctarget);
}
FREE(conflict->file);;
FREE(conflict->target);
FREE(conflict);
}
/* Find file conflicts that may occur during the transaction with two checks: /* Find file conflicts that may occur during the transaction with two checks:
* 1: check every target against every target * 1: check every target against every target
* 2: check every target against the filesystem */ * 2: check every target against the filesystem */

View file

@ -23,27 +23,28 @@
#include "db.h" #include "db.h"
#include "package.h" #include "package.h"
#define CONFLICT_FILE_LEN 512
struct __pmconflict_t { struct __pmconflict_t {
char package1[PKG_NAME_LEN]; char *package1;
char package2[PKG_NAME_LEN]; char *package2;
}; };
struct __pmfileconflict_t { struct __pmfileconflict_t {
char target[PKG_NAME_LEN]; char *target;
pmfileconflicttype_t type; pmfileconflicttype_t type;
char file[CONFLICT_FILE_LEN]; char *file;
char ctarget[PKG_NAME_LEN]; char *ctarget;
}; };
pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2); pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2);
void _alpm_conflict_free(pmconflict_t *conflict);
int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack); int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack);
alpm_list_t *_alpm_innerconflicts(alpm_list_t *packages); alpm_list_t *_alpm_innerconflicts(alpm_list_t *packages);
alpm_list_t *_alpm_outerconflicts(pmdb_t *db, alpm_list_t *packages); alpm_list_t *_alpm_outerconflicts(pmdb_t *db, alpm_list_t *packages);
alpm_list_t *_alpm_checkconflicts(pmdb_t *db, alpm_list_t *packages); alpm_list_t *_alpm_checkconflicts(pmdb_t *db, alpm_list_t *packages);
alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans, char *root); alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans, char *root);
void _alpm_fileconflict_free(pmfileconflict_t *conflict);
#endif /* _ALPM_CONFLICT_H */ #endif /* _ALPM_CONFLICT_H */
/* vim: set ts=2 sw=2 noet: */ /* vim: set ts=2 sw=2 noet: */

View file

@ -500,18 +500,16 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
for(j = _alpm_db_get_pkgcache(db); j; j = j->next) { for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
pmpkg_t *pkg = j->data; pmpkg_t *pkg = j->data;
const char *matched = NULL; const char *matched = NULL;
const char *name = alpm_pkg_get_name(pkg);
const char *desc = alpm_pkg_get_desc(pkg);
/* check name */ /* check name as regex AND as plain text */
if (regexec(&reg, alpm_pkg_get_name(pkg), 0, 0, 0) == 0) { if(name && (regexec(&reg, name, 0, 0, 0) == 0 || strstr(name, targ))) {
matched = alpm_pkg_get_name(pkg); matched = name;
}
/* check plain text name */
else if (strstr(alpm_pkg_get_name(pkg), targ)) {
matched = alpm_pkg_get_name(pkg);
} }
/* check desc */ /* check desc */
else if (regexec(&reg, alpm_pkg_get_desc(pkg), 0, 0, 0) == 0) { else if (desc && regexec(&reg, desc, 0, 0, 0) == 0) {
matched = alpm_pkg_get_desc(pkg); matched = desc;
} }
/* check provides */ /* check provides */
/* TODO: should we be doing this, and should we print something /* TODO: should we be doing this, and should we print something

View file

@ -68,18 +68,26 @@ pmdepmissing_t *_alpm_depmiss_new(const char *target, pmdepmod_t depmod,
MALLOC(miss, sizeof(pmdepmissing_t), RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(miss, sizeof(pmdepmissing_t), RET_ERR(PM_ERR_MEMORY, NULL));
strncpy(miss->target, target, PKG_NAME_LEN); STRDUP(miss->target, target, RET_ERR(PM_ERR_MEMORY, NULL));
miss->depend.mod = depmod; miss->depend.mod = depmod;
strncpy(miss->depend.name, depname, PKG_NAME_LEN); STRDUP(miss->depend.name, depname, RET_ERR(PM_ERR_MEMORY, NULL));
if(depversion) { if(depversion) {
strncpy(miss->depend.version, depversion, PKG_VERSION_LEN); STRDUP(miss->depend.version, depversion, RET_ERR(PM_ERR_MEMORY, NULL));
} else {
miss->depend.version[0] = 0;
} }
return(miss); return(miss);
} }
void _alpm_depmiss_free(pmdepmissing_t *miss)
{
if(miss->depend.version && strlen(miss->depend.version) != 0) {
FREE(miss->depend.version);
}
FREE(miss->depend.name);
FREE(miss->target);
FREE(miss);
}
/* Convert a list of pmpkg_t * to a graph structure, /* Convert a list of pmpkg_t * to a graph structure,
* with a edge for each dependency. * with a edge for each dependency.
* Returns a list of vertices (one vertex = one package) * Returns a list of vertices (one vertex = one package)
@ -362,9 +370,9 @@ pmdepend_t SYMEXPORT *alpm_splitdep(const char *depstring)
if(depstring == NULL) { if(depstring == NULL) {
return(NULL); return(NULL);
} }
newstr = strdup(depstring); STRDUP(newstr, depstring, RET_ERR(PM_ERR_MEMORY, NULL));
MALLOC(depend, sizeof(pmdepend_t), return(NULL)); CALLOC(depend, sizeof(pmdepend_t), 1, RET_ERR(PM_ERR_MEMORY, NULL));
/* Find a version comparator if one exists. If it does, set the type and /* Find a version comparator if one exists. If it does, set the type and
* increment the ptr accordingly so we can copy the right strings. */ * increment the ptr accordingly so we can copy the right strings. */
@ -388,20 +396,19 @@ pmdepend_t SYMEXPORT *alpm_splitdep(const char *depstring)
depend->mod = PM_DEP_MOD_GT; depend->mod = PM_DEP_MOD_GT;
*ptr = '\0'; *ptr = '\0';
ptr += 1; ptr += 1;
} else { } else {
/* no version specified - copy in the name and return it */ /* no version specified - copy the name and return it */
depend->mod = PM_DEP_MOD_ANY; depend->mod = PM_DEP_MOD_ANY;
strncpy(depend->name, newstr, PKG_NAME_LEN); STRDUP(depend->name, newstr, RET_ERR(PM_ERR_MEMORY, NULL));
depend->version[0] = '\0'; depend->version = NULL;
free(newstr); free(newstr);
return(depend); return(depend);
} }
/* if we get here, we have a version comparator, copy the right parts /* if we get here, we have a version comparator, copy the right parts
* to the right places */ * to the right places */
strncpy(depend->name, newstr, PKG_NAME_LEN); STRDUP(depend->name, newstr, RET_ERR(PM_ERR_MEMORY, NULL));
strncpy(depend->version, ptr, PKG_VERSION_LEN); STRDUP(depend->version, ptr, RET_ERR(PM_ERR_MEMORY, NULL));
free(newstr); free(newstr);
return(depend); return(depend);
@ -608,7 +615,8 @@ int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg,
_alpm_log(PM_LOG_DEBUG, "finished resolving dependencies\n"); _alpm_log(PM_LOG_DEBUG, "finished resolving dependencies\n");
FREELIST(deps); alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
alpm_list_free(deps);
return(0); return(0);
@ -674,7 +682,7 @@ const char SYMEXPORT *alpm_dep_get_version(const pmdepend_t *dep)
*/ */
char SYMEXPORT *alpm_dep_get_string(const pmdepend_t *dep) char SYMEXPORT *alpm_dep_get_string(const pmdepend_t *dep)
{ {
char *opr, *str = NULL; char *name, *opr, *ver, *str = NULL;
size_t len; size_t len;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
@ -682,6 +690,12 @@ char SYMEXPORT *alpm_dep_get_string(const pmdepend_t *dep)
/* Sanity checks */ /* Sanity checks */
ASSERT(dep != NULL, return(NULL)); ASSERT(dep != NULL, return(NULL));
if(dep->name) {
name = dep->name;
} else {
name = "";
}
switch(dep->mod) { switch(dep->mod) {
case PM_DEP_MOD_ANY: case PM_DEP_MOD_ANY:
opr = ""; opr = "";
@ -706,11 +720,18 @@ char SYMEXPORT *alpm_dep_get_string(const pmdepend_t *dep)
break; break;
} }
if(dep->version) {
ver = dep->version;
} else {
ver = "";
}
/* we can always compute len and print the string like this because opr /* we can always compute len and print the string like this because opr
* and ver will be empty when PM_DEP_MOD_ANY is the depend type */ * and ver will be empty when PM_DEP_MOD_ANY is the depend type. the
len = strlen(dep->name) + strlen(opr) + strlen(dep->version) + 1; * reassignments above also ensure we do not do a strlen(NULL). */
len = strlen(name) + strlen(opr) + strlen(ver) + 1;
MALLOC(str, len, RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(str, len, RET_ERR(PM_ERR_MEMORY, NULL));
snprintf(str, len, "%s%s%s", dep->name, opr, dep->version); snprintf(str, len, "%s%s%s", name, opr, ver);
return(str); return(str);
} }

View file

@ -29,13 +29,13 @@
/* Dependency */ /* Dependency */
struct __pmdepend_t { struct __pmdepend_t {
pmdepmod_t mod; pmdepmod_t mod;
char name[PKG_NAME_LEN]; char *name;
char version[PKG_VERSION_LEN]; char *version;
}; };
/* Missing dependency */ /* Missing dependency */
struct __pmdepmissing_t { struct __pmdepmissing_t {
char target[PKG_NAME_LEN]; char *target;
pmdepend_t depend; pmdepend_t depend;
}; };
@ -50,6 +50,7 @@ struct __pmgraph_t {
pmdepmissing_t *_alpm_depmiss_new(const char *target, pmdepmod_t depmod, pmdepmissing_t *_alpm_depmiss_new(const char *target, pmdepmod_t depmod,
const char *depname, const char *depversion); const char *depname, const char *depversion);
void _alpm_depmiss_free(pmdepmissing_t *miss);
alpm_list_t *_alpm_sortbydeps(alpm_list_t *targets, pmtranstype_t mode); alpm_list_t *_alpm_sortbydeps(alpm_list_t *targets, pmtranstype_t mode);
void _alpm_recursedeps(pmdb_t *db, alpm_list_t *targs, int include_explicit); void _alpm_recursedeps(pmdb_t *db, alpm_list_t *targs, int include_explicit);
int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg, int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg,

View file

@ -162,18 +162,20 @@ const char SYMEXPORT *alpm_pkg_get_filename(pmpkg_t *pkg)
ASSERT(handle != NULL, return(NULL)); ASSERT(handle != NULL, return(NULL));
ASSERT(pkg != NULL, return(NULL)); ASSERT(pkg != NULL, return(NULL));
if(!strlen(pkg->filename)) { if(pkg->filename == NULL || strlen(pkg->filename) == 0) {
/* construct the file name, it's not in the desc file */ /* construct the file name, it's not in the desc file */
if(pkg->origin == PKG_FROM_CACHE && !(pkg->infolevel & INFRQ_DESC)) { if(pkg->origin == PKG_FROM_CACHE && !(pkg->infolevel & INFRQ_DESC)) {
_alpm_db_read(pkg->origin_data.db, pkg, INFRQ_DESC); _alpm_db_read(pkg->origin_data.db, pkg, INFRQ_DESC);
} }
char buffer[PATH_MAX];
if(pkg->arch && strlen(pkg->arch) > 0) { if(pkg->arch && strlen(pkg->arch) > 0) {
snprintf(pkg->filename, PKG_FILENAME_LEN, "%s-%s-%s" PKGEXT, snprintf(buffer, PATH_MAX, "%s-%s-%s" PKGEXT,
pkg->name, pkg->version, pkg->arch); pkg->name, pkg->version, pkg->arch);
} else { } else {
snprintf(pkg->filename, PKG_FILENAME_LEN, "%s-%s" PKGEXT, snprintf(buffer, PATH_MAX, "%s-%s" PKGEXT,
pkg->name, pkg->version); pkg->name, pkg->version);
} }
STRDUP(pkg->filename, buffer, RET_ERR(PM_ERR_MEMORY, NULL));
} }
return pkg->filename; return pkg->filename;
@ -755,15 +757,12 @@ pmpkg_t *_alpm_pkg_new(const char *name, const char *version)
CALLOC(pkg, 1, sizeof(pmpkg_t), RET_ERR(PM_ERR_MEMORY, NULL)); CALLOC(pkg, 1, sizeof(pmpkg_t), RET_ERR(PM_ERR_MEMORY, NULL));
if(name && name[0] != 0) { if(name) {
strncpy(pkg->name, name, PKG_NAME_LEN); STRDUP(pkg->name, name, RET_ERR(PM_ERR_MEMORY, pkg));
} else {
pkg->name[0] = '\0';
} }
if(version && version[0] != 0) {
strncpy(pkg->version, version, PKG_VERSION_LEN); if(version) {
} else { STRDUP(pkg->version, version, RET_ERR(PM_ERR_MEMORY, pkg));
pkg->version[0] = '\0';
} }
return(pkg); return(pkg);
@ -771,31 +770,49 @@ pmpkg_t *_alpm_pkg_new(const char *name, const char *version)
pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg) pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg)
{ {
pmpkg_t* newpkg; pmpkg_t *newpkg;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
CALLOC(newpkg, 1, sizeof(pmpkg_t), RET_ERR(PM_ERR_MEMORY, NULL)); CALLOC(newpkg, 1, sizeof(pmpkg_t), RET_ERR(PM_ERR_MEMORY, NULL));
memcpy(newpkg, pkg, sizeof(pmpkg_t)); STRDUP(newpkg->filename, pkg->filename, RET_ERR(PM_ERR_MEMORY, newpkg));
STRDUP(newpkg->name, pkg->name, RET_ERR(PM_ERR_MEMORY, newpkg));
STRDUP(newpkg->version, pkg->version, RET_ERR(PM_ERR_MEMORY, newpkg));
STRDUP(newpkg->desc, pkg->desc, RET_ERR(PM_ERR_MEMORY, newpkg));
STRDUP(newpkg->url, pkg->url, RET_ERR(PM_ERR_MEMORY, newpkg));
newpkg->builddate = pkg->builddate;
newpkg->installdate = pkg->installdate;
STRDUP(newpkg->packager, pkg->packager, RET_ERR(PM_ERR_MEMORY, newpkg));
STRDUP(newpkg->md5sum, pkg->md5sum, RET_ERR(PM_ERR_MEMORY, newpkg));
STRDUP(newpkg->arch, pkg->arch, RET_ERR(PM_ERR_MEMORY, newpkg));
newpkg->size = pkg->size;
newpkg->isize = pkg->isize;
newpkg->scriptlet = pkg->scriptlet;
newpkg->force = pkg->force;
newpkg->reason = pkg->reason;
newpkg->licenses = alpm_list_strdup(alpm_pkg_get_licenses(pkg)); newpkg->licenses = alpm_list_strdup(alpm_pkg_get_licenses(pkg));
newpkg->conflicts = alpm_list_strdup(alpm_pkg_get_conflicts(pkg)); newpkg->replaces = alpm_list_strdup(alpm_pkg_get_replaces(pkg));
newpkg->groups = alpm_list_strdup(alpm_pkg_get_groups(pkg));
newpkg->files = alpm_list_strdup(alpm_pkg_get_files(pkg)); newpkg->files = alpm_list_strdup(alpm_pkg_get_files(pkg));
newpkg->backup = alpm_list_strdup(alpm_pkg_get_backup(pkg)); newpkg->backup = alpm_list_strdup(alpm_pkg_get_backup(pkg));
newpkg->depends = alpm_list_copy_data(alpm_pkg_get_depends(pkg), newpkg->depends = alpm_list_copy_data(alpm_pkg_get_depends(pkg),
sizeof(pmdepend_t)); sizeof(pmdepend_t));
newpkg->optdepends = alpm_list_strdup(alpm_pkg_get_optdepends(pkg)); newpkg->optdepends = alpm_list_strdup(alpm_pkg_get_optdepends(pkg));
newpkg->groups = alpm_list_strdup(alpm_pkg_get_groups(pkg)); newpkg->conflicts = alpm_list_strdup(alpm_pkg_get_conflicts(pkg));
newpkg->provides = alpm_list_strdup(alpm_pkg_get_provides(pkg)); newpkg->provides = alpm_list_strdup(alpm_pkg_get_provides(pkg));
newpkg->replaces = alpm_list_strdup(alpm_pkg_get_replaces(pkg));
newpkg->deltas = alpm_list_copy_data(alpm_pkg_get_deltas(pkg), newpkg->deltas = alpm_list_copy_data(alpm_pkg_get_deltas(pkg),
sizeof(pmdelta_t)); sizeof(pmdelta_t));
/* internal */ /* internal */
newpkg->origin = pkg->origin;
if(newpkg->origin == PKG_FROM_FILE) { if(newpkg->origin == PKG_FROM_FILE) {
newpkg->origin_data.file = strdup(pkg->origin_data.file); newpkg->origin_data.file = strdup(pkg->origin_data.file);
} else { } else {
newpkg->origin_data.db = pkg->origin_data.db; newpkg->origin_data.db = pkg->origin_data.db;
} }
newpkg->infolevel = pkg->infolevel;
return(newpkg); return(newpkg);
} }
@ -808,16 +825,25 @@ void _alpm_pkg_free(pmpkg_t *pkg)
return; return;
} }
FREE(pkg->filename);
FREE(pkg->name);
FREE(pkg->version);
FREE(pkg->desc);
FREE(pkg->url);
FREE(pkg->packager);
FREE(pkg->md5sum);
FREE(pkg->arch);
FREELIST(pkg->licenses); FREELIST(pkg->licenses);
FREELIST(pkg->replaces);
FREELIST(pkg->groups);
FREELIST(pkg->files); FREELIST(pkg->files);
FREELIST(pkg->backup); FREELIST(pkg->backup);
FREELIST(pkg->depends); FREELIST(pkg->depends);
FREELIST(pkg->optdepends); FREELIST(pkg->optdepends);
FREELIST(pkg->conflicts); FREELIST(pkg->conflicts);
FREELIST(pkg->groups);
FREELIST(pkg->provides); FREELIST(pkg->provides);
FREELIST(pkg->replaces);
FREELIST(pkg->deltas); FREELIST(pkg->deltas);
if(pkg->origin == PKG_FROM_FILE) { if(pkg->origin == PKG_FROM_FILE) {
FREE(pkg->origin_data.file); FREE(pkg->origin_data.file);
} }
@ -899,18 +925,18 @@ static int parse_descfile(const char *descfile, pmpkg_t *info)
_alpm_log(PM_LOG_DEBUG, "%s: syntax error in description file line %d\n", _alpm_log(PM_LOG_DEBUG, "%s: syntax error in description file line %d\n",
info->name[0] != '\0' ? info->name : "error", linenum); info->name[0] != '\0' ? info->name : "error", linenum);
} else { } else {
_alpm_strtrim(key); key = _alpm_strtrim(key);
_alpm_strtrim(ptr); ptr = _alpm_strtrim(ptr);
if(!strcmp(key, "pkgname")) { if(!strcmp(key, "pkgname")) {
strncpy(info->name, ptr, sizeof(info->name)); STRDUP(info->name, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(!strcmp(key, "pkgver")) { } else if(!strcmp(key, "pkgver")) {
strncpy(info->version, ptr, sizeof(info->version)); STRDUP(info->version, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(!strcmp(key, "pkgdesc")) { } else if(!strcmp(key, "pkgdesc")) {
strncpy(info->desc, ptr, sizeof(info->desc)); STRDUP(info->desc, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(!strcmp(key, "group")) { } else if(!strcmp(key, "group")) {
info->groups = alpm_list_add(info->groups, strdup(ptr)); info->groups = alpm_list_add(info->groups, strdup(ptr));
} else if(!strcmp(key, "url")) { } else if(!strcmp(key, "url")) {
strncpy(info->url, ptr, sizeof(info->url)); STRDUP(info->url, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(!strcmp(key, "license")) { } else if(!strcmp(key, "license")) {
info->licenses = alpm_list_add(info->licenses, strdup(ptr)); info->licenses = alpm_list_add(info->licenses, strdup(ptr));
} else if(!strcmp(key, "builddate")) { } else if(!strcmp(key, "builddate")) {
@ -925,9 +951,9 @@ static int parse_descfile(const char *descfile, pmpkg_t *info)
info->builddate = atol(ptr); info->builddate = atol(ptr);
} }
} else if(!strcmp(key, "packager")) { } else if(!strcmp(key, "packager")) {
strncpy(info->packager, ptr, sizeof(info->packager)); STRDUP(info->packager, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(!strcmp(key, "arch")) { } else if(!strcmp(key, "arch")) {
strncpy(info->arch, ptr, sizeof(info->arch)); STRDUP(info->arch, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(!strcmp(key, "size")) { } else if(!strcmp(key, "size")) {
/* size in the raw package is uncompressed (installed) size */ /* size in the raw package is uncompressed (installed) size */
info->isize = atol(ptr); info->isize = atol(ptr);
@ -1034,11 +1060,11 @@ pmpkg_t *_alpm_pkg_load(const char *pkgfile, unsigned short full)
pkgfile); pkgfile);
goto pkg_invalid; goto pkg_invalid;
} }
if(!strlen(info->name)) { if(info->name == NULL || strlen(info->name) == 0) {
_alpm_log(PM_LOG_ERROR, _("missing package name in %s\n"), pkgfile); _alpm_log(PM_LOG_ERROR, _("missing package name in %s\n"), pkgfile);
goto pkg_invalid; goto pkg_invalid;
} }
if(!strlen(info->version)) { if(info->version == NULL || strlen(info->version) == 0) {
_alpm_log(PM_LOG_ERROR, _("missing package version in %s\n"), pkgfile); _alpm_log(PM_LOG_ERROR, _("missing package version in %s\n"), pkgfile);
goto pkg_invalid; goto pkg_invalid;
} }

View file

@ -33,30 +33,17 @@ typedef enum _pmpkgfrom_t {
PKG_FROM_FILE PKG_FROM_FILE
} pmpkgfrom_t; } pmpkgfrom_t;
/* Packages */
#define PKG_FILENAME_LEN 512
#define PKG_NAME_LEN 256
#define PKG_VERSION_LEN 64
#define PKG_FULLNAME_LEN (PKG_NAME_LEN + PKG_VERSION_LEN)
#define PKG_DESC_LEN 512
#define PKG_URL_LEN 256
#define PKG_DATE_LEN 32
#define PKG_TYPE_LEN 32
#define PKG_PACKAGER_LEN 64
#define PKG_MD5SUM_LEN 33
#define PKG_ARCH_LEN 32
struct __pmpkg_t { struct __pmpkg_t {
char filename[PKG_FILENAME_LEN]; char *filename;
char name[PKG_NAME_LEN]; char *name;
char version[PKG_VERSION_LEN]; char *version;
char desc[PKG_DESC_LEN]; char *desc;
char url[PKG_URL_LEN]; char *url;
time_t builddate; time_t builddate;
time_t installdate; time_t installdate;
char packager[PKG_PACKAGER_LEN]; char *packager;
char md5sum[PKG_MD5SUM_LEN]; char *md5sum;
char arch[PKG_ARCH_LEN]; char *arch;
unsigned long size; unsigned long size;
unsigned long isize; unsigned long isize;
unsigned short scriptlet; unsigned short scriptlet;

View file

@ -120,7 +120,8 @@ int _alpm_remove_prepare(pmtrans_t *trans, pmdb_t *db, alpm_list_t **data)
miss->target); miss->target);
} }
} }
FREELIST(lp); alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
alpm_list_free(lp);
lp = alpm_checkdeps(db, 1, trans->packages, NULL); lp = alpm_checkdeps(db, 1, trans->packages, NULL);
} }
} else { } else {

View file

@ -188,7 +188,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
char realfile[PATH_MAX]; char realfile[PATH_MAX];
char output[PATH_MAX]; char output[PATH_MAX];
char *fn = (char *)lp->data; char *fn = (char *)lp->data;
char pkgname[PKG_NAME_LEN]; char *pkgname;
fileurl = url_for_file(server, fn); fileurl = url_for_file(server, fn);
if(!fileurl) { if(!fileurl) {
@ -196,7 +196,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
} }
/* pass the raw filename for passing to the callback function */ /* pass the raw filename for passing to the callback function */
strncpy(pkgname, fn, PKG_NAME_LEN); STRDUP(pkgname, fn, (void)0);
_alpm_log(PM_LOG_DEBUG, "using '%s' for download progress\n", pkgname); _alpm_log(PM_LOG_DEBUG, "using '%s' for download progress\n", pkgname);
snprintf(realfile, PATH_MAX, "%s%s", localpath, fn); snprintf(realfile, PATH_MAX, "%s%s", localpath, fn);
@ -403,6 +403,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
} }
chdir(cwd); chdir(cwd);
} }
FREE(pkgname);
} }
if(alpm_list_count(complete) == alpm_list_count(files)) { if(alpm_list_count(complete) == alpm_list_count(files)) {

View file

@ -274,7 +274,7 @@ int _alpm_sync_sysupgrade(pmtrans_t *trans,
int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync, char *name) int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync, char *name)
{ {
char targline[PKG_FULLNAME_LEN]; char *targline;
char *targ; char *targ;
alpm_list_t *j; alpm_list_t *j;
pmpkg_t *local; pmpkg_t *local;
@ -287,8 +287,8 @@ int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sy
ASSERT(db_local != NULL, RET_ERR(PM_ERR_DB_NULL, -1)); ASSERT(db_local != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1)); ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
ASSERT(name != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); ASSERT(name != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
STRDUP(targline, name, RET_ERR(PM_ERR_MEMORY, -1));
strncpy(targline, name, PKG_FULLNAME_LEN);
targ = strchr(targline, '/'); targ = strchr(targline, '/');
if(targ) { if(targ) {
/* we are looking for a package in a specific database */ /* we are looking for a package in a specific database */
@ -301,13 +301,15 @@ int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sy
repo_found = 1; repo_found = 1;
spkg = _alpm_db_get_pkgfromcache(db, targ); spkg = _alpm_db_get_pkgfromcache(db, targ);
if(spkg == NULL) { if(spkg == NULL) {
RET_ERR(PM_ERR_PKG_NOT_FOUND, -1); pm_errno = PM_ERR_PKG_NOT_FOUND;
goto error;
} }
} }
} }
if(!repo_found) { if(!repo_found) {
_alpm_log(PM_LOG_ERROR, _("repository '%s' not found\n"), targline); _alpm_log(PM_LOG_ERROR, _("repository '%s' not found\n"), targline);
RET_ERR(PM_ERR_PKG_REPO_NOT_FOUND, -1); pm_errno = PM_ERR_PKG_REPO_NOT_FOUND;
goto error;
} }
} else { } else {
targ = targline; targ = targline;
@ -316,7 +318,8 @@ int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sy
spkg = _alpm_db_get_pkgfromcache(db, targ); spkg = _alpm_db_get_pkgfromcache(db, targ);
} }
if(spkg == NULL) { if(spkg == NULL) {
RET_ERR(PM_ERR_PKG_NOT_FOUND, -1); pm_errno = PM_ERR_PKG_NOT_FOUND;
goto error;
} }
} }
@ -349,20 +352,29 @@ int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sy
if(local) { if(local) {
dummy = _alpm_pkg_dup(local); dummy = _alpm_pkg_dup(local);
if(dummy == NULL) { if(dummy == NULL) {
RET_ERR(PM_ERR_MEMORY, -1); pm_errno = PM_ERR_MEMORY;
goto error;
} }
} }
sync = _alpm_sync_new(PM_SYNC_TYPE_UPGRADE, spkg, dummy); sync = _alpm_sync_new(PM_SYNC_TYPE_UPGRADE, spkg, dummy);
if(sync == NULL) { if(sync == NULL) {
_alpm_pkg_free(dummy); _alpm_pkg_free(dummy);
RET_ERR(PM_ERR_MEMORY, -1); pm_errno = PM_ERR_MEMORY;
goto error;
} }
_alpm_log(PM_LOG_DEBUG, "adding target '%s' to the transaction set\n", _alpm_log(PM_LOG_DEBUG, "adding target '%s' to the transaction set\n",
alpm_pkg_get_name(spkg)); alpm_pkg_get_name(spkg));
trans->packages = alpm_list_add(trans->packages, sync); trans->packages = alpm_list_add(trans->packages, sync);
} }
FREE(targline);
return(0); return(0);
error:
if(targline) {
FREE(targline);
}
return(-1);
} }
/* Helper functions for alpm_list_remove /* Helper functions for alpm_list_remove
@ -618,7 +630,8 @@ int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync
} }
} }
FREELIST(asked); FREELIST(asked);
FREELIST(deps); alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
alpm_list_free(deps);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
@ -776,7 +789,7 @@ static int apply_deltas(pmtrans_t *trans, alpm_list_t *patches)
pmpkg_t *pkg; pmpkg_t *pkg;
pmdelta_t *d; pmdelta_t *d;
char command[PATH_MAX], fname[PATH_MAX]; char command[PATH_MAX], fname[PATH_MAX];
char pkgfilename[PKG_FILENAME_LEN]; char pkgfilename[PATH_MAX];
pkg = alpm_list_getdata(p); pkg = alpm_list_getdata(p);
p = alpm_list_next(p); p = alpm_list_next(p);
@ -811,7 +824,7 @@ static int apply_deltas(pmtrans_t *trans, alpm_list_t *patches)
_alpm_log(PM_LOG_DEBUG, _("command: %s\n"), command); _alpm_log(PM_LOG_DEBUG, _("command: %s\n"), command);
snprintf(pkgfilename, PKG_FILENAME_LEN, "%s-%s-%s" PKGEXT, snprintf(pkgfilename, PATH_MAX, "%s-%s-%s" PKGEXT,
pkg->name, d->to, pkg->arch); pkg->name, d->to, pkg->arch);
EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, pkgfilename, d->filename); EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, pkgfilename, d->filename);

View file

@ -43,7 +43,8 @@
#define MALLOC(p, s, action) do { p = calloc(1, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0) #define MALLOC(p, s, action) do { p = calloc(1, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0)
#define CALLOC(p, l, s, action) do { p = calloc(l, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0) #define CALLOC(p, l, s, action) do { p = calloc(l, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0)
#define STRDUP(r, s, action) do { r = strdup(s); if(r == NULL) { ALLOC_FAIL(strlen(s)); action; } } while(0) /* This strdup macro is NULL safe- copying NULL will yield NULL */
#define STRDUP(r, s, action) do { if(s != NULL) { r = strdup(s); if(r == NULL) { ALLOC_FAIL(strlen(s)); action; } } else { r = NULL; } } while(0)
#define FREE(p) do { if(p) { free(p); p = NULL; } } while(0) #define FREE(p) do { if(p) { free(p); p = NULL; } } while(0)

View file

@ -194,6 +194,10 @@ void indentprint(const char *str, int indent)
const char *p = str; const char *p = str;
int cidx = indent; int cidx = indent;
if(!p) {
return;
}
while(*p) { while(*p) {
if(*p == ' ') { if(*p == ' ') {
const char *next = NULL; const char *next = NULL;