Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/libalpm/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/util.c')
-rw-r--r--lib/libalpm/util.c148
1 files changed, 73 insertions, 75 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 430da92a..6f0763d5 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -53,10 +53,10 @@
/* libalpm */
#include "util.h"
#include "log.h"
-#include "package.h"
#include "alpm.h"
#include "alpm_list.h"
#include "handle.h"
+#include "trans.h"
#ifndef HAVE_STRSEP
/* This is a replacement for strsep which is not portable (missing on Solaris).
@@ -65,14 +65,14 @@ char* strsep(char** str, const char* delims)
{
char* token;
- if (*str==NULL) {
+ if(*str==NULL) {
/* No more tokens */
return NULL;
}
token=*str;
- while (**str!='\0') {
- if (strchr(delims,**str)!=NULL) {
+ while(**str!='\0') {
+ if(strchr(delims,**str)!=NULL) {
**str='\0';
(*str)++;
return token;
@@ -87,7 +87,7 @@ char* strsep(char** str, const char* delims)
int _alpm_makepath(const char *path)
{
- return(_alpm_makepath_mode(path, 0755));
+ return _alpm_makepath_mode(path, 0755);
}
/* does the same thing as 'mkdir -p' */
@@ -123,7 +123,7 @@ int _alpm_makepath_mode(const char *path, mode_t mode)
free(orig);
free(incr);
umask(oldmask);
- return(ret);
+ return ret;
}
#define CPBUFSIZE 8 * 1024
@@ -137,12 +137,12 @@ int _alpm_copyfile(const char *src, const char *dest)
in = fopen(src, "rb");
if(in == NULL) {
- return(1);
+ return 1;
}
out = fopen(dest, "wb");
if(out == NULL) {
fclose(in);
- return(1);
+ return 1;
}
CALLOC(buf, (size_t)CPBUFSIZE, (size_t)1, ret = 1; goto cleanup;);
@@ -175,7 +175,7 @@ cleanup:
fclose(in);
fclose(out);
FREE(buf);
- return(ret);
+ return ret;
}
/* Trim whitespace and newlines from a string
@@ -186,7 +186,7 @@ char *_alpm_strtrim(char *str)
if(*str == '\0') {
/* string is empty, so we're done. */
- return(str);
+ return str;
}
while(isspace((unsigned char)*pch)) {
@@ -198,7 +198,7 @@ char *_alpm_strtrim(char *str)
/* check if there wasn't anything but whitespace in the string. */
if(*str == '\0') {
- return(str);
+ return str;
}
pch = (str + (strlen(str) - 1));
@@ -207,7 +207,7 @@ char *_alpm_strtrim(char *str)
}
*++pch = '\0';
- return(str);
+ return str;
}
/* Compression functions */
@@ -225,12 +225,12 @@ int _alpm_unpack_single(const char *archive, const char *prefix, const char *fn)
alpm_list_t *list = NULL;
int ret = 0;
if(fn == NULL) {
- return(1);
+ return 1;
}
list = alpm_list_add(list, (void *)fn);
ret = _alpm_unpack(archive, prefix, list, 1);
alpm_list_free(list);
- return(ret);
+ return ret;
}
/**
@@ -253,8 +253,6 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int
char cwd[PATH_MAX];
int restore_cwd = 0;
- ALPM_LOG_FUNC;
-
if((_archive = archive_read_new()) == NULL)
RET_ERR(PM_ERR_LIBARCHIVE, 1);
@@ -307,7 +305,7 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int
char *found = alpm_list_find_str(list, prefix);
free(prefix);
if(!found) {
- if (archive_read_data_skip(_archive) != ARCHIVE_OK) {
+ if(archive_read_data_skip(_archive) != ARCHIVE_OK) {
ret = 1;
goto cleanup;
}
@@ -341,7 +339,7 @@ cleanup:
if(restore_cwd && chdir(cwd) != 0) {
_alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
}
- return(ret);
+ return ret;
}
/* does the same thing as 'rm -rf' */
@@ -356,18 +354,18 @@ int _alpm_rmrf(const char *path)
if(_alpm_lstat(path, &st) == 0) {
if(!S_ISDIR(st.st_mode)) {
if(!unlink(path)) {
- return(0);
+ return 0;
} else {
if(errno == ENOENT) {
- return(0);
+ return 0;
} else {
- return(1);
+ return 1;
}
}
} else {
dirp = opendir(path);
if(!dirp) {
- return(1);
+ return 1;
}
for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if(dp->d_ino) {
@@ -382,9 +380,9 @@ int _alpm_rmrf(const char *path)
errflag++;
}
}
- return(errflag);
+ return errflag;
}
- return(0);
+ return 0;
}
int _alpm_logaction(int usesyslog, FILE *f, const char *fmt, va_list args)
@@ -415,10 +413,10 @@ int _alpm_logaction(int usesyslog, FILE *f, const char *fmt, va_list args)
fflush(f);
}
- return(ret);
+ return ret;
}
-int _alpm_run_chroot(const char *root, const char *path, char *const argv[])
+int _alpm_run_chroot(pmhandle_t *handle, const char *path, char *const argv[])
{
char cwd[PATH_MAX];
pid_t pid;
@@ -426,8 +424,6 @@ int _alpm_run_chroot(const char *root, const char *path, char *const argv[])
int restore_cwd = 0;
int retval = 0;
- ALPM_LOG_FUNC;
-
/* save the cwd so we can restore it later */
if(getcwd(cwd, PATH_MAX) == NULL) {
_alpm_log(PM_LOG_ERROR, _("could not get current working directory\n"));
@@ -436,12 +432,14 @@ int _alpm_run_chroot(const char *root, const char *path, char *const argv[])
}
/* just in case our cwd was removed in the upgrade operation */
- if(chdir(root) != 0) {
- _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), root, strerror(errno));
+ if(chdir(handle->root) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"),
+ handle->root, strerror(errno));
goto cleanup;
}
- _alpm_log(PM_LOG_DEBUG, "executing \"%s\" under chroot \"%s\"\n", path, root);
+ _alpm_log(PM_LOG_DEBUG, "executing \"%s\" under chroot \"%s\"\n",
+ path, handle->root);
/* Flush open fds before fork() to avoid cloning buffers */
fflush(NULL);
@@ -470,7 +468,7 @@ int _alpm_run_chroot(const char *root, const char *path, char *const argv[])
close(pipefd[1]);
/* use fprintf instead of _alpm_log to send output through the parent */
- if(chroot(root) != 0) {
+ if(chroot(handle->root) != 0) {
fprintf(stderr, _("could not change the root directory (%s)\n"), strerror(errno));
exit(1);
}
@@ -532,32 +530,32 @@ cleanup:
_alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
}
- return(retval);
+ return retval;
}
-int _alpm_ldconfig(const char *root)
+int _alpm_ldconfig(pmhandle_t *handle)
{
char line[PATH_MAX];
_alpm_log(PM_LOG_DEBUG, "running ldconfig\n");
- snprintf(line, PATH_MAX, "%setc/ld.so.conf", root);
+ snprintf(line, PATH_MAX, "%setc/ld.so.conf", handle->root);
if(access(line, F_OK) == 0) {
- snprintf(line, PATH_MAX, "%ssbin/ldconfig", root);
+ snprintf(line, PATH_MAX, "%ssbin/ldconfig", handle->root);
if(access(line, X_OK) == 0) {
char *argv[] = { "ldconfig", NULL };
- _alpm_run_chroot(root, "/sbin/ldconfig", argv);
+ _alpm_run_chroot(handle, "/sbin/ldconfig", argv);
}
}
- return(0);
+ return 0;
}
/* Helper function for comparing strings using the
* alpm "compare func" signature */
int _alpm_str_cmp(const void *s1, const void *s2)
{
- return(strcmp(s1, s2));
+ return strcmp(s1, s2);
}
/** Find a filename in a registered alpm cachedir.
@@ -573,12 +571,12 @@ char *_alpm_filecache_find(const char* filename)
/* Loop through the cache dirs until we find a matching file */
for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
- snprintf(path, PATH_MAX, "%s%s", (char*)alpm_list_getdata(i),
+ snprintf(path, PATH_MAX, "%s%s", (char *)alpm_list_getdata(i),
filename);
if(stat(path, &buf) == 0 && S_ISREG(buf.st_mode)) {
retpath = strdup(path);
_alpm_log(PM_LOG_DEBUG, "found cached pkg: %s\n", retpath);
- return(retpath);
+ return retpath;
}
}
/* package wasn't found in any cachedir */
@@ -604,20 +602,23 @@ const char *_alpm_filecache_setup(void)
cachedir);
if(_alpm_makepath(cachedir) == 0) {
_alpm_log(PM_LOG_DEBUG, "using cachedir: %s\n", cachedir);
- return(cachedir);
+ return cachedir;
}
} else if(S_ISDIR(buf.st_mode) && (buf.st_mode & S_IWUSR)) {
_alpm_log(PM_LOG_DEBUG, "using cachedir: %s\n", cachedir);
- return(cachedir);
+ return cachedir;
+ } else {
+ _alpm_log(PM_LOG_DEBUG, "skipping cachedir: %s\n", cachedir);
}
}
/* we didn't find a valid cache directory. use /tmp. */
- tmp = alpm_list_add(NULL, strdup("/tmp/"));
+ tmp = alpm_list_add(NULL, "/tmp/");
alpm_option_set_cachedirs(tmp);
- _alpm_log(PM_LOG_DEBUG, "using cachedir: %s", "/tmp/\n");
+ alpm_list_free(tmp);
+ _alpm_log(PM_LOG_DEBUG, "using cachedir: %s\n", "/tmp/");
_alpm_log(PM_LOG_WARNING, _("couldn't create package cache, using /tmp instead\n"));
- return(alpm_list_getdata(tmp));
+ return "/tmp/";
}
/** lstat wrapper that treats /path/dirsymlink/ the same as /path/dirsymlink.
@@ -641,7 +642,7 @@ int _alpm_lstat(const char *path, struct stat *buf)
ret = lstat(newpath, buf);
FREE(newpath);
- return(ret);
+ return ret;
}
#ifdef HAVE_LIBSSL
@@ -652,11 +653,11 @@ static int md5_file(const char *path, unsigned char output[16])
MD5_CTX ctx;
unsigned char *buf;
- CALLOC(buf, 8192, sizeof(unsigned char), return(1));
+ CALLOC(buf, 8192, sizeof(unsigned char), RET_ERR(PM_ERR_MEMORY, 1));
if((f = fopen(path, "rb")) == NULL) {
free(buf);
- return(1);
+ return 1;
}
MD5_Init(&ctx);
@@ -672,11 +673,11 @@ static int md5_file(const char *path, unsigned char output[16])
if(ferror(f) != 0) {
fclose(f);
- return(2);
+ return 2;
}
fclose(f);
- return(0);
+ return 0;
}
#endif
@@ -691,16 +692,14 @@ char SYMEXPORT *alpm_compute_md5sum(const char *filename)
char *md5sum;
int ret, i;
- ALPM_LOG_FUNC;
-
- ASSERT(filename != NULL, return(NULL));
+ ASSERT(filename != NULL, return NULL);
/* allocate 32 chars plus 1 for null */
md5sum = calloc(33, sizeof(char));
/* defined above for OpenSSL, otherwise defined in md5.h */
ret = md5_file(filename, output);
- if (ret > 0) {
+ if(ret > 0) {
RET_ERR(PM_ERR_NOT_A_FILE, NULL);
}
@@ -712,7 +711,7 @@ char SYMEXPORT *alpm_compute_md5sum(const char *filename)
md5sum[32] = '\0';
_alpm_log(PM_LOG_DEBUG, "md5(%s) = %s\n", filename, md5sum);
- return(md5sum);
+ return md5sum;
}
int _alpm_test_md5sum(const char *filepath, const char *md5sum)
@@ -731,7 +730,7 @@ int _alpm_test_md5sum(const char *filepath, const char *md5sum)
}
FREE(md5sum2);
- return(ret);
+ return ret;
}
/* Note: does NOT handle sparse files on purpose for speed. */
@@ -753,7 +752,7 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
}
/* zero-copy - this is the entire next block of data. */
- b->ret = archive_read_data_block(a, (void*)&b->block,
+ b->ret = archive_read_data_block(a, (void *)&b->block,
&b->block_size, &offset);
b->block_offset = b->block;
@@ -776,20 +775,19 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
/* allocate our buffer, or ensure our existing one is big enough */
if(!b->line) {
/* set the initial buffer to the read block_size */
- CALLOC(b->line, b->block_size + 1, sizeof(char),
- RET_ERR(PM_ERR_MEMORY, -1));
+ CALLOC(b->line, b->block_size + 1, sizeof(char), return ENOMEM);
b->line_size = b->block_size + 1;
b->line_offset = b->line;
} else {
size_t needed = (size_t)((b->line_offset - b->line)
+ (i - b->block_offset) + 1);
if(needed > b->max_line_size) {
- RET_ERR(PM_ERR_MEMORY, -1);
+ return ERANGE;
}
if(needed > b->line_size) {
/* need to realloc + copy data to fit total length */
char *new;
- CALLOC(new, needed, sizeof(char), RET_ERR(PM_ERR_MEMORY, -1));
+ CALLOC(new, needed, sizeof(char), return ENOMEM);
memcpy(new, b->line, b->line_size);
b->line_size = needed;
b->line_offset = new + (b->line_offset - b->line);
@@ -804,7 +802,7 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
b->line_offset[len] = '\0';
b->block_offset = ++i;
/* this is the main return point; from here you can read b->line */
- return(ARCHIVE_OK);
+ return ARCHIVE_OK;
} else {
/* we've looked through the whole block but no newline, copy it */
size_t len = (size_t)(b->block + b->block_size - b->block_offset);
@@ -819,7 +817,7 @@ cleanup:
int ret = b->ret;
FREE(b->line);
memset(b, 0, sizeof(b));
- return(ret);
+ return ret;
}
}
@@ -833,12 +831,12 @@ int _alpm_splitname(const char *target, pmpkg_t *pkg)
const char *version, *end;
if(target == NULL || pkg == NULL) {
- return(-1);
+ return -1;
}
end = target + strlen(target);
/* remove any trailing '/' */
- while (*(end - 1) == '/') {
+ while(*(end - 1) == '/') {
--end;
}
@@ -847,7 +845,7 @@ int _alpm_splitname(const char *target, pmpkg_t *pkg)
for(version = end - 1; *version && *version != '-'; version--);
for(version = version - 1; *version && *version != '-'; version--);
if(*version != '-' || version == target) {
- return(-1);
+ return -1;
}
/* copy into fields and return */
@@ -865,7 +863,7 @@ int _alpm_splitname(const char *target, pmpkg_t *pkg)
STRNDUP(pkg->name, target, version - target, RET_ERR(PM_ERR_MEMORY, -1));
pkg->name_hash = _alpm_hash_sdbm(pkg->name);
- return(0);
+ return 0;
}
/**
@@ -880,13 +878,13 @@ unsigned long _alpm_hash_sdbm(const char *str)
int c;
if(!str) {
- return(hash);
+ return hash;
}
while((c = *str++)) {
hash = c + (hash << 6) + (hash << 16) - hash;
}
- return(hash);
+ return hash;
}
long _alpm_parsedate(const char *line)
@@ -897,9 +895,9 @@ long _alpm_parsedate(const char *line)
setlocale(LC_TIME, "C");
strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
setlocale(LC_TIME, "");
- return(mktime(&tmp_tm));
+ return mktime(&tmp_tm);
}
- return(atol(line));
+ return atol(line);
}
#ifndef HAVE_STRNDUP
@@ -908,7 +906,7 @@ static size_t strnlen(const char *s, size_t max)
{
register const char *p;
for(p = s; *p && max--; ++p);
- return(p - s);
+ return (p - s);
}
char *strndup(const char *s, size_t n)
@@ -916,11 +914,11 @@ char *strndup(const char *s, size_t n)
size_t len = strnlen(s, n);
char *new = (char *) malloc(len + 1);
- if (new == NULL)
+ if(new == NULL)
return NULL;
new[len] = '\0';
- return (char *) memcpy(new, s, len);
+ return (char *)memcpy(new, s, len);
}
#endif