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.c120
1 files changed, 52 insertions, 68 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 082c095b..d9108096 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -175,7 +175,7 @@ char *_alpm_strtrim(char *str)
return(str);
}
- while(isspace((int)*pch)) {
+ while(isspace((unsigned char)*pch)) {
pch++;
}
if(pch != str) {
@@ -188,7 +188,7 @@ char *_alpm_strtrim(char *str)
}
pch = (str + (strlen(str) - 1));
- while(isspace((int)*pch)) {
+ while(isspace((unsigned char)*pch)) {
pch--;
}
*++pch = '\0';
@@ -196,57 +196,11 @@ char *_alpm_strtrim(char *str)
return(str);
}
-/* Helper function for _alpm_strreplace */
-static void _strnadd(char **str, const char *append, unsigned int count)
-{
- if(*str) {
- *str = realloc(*str, strlen(*str) + count + 1);
- } else {
- *str = calloc(count + 1, sizeof(char));
- }
-
- strncat(*str, append, count);
-}
-
-/* Replace all occurances of 'needle' with 'replace' in 'str', returning
- * a new string (must be free'd) */
-char *_alpm_strreplace(const char *str, const char *needle, const char *replace)
-{
- const char *p, *q;
- p = q = str;
-
- char *newstr = NULL;
- unsigned int needlesz = strlen(needle),
- replacesz = strlen(replace);
-
- while (1) {
- q = strstr(p, needle);
- if(!q) { /* not found */
- if(*p) {
- /* add the rest of 'p' */
- _strnadd(&newstr, p, strlen(p));
- }
- break;
- } else { /* found match */
- if(q > p){
- /* add chars between this occurance and last occurance, if any */
- _strnadd(&newstr, p, q - p);
- }
- _strnadd(&newstr, replace, replacesz);
- p = q + needlesz;
- }
- }
-
- return newstr;
-}
-
-
/* Create a lock file */
int _alpm_lckmk()
{
int fd;
- pid_t pid;
- char *dir, *ptr, *spid = NULL;
+ char *dir, *ptr;
const char *file = alpm_option_get_lockfile();
/* create the dir of the lockfile first */
@@ -261,13 +215,11 @@ int _alpm_lckmk()
while((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000)) == -1
&& errno == EINTR);
if(fd > 0) {
- pid = getpid();
- size_t len = snprintf(spid, 0, "%ld\n", (long)pid) + 1;
- spid = malloc(len);
- snprintf(spid, len, "%ld\n", (long)pid);
- while(write(fd, (void *)spid, len) == -1 && errno == EINTR);
+ FILE *f = fdopen(fd, "w");
+ fprintf(f, "%ld\n", (long)getpid());
+ fflush(f);
fsync(fd);
- free(spid);
+ fclose(f);
return(fd);
}
return(-1);
@@ -286,14 +238,38 @@ int _alpm_lckrm()
/* Compression functions */
/**
- * @brief Unpack a specific file or all files in an archive.
+ * @brief Unpack a specific file in an archive.
*
* @param archive the archive to unpack
* @param prefix where to extract the files
- * @param fn a file within the archive to unpack or NULL for all
+ * @param fn a file within the archive to unpack
* @return 0 on success, 1 on failure
*/
-int _alpm_unpack(const char *archive, const char *prefix, const char *fn)
+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);
+ }
+ list = alpm_list_add(list, (void *)fn);
+ ret = _alpm_unpack(archive, prefix, list, 1);
+ alpm_list_free(list);
+ return(ret);
+}
+
+/**
+ * @brief Unpack a list of files in an archive.
+ *
+ * @param archive the archive to unpack
+ * @param prefix where to extract the files
+ * @param list a list of files within the archive to unpack or
+ * NULL for all
+ * @param breakfirst break after the first entry found
+ *
+ * @return 0 on success, 1 on failure
+ */
+int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int breakfirst)
{
int ret = 0;
mode_t oldmask;
@@ -346,14 +322,23 @@ int _alpm_unpack(const char *archive, const char *prefix, const char *fn)
archive_entry_set_perm(entry, 0755);
}
- /* If a specific file was requested skip entries that don't match. */
- if (fn && strcmp(fn, entryname)) {
- _alpm_log(PM_LOG_DEBUG, "skipping: %s\n", entryname);
- if (archive_read_data_skip(_archive) != ARCHIVE_OK) {
- ret = 1;
- goto cleanup;
+ /* If specific files were requested, skip entries that don't match. */
+ if(list) {
+ char *prefix = strdup(entryname);
+ char *p = strstr(prefix,"/");
+ if(p) {
+ *(p+1) = '\0';
+ }
+ char *found = alpm_list_find_str(list, prefix);
+ free(prefix);
+ if(!found) {
+ _alpm_log(PM_LOG_DEBUG, "skipping: %s\n", entryname);
+ if (archive_read_data_skip(_archive) != ARCHIVE_OK) {
+ ret = 1;
+ goto cleanup;
+ }
+ continue;
}
- continue;
}
/* Extract the archive entry. */
@@ -369,7 +354,7 @@ int _alpm_unpack(const char *archive, const char *prefix, const char *fn)
goto cleanup;
}
- if(fn) {
+ if(breakfirst) {
break;
}
}
@@ -425,8 +410,7 @@ int _alpm_rmrf(const char *path)
return(0);
}
-int _alpm_logaction(unsigned short usesyslog, FILE *f,
- const char *fmt, va_list args)
+int _alpm_logaction(int usesyslog, FILE *f, const char *fmt, va_list args)
{
int ret = 0;