From 4ea955bc7e5575bc1d8b34c364591c47653f2cfd Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 10 Feb 2017 11:20:23 +0100 Subject: [PATCH] treewrite: use Lindent to reformat to kernel coding style Use the kernels "Lindent" script to reformat files using kernel coding style. This should make subsequent refactoring much easier. Signed-off-by: Jo-Philipp Wich --- libbb/all_read.c | 56 +- libbb/concat_path_file.c | 6 +- libbb/copy_file.c | 44 +- libbb/copy_file_chunk.c | 19 +- libbb/gz_open.c | 31 +- libbb/gzip.c | 96 +- libbb/gzip.h | 2 +- libbb/last_char_is.c | 8 +- libbb/libbb.h | 23 +- libbb/make_directory.c | 34 +- libbb/mode_string.c | 6 +- libbb/parse_mode.c | 87 +- libbb/safe_strncpy.c | 9 +- libbb/time_string.c | 2 - libbb/unarchive.c | 371 ++--- libbb/unzip.c | 467 +++--- libbb/wfopen.c | 1 - libbb/xfuncs.c | 11 +- libbb/xreadlink.c | 9 +- libopkg/active_list.c | 250 ++-- libopkg/active_list.h | 26 +- libopkg/conffile.c | 58 +- libopkg/conffile.h | 8 +- libopkg/conffile_list.c | 25 +- libopkg/conffile_list.h | 13 +- libopkg/file_util.c | 188 ++- libopkg/file_util.h | 2 +- libopkg/hash_table.c | 278 ++-- libopkg/hash_table.h | 42 +- libopkg/list.h | 113 +- libopkg/md5.c | 599 ++++---- libopkg/md5.h | 75 +- libopkg/nv_pair.c | 20 +- libopkg/nv_pair.h | 12 +- libopkg/nv_pair_list.c | 106 +- libopkg/nv_pair_list.h | 27 +- libopkg/opkg.c | 112 +- libopkg/opkg.h | 61 +- libopkg/opkg_cmd.c | 1879 ++++++++++++------------ libopkg/opkg_cmd.h | 17 +- libopkg/opkg_conf.c | 796 +++++----- libopkg/opkg_conf.h | 159 +- libopkg/opkg_configure.c | 28 +- libopkg/opkg_configure.h | 2 +- libopkg/opkg_download.c | 1053 +++++++------- libopkg/opkg_download.h | 13 +- libopkg/opkg_install.c | 2570 ++++++++++++++++----------------- libopkg/opkg_install.h | 2 +- libopkg/opkg_message.c | 32 +- libopkg/opkg_message.h | 12 +- libopkg/opkg_pathfinder.c | 97 +- libopkg/opkg_pathfinder.h | 6 +- libopkg/opkg_remove.c | 666 ++++----- libopkg/opkg_remove.h | 9 +- libopkg/opkg_upgrade.c | 159 +- libopkg/opkg_upgrade.h | 4 +- libopkg/opkg_utils.c | 73 +- libopkg/opkg_utils.h | 2 +- libopkg/parse_util.c | 42 +- libopkg/parse_util.h | 9 +- libopkg/pkg.c | 1893 ++++++++++++------------ libopkg/pkg.h | 243 ++-- libopkg/pkg_depends.c | 1423 +++++++++--------- libopkg/pkg_depends.h | 59 +- libopkg/pkg_dest.c | 75 +- libopkg/pkg_dest.h | 23 +- libopkg/pkg_dest_list.c | 62 +- libopkg/pkg_dest_list.h | 17 +- libopkg/pkg_extract.c | 60 +- libopkg/pkg_extract.h | 10 +- libopkg/pkg_hash.c | 610 ++++---- libopkg/pkg_hash.h | 34 +- libopkg/pkg_parse.c | 113 +- libopkg/pkg_parse.h | 4 +- libopkg/pkg_src.c | 29 +- libopkg/pkg_src.h | 16 +- libopkg/pkg_src_list.c | 50 +- libopkg/pkg_src_list.h | 21 +- libopkg/pkg_vec.c | 255 ++-- libopkg/pkg_vec.h | 46 +- libopkg/sha256.c | 750 +++++----- libopkg/sha256.h | 66 +- libopkg/sprintf_alloc.c | 10 +- libopkg/str_list.c | 106 +- libopkg/str_list.h | 28 +- libopkg/void_list.c | 217 +-- libopkg/void_list.h | 48 +- libopkg/xregex.c | 32 +- libopkg/xregex.h | 7 +- libopkg/xsystem.c | 9 +- libopkg/xsystem.h | 1 - src/opkg-cl.c | 159 +- tests/libopkg_test.c | 410 +++--- tests/opkg_active_list_test.c | 192 +-- tests/opkg_extract_test.c | 68 +- tests/opkg_hash_test.c | 111 +- 96 files changed, 9189 insertions(+), 8995 deletions(-) diff --git a/libbb/all_read.c b/libbb/all_read.c index 6ec731a..371dcaf 100644 --- a/libbb/all_read.c +++ b/libbb/all_read.c @@ -24,15 +24,15 @@ #include #include "libbb.h" -extern void archive_xread_all(int fd , char *buf, size_t count) +extern void archive_xread_all(int fd, char *buf, size_t count) { - ssize_t size; + ssize_t size; - size = full_read(fd, buf, count); - if (size != count) { - perror_msg_and_die("Short read"); - } - return; + size = full_read(fd, buf, count); + if (size != count) { + perror_msg_and_die("Short read"); + } + return; } /* @@ -43,41 +43,37 @@ extern void archive_xread_all(int fd , char *buf, size_t count) */ ssize_t full_read(int fd, char *buf, int len) { - ssize_t cc; - ssize_t total; + ssize_t cc; + ssize_t total; - total = 0; + total = 0; - while (len > 0) { - cc = safe_read(fd, buf, len); + while (len > 0) { + cc = safe_read(fd, buf, len); - if (cc < 0) - return cc; /* read() returns -1 on failure. */ + if (cc < 0) + return cc; /* read() returns -1 on failure. */ - if (cc == 0) - break; + if (cc == 0) + break; - buf = ((char *)buf) + cc; - total += cc; - len -= cc; - } + buf = ((char *)buf) + cc; + total += cc; + len -= cc; + } - return total; + return total; } - ssize_t safe_read(int fd, void *buf, size_t count) { - ssize_t n; + ssize_t n; - do { - n = read(fd, buf, count); - } while (n < 0 && errno == EINTR); + do { + n = read(fd, buf, count); + } while (n < 0 && errno == EINTR); - return n; + return n; } - - /* END CODE */ - diff --git a/libbb/concat_path_file.c b/libbb/concat_path_file.c index e62b99e..f0cb552 100644 --- a/libbb/concat_path_file.c +++ b/libbb/concat_path_file.c @@ -34,12 +34,12 @@ extern char *concat_path_file(const char *path, const char *filename) char *lc; if (!path) - path=""; + path = ""; lc = last_char_is(path, '/'); while (*filename == '/') filename++; - outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL)); - sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename); + outbuf = xmalloc(strlen(path) + strlen(filename) + 1 + (lc == NULL)); + sprintf(outbuf, "%s%s%s", path, (lc == NULL) ? "/" : "", filename); return outbuf; } diff --git a/libbb/copy_file.c b/libbb/copy_file.c index fb76669..849e6d8 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -41,9 +41,9 @@ int copy_file(const char *source, const char *dest, int flags) int status = 0; if (((flags & FILEUTILS_PRESERVE_SYMLINKS) && - lstat(source, &source_stat) < 0) || - (!(flags & FILEUTILS_PRESERVE_SYMLINKS) && - stat(source, &source_stat) < 0)) { + lstat(source, &source_stat) < 0) || + (!(flags & FILEUTILS_PRESERVE_SYMLINKS) && + stat(source, &source_stat) < 0)) { perror_msg("%s", source); return -1; } @@ -57,7 +57,7 @@ int copy_file(const char *source, const char *dest, int flags) } if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev && - source_stat.st_ino == dest_stat.st_ino) { + source_stat.st_ino == dest_stat.st_ino) { error_msg("`%s' and `%s' are the same file", source, dest); return -1; } @@ -89,7 +89,8 @@ int copy_file(const char *source, const char *dest, int flags) if (mkdir(dest, mode) < 0) { umask(saved_umask); - perror_msg("cannot create directory `%s'", dest); + perror_msg("cannot create directory `%s'", + dest); return -1; } @@ -107,7 +108,7 @@ int copy_file(const char *source, const char *dest, int flags) char *new_source, *new_dest; if (strcmp(d->d_name, ".") == 0 || - strcmp(d->d_name, "..") == 0) + strcmp(d->d_name, "..") == 0) continue; new_source = concat_path_file(source, d->d_name); @@ -126,8 +127,9 @@ int copy_file(const char *source, const char *dest, int flags) } if (!dest_exists && - chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { - perror_msg("unable to change permissions of `%s'", dest); + chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { + perror_msg("unable to change permissions of `%s'", + dest); status = -1; } } else if (S_ISREG(source_stat.st_mode)) { @@ -141,7 +143,8 @@ int copy_file(const char *source, const char *dest, int flags) } if (unlink(dest) < 0) { - perror_msg("unable to remove `%s'", dest); + perror_msg("unable to remove `%s'", + dest); return -1; } @@ -152,8 +155,10 @@ int copy_file(const char *source, const char *dest, int flags) if (!dest_exists) { int fd; - if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || - (dfp = fdopen(fd, "w")) == NULL) { + if ((fd = + open(dest, O_WRONLY | O_CREAT, + source_stat.st_mode)) < 0 + || (dfp = fdopen(fd, "w")) == NULL) { if (fd >= 0) close(fd); perror_msg("unable to open `%s'", dest); @@ -180,8 +185,8 @@ int copy_file(const char *source, const char *dest, int flags) perror_msg("unable to close `%s'", source); status = -1; } - } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || - S_ISSOCK(source_stat.st_mode)) { + } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) + || S_ISSOCK(source_stat.st_mode)) { if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { perror_msg("unable to create `%s'", dest); return -1; @@ -201,8 +206,11 @@ int copy_file(const char *source, const char *dest, int flags) #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) if (flags & FILEUTILS_PRESERVE_STATUS) - if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) - perror_msg("unable to preserve ownership of `%s'", dest); + if (lchown(dest, source_stat.st_uid, source_stat.st_gid) + < 0) + perror_msg + ("unable to preserve ownership of `%s'", + dest); #endif return 0; } else { @@ -221,10 +229,12 @@ end: perror_msg("unable to preserve times of `%s'", dest); if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { source_stat.st_mode &= ~(S_ISUID | S_ISGID); - perror_msg("unable to preserve ownership of `%s'", dest); + perror_msg("unable to preserve ownership of `%s'", + dest); } if (chmod(dest, source_stat.st_mode) < 0) - perror_msg("unable to preserve permissions of `%s'", dest); + perror_msg("unable to preserve permissions of `%s'", + dest); } return status; diff --git a/libbb/copy_file_chunk.c b/libbb/copy_file_chunk.c index 63d2ab1..16edfe3 100644 --- a/libbb/copy_file_chunk.c +++ b/libbb/copy_file_chunk.c @@ -27,7 +27,8 @@ /* Copy CHUNKSIZE bytes (or until EOF if CHUNKSIZE equals -1) from SRC_FILE * to DST_FILE. */ -extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize) +extern int copy_file_chunk(FILE * src_file, FILE * dst_file, + unsigned long long chunksize) { size_t nread, nwritten, size; char buffer[BUFSIZ]; @@ -38,27 +39,27 @@ extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long ch else size = chunksize; - nread = fread (buffer, 1, size, src_file); + nread = fread(buffer, 1, size, src_file); - if (nread != size && ferror (src_file)) { - perror_msg ("read"); + if (nread != size && ferror(src_file)) { + perror_msg("read"); return -1; } else if (nread == 0) { if (chunksize != -1) { - error_msg ("Unable to read all data"); + error_msg("Unable to read all data"); return -1; } return 0; } - nwritten = fwrite (buffer, 1, nread, dst_file); + nwritten = fwrite(buffer, 1, nread, dst_file); if (nwritten != nread) { - if (ferror (dst_file)) - perror_msg ("write"); + if (ferror(dst_file)) + perror_msg("write"); else - error_msg ("Unable to write all data"); + error_msg("Unable to write all data"); return -1; } diff --git a/libbb/gz_open.c b/libbb/gz_open.c index bdc7564..bf5efca 100644 --- a/libbb/gz_open.c +++ b/libbb/gz_open.c @@ -31,8 +31,7 @@ static int gz_use_vfork; -FILE * -gz_open(FILE *compressed_file, int *pid) +FILE *gz_open(FILE * compressed_file, int *pid) { int unzip_pipe[2]; off_t floc; @@ -51,9 +50,9 @@ gz_open(FILE *compressed_file, int *pid) setenv("GZIP", "--quiet", 0); } - if (pipe(unzip_pipe)!=0) { + if (pipe(unzip_pipe) != 0) { perror_msg("pipe"); - return(NULL); + return (NULL); } /* If we don't flush, we end up with two copies of anything pending, @@ -67,18 +66,18 @@ gz_open(FILE *compressed_file, int *pid) *pid = fork(); } - if (*pid<0) { + if (*pid < 0) { perror_msg("fork"); - return(NULL); + return (NULL); } - if (*pid==0) { + if (*pid == 0) { /* child process */ close(unzip_pipe[0]); if (gz_use_vfork) { dup2(unzip_pipe[1], 1); dup2(cfile, 0); - execlp("gunzip","gunzip",NULL); + execlp("gunzip", "gunzip", NULL); /* If we get here, we had a failure */ _exit(EXIT_FAILURE); } else { @@ -94,11 +93,10 @@ gz_open(FILE *compressed_file, int *pid) close(cfile); } close(unzip_pipe[1]); - return(fdopen(unzip_pipe[0], "r")); + return (fdopen(unzip_pipe[0], "r")); } -int -gz_close(int gunzip_pid) +int gz_close(int gunzip_pid) { int status; int ret; @@ -115,7 +113,6 @@ gz_close(int gunzip_pid) } } - if (waitpid(gunzip_pid, &status, 0) == -1) { perror_msg("waitpid"); return -1; @@ -128,20 +125,20 @@ gz_close(int gunzip_pid) if (WIFSIGNALED(status)) { error_msg("Unzip process killed by signal %d.\n", - WTERMSIG(status)); + WTERMSIG(status)); return -1; } if (!WIFEXITED(status)) { /* shouldn't happen */ - error_msg("Your system is broken: got status %d from waitpid.\n", - status); + error_msg + ("Your system is broken: got status %d from waitpid.\n", + status); return -1; } if ((ret = WEXITSTATUS(status))) { - error_msg("Unzip process failed with return code %d.\n", - ret); + error_msg("Unzip process failed with return code %d.\n", ret); return -1; } diff --git a/libbb/gzip.c b/libbb/gzip.c index ecdb5cb..db22523 100644 --- a/libbb/gzip.c +++ b/libbb/gzip.c @@ -30,8 +30,7 @@ #include "gzip.h" -static void -to_devnull(int fd) +static void to_devnull(int fd) { int devnull = open("/dev/null", fd ? O_WRONLY : O_RDONLY); @@ -42,8 +41,7 @@ to_devnull(int fd) close(devnull); } -void * -gzip_thread(void *ptr) +void *gzip_thread(void *ptr) { struct gzip_handle *zh = ptr; char buf[4096]; @@ -67,11 +65,11 @@ gzip_thread(void *ptr) zh->wfd = -1; } -int -gzip_exec(struct gzip_handle *zh, const char *filename) +int gzip_exec(struct gzip_handle *zh, const char *filename) { - int rpipe[2] = { -1, -1 }, wpipe[2] = { -1, -1 }; - struct sigaction pipe_sa = { .sa_handler = SIG_IGN }; + int rpipe[2] = { -1, -1 }, wpipe[2] = { + -1, -1}; + struct sigaction pipe_sa = {.sa_handler = SIG_IGN }; zh->rfd = -1; zh->wfd = -1; @@ -91,47 +89,46 @@ gzip_exec(struct gzip_handle *zh, const char *filename) zh->pid = vfork(); switch (zh->pid) { - case -1: - return -1; - - case 0: - to_devnull(STDERR_FILENO); - - if (filename) { - to_devnull(STDIN_FILENO); - } - else { - dup2(wpipe[0], STDIN_FILENO); - close(wpipe[0]); - close(wpipe[1]); - } - - dup2(rpipe[1], STDOUT_FILENO); - close(rpipe[0]); - close(rpipe[1]); - - execlp("gzip", "gzip", "-d", "-c", filename, NULL); - exit(-1); - - default: - zh->rfd = rpipe[0]; - zh->wfd = wpipe[1]; - - fcntl(zh->rfd, F_SETFD, fcntl(zh->rfd, F_GETFD) | FD_CLOEXEC); - close(rpipe[1]); - - if (zh->wfd >= 0) { - fcntl(zh->wfd, F_SETFD, fcntl(zh->wfd, F_GETFD) | FD_CLOEXEC); - close(wpipe[0]); - pthread_create(&zh->thread, NULL, gzip_thread, zh); - } + case -1: + return -1; + + case 0: + to_devnull(STDERR_FILENO); + + if (filename) { + to_devnull(STDIN_FILENO); + } else { + dup2(wpipe[0], STDIN_FILENO); + close(wpipe[0]); + close(wpipe[1]); + } + + dup2(rpipe[1], STDOUT_FILENO); + close(rpipe[0]); + close(rpipe[1]); + + execlp("gzip", "gzip", "-d", "-c", filename, NULL); + exit(-1); + + default: + zh->rfd = rpipe[0]; + zh->wfd = wpipe[1]; + + fcntl(zh->rfd, F_SETFD, fcntl(zh->rfd, F_GETFD) | FD_CLOEXEC); + close(rpipe[1]); + + if (zh->wfd >= 0) { + fcntl(zh->wfd, F_SETFD, + fcntl(zh->wfd, F_GETFD) | FD_CLOEXEC); + close(wpipe[0]); + pthread_create(&zh->thread, NULL, gzip_thread, zh); + } } return 0; } -ssize_t -gzip_read(struct gzip_handle *zh, char *buf, ssize_t len) +ssize_t gzip_read(struct gzip_handle * zh, char *buf, ssize_t len) { ssize_t ret; @@ -142,15 +139,14 @@ gzip_read(struct gzip_handle *zh, char *buf, ssize_t len) return ret; } -ssize_t -gzip_copy(struct gzip_handle *zh, FILE *out, ssize_t len) +ssize_t gzip_copy(struct gzip_handle * zh, FILE * out, ssize_t len) { char buf[4096]; ssize_t rlen, total = 0; while (len > 0) { rlen = gzip_read(zh, buf, - (len > sizeof(buf)) ? sizeof(buf) : len); + (len > sizeof(buf)) ? sizeof(buf) : len); if (rlen <= 0) break; @@ -167,8 +163,7 @@ gzip_copy(struct gzip_handle *zh, FILE *out, ssize_t len) return total; } -FILE * -gzip_fdopen(struct gzip_handle *zh, const char *filename) +FILE *gzip_fdopen(struct gzip_handle * zh, const char *filename) { memset(zh, 0, sizeof(*zh)); @@ -180,8 +175,7 @@ gzip_fdopen(struct gzip_handle *zh, const char *filename) return fdopen(zh->rfd, "r"); } -int -gzip_close(struct gzip_handle *zh) +int gzip_close(struct gzip_handle *zh) { int code = -1; diff --git a/libbb/gzip.h b/libbb/gzip.h index 07e8e7a..13398d4 100644 --- a/libbb/gzip.h +++ b/libbb/gzip.h @@ -34,7 +34,7 @@ struct gzip_handle { int gzip_exec(struct gzip_handle *zh, const char *filename); ssize_t gzip_read(struct gzip_handle *zh, char *buf, ssize_t len); -ssize_t gzip_copy(struct gzip_handle *zh, FILE *out, ssize_t len); +ssize_t gzip_copy(struct gzip_handle *zh, FILE * out, ssize_t len); int gzip_close(struct gzip_handle *zh); FILE *gzip_fdopen(struct gzip_handle *zh, const char *filename); diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c index 26c2423..8c6dd6f 100644 --- a/libbb/last_char_is.c +++ b/libbb/last_char_is.c @@ -26,13 +26,13 @@ * underrun the buffer if the string length is 0. Also avoids a possible * space-hogging inline of strlen() per usage. */ -char * last_char_is(const char *s, int c) +char *last_char_is(const char *s, int c) { char *sret; if (!s) - return NULL; - sret = (char *)s+strlen(s)-1; - if (sret>=s && *sret == c) { + return NULL; + sret = (char *)s + strlen(s) - 1; + if (sret >= s && *sret == c) { return sret; } else { return NULL; diff --git a/libbb/libbb.h b/libbb/libbb.h index 4e1fafc..bc1ae21 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h @@ -56,20 +56,21 @@ const char *mode_string(int mode); const char *time_string(time_t timeVal); int copy_file(const char *source, const char *dest, int flags); -int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); +int copy_file_chunk(FILE * src_file, FILE * dst_file, + unsigned long long chunksize); ssize_t safe_read(int fd, void *buf, size_t count); ssize_t full_read(int fd, char *buf, int len); -extern int parse_mode( const char* s, mode_t* theMode); +extern int parse_mode(const char *s, mode_t * theMode); extern FILE *wfopen(const char *path, const char *mode); extern FILE *xfopen(const char *path, const char *mode); -extern void *xmalloc (size_t size); +extern void *xmalloc(size_t size); extern void *xrealloc(void *old, size_t size); extern void *xcalloc(size_t nmemb, size_t size); -extern char *xstrdup (const char *s); -extern char *xstrndup (const char *s, int n); +extern char *xstrdup(const char *s); +extern char *xstrndup(const char *s, int n); extern char *safe_strncpy(char *dst, const char *src, size_t size); char *xreadlink(const char *path); @@ -103,15 +104,15 @@ enum extract_functions_e { extract_exclude_list = 4096 }; -char *deb_extract(const char *package_filename, FILE *out_stream, - const int extract_function, const char *prefix, - const char *filename, int *err); +char *deb_extract(const char *package_filename, FILE * out_stream, + const int extract_function, const char *prefix, + const char *filename, int *err); -extern int unzip(FILE *l_in_file, FILE *l_out_file); +extern int unzip(FILE * l_in_file, FILE * l_out_file); extern int gz_close(int gunzip_pid); -extern FILE *gz_open(FILE *compressed_file, int *pid); +extern FILE *gz_open(FILE * compressed_file, int *pid); -int make_directory (const char *path, long mode, int flags); +int make_directory(const char *path, long mode, int flags); enum { FILEUTILS_PRESERVE_STATUS = 1, diff --git a/libbb/make_directory.c b/libbb/make_directory.c index 86ab554..3dfef7b 100644 --- a/libbb/make_directory.c +++ b/libbb/make_directory.c @@ -34,44 +34,44 @@ * Also create parent directories as necessary if flags contains * FILEUTILS_RECUR. */ -int make_directory (const char *path, long mode, int flags) +int make_directory(const char *path, long mode, int flags) { if (!(flags & FILEUTILS_RECUR)) { - if (mkdir (path, 0777) < 0) { - perror_msg ("Cannot create directory `%s'", path); + if (mkdir(path, 0777) < 0) { + perror_msg("Cannot create directory `%s'", path); return -1; } - if (mode != -1 && chmod (path, mode) < 0) { - perror_msg ("Cannot set permissions of directory `%s'", path); + if (mode != -1 && chmod(path, mode) < 0) { + perror_msg("Cannot set permissions of directory `%s'", + path); return -1; } } else { struct stat st; - if (stat (path, &st) < 0 && errno == ENOENT) { + if (stat(path, &st) < 0 && errno == ENOENT) { int status; char *pathcopy, *parent, *parentcopy; mode_t mask; - mask = umask (0); - umask (mask); + mask = umask(0); + umask(mask); /* dirname is unsafe, it may both modify the memory of the path argument and may return a pointer to static memory, which can then be modified by consequtive calls to dirname */ - pathcopy = xstrdup (path); - parent = dirname (pathcopy); - parentcopy = xstrdup (parent); - status = make_directory (parentcopy, (0777 & ~mask) - | 0300, FILEUTILS_RECUR); - free (pathcopy); - free (parentcopy); + pathcopy = xstrdup(path); + parent = dirname(pathcopy); + parentcopy = xstrdup(parent); + status = make_directory(parentcopy, (0777 & ~mask) + | 0300, FILEUTILS_RECUR); + free(pathcopy); + free(parentcopy); - - if (status < 0 || make_directory (path, mode, 0) < 0) + if (status < 0 || make_directory(path, mode, 0) < 0) return -1; } } diff --git a/libbb/mode_string.c b/libbb/mode_string.c index 12dc179..f1a6933 100644 --- a/libbb/mode_string.c +++ b/libbb/mode_string.c @@ -24,8 +24,6 @@ #include #include "libbb.h" - - #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) @@ -43,8 +41,8 @@ static const mode_t MBIT[] = { S_IROTH, S_IWOTH, S_IXOTH }; -static const char MODE1[] = "rwxrwxrwx"; -static const char MODE0[] = "---------"; +static const char MODE1[] = "rwxrwxrwx"; +static const char MODE0[] = "---------"; static const char SMODE1[] = "..s..s..t"; static const char SMODE0[] = "..S..S..T"; diff --git a/libbb/parse_mode.c b/libbb/parse_mode.c index 02668c7..b43b0ea 100644 --- a/libbb/parse_mode.c +++ b/libbb/parse_mode.c @@ -25,25 +25,24 @@ #include #include "libbb.h" - /* This function parses the sort of string you might pass * to chmod (i.e., [ugoa]{+|-|=}[rwxst] ) and returns the * correct mode described by the string. */ extern int parse_mode(const char *s, mode_t * theMode) { static const mode_t group_set[] = { - S_ISUID | S_IRWXU, /* u */ - S_ISGID | S_IRWXG, /* g */ - S_IRWXO, /* o */ - S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */ + S_ISUID | S_IRWXU, /* u */ + S_ISGID | S_IRWXG, /* g */ + S_IRWXO, /* o */ + S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */ }; static const mode_t mode_set[] = { - S_IRUSR | S_IRGRP | S_IROTH, /* r */ - S_IWUSR | S_IWGRP | S_IWOTH, /* w */ - S_IXUSR | S_IXGRP | S_IXOTH, /* x */ - S_ISUID | S_ISGID, /* s */ - S_ISVTX /* t */ + S_IRUSR | S_IRGRP | S_IROTH, /* r */ + S_IWUSR | S_IWGRP | S_IWOTH, /* w */ + S_IXUSR | S_IXGRP | S_IXOTH, /* x */ + S_ISUID | S_ISGID, /* s */ + S_ISVTX /* t */ }; static const char group_chars[] = "ugoa"; @@ -52,69 +51,69 @@ extern int parse_mode(const char *s, mode_t * theMode) const char *p; mode_t andMode = - S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; + S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; mode_t orMode = 0; mode_t mode; mode_t groups; char type; char c; - if (s==NULL) { + if (s == NULL) { return (FALSE); } do { mode = 0; groups = 0; - NEXT_GROUP: +NEXT_GROUP: if ((c = *s++) == '\0') { return -1; } - for (p=group_chars ; *p ; p++) { + for (p = group_chars; *p; p++) { if (*p == c) { - groups |= group_set[(int)(p-group_chars)]; + groups |= group_set[(int)(p - group_chars)]; goto NEXT_GROUP; } } switch (c) { - case '=': - case '+': - case '-': - type = c; - if (groups == 0) { /* The default is "all" */ - groups |= S_ISUID | S_ISGID | S_ISVTX - | S_IRWXU | S_IRWXG | S_IRWXO; - } - break; - default: - if ((c < '0') || (c > '7') || (mode | groups)) { - return (FALSE); - } else { - *theMode = strtol(--s, NULL, 8); - return (TRUE); - } + case '=': + case '+': + case '-': + type = c; + if (groups == 0) { /* The default is "all" */ + groups |= S_ISUID | S_ISGID | S_ISVTX + | S_IRWXU | S_IRWXG | S_IRWXO; + } + break; + default: + if ((c < '0') || (c > '7') || (mode | groups)) { + return (FALSE); + } else { + *theMode = strtol(--s, NULL, 8); + return (TRUE); + } } - NEXT_MODE: +NEXT_MODE: if (((c = *s++) != '\0') && (c != ',')) { - for (p=mode_chars ; *p ; p++) { + for (p = mode_chars; *p; p++) { if (*p == c) { - mode |= mode_set[(int)(p-mode_chars)]; + mode |= mode_set[(int)(p - mode_chars)]; goto NEXT_MODE; } } - break; /* We're done so break out of loop.*/ + break; /* We're done so break out of loop. */ } switch (type) { - case '=': - andMode &= ~(groups); /* Now fall through. */ - case '+': - orMode |= mode & groups; - break; - case '-': - andMode &= ~(mode & groups); - orMode &= ~(mode & groups); - break; + case '=': + andMode &= ~(groups); /* Now fall through. */ + case '+': + orMode |= mode & groups; + break; + case '-': + andMode &= ~(mode & groups); + orMode &= ~(mode & groups); + break; } } while (c == ','); diff --git a/libbb/safe_strncpy.c b/libbb/safe_strncpy.c index eb2dbab..a81381f 100644 --- a/libbb/safe_strncpy.c +++ b/libbb/safe_strncpy.c @@ -22,16 +22,13 @@ #include #include "libbb.h" - - /* Like strncpy but make sure the resulting string is always 0 terminated. */ -extern char * safe_strncpy(char *dst, const char *src, size_t size) +extern char *safe_strncpy(char *dst, const char *src, size_t size) { - dst[size-1] = '\0'; - return strncpy(dst, src, size-1); + dst[size - 1] = '\0'; + return strncpy(dst, src, size - 1); } - /* END CODE */ /* Local Variables: diff --git a/libbb/time_string.c b/libbb/time_string.c index d103a02..af90a1e 100644 --- a/libbb/time_string.c +++ b/libbb/time_string.c @@ -25,7 +25,6 @@ #include #include "libbb.h" - /* * Return the standard ls-like time string from a time_t * This is static and so is overwritten on each call. @@ -51,7 +50,6 @@ const char *time_string(time_t timeVal) return buf; } - /* END CODE */ /* Local Variables: diff --git a/libbb/unarchive.c b/libbb/unarchive.c index 6ee473b..08f50bf 100644 --- a/libbb/unarchive.c +++ b/libbb/unarchive.c @@ -40,8 +40,7 @@ static char *linkname = NULL; off_t archive_offset; -static ssize_t -seek_forward(struct gzip_handle *zh, ssize_t len) +static ssize_t seek_forward(struct gzip_handle *zh, ssize_t len) { ssize_t slen = gzip_seek(zh, len); @@ -51,7 +50,6 @@ seek_forward(struct gzip_handle *zh, ssize_t len) return slen; } - /* Extract the data postioned at src_stream to either filesystem, stdout or * buffer depending on the value of 'function' which is defined in libbb.h * @@ -64,11 +62,9 @@ seek_forward(struct gzip_handle *zh, ssize_t len) * For this reason if prefix does point to a dir then it must end with a * trailing '/' or else the last dir will be assumed to be the file prefix */ -static char * -extract_archive(struct gzip_handle *src_stream, FILE *out_stream, - const file_header_t *file_entry, const int function, - const char *prefix, - int *err) +static char *extract_archive(struct gzip_handle *src_stream, FILE * out_stream, + const file_header_t * file_entry, + const int function, const char *prefix, int *err) { FILE *dst_stream = NULL; char *full_name = NULL; @@ -93,118 +89,137 @@ extract_archive(struct gzip_handle *src_stream, FILE *out_stream, full_name = xmalloc(strlen(prefix) + strlen(path) + 1); strcpy(full_name, prefix); strcat(full_name, path); - if ( file_entry->link_name ){ - full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1); - strcpy(full_link_name, prefix); - strcat(full_link_name, file_entry->link_name); - } + if (file_entry->link_name) { + full_link_name = + xmalloc(strlen(prefix) + + strlen(file_entry->link_name) + 1); + strcpy(full_link_name, prefix); + strcat(full_link_name, file_entry->link_name); + } } else { full_name = xstrdup(file_entry->name); - if ( file_entry->link_name ) - full_link_name = xstrdup(file_entry->link_name); + if (file_entry->link_name) + full_link_name = xstrdup(file_entry->link_name); } - if (function & extract_to_stream) { if (S_ISREG(file_entry->mode)) { - *err = gzip_copy(src_stream, out_stream, file_entry->size); + *err = + gzip_copy(src_stream, out_stream, file_entry->size); archive_offset += file_entry->size; } - } - else if (function & extract_one_to_buffer) { + } else if (function & extract_one_to_buffer) { if (S_ISREG(file_entry->mode)) { - buffer = (char *) xmalloc(file_entry->size + 1); + buffer = (char *)xmalloc(file_entry->size + 1); gzip_read(src_stream, buffer, file_entry->size); buffer[file_entry->size] = '\0'; archive_offset += file_entry->size; goto cleanup; } - } - else if (function & extract_all_to_fs) { + } else if (function & extract_all_to_fs) { struct stat oldfile; int stat_res; - stat_res = lstat (full_name, &oldfile); - if (stat_res == 0) { /* The file already exists */ - if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) { + stat_res = lstat(full_name, &oldfile); + if (stat_res == 0) { /* The file already exists */ + if ((function & extract_unconditional) + || (oldfile.st_mtime < file_entry->mtime)) { if (!S_ISDIR(oldfile.st_mode)) { - unlink(full_name); /* Directories might not be empty etc */ + unlink(full_name); /* Directories might not be empty etc */ } } else { if ((function & extract_quiet) != extract_quiet) { *err = -1; - error_msg("%s not created: newer or same age file exists", file_entry->name); + error_msg + ("%s not created: newer or same age file exists", + file_entry->name); } seek_forward(src_stream, file_entry->size); goto cleanup; } } - if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ + if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ char *buf, *parent; buf = xstrdup(full_name); parent = dirname(buf); - if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) { + if (make_directory(parent, -1, FILEUTILS_RECUR) != 0) { if ((function & extract_quiet) != extract_quiet) { *err = -1; - error_msg("couldn't create leading directories"); + error_msg + ("couldn't create leading directories"); } } - free (buf); + free(buf); } - switch(file_entry->mode & S_IFMT) { - case S_IFREG: - if (file_entry->link_name) { /* Found a cpio hard link */ - if (link(full_link_name, full_name) != 0) { - if ((function & extract_quiet) != extract_quiet) { - *err = -1; - perror_msg("Cannot link from %s to '%s'", - file_entry->name, file_entry->link_name); - } - } - } else { - if ((dst_stream = wfopen(full_name, "w")) == NULL) { + switch (file_entry->mode & S_IFMT) { + case S_IFREG: + if (file_entry->link_name) { /* Found a cpio hard link */ + if (link(full_link_name, full_name) != 0) { + if ((function & extract_quiet) != + extract_quiet) { *err = -1; - seek_forward(src_stream, file_entry->size); - goto cleanup; - } - archive_offset += file_entry->size; - *err = gzip_copy(src_stream, dst_stream, file_entry->size); - fclose(dst_stream); - } - break; - case S_IFDIR: - if (stat_res != 0) { - if (mkdir(full_name, file_entry->mode) < 0) { - if ((function & extract_quiet) != extract_quiet) { - *err = -1; - perror_msg("Cannot make dir %s", full_name); - } + perror_msg + ("Cannot link from %s to '%s'", + file_entry->name, + file_entry->link_name); } } - break; - case S_IFLNK: - if (symlink(file_entry->link_name, full_name) < 0) { - if ((function & extract_quiet) != extract_quiet) { - *err = -1; - perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name); - } + } else { + if ((dst_stream = + wfopen(full_name, "w")) == NULL) { + *err = -1; + seek_forward(src_stream, + file_entry->size); goto cleanup; } - break; - case S_IFSOCK: - case S_IFBLK: - case S_IFCHR: - case S_IFIFO: - if (mknod(full_name, file_entry->mode, file_entry->device) == -1) { - if ((function & extract_quiet) != extract_quiet) { + archive_offset += file_entry->size; + *err = + gzip_copy(src_stream, dst_stream, + file_entry->size); + fclose(dst_stream); + } + break; + case S_IFDIR: + if (stat_res != 0) { + if (mkdir(full_name, file_entry->mode) < 0) { + if ((function & extract_quiet) != + extract_quiet) { *err = -1; - perror_msg("Cannot create node %s", file_entry->name); + perror_msg("Cannot make dir %s", + full_name); } - goto cleanup; } - break; - default: - *err = -1; - perror_msg("Don't know how to handle %s", full_name); + } + break; + case S_IFLNK: + if (symlink(file_entry->link_name, full_name) < 0) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg + ("Cannot create symlink from %s to '%s'", + file_entry->name, + file_entry->link_name); + } + goto cleanup; + } + break; + case S_IFSOCK: + case S_IFBLK: + case S_IFCHR: + case S_IFIFO: + if (mknod + (full_name, file_entry->mode, + file_entry->device) == -1) { + if ((function & extract_quiet) != extract_quiet) { + *err = -1; + perror_msg("Cannot create node %s", + file_entry->name); + } + goto cleanup; + } + break; + default: + *err = -1; + perror_msg("Don't know how to handle %s", full_name); } @@ -234,31 +249,29 @@ extract_archive(struct gzip_handle *src_stream, FILE *out_stream, /* extract_list and extract_verbose_list can be used in conjunction * with one of the above four extraction functions, so do this seperately */ if (function & extract_verbose_list) { - fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), - file_entry->uid, file_entry->gid, - (int) file_entry->size, time_string(file_entry->mtime)); + fprintf(out_stream, "%s %d/%d %8d %s ", + mode_string(file_entry->mode), file_entry->uid, + file_entry->gid, (int)file_entry->size, + time_string(file_entry->mtime)); } - if ((function & extract_list) || (function & extract_verbose_list)){ + if ((function & extract_list) || (function & extract_verbose_list)) { /* fputs doesnt add a trailing \n, so use fprintf */ fprintf(out_stream, "%s\n", file_entry->name); } cleanup: free(full_name); - if ( full_link_name ) - free(full_link_name); + if (full_link_name) + free(full_link_name); return buffer; } -static char * -unarchive(struct gzip_handle *src_stream, FILE *out_stream, - file_header_t *(*get_headers)(struct gzip_handle *), - void (*free_headers)(file_header_t *), - const int extract_function, - const char *prefix, - const char **extract_names, - int *err) +static char *unarchive(struct gzip_handle *src_stream, FILE * out_stream, + file_header_t * (*get_headers) (struct gzip_handle *), + void (*free_headers) (file_header_t *), + const int extract_function, + const char *prefix, const char **extract_names, int *err) { file_header_t *file_entry; int extract_flag; @@ -278,7 +291,7 @@ unarchive(struct gzip_handle *src_stream, FILE *out_stream, if (p[0] == '.' && p[1] == '/') p += 2; - for(i = 0; extract_names[i] != 0; i++) { + for (i = 0; extract_names[i] != 0; i++) { if (strcmp(extract_names[i], p) == 0) { found_flag = TRUE; break; @@ -298,9 +311,9 @@ unarchive(struct gzip_handle *src_stream, FILE *out_stream, if (extract_flag == TRUE) { buffer = extract_archive(src_stream, out_stream, - file_entry, extract_function, - prefix, err); - *err = 0; /* XXX: ignore extraction errors */ + file_entry, extract_function, + prefix, err); + *err = 0; /* XXX: ignore extraction errors */ if (*err) { free_headers(file_entry); break; @@ -315,26 +328,24 @@ unarchive(struct gzip_handle *src_stream, FILE *out_stream, return buffer; } - -static file_header_t * -get_header_tar(struct gzip_handle *tar_stream) +static file_header_t *get_header_tar(struct gzip_handle *tar_stream) { union { unsigned char raw[512]; struct { - char name[100]; /* 0-99 */ - char mode[8]; /* 100-107 */ - char uid[8]; /* 108-115 */ - char gid[8]; /* 116-123 */ - char size[12]; /* 124-135 */ - char mtime[12]; /* 136-147 */ - char chksum[8]; /* 148-155 */ - char typeflag; /* 156-156 */ + char name[100]; /* 0-99 */ + char mode[8]; /* 100-107 */ + char uid[8]; /* 108-115 */ + char gid[8]; /* 116-123 */ + char size[12]; /* 124-135 */ + char mtime[12]; /* 136-147 */ + char chksum[8]; /* 148-155 */ + char typeflag; /* 156-156 */ char linkname[100]; /* 157-256 */ - char magic[6]; /* 257-262 */ + char magic[6]; /* 257-262 */ char version[2]; /* 263-264 */ - char uname[32]; /* 265-296 */ - char gname[32]; /* 297-328 */ + char uname[32]; /* 265-296 */ + char gname[32]; /* 297-328 */ char devmajor[8]; /* 329-336 */ char devminor[8]; /* 337-344 */ char prefix[155]; /* 345-499 */ @@ -352,8 +363,8 @@ get_header_tar(struct gzip_handle *tar_stream) if (gzip_read(tar_stream, tar.raw, 512) != 512) { /* Unfortunately its common for tar files to have all sorts of * trailing garbage, fail silently */ -// error_msg("Couldnt read header"); - return(NULL); +// error_msg("Couldnt read header"); + return (NULL); } archive_offset += 512; @@ -365,57 +376,55 @@ get_header_tar(struct gzip_handle *tar_stream) #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) #endif - return(NULL); + return (NULL); } /* Do checksum on headers */ - for (i = 0; i < 148 ; i++) { + for (i = 0; i < 148; i++) { sum += tar.raw[i]; } sum += ' ' * 8; - for (i = 156; i < 512 ; i++) { + for (i = 156; i < 512; i++) { sum += tar.raw[i]; } if (sum != strtol(tar.formated.chksum, NULL, 8)) { - if ( strtol(tar.formated.chksum,NULL,8) != 0 ) + if (strtol(tar.formated.chksum, NULL, 8) != 0) error_msg("Invalid tar header checksum"); - return(NULL); - } + return (NULL); + } /* convert to type'ed variables */ tar_entry = xcalloc(1, sizeof(file_header_t)); - - // tar_entry->name = xstrdup(tar.formated.name); /* parse_mode(tar.formated.mode, &tar_entry->mode); */ - tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8); + tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8); - tar_entry->uid = strtol(tar.formated.uid, NULL, 8); - tar_entry->gid = strtol(tar.formated.gid, NULL, 8); - tar_entry->size = strtol(tar.formated.size, NULL, 8); + tar_entry->uid = strtol(tar.formated.uid, NULL, 8); + tar_entry->gid = strtol(tar.formated.gid, NULL, 8); + tar_entry->size = strtol(tar.formated.size, NULL, 8); tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) + - strtol(tar.formated.devminor, NULL, 8); + strtol(tar.formated.devminor, NULL, 8); /* Fix mode, used by the old format */ switch (tar.formated.typeflag) { - /* hard links are detected as regular files with 0 size and a link name */ + /* hard links are detected as regular files with 0 size and a link name */ case '1': - tar_entry->mode |= S_IFREG ; + tar_entry->mode |= S_IFREG; break; - case 0: - case '0': + case 0: + case '0': -# ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY +#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY if (last_char_is(tar_entry->name, '/')) { tar_entry->mode |= S_IFDIR; } else -# endif +#endif tar_entry->mode |= S_IFREG; break; case '2': @@ -433,71 +442,71 @@ get_header_tar(struct gzip_handle *tar_stream) case '6': tar_entry->mode |= S_IFIFO; break; -# ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS - case 'L': { +#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS + case 'L':{ longname = xmalloc(tar_entry->size + 1); - if(gzip_read(tar_stream, longname, tar_entry->size) != tar_entry->size) - return NULL; + if (gzip_read(tar_stream, longname, tar_entry->size) != + tar_entry->size) + return NULL; longname[tar_entry->size] = '\0'; archive_offset += tar_entry->size; - return(get_header_tar(tar_stream)); + return (get_header_tar(tar_stream)); } - case 'K': { + case 'K':{ linkname = xmalloc(tar_entry->size + 1); - if(gzip_read(tar_stream, linkname, tar_entry->size) != tar_entry->size) - return NULL; + if (gzip_read(tar_stream, linkname, tar_entry->size) != + tar_entry->size) + return NULL; linkname[tar_entry->size] = '\0'; archive_offset += tar_entry->size; - return(get_header_tar(tar_stream)); + return (get_header_tar(tar_stream)); } case 'D': case 'M': case 'N': case 'S': case 'V': - perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag); -# endif - default: - perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag); - break; + perror_msg("Ignoring GNU extension type %c", + tar.formated.typeflag); +#endif + default: + perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag); + break; } - #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS - if (longname) { - tar_entry->name = longname; - longname = NULL; - } else + if (longname) { + tar_entry->name = longname; + longname = NULL; + } else #endif - { - tar_entry->name = xstrndup(tar.formated.name, 100); - - if (tar.formated.prefix[0]) { - char *temp = tar_entry->name; - char *prefixTemp = xstrndup(tar.formated.prefix, 155); - tar_entry->name = concat_path_file(prefixTemp, temp); - free(temp); - free(prefixTemp); - } - } + { + tar_entry->name = xstrndup(tar.formated.name, 100); + + if (tar.formated.prefix[0]) { + char *temp = tar_entry->name; + char *prefixTemp = xstrndup(tar.formated.prefix, 155); + tar_entry->name = concat_path_file(prefixTemp, temp); + free(temp); + free(prefixTemp); + } + } if (linkname) { tar_entry->link_name = linkname; linkname = NULL; - } else - { + } else { tar_entry->link_name = *tar.formated.linkname != '\0' ? - xstrndup(tar.formated.linkname, 100) : NULL; + xstrndup(tar.formated.linkname, 100) : NULL; } - return(tar_entry); + return (tar_entry); } -static void -free_header_tar(file_header_t *tar_entry) +static void free_header_tar(file_header_t * tar_entry) { if (tar_entry == NULL) return; @@ -509,10 +518,9 @@ free_header_tar(file_header_t *tar_entry) free(tar_entry); } -char * -deb_extract(const char *package_filename, FILE *out_stream, - const int extract_function, const char *prefix, - const char *filename, int *err) +char *deb_extract(const char *package_filename, FILE * out_stream, + const int extract_function, const char *prefix, + const char *filename, int *err) { FILE *deb_stream = NULL; file_header_t *ar_header = NULL; @@ -535,15 +543,14 @@ deb_extract(const char *package_filename, FILE *out_stream, if (extract_function & extract_control_tar_gz) { ared_file = "control.tar.gz"; - } - else if (extract_function & extract_data_tar_gz) { + } else if (extract_function & extract_data_tar_gz) { ared_file = "data.tar.gz"; } else { - opkg_msg(ERROR, "Internal error: extract_function=%x\n", - extract_function); + opkg_msg(ERROR, "Internal error: extract_function=%x\n", + extract_function); *err = -1; goto cleanup; - } + } /* open the debian package to be worked on */ deb_stream = wfopen(package_filename, "r"); @@ -560,11 +567,11 @@ deb_extract(const char *package_filename, FILE *out_stream, /* walk through outer tar file to find ared_file */ while ((tar_header = get_header_tar(&tar_outer)) != NULL) { - int name_offset = 0; - if (strncmp(tar_header->name, "./", 2) == 0) - name_offset = 2; + int name_offset = 0; + if (strncmp(tar_header->name, "./", 2) == 0) + name_offset = 2; - if (strcmp(ared_file, tar_header->name+name_offset) == 0) { + if (strcmp(ared_file, tar_header->name + name_offset) == 0) { memset(&tar_inner, 0, sizeof(tar_inner)); tar_inner.gzip = &tar_outer; gzip_exec(&tar_inner, NULL); @@ -576,9 +583,7 @@ deb_extract(const char *package_filename, FILE *out_stream, get_header_tar, free_header_tar, extract_function, - prefix, - file_list, - err); + prefix, file_list, err); free_header_tar(tar_header); gzip_close(&tar_inner); diff --git a/libbb/unzip.c b/libbb/unzip.c index 435effb..f09f032 100644 --- a/libbb/unzip.c +++ b/libbb/unzip.c @@ -54,7 +54,7 @@ static FILE *in_file, *out_file; static unsigned char *window; static unsigned long *crc_table = NULL; -static unsigned long crc; /* shift register contents */ +static unsigned long crc; /* shift register contents */ /* * window size--must be a power of two, and @@ -63,21 +63,21 @@ static unsigned long crc; /* shift register contents */ static const int WSIZE = 0x8000; /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ -static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */ -static const int N_MAX = 288; /* maximum number of codes in any set */ +static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */ +static const int N_MAX = 288; /* maximum number of codes in any set */ static long bytes_out; /* number of output bytes */ static unsigned long outcnt; /* bytes in output buffer */ static unsigned hufts; /* track memory usage */ -static unsigned long bb; /* bit buffer */ +static unsigned long bb; /* bit buffer */ static unsigned bk; /* bits in bit buffer */ typedef struct huft_s { - unsigned char e; /* number of extra bits or operation */ - unsigned char b; /* number of bits in this code or subcode */ + unsigned char e; /* number of extra bits or operation */ + unsigned char b; /* number of bits in this code or subcode */ union { - unsigned short n; /* literal, length base, or distance base */ + unsigned short n; /* literal, length base, or distance base */ struct huft_s *t; /* pointer to next level of table */ } v; } huft_t; @@ -101,32 +101,34 @@ static void abort_gzip() static void make_crc_table() { - unsigned long table_entry; /* crc shift register */ - unsigned long poly = 0; /* polynomial exclusive-or pattern */ - int i; /* counter for all possible eight bit values */ - int k; /* byte being shifted into crc apparatus */ + unsigned long table_entry; /* crc shift register */ + unsigned long poly = 0; /* polynomial exclusive-or pattern */ + int i; /* counter for all possible eight bit values */ + int k; /* byte being shifted into crc apparatus */ /* terms of polynomial defining this crc (except x^32): */ - static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + static int p[] = { 0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26 }; /* initial shift register value */ crc = 0xffffffffL; - crc_table = (unsigned long *) xmalloc(256 * sizeof(unsigned long)); + crc_table = (unsigned long *)xmalloc(256 * sizeof(unsigned long)); /* Make exclusive-or pattern from polynomial (0xedb88320) */ - for (i = 0; i < sizeof(p)/sizeof(int); i++) + for (i = 0; i < sizeof(p) / sizeof(int); i++) poly |= 1L << (31 - p[i]); /* Compute and print table of CRC's, five per line */ for (i = 0; i < 256; i++) { table_entry = i; - /* The idea to initialize the register with the byte instead of - * zero was stolen from Haruhiko Okumura's ar002 - */ + /* The idea to initialize the register with the byte instead of + * zero was stolen from Haruhiko Okumura's ar002 + */ for (k = 8; k; k--) { - table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1; + table_entry = + table_entry & 1 ? (table_entry >> 1) ^ poly : + table_entry >> 1; } - crc_table[i]=table_entry; + crc_table[i] = table_entry; } } @@ -142,7 +144,7 @@ static void flush_window(void) return; for (n = 0; n < outcnt; n++) { - crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8); + crc = crc_table[((int)crc ^ (window[n])) & 0xff] ^ (crc >> 8); } if (fwrite(window, 1, outcnt, out_file) != outcnt) { @@ -157,7 +159,7 @@ static void flush_window(void) error_msg("Couldnt write"); _exit(EXIT_FAILURE); } - bytes_out += (unsigned long) outcnt; + bytes_out += (unsigned long)outcnt; outcnt = 0; } @@ -167,7 +169,7 @@ static void flush_window(void) * each table. * t: table to free */ -static int huft_free(huft_t *t) +static int huft_free(huft_t * t) { huft_t *p, *q; @@ -175,7 +177,7 @@ static int huft_free(huft_t *t) p = t; while (p != (huft_t *) NULL) { q = (--p)->v.t; - free((char *) p); + free((char *)p); p = q; } return 0; @@ -195,31 +197,32 @@ static int huft_free(huft_t *t) * t: result: starting table * m: maximum lookup bits, returns actual */ -static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s, - const unsigned short *d, const unsigned short *e, huft_t **t, int *m) +static int huft_build(unsigned int *b, const unsigned int n, + const unsigned int s, const unsigned short *d, + const unsigned short *e, huft_t ** t, int *m) { unsigned a; /* counter for codes of length k */ unsigned c[BMAX + 1]; /* bit length count table */ unsigned f; /* i repeats in table every f entries */ int g; /* maximum code length */ int h; /* table level */ - unsigned i; /* counter, current code */ - unsigned j; /* counter */ - int k; /* number of bits in current code */ + unsigned i; /* counter, current code */ + unsigned j; /* counter */ + int k; /* number of bits in current code */ int l; /* bits per table (returned in m) */ unsigned *p; /* pointer into c[], b[], or v[] */ - huft_t *q; /* points to current table */ + huft_t *q; /* points to current table */ huft_t r; /* table entry for structure assignment */ huft_t *u[BMAX]; /* table stack */ unsigned v[N_MAX]; /* values in order of bit length */ - int w; /* bits before this table == (l * h) */ + int w; /* bits before this table == (l * h) */ unsigned x[BMAX + 1]; /* bit offsets, then code stack */ unsigned *xp; /* pointer into x */ int y; /* number of dummy codes added */ unsigned z; /* number of entries in current table */ /* Generate counts for each bit length */ - memset ((void *)(c), 0, sizeof(c)); + memset((void *)(c), 0, sizeof(c)); p = b; i = n; do { @@ -237,14 +240,14 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int for (j = 1; j <= BMAX; j++) if (c[j]) break; - k = j; /* minimum code length */ - if ((unsigned) l < j) + k = j; /* minimum code length */ + if ((unsigned)l < j) l = j; for (i = BMAX; i; i--) if (c[i]) break; - g = i; /* maximum code length */ - if ((unsigned) l > i) + g = i; /* maximum code length */ + if ((unsigned)l > i) l = i; *m = l; @@ -260,7 +263,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int x[1] = j = 0; p = c + 1; xp = x + 2; - while (--i) { /* note that i == g from above */ + while (--i) { /* note that i == g from above */ *xp++ = (j += *p++); } @@ -273,13 +276,13 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int } while (++i < n); /* Generate the Huffman codes and for each, make the table entries */ - x[0] = i = 0; /* first Huffman code is zero */ - p = v; /* grab values in bit order */ - h = -1; /* no tables yet--level -1 */ - w = -l; /* bits decoded == (l * h) */ + x[0] = i = 0; /* first Huffman code is zero */ + p = v; /* grab values in bit order */ + h = -1; /* no tables yet--level -1 */ + w = -l; /* bits decoded == (l * h) */ u[0] = (huft_t *) NULL; /* just to keep compilers happy */ q = (huft_t *) NULL; /* ditto */ - z = 0; /* ditto */ + z = 0; /* ditto */ /* go through the bit lengths (k already is bits in shortest code) */ for (; k <= g; k++) { @@ -289,10 +292,10 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int /* make tables up to required level */ while (k > w + l) { h++; - w += l; /* previous table always l bits */ + w += l; /* previous table always l bits */ /* compute minimum size table less than or equal to l bits */ - z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ + z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */ if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */ f -= a + 1; /* deduct codes from patterns left */ xp = c + k; @@ -302,25 +305,28 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int f -= *xp; /* else deduct codes from patterns */ } } - z = 1 << j; /* table entries for j-bit table */ + z = 1 << j; /* table entries for j-bit table */ /* allocate and link in new table */ - if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) { + if ((q = + (huft_t *) xmalloc((z + 1) * + sizeof(huft_t))) == + NULL) { if (h) { huft_free(u[0]); } return 3; /* not enough memory */ } hufts += z + 1; /* track memory usage */ - *t = q + 1; /* link to list for huft_free() */ + *t = q + 1; /* link to list for huft_free() */ *(t = &(q->v.t)) = NULL; - u[h] = ++q; /* table starts after link */ + u[h] = ++q; /* table starts after link */ /* connect to last table, if there is one */ if (h) { x[h] = i; /* save pattern for backing up */ - r.b = (unsigned char) l; /* bits to dump before this table */ - r.e = (unsigned char) (16 + j); /* bits in this table */ + r.b = (unsigned char)l; /* bits to dump before this table */ + r.e = (unsigned char)(16 + j); /* bits in this table */ r.v.t = q; /* pointer to this table */ j = i >> (w - l); /* (get around Turbo C bug) */ u[h - 1][j] = r; /* connect to last table */ @@ -328,15 +334,15 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int } /* set up table entry in r */ - r.b = (unsigned char) (k - w); + r.b = (unsigned char)(k - w); if (p >= v + n) - r.e = 99; /* out of values--invalid code */ + r.e = 99; /* out of values--invalid code */ else if (*p < s) { - r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ - r.v.n = (unsigned short) (*p); /* simple code is just the value */ - p++; /* one compiler does not like *p++ */ + r.e = (unsigned char)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ + r.v.n = (unsigned short)(*p); /* simple code is just the value */ + p++; /* one compiler does not like *p++ */ } else { - r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ + r.e = (unsigned char)e[*p - s]; /* non-simple--look up in lists */ r.v.n = d[*p++ - s]; } @@ -352,7 +358,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int /* backup over finished tables */ while ((i & ((1 << w) - 1)) != x[h]) { - h--; /* don't need to update q */ + h--; /* don't need to update q */ w -= l; } } @@ -368,52 +374,55 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int * tl, td: literal/length and distance decoder tables * bl, bd: number of bits decoded by tl[] and td[] */ -static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) +static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd) { - unsigned long e; /* table entry flag/number of extra bits */ - unsigned long n, d; /* length and index for copy */ - unsigned long w; /* current window position */ - huft_t *t; /* pointer to table entry */ - unsigned ml, md; /* masks for bl and bd bits */ - unsigned long b; /* bit buffer */ + unsigned long e; /* table entry flag/number of extra bits */ + unsigned long n, d; /* length and index for copy */ + unsigned long w; /* current window position */ + huft_t *t; /* pointer to table entry */ + unsigned ml, md; /* masks for bl and bd bits */ + unsigned long b; /* bit buffer */ unsigned k; /* number of bits in bit buffer */ /* make local copies of globals */ - b = bb; /* initialize bit buffer */ + b = bb; /* initialize bit buffer */ k = bk; - w = outcnt; /* initialize window position */ + w = outcnt; /* initialize window position */ /* inflate the coded data */ - ml = mask_bits[bl]; /* precompute masks for speed */ + ml = mask_bits[bl]; /* precompute masks for speed */ md = mask_bits[bd]; - for (;;) { /* do until end of block */ - while (k < (unsigned) bl) { + for (;;) { /* do until end of block */ + while (k < (unsigned)bl) { b |= ((unsigned long)fgetc(in_file)) << k; k += 8; } - if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) - do { - if (e == 99) { - return 1; - } - b >>= t->b; - k -= t->b; - e -= 16; - while (k < e) { - b |= ((unsigned long)fgetc(in_file)) << k; - k += 8; - } - } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); + if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) + do { + if (e == 99) { + return 1; + } + b >>= t->b; + k -= t->b; + e -= 16; + while (k < e) { + b |= ((unsigned long)fgetc(in_file)) << + k; + k += 8; + } + } while ((e = + (t = + t->v.t + ((unsigned)b & mask_bits[e]))->e) > + 16); b >>= t->b; k -= t->b; - if (e == 16) { /* then it's a literal */ - window[w++] = (unsigned char) t->v.n; + if (e == 16) { /* then it's a literal */ + window[w++] = (unsigned char)t->v.n; if (w == WSIZE) { - outcnt=(w), - flush_window(); + outcnt = (w), flush_window(); w = 0; } - } else { /* it's an EOB or a length */ + } else { /* it's an EOB or a length */ /* exit if end of block */ if (e == 15) { @@ -425,17 +434,17 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) b |= ((unsigned long)fgetc(in_file)) << k; k += 8; } - n = t->v.n + ((unsigned) b & mask_bits[e]); + n = t->v.n + ((unsigned)b & mask_bits[e]); b >>= e; k -= e; /* decode distance of block to copy */ - while (k < (unsigned) bd) { + while (k < (unsigned)bd) { b |= ((unsigned long)fgetc(in_file)) << k; k += 8; } - if ((e = (t = td + ((unsigned) b & md))->e) > 16) + if ((e = (t = td + ((unsigned)b & md))->e) > 16) do { if (e == 99) return 1; @@ -443,36 +452,43 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) k -= t->b; e -= 16; while (k < e) { - b |= ((unsigned long)fgetc(in_file)) << k; + b |= ((unsigned long) + fgetc(in_file)) << k; k += 8; } - } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); + } while ((e = + (t = + t->v.t + + ((unsigned)b & mask_bits[e]))->e) > + 16); b >>= t->b; k -= t->b; while (k < e) { b |= ((unsigned long)fgetc(in_file)) << k; k += 8; } - d = w - t->v.n - ((unsigned) b & mask_bits[e]); + d = w - t->v.n - ((unsigned)b & mask_bits[e]); b >>= e; k -= e; /* do the copy */ do { - n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); + n -= (e = + (e = + WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > + n ? n : e); #if !defined(NOMEMCPY) && !defined(DEBUG) if (w - d >= e) { /* (this test assumes unsigned comparison) */ memcpy(window + w, window + d, e); w += e; d += e; - } else /* do it slow to avoid memcpy() overlap */ -#endif /* !NOMEMCPY */ + } else /* do it slow to avoid memcpy() overlap */ +#endif /* !NOMEMCPY */ do { window[w++] = window[d++]; } while (--e); if (w == WSIZE) { - outcnt=(w), - flush_window(); + outcnt = (w), flush_window(); w = 0; } } while (n); @@ -480,8 +496,8 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) } /* restore the globals from the locals */ - outcnt = w; /* restore global window pointer */ - bb = b; /* restore global bit buffer */ + outcnt = w; /* restore global window pointer */ + bb = b; /* restore global bit buffer */ bk = k; /* done */ @@ -496,24 +512,24 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) */ static int inflate_block(int *e) { - unsigned t; /* block type */ - unsigned long b; /* bit buffer */ + unsigned t; /* block type */ + unsigned long b; /* bit buffer */ unsigned k; /* number of bits in bit buffer */ - static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ + static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; /* note: see note #13 above about the 258 in this list. */ - static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */ + static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 - }; /* 99==invalid */ - static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ + }; /* 99==invalid */ + static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 }; - static unsigned short cpdext[] = { /* Extra bits for distance codes */ + static unsigned short cpdext[] = { /* Extra bits for distance codes */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 @@ -528,7 +544,7 @@ static int inflate_block(int *e) b |= ((unsigned long)fgetc(in_file)) << k; k += 8; } - *e = (int) b & 1; + *e = (int)b & 1; b >>= 1; k -= 1; @@ -537,7 +553,7 @@ static int inflate_block(int *e) b |= ((unsigned long)fgetc(in_file)) << k; k += 8; } - t = (unsigned) b & 3; + t = (unsigned)b & 3; b >>= 2; k -= 2; @@ -547,17 +563,17 @@ static int inflate_block(int *e) /* inflate that block type */ switch (t) { - case 0: /* Inflate stored */ + case 0: /* Inflate stored */ { - unsigned long n; /* number of bytes in block */ - unsigned long w; /* current window position */ - unsigned long b_stored; /* bit buffer */ - unsigned long k_stored; /* number of bits in bit buffer */ + unsigned long n; /* number of bytes in block */ + unsigned long w; /* current window position */ + unsigned long b_stored; /* bit buffer */ + unsigned long k_stored; /* number of bits in bit buffer */ /* make local copies of globals */ - b_stored = bb; /* initialize bit buffer */ + b_stored = bb; /* initialize bit buffer */ k_stored = bk; - w = outcnt; /* initialize window position */ + w = outcnt; /* initialize window position */ /* go to byte boundary */ n = k_stored & 7; @@ -566,18 +582,20 @@ static int inflate_block(int *e) /* get the length and its complement */ while (k_stored < 16) { - b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + b_stored |= + ((unsigned long)fgetc(in_file)) << k_stored; k_stored += 8; } - n = ((unsigned) b_stored & 0xffff); + n = ((unsigned)b_stored & 0xffff); b_stored >>= 16; k_stored -= 16; while (k_stored < 16) { - b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + b_stored |= + ((unsigned long)fgetc(in_file)) << k_stored; k_stored += 8; } - if (n != (unsigned) ((~b_stored) & 0xffff)) { - return 1; /* error in compressed data */ + if (n != (unsigned)((~b_stored) & 0xffff)) { + return 1; /* error in compressed data */ } b_stored >>= 16; k_stored -= 16; @@ -585,13 +603,14 @@ static int inflate_block(int *e) /* read and output the compressed data */ while (n--) { while (k_stored < 8) { - b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; + b_stored |= + ((unsigned long)fgetc(in_file)) << + k_stored; k_stored += 8; } - window[w++] = (unsigned char) b_stored; + window[w++] = (unsigned char)b_stored; if (w == (unsigned long)WSIZE) { - outcnt=(w), - flush_window(); + outcnt = (w), flush_window(); w = 0; } b_stored >>= 8; @@ -599,22 +618,22 @@ static int inflate_block(int *e) } /* restore the globals from the locals */ - outcnt = w; /* restore global window pointer */ - bb = b_stored; /* restore global bit buffer */ + outcnt = w; /* restore global window pointer */ + bb = b_stored; /* restore global bit buffer */ bk = k_stored; return 0; } - case 1: /* Inflate fixed - * decompress an inflated type 1 (fixed Huffman codes) block. We should - * either replace this with a custom decoder, or at least precompute the - * Huffman tables. - */ + case 1: /* Inflate fixed + * decompress an inflated type 1 (fixed Huffman codes) block. We should + * either replace this with a custom decoder, or at least precompute the + * Huffman tables. + */ { - int i; /* temporary variable */ - huft_t *tl; /* literal/length code table */ - huft_t *td; /* distance code table */ - int bl; /* lookup bits for tl */ - int bd; /* lookup bits for td */ + int i; /* temporary variable */ + huft_t *tl; /* literal/length code table */ + huft_t *td; /* distance code table */ + int bl; /* lookup bits for tl */ + int bd; /* lookup bits for td */ unsigned int l[288]; /* length list for huft_build */ /* set up literal table */ @@ -631,7 +650,9 @@ static int inflate_block(int *e) l[i] = 8; } bl = 7; - if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) { + if ((i = + huft_build(l, 288, 257, cplens, cplext, &tl, + &bl)) != 0) { return i; } @@ -640,7 +661,9 @@ static int inflate_block(int *e) l[i] = 5; } bd = 5; - if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { + if ((i = + huft_build(l, 30, 0, cpdist, cpdext, &td, + &bd)) > 1) { huft_free(tl); return i; } @@ -657,31 +680,32 @@ static int inflate_block(int *e) huft_free(td); return 0; } - case 2: /* Inflate dynamic */ + case 2: /* Inflate dynamic */ { /* Tables for deflate from PKZIP's appnote.txt. */ static unsigned border[] = { /* Order of the bit length code lengths */ - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, + 13, 2, 14, 1, 15 }; - int dbits = 6; /* bits in base distance lookup table */ - int lbits = 9; /* bits in base literal/length lookup table */ + int dbits = 6; /* bits in base distance lookup table */ + int lbits = 9; /* bits in base literal/length lookup table */ - int i; /* temporary variables */ + int i; /* temporary variables */ unsigned j; - unsigned l; /* last length */ - unsigned m; /* mask for bit lengths table */ - unsigned n; /* number of lengths to get */ - huft_t *tl; /* literal/length code table */ - huft_t *td; /* distance code table */ - int bl; /* lookup bits for tl */ - int bd; /* lookup bits for td */ - unsigned nb; /* number of bit length codes */ - unsigned nl; /* number of literal/length codes */ - unsigned nd; /* number of distance codes */ - - unsigned ll[286 + 30]; /* literal/length and distance code lengths */ + unsigned l; /* last length */ + unsigned m; /* mask for bit lengths table */ + unsigned n; /* number of lengths to get */ + huft_t *tl; /* literal/length code table */ + huft_t *td; /* distance code table */ + int bl; /* lookup bits for tl */ + int bd; /* lookup bits for td */ + unsigned nb; /* number of bit length codes */ + unsigned nl; /* number of literal/length codes */ + unsigned nd; /* number of distance codes */ + + unsigned ll[286 + 30]; /* literal/length and distance code lengths */ unsigned long b_dynamic; /* bit buffer */ - unsigned k_dynamic; /* number of bits in bit buffer */ + unsigned k_dynamic; /* number of bits in bit buffer */ /* make local bit buffer */ b_dynamic = bb; @@ -689,24 +713,30 @@ static int inflate_block(int *e) /* read in table lengths */ while (k_dynamic < 5) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long)fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ + nl = 257 + ((unsigned)b_dynamic & 0x1f); /* number of literal/length codes */ b_dynamic >>= 5; k_dynamic -= 5; while (k_dynamic < 5) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long)fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ + nd = 1 + ((unsigned)b_dynamic & 0x1f); /* number of distance codes */ b_dynamic >>= 5; k_dynamic -= 5; while (k_dynamic < 4) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long)fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ + nb = 4 + ((unsigned)b_dynamic & 0xf); /* number of bit length codes */ b_dynamic >>= 4; k_dynamic -= 4; if (nl > 286 || nd > 30) { @@ -716,10 +746,12 @@ static int inflate_block(int *e) /* read in bit-length-code lengths */ for (j = 0; j < nb; j++) { while (k_dynamic < 3) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long)fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - ll[border[j]] = (unsigned) b_dynamic & 7; + ll[border[j]] = (unsigned)b_dynamic & 7; b_dynamic >>= 3; k_dynamic -= 3; } @@ -729,38 +761,44 @@ static int inflate_block(int *e) /* build decoding table for trees--single level, 7 bit lookup */ bl = 7; - if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) { + if ((i = + huft_build(ll, 19, 19, NULL, NULL, &tl, + &bl)) != 0) { if (i == 1) { huft_free(tl); } - return i; /* incomplete code set */ + return i; /* incomplete code set */ } /* read in literal and distance code lengths */ n = nl + nd; m = mask_bits[bl]; i = l = 0; - while ((unsigned) i < n) { - while (k_dynamic < (unsigned) bl) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + while ((unsigned)i < n) { + while (k_dynamic < (unsigned)bl) { + b_dynamic |= + ((unsigned long)fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - j = (td = tl + ((unsigned) b_dynamic & m))->b; + j = (td = tl + ((unsigned)b_dynamic & m))->b; b_dynamic >>= j; k_dynamic -= j; j = td->v.n; - if (j < 16) { /* length of code in bits (0..15) */ + if (j < 16) { /* length of code in bits (0..15) */ ll[i++] = l = j; /* save last length in l */ - } - else if (j == 16) { /* repeat last length 3 to 6 times */ + } else if (j == 16) { /* repeat last length 3 to 6 times */ while (k_dynamic < 2) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long) + fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - j = 3 + ((unsigned) b_dynamic & 3); + j = 3 + ((unsigned)b_dynamic & 3); b_dynamic >>= 2; k_dynamic -= 2; - if ((unsigned) i + j > n) { + if ((unsigned)i + j > n) { return 1; } while (j--) { @@ -768,28 +806,34 @@ static int inflate_block(int *e) } } else if (j == 17) { /* 3 to 10 zero length codes */ while (k_dynamic < 3) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long) + fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - j = 3 + ((unsigned) b_dynamic & 7); + j = 3 + ((unsigned)b_dynamic & 7); b_dynamic >>= 3; k_dynamic -= 3; - if ((unsigned) i + j > n) { + if ((unsigned)i + j > n) { return 1; } while (j--) { ll[i++] = 0; } l = 0; - } else { /* j == 18: 11 to 138 zero length codes */ + } else { /* j == 18: 11 to 138 zero length codes */ while (k_dynamic < 7) { - b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; + b_dynamic |= + ((unsigned long) + fgetc(in_file)) << + k_dynamic; k_dynamic += 8; } - j = 11 + ((unsigned) b_dynamic & 0x7f); + j = 11 + ((unsigned)b_dynamic & 0x7f); b_dynamic >>= 7; k_dynamic -= 7; - if ((unsigned) i + j > n) { + if ((unsigned)i + j > n) { return 1; } while (j--) { @@ -808,21 +852,25 @@ static int inflate_block(int *e) /* build the decoding tables for literal/length and distance codes */ bl = lbits; - if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { + if ((i = + huft_build(ll, nl, 257, cplens, cplext, &tl, + &bl)) != 0) { if (i == 1) { error_msg("Incomplete literal tree"); huft_free(tl); } - return i; /* incomplete code set */ + return i; /* incomplete code set */ } bd = dbits; - if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { + if ((i = + huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, + &bd)) != 0) { if (i == 1) { error_msg("incomplete distance tree"); huft_free(td); } huft_free(tl); - return i; /* incomplete code set */ + return i; /* incomplete code set */ } /* decompress until an end-of-block code */ @@ -850,8 +898,8 @@ static int inflate_block(int *e) */ static int inflate() { - int e; /* last block flag */ - int r; /* result code */ + int e; /* last block flag */ + int r; /* result code */ unsigned h = 0; /* maximum struct huft's malloc'ed */ /* initialize window, bit buffer */ @@ -892,40 +940,41 @@ static int inflate() * The magic header has already been checked. The output buffer is cleared. * in, out: input and output file descriptors */ -extern int unzip(FILE *l_in_file, FILE *l_out_file) +extern int unzip(FILE * l_in_file, FILE * l_out_file) { const int extra_field = 0x04; /* bit 2 set: extra field present */ const int orig_name = 0x08; /* bit 3 set: original file name present */ const int comment = 0x10; /* bit 4 set: file comment present */ unsigned char buf[8]; /* extended local header */ unsigned char flags; /* compression flags */ - char magic[2]; /* magic header */ + char magic[2]; /* magic header */ int method; typedef void (*sig_type) (int); - int exit_code=0; /* program exit code */ + int exit_code = 0; /* program exit code */ int i; in_file = l_in_file; out_file = l_out_file; if (signal(SIGINT, SIG_IGN) != SIG_IGN) { - (void) signal(SIGINT, (sig_type) abort_gzip); + (void)signal(SIGINT, (sig_type) abort_gzip); } #ifdef SIGTERM -// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { -// (void) signal(SIGTERM, (sig_type) abort_gzip); -// } +// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { +// (void) signal(SIGTERM, (sig_type) abort_gzip); +// } #endif #ifdef SIGHUP if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { - (void) signal(SIGHUP, (sig_type) abort_gzip); + (void)signal(SIGHUP, (sig_type) abort_gzip); } #endif signal(SIGPIPE, SIG_IGN); /* Allocate all global buffers (for DYN_ALLOC option) */ - window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char))); + window = + xmalloc((size_t) (((2L * WSIZE) + 1L) * sizeof(unsigned char))); outcnt = 0; bytes_out = 0L; @@ -938,14 +987,15 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file) return EXIT_FAILURE; } - method = (int) fgetc(in_file); + method = (int)fgetc(in_file); if (method != 8) { - error_msg("unknown method %d -- get newer version of gzip", method); + error_msg("unknown method %d -- get newer version of gzip", + method); exit_code = 1; return -1; } - flags = (unsigned char) fgetc(in_file); + flags = (unsigned char)fgetc(in_file); /* Ignore time stamp(4), extra flags(1), OS type(1) */ for (i = 0; i < 6; i++) @@ -962,16 +1012,16 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file) /* Discard original name if any */ if ((flags & orig_name) != 0) { - while (fgetc(in_file) != 0); /* null */ + while (fgetc(in_file) != 0) ; /* null */ } /* Discard file comment if any */ if ((flags & comment) != 0) { - while (fgetc(in_file) != 0); /* null */ + while (fgetc(in_file) != 0) ; /* null */ } if (method < 0) { - return(exit_code); + return (exit_code); } make_crc_table(); @@ -1001,12 +1051,17 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file) fread(buf, 1, 8, in_file); /* Validate decompression - crc */ - if (!exit_code && (unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) { + if (!exit_code + && (unsigned int)((buf[0] | (buf[1] << 8)) | + ((buf[2] | (buf[3] << 8)) << 16)) != + (crc ^ 0xffffffffL)) { error_msg("invalid compressed data--crc error"); exit_code = 1; } /* Validate decompression - size */ - if (!exit_code && ((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) { + if (!exit_code + && ((buf[4] | (buf[5] << 8)) | ((buf[6] | (buf[7] << 8)) << 16)) != + (unsigned long)bytes_out) { error_msg("invalid compressed data--length error"); exit_code = 1; } diff --git a/libbb/wfopen.c b/libbb/wfopen.c index f58ec90..76f220b 100644 --- a/libbb/wfopen.c +++ b/libbb/wfopen.c @@ -33,7 +33,6 @@ FILE *wfopen(const char *path, const char *mode) return fp; } - /* END CODE */ /* Local Variables: diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index f577315..2469b6a 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -25,7 +25,6 @@ #include #include "libbb.h" - extern void *xmalloc(size_t size) { void *ptr = malloc(size); @@ -50,13 +49,14 @@ extern void *xcalloc(size_t nmemb, size_t size) return ptr; } -extern char * xstrdup (const char *s) { +extern char *xstrdup(const char *s) +{ char *t; if (s == NULL) return NULL; - t = strdup (s); + t = strdup(s); if (t == NULL) perror_msg_and_die("strdup"); @@ -64,7 +64,8 @@ extern char * xstrdup (const char *s) { return t; } -extern char * xstrndup (const char *s, int n) { +extern char *xstrndup(const char *s, int n) +{ char *t; if (s == NULL) @@ -72,7 +73,7 @@ extern char * xstrndup (const char *s, int n) { t = xmalloc(++n); - return safe_strncpy(t,s,n); + return safe_strncpy(t, s, n); } FILE *xfopen(const char *path, const char *mode) diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c index 7d77a3b..596a763 100644 --- a/libbb/xreadlink.c +++ b/libbb/xreadlink.c @@ -15,17 +15,17 @@ extern char *xreadlink(const char *path) { - static const int GROWBY = 80; /* how large we will grow strings by */ + static const int GROWBY = 80; /* how large we will grow strings by */ char *buf = NULL; int bufsize = 0, readsize = 0; do { buf = xrealloc(buf, bufsize += GROWBY); - readsize = readlink(path, buf, bufsize); /* 1st try */ + readsize = readlink(path, buf, bufsize); /* 1st try */ if (readsize == -1) { - perror_msg("%s", path); - return NULL; + perror_msg("%s", path); + return NULL; } } while (bufsize < readsize + 1); @@ -34,4 +34,3 @@ extern char *xreadlink(const char *path) return buf; } - diff --git a/libopkg/active_list.c b/libopkg/active_list.c index 69ac1d1..2c91965 100644 --- a/libopkg/active_list.c +++ b/libopkg/active_list.c @@ -15,7 +15,6 @@ General Public License for more details. */ - #include "active_list.h" #include #include @@ -23,111 +22,128 @@ #include "libbb/libbb.h" -void active_list_init(struct active_list *ptr) { - INIT_LIST_HEAD(&ptr->node); - INIT_LIST_HEAD(&ptr->depend); - ptr->depended = NULL; +void active_list_init(struct active_list *ptr) +{ + INIT_LIST_HEAD(&ptr->node); + INIT_LIST_HEAD(&ptr->depend); + ptr->depended = NULL; } /** */ -struct active_list * active_list_next(struct active_list *head, struct active_list *ptr) { - struct active_list *next=NULL; - if ( !head ) { - opkg_msg(ERROR, "Internal error: head=%p, ptr=%p\n", head, ptr); - return NULL; - } - if ( !ptr ) - ptr = head; - next = list_entry(ptr->node.next, struct active_list, node); - if ( next == head ) { - return NULL; - } - if ( ptr->depended && &ptr->depended->depend == ptr->node.next ) { - return ptr->depended; - } - while ( next->depend.next != &next->depend ) { - next = list_entry(next->depend.next, struct active_list, node); - } - return next; +struct active_list *active_list_next(struct active_list *head, + struct active_list *ptr) +{ + struct active_list *next = NULL; + if (!head) { + opkg_msg(ERROR, "Internal error: head=%p, ptr=%p\n", head, ptr); + return NULL; + } + if (!ptr) + ptr = head; + next = list_entry(ptr->node.next, struct active_list, node); + if (next == head) { + return NULL; + } + if (ptr->depended && &ptr->depended->depend == ptr->node.next) { + return ptr->depended; + } + while (next->depend.next != &next->depend) { + next = list_entry(next->depend.next, struct active_list, node); + } + return next; } - -struct active_list * active_list_prev(struct active_list *head, struct active_list *ptr) { - struct active_list *prev=NULL; - if ( !head ) { - opkg_msg(ERROR, "Internal error: head=%p, ptr=%p\n", head, ptr); - return NULL; - } - if ( !ptr ) - ptr = head; - if ( ptr->depend.prev != &ptr->depend ) { - prev = list_entry(ptr->depend.prev, struct active_list, node); - return prev; - } - if ( ptr->depended && ptr->depended != head && &ptr->depended->depend == ptr->node.prev ) { - prev = list_entry(ptr->depended->node.prev, struct active_list, node); - } else - prev = list_entry(ptr->node.prev, struct active_list, node); - if ( prev == head ) - return NULL; - return prev; +struct active_list *active_list_prev(struct active_list *head, + struct active_list *ptr) +{ + struct active_list *prev = NULL; + if (!head) { + opkg_msg(ERROR, "Internal error: head=%p, ptr=%p\n", head, ptr); + return NULL; + } + if (!ptr) + ptr = head; + if (ptr->depend.prev != &ptr->depend) { + prev = list_entry(ptr->depend.prev, struct active_list, node); + return prev; + } + if (ptr->depended && ptr->depended != head + && &ptr->depended->depend == ptr->node.prev) { + prev = + list_entry(ptr->depended->node.prev, struct active_list, + node); + } else + prev = list_entry(ptr->node.prev, struct active_list, node); + if (prev == head) + return NULL; + return prev; } - -struct active_list *active_list_move_node(struct active_list *old_head, struct active_list *new_head, struct active_list *node) { - struct active_list *prev; - if (!old_head || !new_head || !node) - return NULL; - if (old_head == new_head) - return node; - prev = active_list_prev(old_head, node); - active_list_add(new_head, node); - return prev; +struct active_list *active_list_move_node(struct active_list *old_head, + struct active_list *new_head, + struct active_list *node) +{ + struct active_list *prev; + if (!old_head || !new_head || !node) + return NULL; + if (old_head == new_head) + return node; + prev = active_list_prev(old_head, node); + active_list_add(new_head, node); + return prev; } -static void list_head_clear (struct list_head *head) { - struct active_list *next; - struct list_head *n, *ptr; - if (!head) - return; - list_for_each_safe(ptr, n , head) { - next = list_entry(ptr, struct active_list, node); - if (next->depend.next != &next->depend) { - list_head_clear(&next->depend); - } - active_list_init(next); - } +static void list_head_clear(struct list_head *head) +{ + struct active_list *next; + struct list_head *n, *ptr; + if (!head) + return; + list_for_each_safe(ptr, n, head) { + next = list_entry(ptr, struct active_list, node); + if (next->depend.next != &next->depend) { + list_head_clear(&next->depend); + } + active_list_init(next); + } } -void active_list_clear(struct active_list *head) { - list_head_clear(&head->node); - if (head->depend.next != &head->depend) { - list_head_clear(&head->depend); - } - active_list_init(head); + +void active_list_clear(struct active_list *head) +{ + list_head_clear(&head->node); + if (head->depend.next != &head->depend) { + list_head_clear(&head->depend); + } + active_list_init(head); } -void active_list_add_depend(struct active_list *node, struct active_list *depend) { - list_del_init(&depend->node); - list_add_tail(&depend->node, &node->depend); - depend->depended = node; +void active_list_add_depend(struct active_list *node, + struct active_list *depend) +{ + list_del_init(&depend->node); + list_add_tail(&depend->node, &node->depend); + depend->depended = node; } -void active_list_add(struct active_list *head, struct active_list *node) { - list_del_init(&node->node); - list_add_tail(&node->node, &head->node); - node->depended = head; +void active_list_add(struct active_list *head, struct active_list *node) +{ + list_del_init(&node->node); + list_add_tail(&node->node, &head->node); + node->depended = head; } -struct active_list * active_list_head_new(void) { - struct active_list * head = xcalloc(1, sizeof(struct active_list)); - active_list_init(head); - return head; +struct active_list *active_list_head_new(void) +{ + struct active_list *head = xcalloc(1, sizeof(struct active_list)); + active_list_init(head); + return head; } -void active_list_head_delete(struct active_list *head) { - active_list_clear(head); - free(head); +void active_list_head_delete(struct active_list *head) +{ + active_list_clear(head); + free(head); } /* @@ -135,31 +151,37 @@ void active_list_head_delete(struct active_list *head) { * Note. the list should not be large, or it will be very inefficient. * */ -struct active_list * active_list_sort(struct active_list *head, int (*compare)(const void *, const void *)) { - struct active_list tmphead; - struct active_list *node, *ptr; - if ( !head ) - return NULL; - active_list_init(&tmphead); - for (node = active_list_next(head, NULL); node; node = active_list_next(head, NULL)) { - if (tmphead.node.next == &tmphead.node) { - active_list_move_node(head, &tmphead, node); - } else { - for (ptr = active_list_next(&tmphead, NULL); ptr; ptr=active_list_next(&tmphead, ptr)) { - if (compare(ptr, node) <= 0) { - break; - } - } - if (!ptr) { - active_list_move_node(head, &tmphead, node); - } else { - active_list_move_node(head, ptr, node); - } - } - node->depended = &tmphead; - } - for (ptr = active_list_prev(&tmphead, NULL); ptr; ptr=active_list_prev(&tmphead, NULL)) { - active_list_move_node(&tmphead, head, ptr); - } - return head; +struct active_list *active_list_sort(struct active_list *head, + int (*compare) (const void *, + const void *)) +{ + struct active_list tmphead; + struct active_list *node, *ptr; + if (!head) + return NULL; + active_list_init(&tmphead); + for (node = active_list_next(head, NULL); node; + node = active_list_next(head, NULL)) { + if (tmphead.node.next == &tmphead.node) { + active_list_move_node(head, &tmphead, node); + } else { + for (ptr = active_list_next(&tmphead, NULL); ptr; + ptr = active_list_next(&tmphead, ptr)) { + if (compare(ptr, node) <= 0) { + break; + } + } + if (!ptr) { + active_list_move_node(head, &tmphead, node); + } else { + active_list_move_node(head, ptr, node); + } + } + node->depended = &tmphead; + } + for (ptr = active_list_prev(&tmphead, NULL); ptr; + ptr = active_list_prev(&tmphead, NULL)) { + active_list_move_node(&tmphead, head, ptr); + } + return head; } diff --git a/libopkg/active_list.h b/libopkg/active_list.h index ecb79a6..396e051 100644 --- a/libopkg/active_list.h +++ b/libopkg/active_list.h @@ -21,24 +21,30 @@ #include "list.h" struct active_list { - struct list_head node; - struct list_head depend; - struct active_list *depended; + struct list_head node; + struct list_head depend; + struct active_list *depended; }; - -struct active_list * active_list_head_new(void); +struct active_list *active_list_head_new(void); void active_list_head_delete(struct active_list *); void active_list_init(struct active_list *ptr); void active_list_clear(struct active_list *head); -void active_list_add_depend(struct active_list *node, struct active_list *depend); +void active_list_add_depend(struct active_list *node, + struct active_list *depend); void active_list_add(struct active_list *head, struct active_list *node); -struct active_list *active_list_move_node(struct active_list *old_head, struct active_list *new_head, struct active_list *node); +struct active_list *active_list_move_node(struct active_list *old_head, + struct active_list *new_head, + struct active_list *node); -struct active_list * active_list_sort(struct active_list *head, int (*compare_fcn_t)(const void *, const void *)); +struct active_list *active_list_sort(struct active_list *head, + int (*compare_fcn_t) (const void *, + const void *)); -struct active_list * active_list_next(struct active_list *head, struct active_list *ptr); +struct active_list *active_list_next(struct active_list *head, + struct active_list *ptr); -struct active_list * active_list_prev(struct active_list *head, struct active_list *ptr); +struct active_list *active_list_prev(struct active_list *head, + struct active_list *ptr); #endif diff --git a/libopkg/conffile.c b/libopkg/conffile.c index c45a6b9..434edf6 100644 --- a/libopkg/conffile.c +++ b/libopkg/conffile.c @@ -24,47 +24,49 @@ #include "sprintf_alloc.h" #include "opkg_conf.h" -int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum) +int conffile_init(conffile_t * conffile, const char *file_name, + const char *md5sum) { - return nv_pair_init(conffile, file_name, md5sum); + return nv_pair_init(conffile, file_name, md5sum); } -void conffile_deinit(conffile_t *conffile) +void conffile_deinit(conffile_t * conffile) { - nv_pair_deinit(conffile); + nv_pair_deinit(conffile); } -int conffile_has_been_modified(conffile_t *conffile) +int conffile_has_been_modified(conffile_t * conffile) { - char *chksum; - char *filename = conffile->name; - char *root_filename; - int ret = 1; + char *chksum; + char *filename = conffile->name; + char *root_filename; + int ret = 1; - if (conffile->value == NULL) { - opkg_msg(NOTICE, "Conffile %s has no md5sum.\n", conffile->name); - return 1; - } + if (conffile->value == NULL) { + opkg_msg(NOTICE, "Conffile %s has no md5sum.\n", + conffile->name); + return 1; + } - root_filename = root_filename_alloc(filename); + root_filename = root_filename_alloc(filename); #ifdef HAVE_MD5 - if(conffile->value && strlen(conffile->value) > 33) { - chksum = file_sha256sum_alloc(root_filename); - } else { - chksum = file_md5sum_alloc(root_filename); - } + if (conffile->value && strlen(conffile->value) > 33) { + chksum = file_sha256sum_alloc(root_filename); + } else { + chksum = file_md5sum_alloc(root_filename); + } #else - chksum = file_sha256sum_alloc(root_filename); + chksum = file_sha256sum_alloc(root_filename); #endif - if (chksum && (ret = strcmp(chksum, conffile->value))) { - opkg_msg(INFO, "Conffile %s:\n\told chk=%s\n\tnew chk=%s\n", - conffile->name, chksum, conffile->value); - } + if (chksum && (ret = strcmp(chksum, conffile->value))) { + opkg_msg(INFO, "Conffile %s:\n\told chk=%s\n\tnew chk=%s\n", + conffile->name, chksum, conffile->value); + } - free(root_filename); - if (chksum) - free(chksum); + free(root_filename); + if (chksum) + free(chksum); - return ret; + return ret; } diff --git a/libopkg/conffile.h b/libopkg/conffile.h index a188c6d..091bf5e 100644 --- a/libopkg/conffile.h +++ b/libopkg/conffile.h @@ -21,9 +21,9 @@ #include "nv_pair.h" typedef struct nv_pair conffile_t; -int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum); -void conffile_deinit(conffile_t *conffile); -int conffile_has_been_modified(conffile_t *conffile); +int conffile_init(conffile_t * conffile, const char *file_name, + const char *md5sum); +void conffile_deinit(conffile_t * conffile); +int conffile_has_been_modified(conffile_t * conffile); #endif - diff --git a/libopkg/conffile_list.c b/libopkg/conffile_list.c index 1db7790..25f5b9d 100644 --- a/libopkg/conffile_list.c +++ b/libopkg/conffile_list.c @@ -17,30 +17,29 @@ #include "conffile_list.h" -void conffile_list_init(conffile_list_t *list) +void conffile_list_init(conffile_list_t * list) { - nv_pair_list_init(list); + nv_pair_list_init(list); } -void conffile_list_deinit(conffile_list_t *list) +void conffile_list_deinit(conffile_list_t * list) { - nv_pair_list_deinit(list); + nv_pair_list_deinit(list); } -conffile_t *conffile_list_append(conffile_list_t *list, const char *file_name, - const char *md5sum) +conffile_t *conffile_list_append(conffile_list_t * list, const char *file_name, + const char *md5sum) { - return nv_pair_list_append(list, file_name, md5sum); + return nv_pair_list_append(list, file_name, md5sum); } -void conffile_list_push(conffile_list_t *list, conffile_t *data) +void conffile_list_push(conffile_list_t * list, conffile_t * data) { - nv_pair_list_push(list, data); + nv_pair_list_push(list, data); } -conffile_list_elt_t *conffile_list_pop(conffile_list_t *list) +conffile_list_elt_t *conffile_list_pop(conffile_list_t * list) { - conffile_list_elt_t *pos = nv_pair_list_pop(list); - return pos; + conffile_list_elt_t *pos = nv_pair_list_pop(list); + return pos; } - diff --git a/libopkg/conffile_list.h b/libopkg/conffile_list.h index 942f68e..c689d8f 100644 --- a/libopkg/conffile_list.h +++ b/libopkg/conffile_list.h @@ -25,13 +25,12 @@ typedef nv_pair_list_t conffile_list_t; #include "conffile.h" -void conffile_list_init(conffile_list_t *list); -void conffile_list_deinit(conffile_list_t *list); +void conffile_list_init(conffile_list_t * list); +void conffile_list_deinit(conffile_list_t * list); -conffile_t *conffile_list_append(conffile_list_t *list, const char *name, - const char *root_dir); -void conffile_list_push(conffile_list_t *list, conffile_t *data); -conffile_list_elt_t *conffile_list_pop(conffile_list_t *list); +conffile_t *conffile_list_append(conffile_list_t * list, const char *name, + const char *root_dir); +void conffile_list_push(conffile_list_t * list, conffile_t * data); +conffile_list_elt_t *conffile_list_pop(conffile_list_t * list); #endif - diff --git a/libopkg/file_util.c b/libopkg/file_util.c index c54903c..4f949ac 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -35,8 +35,7 @@ #include "sha256.h" #endif -int -file_exists(const char *file_name) +int file_exists(const char *file_name) { struct stat st; @@ -46,8 +45,7 @@ file_exists(const char *file_name) return 1; } -int -file_is_dir(const char *file_name) +int file_is_dir(const char *file_name) { struct stat st; @@ -64,8 +62,7 @@ file_is_dir(const char *file_name) Return value is NULL if the file is at EOF when called. */ -char * -file_read_line_alloc(FILE *fp) +char *file_read_line_alloc(FILE * fp) { char buf[BUFSIZ]; unsigned int buf_len; @@ -84,7 +81,7 @@ file_read_line_alloc(FILE *fp) } if (line) { line_size += buf_len; - line = xrealloc(line, line_size+1); + line = xrealloc(line, line_size + 1); strncat(line, buf, line_size); } else { line_size = buf_len + 1; @@ -97,8 +94,7 @@ file_read_line_alloc(FILE *fp) return line; } -int -file_move(const char *src, const char *dest) +int file_move(const char *src, const char *dest) { int err; @@ -111,28 +107,25 @@ file_move(const char *src, const char *dest) unlink(src); } else { opkg_perror(ERROR, "Failed to rename %s to %s", - src, dest); + src, dest); } } return err; } -int -file_copy(const char *src, const char *dest) +int file_copy(const char *src, const char *dest) { int err; err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS); if (err) - opkg_msg(ERROR, "Failed to copy file %s to %s.\n", - src, dest); + opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest); return err; } -int -file_mkdir_hier(const char *path, long mode) +int file_mkdir_hier(const char *path, long mode) { return make_directory(path, mode, FILEUTILS_RECUR); } @@ -140,103 +133,102 @@ file_mkdir_hier(const char *path, long mode) #ifdef HAVE_MD5 char *file_md5sum_alloc(const char *file_name) { - static const int md5sum_bin_len = 16; - static const int md5sum_hex_len = 32; - - static const unsigned char bin2hex[16] = { - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f' - }; - - int i, err; - FILE *file; - char *md5sum_hex; - unsigned char md5sum_bin[md5sum_bin_len]; - - md5sum_hex = xcalloc(1, md5sum_hex_len + 1); - - file = fopen(file_name, "r"); - if (file == NULL) { - opkg_perror(ERROR, "Failed to open file %s", file_name); - free(md5sum_hex); - return NULL; - } - - err = md5_stream(file, md5sum_bin); - if (err) { - opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name); - fclose(file); - free(md5sum_hex); - return NULL; - } + static const int md5sum_bin_len = 16; + static const int md5sum_hex_len = 32; + + static const unsigned char bin2hex[16] = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' + }; + + int i, err; + FILE *file; + char *md5sum_hex; + unsigned char md5sum_bin[md5sum_bin_len]; + + md5sum_hex = xcalloc(1, md5sum_hex_len + 1); + + file = fopen(file_name, "r"); + if (file == NULL) { + opkg_perror(ERROR, "Failed to open file %s", file_name); + free(md5sum_hex); + return NULL; + } - fclose(file); + err = md5_stream(file, md5sum_bin); + if (err) { + opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name); + fclose(file); + free(md5sum_hex); + return NULL; + } - for (i=0; i < md5sum_bin_len; i++) { - md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4]; - md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf]; - } + fclose(file); - md5sum_hex[md5sum_hex_len] = '\0'; + for (i = 0; i < md5sum_bin_len; i++) { + md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4]; + md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf]; + } + + md5sum_hex[md5sum_hex_len] = '\0'; - return md5sum_hex; + return md5sum_hex; } #endif #ifdef HAVE_SHA256 char *file_sha256sum_alloc(const char *file_name) { - static const int sha256sum_bin_len = 32; - static const int sha256sum_hex_len = 64; - - static const unsigned char bin2hex[16] = { - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f' - }; - - int i, err; - FILE *file; - char *sha256sum_hex; - unsigned char sha256sum_bin[sha256sum_bin_len]; - - sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1); - - file = fopen(file_name, "r"); - if (file == NULL) { - opkg_perror(ERROR, "Failed to open file %s", file_name); - free(sha256sum_hex); - return NULL; - } - - err = sha256_stream(file, sha256sum_bin); - if (err) { - opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name); - fclose(file); - free(sha256sum_hex); - return NULL; - } + static const int sha256sum_bin_len = 32; + static const int sha256sum_hex_len = 64; + + static const unsigned char bin2hex[16] = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' + }; + + int i, err; + FILE *file; + char *sha256sum_hex; + unsigned char sha256sum_bin[sha256sum_bin_len]; + + sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1); + + file = fopen(file_name, "r"); + if (file == NULL) { + opkg_perror(ERROR, "Failed to open file %s", file_name); + free(sha256sum_hex); + return NULL; + } + + err = sha256_stream(file, sha256sum_bin); + if (err) { + opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", + file_name); + fclose(file); + free(sha256sum_hex); + return NULL; + } - fclose(file); + fclose(file); - for (i=0; i < sha256sum_bin_len; i++) { - sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4]; - sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf]; - } + for (i = 0; i < sha256sum_bin_len; i++) { + sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4]; + sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf]; + } - sha256sum_hex[sha256sum_hex_len] = '\0'; + sha256sum_hex[sha256sum_hex_len] = '\0'; - return sha256sum_hex; + return sha256sum_hex; } #endif - -int -rm_r(const char *path) +int rm_r(const char *path) { int ret = 0; DIR *dir; @@ -264,7 +256,7 @@ rm_r(const char *path) if ((dent = readdir(dir)) == NULL) { if (errno) { opkg_perror(ERROR, "Failed to read dir %s", - path); + path); ret = -1; } break; @@ -284,7 +276,7 @@ rm_r(const char *path) struct stat st; if ((ret = lstat(dent->d_name, &st)) == -1) { opkg_perror(ERROR, "Failed to lstat %s", - dent->d_name); + dent->d_name); break; } if (S_ISDIR(st.st_mode)) { @@ -305,7 +297,7 @@ rm_r(const char *path) opkg_perror(ERROR, "Failed to change to dir %s/..", path); } - if (rmdir(path) == -1 ) { + if (rmdir(path) == -1) { ret = -1; opkg_perror(ERROR, "Failed to remove dir %s", path); } diff --git a/libopkg/file_util.h b/libopkg/file_util.h index cfad551..a7e2a38 100644 --- a/libopkg/file_util.h +++ b/libopkg/file_util.h @@ -20,7 +20,7 @@ int file_exists(const char *file_name); int file_is_dir(const char *file_name); -char *file_read_line_alloc(FILE *file); +char *file_read_line_alloc(FILE * file); int file_move(const char *src, const char *dest); int file_copy(const char *src, const char *dest); int file_mkdir_hier(const char *path, long mode); diff --git a/libopkg/hash_table.c b/libopkg/hash_table.c index 37b53e9..791cf42 100644 --- a/libopkg/hash_table.c +++ b/libopkg/hash_table.c @@ -22,19 +22,16 @@ #include "opkg_message.h" #include "libbb/libbb.h" - -static unsigned long -djb2_hash(const unsigned char *str) +static unsigned long djb2_hash(const unsigned char *str) { unsigned long hash = 5381; int c; while ((c = *str++)) - hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ return hash; } -static int -hash_index(hash_table_t *hash, const char *key) +static int hash_index(hash_table_t * hash, const char *key) { return djb2_hash((const unsigned char *)key) % hash->n_buckets; } @@ -42,8 +39,7 @@ hash_index(hash_table_t *hash, const char *key) /* * this is an open table keyed by strings */ -void -hash_table_init(const char *name, hash_table_t *hash, int len) +void hash_table_init(const char *name, hash_table_t * hash, int len) { if (hash->entries != NULL) { opkg_msg(ERROR, "Internal error: non empty hash table.\n"); @@ -57,159 +53,155 @@ hash_table_init(const char *name, hash_table_t *hash, int len) hash->entries = xcalloc(hash->n_buckets, sizeof(hash_entry_t)); } -void -hash_print_stats(hash_table_t *hash) +void hash_print_stats(hash_table_t * hash) { printf("hash_table: %s, %d bytes\n" - "\tn_buckets=%d, n_elements=%d, n_collisions=%d\n" - "\tmax_bucket_len=%d, n_used_buckets=%d, ave_bucket_len=%.2f\n" - "\tn_hits=%d, n_misses=%d\n", - hash->name, - hash->n_buckets*(int)sizeof(hash_entry_t), - hash->n_buckets, - hash->n_elements, - hash->n_collisions, - hash->max_bucket_len, - hash->n_used_buckets, - (hash->n_used_buckets ? - ((float)hash->n_elements)/hash->n_used_buckets : 0.0f), - hash->n_hits, - hash->n_misses); + "\tn_buckets=%d, n_elements=%d, n_collisions=%d\n" + "\tmax_bucket_len=%d, n_used_buckets=%d, ave_bucket_len=%.2f\n" + "\tn_hits=%d, n_misses=%d\n", + hash->name, + hash->n_buckets * (int)sizeof(hash_entry_t), + hash->n_buckets, + hash->n_elements, + hash->n_collisions, + hash->max_bucket_len, + hash->n_used_buckets, + (hash->n_used_buckets ? + ((float)hash->n_elements) / hash->n_used_buckets : 0.0f), + hash->n_hits, hash->n_misses); } -void hash_table_deinit(hash_table_t *hash) +void hash_table_deinit(hash_table_t * hash) { - int i; - if (!hash) - return; - - /* free the reminaing entries */ - for (i = 0; i < hash->n_buckets; i++) { - hash_entry_t *hash_entry = (hash->entries + i); - free (hash_entry->key); - /* skip the first entry as this is part of the array */ - hash_entry = hash_entry->next; - while (hash_entry) - { - hash_entry_t *old = hash_entry; - hash_entry = hash_entry->next; - free (old->key); - free (old); + int i; + if (!hash) + return; + + /* free the reminaing entries */ + for (i = 0; i < hash->n_buckets; i++) { + hash_entry_t *hash_entry = (hash->entries + i); + free(hash_entry->key); + /* skip the first entry as this is part of the array */ + hash_entry = hash_entry->next; + while (hash_entry) { + hash_entry_t *old = hash_entry; + hash_entry = hash_entry->next; + free(old->key); + free(old); + } } - } - free (hash->entries); + free(hash->entries); - hash->entries = NULL; - hash->n_buckets = 0; + hash->entries = NULL; + hash->n_buckets = 0; } -void *hash_table_get(hash_table_t *hash, const char *key) +void *hash_table_get(hash_table_t * hash, const char *key) { - int ndx= hash_index(hash, key); - hash_entry_t *hash_entry = hash->entries + ndx; - while (hash_entry) - { - if (hash_entry->key) - { - if (strcmp(key, hash_entry->key) == 0) { - hash->n_hits++; - return hash_entry->data; - } - } - hash_entry = hash_entry->next; - } - hash->n_misses++; - return NULL; + int ndx = hash_index(hash, key); + hash_entry_t *hash_entry = hash->entries + ndx; + while (hash_entry) { + if (hash_entry->key) { + if (strcmp(key, hash_entry->key) == 0) { + hash->n_hits++; + return hash_entry->data; + } + } + hash_entry = hash_entry->next; + } + hash->n_misses++; + return NULL; } -int hash_table_insert(hash_table_t *hash, const char *key, void *value) +int hash_table_insert(hash_table_t * hash, const char *key, void *value) { - int bucket_len = 0; - int ndx= hash_index(hash, key); - hash_entry_t *hash_entry = hash->entries + ndx; - if (hash_entry->key) { - if (strcmp(hash_entry->key, key) == 0) { - /* alread in table, update the value */ - hash_entry->data = value; - return 0; - } else { - /* - * if this is a collision, we have to go to the end of the ll, - * then add a new entry - * before we can hook up the value - */ - while (hash_entry->next) { - hash_entry = hash_entry->next; - if (strcmp(hash_entry->key, key) == 0) { - hash_entry->data = value; - return 0; - } - bucket_len++; - } - hash_entry->next = xcalloc(1, sizeof(hash_entry_t)); - hash_entry = hash_entry->next; - hash_entry->next = NULL; - - hash->n_collisions++; - if (++bucket_len > hash->max_bucket_len) - hash->max_bucket_len = bucket_len; - } - } else - hash->n_used_buckets++; - - hash->n_elements++; - hash_entry->key = xstrdup(key); - hash_entry->data = value; - - return 0; + int bucket_len = 0; + int ndx = hash_index(hash, key); + hash_entry_t *hash_entry = hash->entries + ndx; + if (hash_entry->key) { + if (strcmp(hash_entry->key, key) == 0) { + /* alread in table, update the value */ + hash_entry->data = value; + return 0; + } else { + /* + * if this is a collision, we have to go to the end of the ll, + * then add a new entry + * before we can hook up the value + */ + while (hash_entry->next) { + hash_entry = hash_entry->next; + if (strcmp(hash_entry->key, key) == 0) { + hash_entry->data = value; + return 0; + } + bucket_len++; + } + hash_entry->next = xcalloc(1, sizeof(hash_entry_t)); + hash_entry = hash_entry->next; + hash_entry->next = NULL; + + hash->n_collisions++; + if (++bucket_len > hash->max_bucket_len) + hash->max_bucket_len = bucket_len; + } + } else + hash->n_used_buckets++; + + hash->n_elements++; + hash_entry->key = xstrdup(key); + hash_entry->data = value; + + return 0; } -int hash_table_remove(hash_table_t *hash, const char *key) +int hash_table_remove(hash_table_t * hash, const char *key) { - int ndx= hash_index(hash, key); - hash_entry_t *hash_entry = hash->entries + ndx; - hash_entry_t *next_entry=NULL, *last_entry=NULL; - while (hash_entry) - { - if (hash_entry->key) - { - if (strcmp(key, hash_entry->key) == 0) { - free(hash_entry->key); - if (last_entry) { - last_entry->next = hash_entry->next; - free(hash_entry); - } else { - next_entry = hash_entry->next; - if (next_entry) { - memmove(hash_entry, next_entry, sizeof(hash_entry_t)); - free(next_entry); - } else { - memset(hash_entry, 0 , sizeof(hash_entry_t)); - } - } - return 1; - } - } - last_entry = hash_entry; - hash_entry = hash_entry->next; - } - return 0; + int ndx = hash_index(hash, key); + hash_entry_t *hash_entry = hash->entries + ndx; + hash_entry_t *next_entry = NULL, *last_entry = NULL; + while (hash_entry) { + if (hash_entry->key) { + if (strcmp(key, hash_entry->key) == 0) { + free(hash_entry->key); + if (last_entry) { + last_entry->next = hash_entry->next; + free(hash_entry); + } else { + next_entry = hash_entry->next; + if (next_entry) { + memmove(hash_entry, next_entry, + sizeof(hash_entry_t)); + free(next_entry); + } else { + memset(hash_entry, 0, + sizeof(hash_entry_t)); + } + } + return 1; + } + } + last_entry = hash_entry; + hash_entry = hash_entry->next; + } + return 0; } -void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data) +void hash_table_foreach(hash_table_t * hash, + void (*f) (const char *key, void *entry, void *data), + void *data) { - int i; - if (!hash || !f) - return; - - for (i = 0; i < hash->n_buckets; i++) { - hash_entry_t *hash_entry = (hash->entries + i); - do { - if(hash_entry->key) { - f(hash_entry->key, hash_entry->data, data); - } - } while((hash_entry = hash_entry->next)); - } -} + int i; + if (!hash || !f) + return; + for (i = 0; i < hash->n_buckets; i++) { + hash_entry_t *hash_entry = (hash->entries + i); + do { + if (hash_entry->key) { + f(hash_entry->key, hash_entry->data, data); + } + } while ((hash_entry = hash_entry->next)); + } +} diff --git a/libopkg/hash_table.h b/libopkg/hash_table.h index 472b3e2..8da5219 100644 --- a/libopkg/hash_table.h +++ b/libopkg/hash_table.h @@ -22,30 +22,32 @@ typedef struct hash_entry hash_entry_t; typedef struct hash_table hash_table_t; struct hash_entry { - char * key; - void * data; - struct hash_entry * next; + char *key; + void *data; + struct hash_entry *next; }; struct hash_table { - const char *name; - hash_entry_t * entries; - unsigned int n_buckets; - unsigned int n_elements; - - /* useful stats */ - unsigned int n_used_buckets; - unsigned int n_collisions; - unsigned int max_bucket_len; - unsigned int n_hits, n_misses; + const char *name; + hash_entry_t *entries; + unsigned int n_buckets; + unsigned int n_elements; + + /* useful stats */ + unsigned int n_used_buckets; + unsigned int n_collisions; + unsigned int max_bucket_len; + unsigned int n_hits, n_misses; }; -void hash_table_init(const char *name, hash_table_t *hash, int len); -void hash_table_deinit(hash_table_t *hash); -void hash_print_stats(hash_table_t *hash); -void *hash_table_get(hash_table_t *hash, const char *key); -int hash_table_insert(hash_table_t *hash, const char *key, void *value); -int hash_table_remove(hash_table_t *has, const char *key); -void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data); +void hash_table_init(const char *name, hash_table_t * hash, int len); +void hash_table_deinit(hash_table_t * hash); +void hash_print_stats(hash_table_t * hash); +void *hash_table_get(hash_table_t * hash, const char *key); +int hash_table_insert(hash_table_t * hash, const char *key, void *value); +int hash_table_remove(hash_table_t * has, const char *key); +void hash_table_foreach(hash_table_t * hash, + void (*f) (const char *key, void *entry, void *data), + void *data); #endif /* _HASH_TABLE_H_ */ diff --git a/libopkg/list.h b/libopkg/list.h index c1325db..2b9aac3 100644 --- a/libopkg/list.h +++ b/libopkg/list.h @@ -21,7 +21,7 @@ #define _LINUX_LIST_H struct list_head { - struct list_head *next, *prev; + struct list_head *next, *prev; }; #define LIST_POISON1 ((struct list_head *) 0x00100100) @@ -37,12 +37,12 @@ struct list_head { } while (0) static inline void __list_add(struct list_head *newitem, - struct list_head *prev, - struct list_head *next) { - next->prev = newitem; - newitem->next = next; - newitem->prev = prev; - prev->next = newitem; + struct list_head *prev, struct list_head *next) +{ + next->prev = newitem; + newitem->next = next; + newitem->prev = prev; + prev->next = newitem; } /** @@ -53,8 +53,9 @@ static inline void __list_add(struct list_head *newitem, * Insert a new entry after the specified head. * This is good for implementing stacks. */ -static inline void list_add(struct list_head *newitem, struct list_head *head) { - __list_add(newitem, head, head->next); +static inline void list_add(struct list_head *newitem, struct list_head *head) +{ + __list_add(newitem, head, head->next); } /** @@ -65,11 +66,12 @@ static inline void list_add(struct list_head *newitem, struct list_head *head) { * Insert a new entry before the specified head. * This is useful for implementing queues. */ -static inline void list_add_tail(struct list_head *newitem, struct list_head *head) { - __list_add(newitem, head->prev, head); +static inline void list_add_tail(struct list_head *newitem, + struct list_head *head) +{ + __list_add(newitem, head->prev, head); } - /* * Delete a list entry by making the prev/next entries * point to each other. @@ -77,9 +79,10 @@ static inline void list_add_tail(struct list_head *newitem, struct list_head *he * This is only for internal list manipulation where we know * the prev/next entries already! */ -static inline void __list_del(struct list_head * prev, struct list_head * next) { - next->prev = prev; - prev->next = next; +static inline void __list_del(struct list_head *prev, struct list_head *next) +{ + next->prev = prev; + prev->next = next; } /** @@ -88,19 +91,21 @@ static inline void __list_del(struct list_head * prev, struct list_head * next) * Note: list_empty on entry does not return true after this, the entry is * in an undefined state. */ -static inline void list_del(struct list_head *entry) { - __list_del(entry->prev, entry->next); - entry->next = LIST_POISON1; - entry->prev = LIST_POISON2; +static inline void list_del(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + entry->next = LIST_POISON1; + entry->prev = LIST_POISON2; } /** * list_del_init - deletes entry from list and reinitialize it. * @entry: the element to delete from the list. */ -static inline void list_del_init(struct list_head *entry) { - __list_del(entry->prev, entry->next); - INIT_LIST_HEAD(entry); +static inline void list_del_init(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + INIT_LIST_HEAD(entry); } /** @@ -108,9 +113,10 @@ static inline void list_del_init(struct list_head *entry) { * @list: the entry to move * @head: the head that will precede our entry */ -static inline void list_move(struct list_head *list, struct list_head *head) { - __list_del(list->prev, list->next); - list_add(list, head); +static inline void list_move(struct list_head *list, struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add(list, head); } /** @@ -119,17 +125,19 @@ static inline void list_move(struct list_head *list, struct list_head *head) { * @head: the head that will follow our entry */ static inline void list_move_tail(struct list_head *list, - struct list_head *head) { - __list_del(list->prev, list->next); - list_add_tail(list, head); + struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add_tail(list, head); } /** * list_empty - tests whether a list is empty * @head: the list to test. */ -static inline int list_empty(const struct list_head *head) { - return head->next == head; +static inline int list_empty(const struct list_head *head) +{ + return head->next == head; } /** @@ -144,22 +152,23 @@ static inline int list_empty(const struct list_head *head) { * * @head: the list to test. */ -static inline int list_empty_careful(const struct list_head *head) { - struct list_head *next = head->next; - return (next == head) && (next == head->prev); +static inline int list_empty_careful(const struct list_head *head) +{ + struct list_head *next = head->next; + return (next == head) && (next == head->prev); } -static inline void __list_splice(struct list_head *list, - struct list_head *head) { - struct list_head *first = list->next; - struct list_head *last = list->prev; - struct list_head *at = head->next; +static inline void __list_splice(struct list_head *list, struct list_head *head) +{ + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *at = head->next; - first->prev = head; - head->next = first; + first->prev = head; + head->next = first; - last->next = at; - at->prev = last; + last->next = at; + at->prev = last; } /** @@ -167,9 +176,10 @@ static inline void __list_splice(struct list_head *list, * @list: the new list to add. * @head: the place to add it in the first list. */ -static inline void list_splice(struct list_head *list, struct list_head *head) { - if (!list_empty(list)) - __list_splice(list, head); +static inline void list_splice(struct list_head *list, struct list_head *head) +{ + if (!list_empty(list)) + __list_splice(list, head); } /** @@ -180,15 +190,14 @@ static inline void list_splice(struct list_head *list, struct list_head *head) { * The list at @list is reinitialised */ static inline void list_splice_init(struct list_head *list, - struct list_head *head) { - if (!list_empty(list)) { - __list_splice(list, head); - INIT_LIST_HEAD(list); - } + struct list_head *head) +{ + if (!list_empty(list)) { + __list_splice(list, head); + INIT_LIST_HEAD(list); + } } - - #define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ diff --git a/libopkg/md5.c b/libopkg/md5.c index 2213dc1..5db235e 100644 --- a/libopkg/md5.c +++ b/libopkg/md5.c @@ -30,259 +30,242 @@ #include #if USE_UNLOCKED_IO -# include "unlocked-io.h" +#include "unlocked-io.h" #endif #ifdef _LIBC -# include -# if __BYTE_ORDER == __BIG_ENDIAN -# define WORDS_BIGENDIAN 1 -# endif +#include +#if __BYTE_ORDER == __BIG_ENDIAN +#define WORDS_BIGENDIAN 1 +#endif /* We need to keep the namespace clean so define the MD5 function protected using leading __ . */ -# define md5_init_ctx __md5_init_ctx -# define md5_process_block __md5_process_block -# define md5_process_bytes __md5_process_bytes -# define md5_finish_ctx __md5_finish_ctx -# define md5_read_ctx __md5_read_ctx -# define md5_stream __md5_stream -# define md5_buffer __md5_buffer +#define md5_init_ctx __md5_init_ctx +#define md5_process_block __md5_process_block +#define md5_process_bytes __md5_process_bytes +#define md5_finish_ctx __md5_finish_ctx +#define md5_read_ctx __md5_read_ctx +#define md5_stream __md5_stream +#define md5_buffer __md5_buffer #endif #ifdef WORDS_BIGENDIAN -# define SWAP(n) \ +#define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) #else -# define SWAP(n) (n) +#define SWAP(n) (n) #endif #define BLOCKSIZE 4096 #if BLOCKSIZE % 64 != 0 -# error "invalid BLOCKSIZE" +#error "invalid BLOCKSIZE" #endif /* This array contains the bytes used to pad the buffer to the next 64-byte boundary. (RFC 1321, 3.1: Step 1) */ -static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; /* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */ -void -md5_init_ctx (struct md5_ctx *ctx) +void md5_init_ctx(struct md5_ctx *ctx) { - ctx->A = 0x67452301; - ctx->B = 0xefcdab89; - ctx->C = 0x98badcfe; - ctx->D = 0x10325476; + ctx->A = 0x67452301; + ctx->B = 0xefcdab89; + ctx->C = 0x98badcfe; + ctx->D = 0x10325476; - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; } /* Copy the 4 byte value from v into the memory location pointed to by *cp, If your architecture allows unaligned access this is equivalent to * (uint32_t *) cp = v */ -static inline void -set_uint32 (char *cp, uint32_t v) +static inline void set_uint32(char *cp, uint32_t v) { - memcpy (cp, &v, sizeof v); + memcpy(cp, &v, sizeof v); } /* Put result from CTX in first 16 bytes following RESBUF. The result must be in little endian byte order. */ -void * -md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) +void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf) { - char *r = resbuf; - set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A)); - set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B)); - set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C)); - set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D)); + char *r = resbuf; + set_uint32(r + 0 * sizeof ctx->A, SWAP(ctx->A)); + set_uint32(r + 1 * sizeof ctx->B, SWAP(ctx->B)); + set_uint32(r + 2 * sizeof ctx->C, SWAP(ctx->C)); + set_uint32(r + 3 * sizeof ctx->D, SWAP(ctx->D)); - return resbuf; + return resbuf; } /* Process the remaining bytes in the internal buffer and the usual prolog according to the standard and write the result to RESBUF. */ -void * -md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) +void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf) { - /* Take yet unprocessed bytes into account. */ - uint32_t bytes = ctx->buflen; - size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; + /* Take yet unprocessed bytes into account. */ + uint32_t bytes = ctx->buflen; + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; - /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3); - ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + ctx->buffer[size - 2] = SWAP(ctx->total[0] << 3); + ctx->buffer[size - 1] = + SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29)); - memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); + memcpy(&((char *)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); - /* Process last bytes. */ - md5_process_block (ctx->buffer, size * 4, ctx); + /* Process last bytes. */ + md5_process_block(ctx->buffer, size * 4, ctx); - return md5_read_ctx (ctx, resbuf); + return md5_read_ctx(ctx, resbuf); } /* Compute MD5 message digest for bytes read from STREAM. The resulting message digest number will be written into the 16 bytes beginning at RESBLOCK. */ -int -md5_stream (FILE *stream, void *resblock) +int md5_stream(FILE * stream, void *resblock) { - struct md5_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - while (1) - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - - if (sum == BLOCKSIZE) - break; - - if (n == 0) - { - /* Check for the error flag IFF N == 0, so that we don't - exit the loop after a partial read due to e.g., EAGAIN - or EWOULDBLOCK. */ - if (ferror (stream)) - return 1; - goto process_partial_block; - } - - /* We've read at least one byte, so ignore errors. But always - check for EOF, since feof may be true even though N > 0. - Otherwise, we could end up calling fread after EOF. */ - if (feof (stream)) - goto process_partial_block; + struct md5_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + md5_init_ctx(&ctx); + + /* Iterate over full file contents. */ + while (1) { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + while (1) { + n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + + if (sum == BLOCKSIZE) + break; + + if (n == 0) { + /* Check for the error flag IFF N == 0, so that we don't + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ + if (ferror(stream)) + return 1; + goto process_partial_block; + } + + /* We've read at least one byte, so ignore errors. But always + check for EOF, since feof may be true even though N > 0. + Otherwise, we could end up calling fread after EOF. */ + if (feof(stream)) + goto process_partial_block; + } + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + md5_process_block(buffer, BLOCKSIZE, &ctx); } - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - md5_process_block (buffer, BLOCKSIZE, &ctx); - } - process_partial_block: - /* Process any remaining bytes. */ - if (sum > 0) - md5_process_bytes (buffer, sum, &ctx); + /* Process any remaining bytes. */ + if (sum > 0) + md5_process_bytes(buffer, sum, &ctx); - /* Construct result in desired memory. */ - md5_finish_ctx (&ctx, resblock); - return 0; + /* Construct result in desired memory. */ + md5_finish_ctx(&ctx, resblock); + return 0; } /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -void * -md5_buffer (const char *buffer, size_t len, void *resblock) +void *md5_buffer(const char *buffer, size_t len, void *resblock) { - struct md5_ctx ctx; + struct md5_ctx ctx; - /* Initialize the computation context. */ - md5_init_ctx (&ctx); + /* Initialize the computation context. */ + md5_init_ctx(&ctx); - /* Process whole buffer but last len % 64 bytes. */ - md5_process_bytes (buffer, len, &ctx); + /* Process whole buffer but last len % 64 bytes. */ + md5_process_bytes(buffer, len, &ctx); - /* Put result in desired memory area. */ - return md5_finish_ctx (&ctx, resblock); + /* Put result in desired memory area. */ + return md5_finish_ctx(&ctx, resblock); } - -void -md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) +void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx) { - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (ctx->buflen != 0) - { - size_t left_over = ctx->buflen; - size_t add = 128 - left_over > len ? len : 128 - left_over; - - memcpy (&((char *) ctx->buffer)[left_over], buffer, add); - ctx->buflen += add; - - if (ctx->buflen > 64) - { - md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); - - ctx->buflen &= 63; - /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, - &((char *) ctx->buffer)[(left_over + add) & ~63], - ctx->buflen); + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > len ? len : 128 - left_over; + + memcpy(&((char *)ctx->buffer)[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 64) { + md5_process_block(ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; + /* The regions in the following copy operation cannot overlap. */ + memcpy(ctx->buffer, + &((char *)ctx->buffer)[(left_over + add) & ~63], + ctx->buflen); + } + + buffer = (const char *)buffer + add; + len -= add; } - buffer = (const char *) buffer + add; - len -= add; - } - - /* Process available complete blocks. */ - if (len >= 64) - { + /* Process available complete blocks. */ + if (len >= 64) { #if !_STRING_ARCH_unaligned -# define alignof(type) offsetof (struct { char c; type x; }, x) -# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) - if (UNALIGNED_P (buffer)) - while (len > 64) - { - md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); - buffer = (const char *) buffer + 64; - len -= 64; - } - else +#define alignof(type) offsetof (struct { char c; type x; }, x) +#define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) + if (UNALIGNED_P(buffer)) + while (len > 64) { + md5_process_block(memcpy + (ctx->buffer, buffer, 64), 64, + ctx); + buffer = (const char *)buffer + 64; + len -= 64; + } else #endif - { - md5_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; + { + md5_process_block(buffer, len & ~63, ctx); + buffer = (const char *)buffer + (len & ~63); + len &= 63; + } } - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0) - { - size_t left_over = ctx->buflen; - - memcpy (&((char *) ctx->buffer)[left_over], buffer, len); - left_over += len; - if (left_over >= 64) - { - md5_process_block (ctx->buffer, 64, ctx); - left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[16], left_over); + + /* Move remaining bytes in internal buffer. */ + if (len > 0) { + size_t left_over = ctx->buflen; + + memcpy(&((char *)ctx->buffer)[left_over], buffer, len); + left_over += len; + if (left_over >= 64) { + md5_process_block(ctx->buffer, 64, ctx); + left_over -= 64; + memcpy(ctx->buffer, &ctx->buffer[16], left_over); + } + ctx->buflen = left_over; } - ctx->buflen = left_over; - } } - /* These are the four functions used in the four steps of the MD5 algorithm and defined in the RFC 1321. The first function is a little bit optimized (as found in Colin Plumbs public domain implementation). */ @@ -295,41 +278,39 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) /* Process LEN bytes of BUFFER, accumulating context into CTX. It is assumed that LEN % 64 == 0. */ -void -md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) +void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx) { - uint32_t correct_words[16]; - const uint32_t *words = buffer; - size_t nwords = len / sizeof (uint32_t); - const uint32_t *endp = words + nwords; - uint32_t A = ctx->A; - uint32_t B = ctx->B; - uint32_t C = ctx->C; - uint32_t D = ctx->D; - - /* First increment the byte count. RFC 1321 specifies the possible - length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; - - /* Process all bytes in the buffer with 64 bytes in each round of - the loop. */ - while (words < endp) - { - uint32_t *cwp = correct_words; - uint32_t A_save = A; - uint32_t B_save = B; - uint32_t C_save = C; - uint32_t D_save = D; - - /* First round: using the given function, the context and a constant - the next context is computed. Because the algorithms processing - unit is a 32-bit word and it is determined to work on words in - little endian byte order we perhaps have to change the byte order - before the computation. To reduce the work for the next steps - we store the swapped words in the array CORRECT_WORDS. */ + uint32_t correct_words[16]; + const uint32_t *words = buffer; + size_t nwords = len / sizeof(uint32_t); + const uint32_t *endp = words + nwords; + uint32_t A = ctx->A; + uint32_t B = ctx->B; + uint32_t C = ctx->C; + uint32_t D = ctx->D; + + /* First increment the byte count. RFC 1321 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 64 bytes in each round of + the loop. */ + while (words < endp) { + uint32_t *cwp = correct_words; + uint32_t A_save = A; + uint32_t B_save = B; + uint32_t C_save = C; + uint32_t D_save = D; + + /* First round: using the given function, the context and a constant + the next context is computed. Because the algorithms processing + unit is a 32-bit word and it is determined to work on words in + little endian byte order we perhaps have to change the byte order + before the computation. To reduce the work for the next steps + we store the swapped words in the array CORRECT_WORDS. */ #define OP(a, b, c, d, s, T) \ do \ @@ -341,41 +322,41 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) } \ while (0) - /* It is unfortunate that C does not provide an operator for - cyclic rotation. Hope the C compiler is smart enough. */ + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) - /* Before we start, one word to the strange constants. - They are defined in RFC 1321 as - - T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 - - Here is an equivalent invocation using Perl: - - perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' - */ - - /* Round 1. */ - OP (A, B, C, D, 7, 0xd76aa478); - OP (D, A, B, C, 12, 0xe8c7b756); - OP (C, D, A, B, 17, 0x242070db); - OP (B, C, D, A, 22, 0xc1bdceee); - OP (A, B, C, D, 7, 0xf57c0faf); - OP (D, A, B, C, 12, 0x4787c62a); - OP (C, D, A, B, 17, 0xa8304613); - OP (B, C, D, A, 22, 0xfd469501); - OP (A, B, C, D, 7, 0x698098d8); - OP (D, A, B, C, 12, 0x8b44f7af); - OP (C, D, A, B, 17, 0xffff5bb1); - OP (B, C, D, A, 22, 0x895cd7be); - OP (A, B, C, D, 7, 0x6b901122); - OP (D, A, B, C, 12, 0xfd987193); - OP (C, D, A, B, 17, 0xa679438e); - OP (B, C, D, A, 22, 0x49b40821); - - /* For the second to fourth round we have the possibly swapped words - in CORRECT_WORDS. Redefine the macro to take an additional first - argument specifying the function to use. */ + /* Before we start, one word to the strange constants. + They are defined in RFC 1321 as + + T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + + Here is an equivalent invocation using Perl: + + perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' + */ + + /* Round 1. */ + OP(A, B, C, D, 7, 0xd76aa478); + OP(D, A, B, C, 12, 0xe8c7b756); + OP(C, D, A, B, 17, 0x242070db); + OP(B, C, D, A, 22, 0xc1bdceee); + OP(A, B, C, D, 7, 0xf57c0faf); + OP(D, A, B, C, 12, 0x4787c62a); + OP(C, D, A, B, 17, 0xa8304613); + OP(B, C, D, A, 22, 0xfd469501); + OP(A, B, C, D, 7, 0x698098d8); + OP(D, A, B, C, 12, 0x8b44f7af); + OP(C, D, A, B, 17, 0xffff5bb1); + OP(B, C, D, A, 22, 0x895cd7be); + OP(A, B, C, D, 7, 0x6b901122); + OP(D, A, B, C, 12, 0xfd987193); + OP(C, D, A, B, 17, 0xa679438e); + OP(B, C, D, A, 22, 0x49b40821); + + /* For the second to fourth round we have the possibly swapped words + in CORRECT_WORDS. Redefine the macro to take an additional first + argument specifying the function to use. */ #undef OP #define OP(f, a, b, c, d, k, s, T) \ do \ @@ -386,70 +367,70 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) } \ while (0) - /* Round 2. */ - OP (FG, A, B, C, D, 1, 5, 0xf61e2562); - OP (FG, D, A, B, C, 6, 9, 0xc040b340); - OP (FG, C, D, A, B, 11, 14, 0x265e5a51); - OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); - OP (FG, A, B, C, D, 5, 5, 0xd62f105d); - OP (FG, D, A, B, C, 10, 9, 0x02441453); - OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); - OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); - OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); - OP (FG, D, A, B, C, 14, 9, 0xc33707d6); - OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); - OP (FG, B, C, D, A, 8, 20, 0x455a14ed); - OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); - OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); - OP (FG, C, D, A, B, 7, 14, 0x676f02d9); - OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); - - /* Round 3. */ - OP (FH, A, B, C, D, 5, 4, 0xfffa3942); - OP (FH, D, A, B, C, 8, 11, 0x8771f681); - OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); - OP (FH, B, C, D, A, 14, 23, 0xfde5380c); - OP (FH, A, B, C, D, 1, 4, 0xa4beea44); - OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); - OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); - OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); - OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); - OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); - OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); - OP (FH, B, C, D, A, 6, 23, 0x04881d05); - OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); - OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); - OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); - OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); - - /* Round 4. */ - OP (FI, A, B, C, D, 0, 6, 0xf4292244); - OP (FI, D, A, B, C, 7, 10, 0x432aff97); - OP (FI, C, D, A, B, 14, 15, 0xab9423a7); - OP (FI, B, C, D, A, 5, 21, 0xfc93a039); - OP (FI, A, B, C, D, 12, 6, 0x655b59c3); - OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); - OP (FI, C, D, A, B, 10, 15, 0xffeff47d); - OP (FI, B, C, D, A, 1, 21, 0x85845dd1); - OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); - OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); - OP (FI, C, D, A, B, 6, 15, 0xa3014314); - OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); - OP (FI, A, B, C, D, 4, 6, 0xf7537e82); - OP (FI, D, A, B, C, 11, 10, 0xbd3af235); - OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); - OP (FI, B, C, D, A, 9, 21, 0xeb86d391); - - /* Add the starting values of the context. */ - A += A_save; - B += B_save; - C += C_save; - D += D_save; - } - - /* Put checksum in context given as argument. */ - ctx->A = A; - ctx->B = B; - ctx->C = C; - ctx->D = D; + /* Round 2. */ + OP(FG, A, B, C, D, 1, 5, 0xf61e2562); + OP(FG, D, A, B, C, 6, 9, 0xc040b340); + OP(FG, C, D, A, B, 11, 14, 0x265e5a51); + OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa); + OP(FG, A, B, C, D, 5, 5, 0xd62f105d); + OP(FG, D, A, B, C, 10, 9, 0x02441453); + OP(FG, C, D, A, B, 15, 14, 0xd8a1e681); + OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8); + OP(FG, A, B, C, D, 9, 5, 0x21e1cde6); + OP(FG, D, A, B, C, 14, 9, 0xc33707d6); + OP(FG, C, D, A, B, 3, 14, 0xf4d50d87); + OP(FG, B, C, D, A, 8, 20, 0x455a14ed); + OP(FG, A, B, C, D, 13, 5, 0xa9e3e905); + OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8); + OP(FG, C, D, A, B, 7, 14, 0x676f02d9); + OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a); + + /* Round 3. */ + OP(FH, A, B, C, D, 5, 4, 0xfffa3942); + OP(FH, D, A, B, C, 8, 11, 0x8771f681); + OP(FH, C, D, A, B, 11, 16, 0x6d9d6122); + OP(FH, B, C, D, A, 14, 23, 0xfde5380c); + OP(FH, A, B, C, D, 1, 4, 0xa4beea44); + OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9); + OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60); + OP(FH, B, C, D, A, 10, 23, 0xbebfbc70); + OP(FH, A, B, C, D, 13, 4, 0x289b7ec6); + OP(FH, D, A, B, C, 0, 11, 0xeaa127fa); + OP(FH, C, D, A, B, 3, 16, 0xd4ef3085); + OP(FH, B, C, D, A, 6, 23, 0x04881d05); + OP(FH, A, B, C, D, 9, 4, 0xd9d4d039); + OP(FH, D, A, B, C, 12, 11, 0xe6db99e5); + OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8); + OP(FH, B, C, D, A, 2, 23, 0xc4ac5665); + + /* Round 4. */ + OP(FI, A, B, C, D, 0, 6, 0xf4292244); + OP(FI, D, A, B, C, 7, 10, 0x432aff97); + OP(FI, C, D, A, B, 14, 15, 0xab9423a7); + OP(FI, B, C, D, A, 5, 21, 0xfc93a039); + OP(FI, A, B, C, D, 12, 6, 0x655b59c3); + OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92); + OP(FI, C, D, A, B, 10, 15, 0xffeff47d); + OP(FI, B, C, D, A, 1, 21, 0x85845dd1); + OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f); + OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0); + OP(FI, C, D, A, B, 6, 15, 0xa3014314); + OP(FI, B, C, D, A, 13, 21, 0x4e0811a1); + OP(FI, A, B, C, D, 4, 6, 0xf7537e82); + OP(FI, D, A, B, C, 11, 10, 0xbd3af235); + OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb); + OP(FI, B, C, D, A, 9, 21, 0xeb86d391); + + /* Add the starting values of the context. */ + A += A_save; + B += B_save; + C += C_save; + D += D_save; + } + + /* Put checksum in context given as argument. */ + ctx->A = A; + ctx->B = B; + ctx->C = C; + ctx->D = D; } diff --git a/libopkg/md5.h b/libopkg/md5.h index 3ae657b..717e9ea 100644 --- a/libopkg/md5.h +++ b/libopkg/md5.h @@ -28,43 +28,42 @@ #define MD5_BLOCK_SIZE 64 #ifndef __GNUC_PREREQ -# if defined __GNUC__ && defined __GNUC_MINOR__ -# define __GNUC_PREREQ(maj, min) \ +#if defined __GNUC__ && defined __GNUC_MINOR__ +#define __GNUC_PREREQ(maj, min) \ ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) -# else -# define __GNUC_PREREQ(maj, min) 0 -# endif +#else +#define __GNUC_PREREQ(maj, min) 0 +#endif #endif #ifndef __THROW -# if defined __cplusplus && __GNUC_PREREQ (2,8) -# define __THROW throw () -# else -# define __THROW -# endif +#if defined __cplusplus && __GNUC_PREREQ (2,8) +#define __THROW throw () +#else +#define __THROW +#endif #endif #ifndef _LIBC -# define __md5_buffer md5_buffer -# define __md5_finish_ctx md5_finish_ctx -# define __md5_init_ctx md5_init_ctx -# define __md5_process_block md5_process_block -# define __md5_process_bytes md5_process_bytes -# define __md5_read_ctx md5_read_ctx -# define __md5_stream md5_stream +#define __md5_buffer md5_buffer +#define __md5_finish_ctx md5_finish_ctx +#define __md5_init_ctx md5_init_ctx +#define __md5_process_block md5_process_block +#define __md5_process_bytes md5_process_bytes +#define __md5_read_ctx md5_read_ctx +#define __md5_stream md5_stream #endif /* Structure to save state of computation between the single steps. */ -struct md5_ctx -{ - uint32_t A; - uint32_t B; - uint32_t C; - uint32_t D; - - uint32_t total[2]; - uint32_t buflen; - uint32_t buffer[32]; +struct md5_ctx { + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + + uint32_t total[2]; + uint32_t buflen; + uint32_t buffer[32]; }; /* @@ -74,45 +73,43 @@ struct md5_ctx /* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */ -extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW; +extern void __md5_init_ctx(struct md5_ctx *ctx) __THROW; /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 64!!! */ -extern void __md5_process_block (const void *buffer, size_t len, - struct md5_ctx *ctx) __THROW; +extern void __md5_process_block(const void *buffer, size_t len, + struct md5_ctx *ctx) __THROW; /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void __md5_process_bytes (const void *buffer, size_t len, - struct md5_ctx *ctx) __THROW; +extern void __md5_process_bytes(const void *buffer, size_t len, + struct md5_ctx *ctx) __THROW; /* Process the remaining bytes in the buffer and put result from CTX in first 16 bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW; - +extern void *__md5_finish_ctx(struct md5_ctx *ctx, void *resbuf) __THROW; /* Put result from CTX in first 16 bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW; - +extern void *__md5_read_ctx(const struct md5_ctx *ctx, void *resbuf) __THROW; /* Compute MD5 message digest for bytes read from STREAM. The resulting message digest number will be written into the 16 bytes beginning at RESBLOCK. */ -extern int __md5_stream (FILE *stream, void *resblock) __THROW; +extern int __md5_stream(FILE * stream, void *resblock) __THROW; /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *__md5_buffer (const char *buffer, size_t len, - void *resblock) __THROW; +extern void *__md5_buffer(const char *buffer, size_t len, + void *resblock) __THROW; #endif /* md5.h */ diff --git a/libopkg/nv_pair.c b/libopkg/nv_pair.c index 2728100..d323ce5 100644 --- a/libopkg/nv_pair.c +++ b/libopkg/nv_pair.c @@ -18,22 +18,20 @@ #include "nv_pair.h" #include "libbb/libbb.h" -int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value) +int nv_pair_init(nv_pair_t * nv_pair, const char *name, const char *value) { - nv_pair->name = xstrdup(name); - nv_pair->value = xstrdup(value); + nv_pair->name = xstrdup(name); + nv_pair->value = xstrdup(value); - return 0; + return 0; } -void nv_pair_deinit(nv_pair_t *nv_pair) +void nv_pair_deinit(nv_pair_t * nv_pair) { - free(nv_pair->name); - nv_pair->name = NULL; + free(nv_pair->name); + nv_pair->name = NULL; - free(nv_pair->value); - nv_pair->value = NULL; + free(nv_pair->value); + nv_pair->value = NULL; } - - diff --git a/libopkg/nv_pair.h b/libopkg/nv_pair.h index 72513e4..478435f 100644 --- a/libopkg/nv_pair.h +++ b/libopkg/nv_pair.h @@ -19,14 +19,12 @@ #define NV_PAIR_H typedef struct nv_pair nv_pair_t; -struct nv_pair -{ - char *name; - char *value; +struct nv_pair { + char *name; + char *value; }; -int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value); -void nv_pair_deinit(nv_pair_t *nv_pair); +int nv_pair_init(nv_pair_t * nv_pair, const char *name, const char *value); +void nv_pair_deinit(nv_pair_t * nv_pair); #endif - diff --git a/libopkg/nv_pair_list.c b/libopkg/nv_pair_list.c index 333e721..154f136 100644 --- a/libopkg/nv_pair_list.c +++ b/libopkg/nv_pair_list.c @@ -20,79 +20,85 @@ #include "nv_pair_list.h" #include "libbb/libbb.h" -void nv_pair_list_init(nv_pair_list_t *list) +void nv_pair_list_init(nv_pair_list_t * list) { - void_list_init((void_list_t *) list); + void_list_init((void_list_t *) list); } -void nv_pair_list_deinit(nv_pair_list_t *list) +void nv_pair_list_deinit(nv_pair_list_t * list) { - nv_pair_list_elt_t *pos; - nv_pair_t *nv_pair; - - while(!void_list_empty(list)) { - pos = nv_pair_list_pop(list); - if (!pos) - break; - nv_pair = (nv_pair_t *) pos->data; - nv_pair_deinit(nv_pair); - /* malloced in nv_pair_list_append */ - free(nv_pair); - pos->data = NULL; - free(pos); - } - void_list_deinit((void_list_t *) list); + nv_pair_list_elt_t *pos; + nv_pair_t *nv_pair; + + while (!void_list_empty(list)) { + pos = nv_pair_list_pop(list); + if (!pos) + break; + nv_pair = (nv_pair_t *) pos->data; + nv_pair_deinit(nv_pair); + /* malloced in nv_pair_list_append */ + free(nv_pair); + pos->data = NULL; + free(pos); + } + void_list_deinit((void_list_t *) list); } -nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value) +nv_pair_t *nv_pair_list_append(nv_pair_list_t * list, const char *name, + const char *value) { - /* freed in nv_pair_list_deinit */ - nv_pair_t *nv_pair = xcalloc(1, sizeof(nv_pair_t)); - nv_pair_init(nv_pair, name, value); - void_list_append((void_list_t *) list, nv_pair); + /* freed in nv_pair_list_deinit */ + nv_pair_t *nv_pair = xcalloc(1, sizeof(nv_pair_t)); + nv_pair_init(nv_pair, name, value); + void_list_append((void_list_t *) list, nv_pair); - return nv_pair; + return nv_pair; } -void nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data) +void nv_pair_list_push(nv_pair_list_t * list, nv_pair_t * data) { - void_list_push((void_list_t *) list, data); + void_list_push((void_list_t *) list, data); } -nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list) +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t * list) { - return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list); + return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list); } -char *nv_pair_list_find(nv_pair_list_t *list, char *name) +char *nv_pair_list_find(nv_pair_list_t * list, char *name) { - nv_pair_list_elt_t *iter; - nv_pair_t *nv_pair; - - list_for_each_entry(iter, &list->head, node) { - nv_pair = (nv_pair_t *)iter->data; - if (strcmp(nv_pair->name, name) == 0) { - return nv_pair->value; - } - } - return NULL; + nv_pair_list_elt_t *iter; + nv_pair_t *nv_pair; + + list_for_each_entry(iter, &list->head, node) { + nv_pair = (nv_pair_t *) iter->data; + if (strcmp(nv_pair->name, name) == 0) { + return nv_pair->value; + } + } + return NULL; } -nv_pair_list_elt_t *nv_pair_list_first(nv_pair_list_t *list) { - return (nv_pair_list_elt_t * )void_list_first((void_list_t *) list); +nv_pair_list_elt_t *nv_pair_list_first(nv_pair_list_t * list) +{ + return (nv_pair_list_elt_t *) void_list_first((void_list_t *) list); } -nv_pair_list_elt_t *nv_pair_list_prev(nv_pair_list_t *list, nv_pair_list_elt_t *node) { - return (nv_pair_list_elt_t * )void_list_prev((void_list_t *) list, (void_list_elt_t *)node); +nv_pair_list_elt_t *nv_pair_list_prev(nv_pair_list_t * list, + nv_pair_list_elt_t * node) +{ + return (nv_pair_list_elt_t *) void_list_prev((void_list_t *) list, + (void_list_elt_t *) node); } -nv_pair_list_elt_t *nv_pair_list_next(nv_pair_list_t *list, nv_pair_list_elt_t *node) { - return (nv_pair_list_elt_t * )void_list_next((void_list_t *) list, (void_list_elt_t *)node); +nv_pair_list_elt_t *nv_pair_list_next(nv_pair_list_t * list, + nv_pair_list_elt_t * node) +{ + return (nv_pair_list_elt_t *) void_list_next((void_list_t *) list, + (void_list_elt_t *) node); } -nv_pair_list_elt_t *nv_pair_list_last(nv_pair_list_t *list) { - return (nv_pair_list_elt_t * )void_list_last((void_list_t *) list); +nv_pair_list_elt_t *nv_pair_list_last(nv_pair_list_t * list) +{ + return (nv_pair_list_elt_t *) void_list_last((void_list_t *) list); } - - - diff --git a/libopkg/nv_pair_list.h b/libopkg/nv_pair_list.h index 1223a1f..d314d7b 100644 --- a/libopkg/nv_pair_list.h +++ b/libopkg/nv_pair_list.h @@ -25,24 +25,25 @@ typedef struct void_list_elt nv_pair_list_elt_t; typedef struct void_list nv_pair_list_t; -static inline int nv_pair_list_empty(nv_pair_list_t *list) +static inline int nv_pair_list_empty(nv_pair_list_t * list) { - return void_list_empty ((void_list_t *)list); + return void_list_empty((void_list_t *) list); } -void nv_pair_list_init(nv_pair_list_t *list); -void nv_pair_list_deinit(nv_pair_list_t *list); +void nv_pair_list_init(nv_pair_list_t * list); +void nv_pair_list_deinit(nv_pair_list_t * list); -nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, +nv_pair_t *nv_pair_list_append(nv_pair_list_t * list, const char *name, const char *value); -void nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data); -nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list); -char *nv_pair_list_find(nv_pair_list_t *list, char *name); +void nv_pair_list_push(nv_pair_list_t * list, nv_pair_t * data); +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t * list); +char *nv_pair_list_find(nv_pair_list_t * list, char *name); -nv_pair_list_elt_t *nv_pair_list_first(nv_pair_list_t *list); -nv_pair_list_elt_t *nv_pair_list_prev(nv_pair_list_t *list, nv_pair_list_elt_t *node); -nv_pair_list_elt_t *nv_pair_list_next(nv_pair_list_t *list, nv_pair_list_elt_t *node); -nv_pair_list_elt_t *nv_pair_list_last(nv_pair_list_t *list); +nv_pair_list_elt_t *nv_pair_list_first(nv_pair_list_t * list); +nv_pair_list_elt_t *nv_pair_list_prev(nv_pair_list_t * list, + nv_pair_list_elt_t * node); +nv_pair_list_elt_t *nv_pair_list_next(nv_pair_list_t * list, + nv_pair_list_elt_t * node); +nv_pair_list_elt_t *nv_pair_list_last(nv_pair_list_t * list); #endif - diff --git a/libopkg/opkg.c b/libopkg/opkg.c index dbb904a..351983d 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -43,8 +43,7 @@ /** Private Functions ***/ -static int -opkg_configure_packages(char *pkg_name) +static int opkg_configure_packages(char *pkg_name) { pkg_vec_t *all; int i; @@ -85,10 +84,9 @@ struct _curl_cb_data { int finish_range; }; -int -curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */ - double d, /* dlnow */ - double ultotal, double ulnow) +int curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */ + double d, /* dlnow */ + double ultotal, double ulnow) { int p = (t) ? d * 100 / t : 0; static int prev = -1; @@ -111,12 +109,10 @@ curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */ return 0; } - static struct opkg_conf saved_conf; /*** Public API ***/ -int -opkg_new() +int opkg_new() { saved_conf = *conf; @@ -141,8 +137,7 @@ err0: return -1; } -void -opkg_free(void) +void opkg_free(void) { #ifdef HAVE_CURL opkg_curl_cleanup(); @@ -150,16 +145,14 @@ opkg_free(void) opkg_conf_deinit(); } -int -opkg_re_read_config_files(void) +int opkg_re_read_config_files(void) { opkg_free(); *conf = saved_conf; return opkg_new(); } -void -opkg_get_option(char *option, void **value) +void opkg_get_option(char *option, void **value) { int i = 0; extern opkg_option_t options[]; @@ -177,22 +170,21 @@ opkg_get_option(char *option, void **value) /* get the option */ switch (options[i].type) { case OPKG_OPT_TYPE_BOOL: - *((int *) value) = *((int *) options[i].value); + *((int *)value) = *((int *)options[i].value); return; case OPKG_OPT_TYPE_INT: - *((int *) value) = *((int *) options[i].value); + *((int *)value) = *((int *)options[i].value); return; case OPKG_OPT_TYPE_STRING: - *((char **) value) = xstrdup(options[i].value); + *((char **)value) = xstrdup(options[i].value); return; } } -void -opkg_set_option(char *option, void *value) +void opkg_set_option(char *option, void *value) { int i = 0, found = 0; extern opkg_option_t options[]; @@ -219,18 +211,18 @@ opkg_set_option(char *option, void *value) /* set the option */ switch (options[i].type) { case OPKG_OPT_TYPE_BOOL: - if (*((int *) value) == 0) - *((int *) options[i].value) = 0; + if (*((int *)value) == 0) + *((int *)options[i].value) = 0; else - *((int *) options[i].value) = 1; + *((int *)options[i].value) = 1; return; case OPKG_OPT_TYPE_INT: - *((int *) options[i].value) = *((int *) value); + *((int *)options[i].value) = *((int *)value); return; case OPKG_OPT_TYPE_STRING: - *((char **) options[i].value) = xstrdup(value); + *((char **)options[i].value) = xstrdup(value); return; } @@ -243,8 +235,8 @@ opkg_set_option(char *option, void *value) */ int opkg_install_package(const char *package_name, - opkg_progress_callback_t progress_callback, - void *user_data) + opkg_progress_callback_t progress_callback, + void *user_data) { int err; char *stripped_filename; @@ -259,12 +251,11 @@ opkg_install_package(const char *package_name, /* ... */ pkg_info_preinstall_check(); - /* check to ensure package is not already installed */ old = pkg_hash_fetch_installed_by_name(package_name); if (old) { opkg_msg(ERROR, "Package %s is already installed\n", - package_name); + package_name); return -1; } @@ -285,11 +276,11 @@ opkg_install_package(const char *package_name, deps = pkg_vec_alloc(); /* this function does not return the original package, so we insert it later */ ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps, - &unresolved); + &unresolved); if (unresolved) { char **tmp = unresolved; opkg_msg(ERROR, "Couldn't satisfy the following dependencies" - " for %s:\n", package_name); + " for %s:\n", package_name); while (*tmp) { opkg_msg(ERROR, "\t%s", *tmp); free(*tmp); @@ -319,7 +310,7 @@ opkg_install_package(const char *package_name, if (pkg->src == NULL) { opkg_msg(ERROR, "Package %s not available from any " - "configured src\n", package_name); + "configured src\n", package_name); return -1; } @@ -361,7 +352,6 @@ opkg_install_package(const char *package_name, } pkg_vec_free(all); - /* 75% of "install" progress is for downloading */ pdata.pkg = new; pdata.action = OPKG_INSTALL; @@ -392,7 +382,7 @@ opkg_install_package(const char *package_name, int opkg_remove_package(const char *package_name, - opkg_progress_callback_t progress_callback, void *user_data) + opkg_progress_callback_t progress_callback, void *user_data) { int err; pkg_t *pkg = NULL; @@ -416,12 +406,12 @@ opkg_remove_package(const char *package_name, if (conf->restrict_to_default_dest) { pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name, - conf->default_dest); + conf-> + default_dest); } else { pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name); } - progress(pdata, 75); err = opkg_remove_pkg(pkg_to_remove, 0); @@ -430,15 +420,14 @@ opkg_remove_package(const char *package_name, opkg_conf_write_status_files(); pkg_write_changed_filelists(); - progress(pdata, 100); return (err) ? -1 : 0; } int opkg_upgrade_package(const char *package_name, - opkg_progress_callback_t progress_callback, - void *user_data) + opkg_progress_callback_t progress_callback, + void *user_data) { int err; pkg_t *pkg; @@ -529,7 +518,7 @@ opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data) int opkg_update_package_lists(opkg_progress_callback_t progress_callback, - void *user_data) + void *user_data) { char *tmp; int err, result = 0; @@ -544,7 +533,7 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback, progress(pdata, 0); sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest) - ? conf->default_dest->lists_dir : conf->lists_dir); + ? conf->default_dest->lists_dir : conf->lists_dir); if (!file_is_dir(lists_dir)) { if (file_exists(lists_dir)) { @@ -556,7 +545,7 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback, err = file_mkdir_hier(lists_dir, 0755); if (err) { opkg_msg(ERROR, "Couldn't create lists_dir %s\n", - lists_dir); + lists_dir); free(lists_dir); return 1; } @@ -565,7 +554,7 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback, sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir); if (mkdtemp(tmp) == NULL) { opkg_perror(ERROR, "Coundn't create temporary directory %s", - tmp); + tmp); free(lists_dir); free(tmp); return 1; @@ -624,15 +613,15 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback, } else { int err; err = opkg_verify_file(list_file_name, - sig_file_name); + sig_file_name); if (err == 0) { opkg_msg(INFO, "Signature check " - "passed for %s", - list_file_name); + "passed for %s", + list_file_name); } else { opkg_msg(ERROR, "Signature check " - "failed for %s", - list_file_name); + "failed for %s", + list_file_name); } } free(sig_file_name); @@ -640,8 +629,8 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback, } #else opkg_msg(INFO, "Signature check skipped for %s as GPG support" - " has not been enabled in this build\n", - list_file_name); + " has not been enabled in this build\n", + list_file_name); #endif free(list_file_name); @@ -659,8 +648,7 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback, return result; } -static int -pkg_compare_names_and_version(const void *a0, const void *b0) +static int pkg_compare_names_and_version(const void *a0, const void *b0) { const pkg_t *a = *(const pkg_t **)a0; const pkg_t *b = *(const pkg_t **)b0; @@ -674,8 +662,7 @@ pkg_compare_names_and_version(const void *a0, const void *b0) return ret; } -int -opkg_list_packages(opkg_package_callback_t callback, void *user_data) +int opkg_list_packages(opkg_package_callback_t callback, void *user_data) { pkg_vec_t *all; int i; @@ -716,7 +703,9 @@ opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data) for (node = active_list_next(head, head); node; node = active_list_next(head, node)) { old = list_entry(node, pkg_t, list); - new = pkg_hash_fetch_best_installation_candidate_by_name(old->name); + new = + pkg_hash_fetch_best_installation_candidate_by_name(old-> + name); if (new == NULL) continue; callback(new, user_data); @@ -725,9 +714,8 @@ opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data) return 0; } -pkg_t * -opkg_find_package(const char *name, const char *ver, const char *arch, - const char *repo) +pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch, + const char *repo) { int pkg_found = 0; pkg_t *pkg = NULL; @@ -780,8 +768,7 @@ opkg_find_package(const char *name, const char *ver, const char *arch, * @brief Check the accessibility of repositories. * @return return how many repositories cannot access. 0 means all okay. */ -int -opkg_repository_accessibility_check(void) +int opkg_repository_accessibility_check(void) { pkg_src_list_elt_t *iter; str_list_elt_t *iter1; @@ -795,11 +782,12 @@ opkg_repository_accessibility_check(void) src = str_list_alloc(); list_for_each_entry(iter, &conf->pkg_src_list.head, node) { - host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3; + host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3; end = index(host, '/'); if (strstr(((pkg_src_t *) iter->data)->value, "://") && end) stmp = xstrndup(((pkg_src_t *) iter->data)->value, - end - ((pkg_src_t *) iter->data)->value); + end - + ((pkg_src_t *) iter->data)->value); else stmp = xstrdup(((pkg_src_t *) iter->data)->value); diff --git a/libopkg/opkg.h b/libopkg/opkg.h index b39f7fb..9a145e7 100644 --- a/libopkg/opkg.h +++ b/libopkg/opkg.h @@ -23,38 +23,43 @@ typedef struct _opkg_progress_data_t opkg_progress_data_t; -typedef void (*opkg_progress_callback_t) (const opkg_progress_data_t *progress, void *user_data); -typedef void (*opkg_package_callback_t) (pkg_t *pkg, void *user_data); - -enum _opkg_action_t -{ - OPKG_INSTALL, - OPKG_REMOVE, - OPKG_DOWNLOAD +typedef void (*opkg_progress_callback_t) (const opkg_progress_data_t * progress, + void *user_data); +typedef void (*opkg_package_callback_t) (pkg_t * pkg, void *user_data); + +enum _opkg_action_t { + OPKG_INSTALL, + OPKG_REMOVE, + OPKG_DOWNLOAD }; -struct _opkg_progress_data_t -{ - int percentage; - int action; - pkg_t *pkg; +struct _opkg_progress_data_t { + int percentage; + int action; + pkg_t *pkg; }; -int opkg_new (void); -void opkg_free (void); -int opkg_re_read_config_files (void); -void opkg_get_option (char *option, void **value); -void opkg_set_option (char *option, void *value); - -int opkg_install_package (const char *package_name, opkg_progress_callback_t callback, void *user_data); -int opkg_remove_package (const char *package_name, opkg_progress_callback_t callback, void *user_data); -int opkg_upgrade_package (const char *package_name, opkg_progress_callback_t callback, void *user_data); -int opkg_upgrade_all (opkg_progress_callback_t callback, void *user_data); -int opkg_update_package_lists (opkg_progress_callback_t callback, void *user_data); - -int opkg_list_packages (opkg_package_callback_t callback, void *user_data); -int opkg_list_upgradable_packages (opkg_package_callback_t callback, void *user_data); -pkg_t* opkg_find_package (const char *name, const char *version, const char *architecture, const char *repository); +int opkg_new(void); +void opkg_free(void); +int opkg_re_read_config_files(void); +void opkg_get_option(char *option, void **value); +void opkg_set_option(char *option, void *value); + +int opkg_install_package(const char *package_name, + opkg_progress_callback_t callback, void *user_data); +int opkg_remove_package(const char *package_name, + opkg_progress_callback_t callback, void *user_data); +int opkg_upgrade_package(const char *package_name, + opkg_progress_callback_t callback, void *user_data); +int opkg_upgrade_all(opkg_progress_callback_t callback, void *user_data); +int opkg_update_package_lists(opkg_progress_callback_t callback, + void *user_data); + +int opkg_list_packages(opkg_package_callback_t callback, void *user_data); +int opkg_list_upgradable_packages(opkg_package_callback_t callback, + void *user_data); +pkg_t *opkg_find_package(const char *name, const char *version, + const char *architecture, const char *repository); int opkg_repository_accessibility_check(void); diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index d2adc31..d03e27c 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -43,8 +43,7 @@ #include "opkg_configure.h" #include "xsystem.h" -static void -print_pkg(pkg_t *pkg) +static void print_pkg(pkg_t * pkg) { char *version = pkg_version_str_alloc(pkg); printf("%s - %s", pkg->name, version); @@ -58,210 +57,218 @@ print_pkg(pkg_t *pkg) int opkg_state_changed; -static void -write_status_files_if_changed(void) +static void write_status_files_if_changed(void) { - if (opkg_state_changed && !conf->noaction) { - opkg_msg(INFO, "Writing status file.\n"); - opkg_conf_write_status_files(); - pkg_write_changed_filelists(); - } else { - opkg_msg(DEBUG, "Nothing to be done.\n"); - } + if (opkg_state_changed && !conf->noaction) { + opkg_msg(INFO, "Writing status file.\n"); + opkg_conf_write_status_files(); + pkg_write_changed_filelists(); + } else { + opkg_msg(DEBUG, "Nothing to be done.\n"); + } } -static void -sigint_handler(int sig) +static void sigint_handler(int sig) { - signal(sig, SIG_DFL); - opkg_msg(NOTICE, "Interrupted. Writing out status database.\n"); - write_status_files_if_changed(); - exit(128 + sig); + signal(sig, SIG_DFL); + opkg_msg(NOTICE, "Interrupted. Writing out status database.\n"); + write_status_files_if_changed(); + exit(128 + sig); } -static int -opkg_update_cmd(int argc, char **argv) +static int opkg_update_cmd(int argc, char **argv) { - char *tmp; - int err; - int failures; - int pkglist_dl_error; - char *lists_dir; - pkg_src_list_elt_t *iter; - pkg_src_t *src; - - - sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir); - - if (! file_is_dir(lists_dir)) { - if (file_exists(lists_dir)) { - opkg_msg(ERROR, "%s exists, but is not a directory.\n", - lists_dir); - free(lists_dir); - return -1; - } - err = file_mkdir_hier(lists_dir, 0755); - if (err) { - free(lists_dir); - return -1; - } - } - - failures = 0; - - sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir); - if (mkdtemp (tmp) == NULL) { - opkg_perror(ERROR, "Failed to make temp dir %s", conf->tmp_dir); - return -1; - } - - - for (iter = void_list_first(&conf->pkg_src_list); iter; iter = void_list_next(&conf->pkg_src_list, iter)) { - char *url, *list_file_name; - - src = (pkg_src_t *)iter->data; - - if (src->extra_data && strcmp(src->extra_data, "__dummy__ ")) - continue; - - if (src->extra_data) /* debian style? */ - sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, - src->gzip ? "Packages.gz" : "Packages"); - else - sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages"); - - sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name); - pkglist_dl_error = 0; - if (opkg_download(url, list_file_name, NULL, NULL, 0)) { - failures++; - pkglist_dl_error = 1; - opkg_msg(NOTICE, "*** Failed to download the package list from %s\n\n", - url); - } else { - opkg_msg(NOTICE, "Updated list of available packages in %s\n", - list_file_name); - } - free(url); + char *tmp; + int err; + int failures; + int pkglist_dl_error; + char *lists_dir; + pkg_src_list_elt_t *iter; + pkg_src_t *src; + + sprintf_alloc(&lists_dir, "%s", + conf->restrict_to_default_dest ? conf->default_dest-> + lists_dir : conf->lists_dir); + + if (!file_is_dir(lists_dir)) { + if (file_exists(lists_dir)) { + opkg_msg(ERROR, "%s exists, but is not a directory.\n", + lists_dir); + free(lists_dir); + return -1; + } + err = file_mkdir_hier(lists_dir, 0755); + if (err) { + free(lists_dir); + return -1; + } + } + + failures = 0; + + sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir); + if (mkdtemp(tmp) == NULL) { + opkg_perror(ERROR, "Failed to make temp dir %s", conf->tmp_dir); + return -1; + } + + for (iter = void_list_first(&conf->pkg_src_list); iter; + iter = void_list_next(&conf->pkg_src_list, iter)) { + char *url, *list_file_name; + + src = (pkg_src_t *) iter->data; + + if (src->extra_data && strcmp(src->extra_data, "__dummy__ ")) + continue; + + if (src->extra_data) /* debian style? */ + sprintf_alloc(&url, "%s/%s/%s", src->value, + src->extra_data, + src->gzip ? "Packages.gz" : "Packages"); + else + sprintf_alloc(&url, "%s/%s", src->value, + src->gzip ? "Packages.gz" : "Packages"); + + sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name); + pkglist_dl_error = 0; + if (opkg_download(url, list_file_name, NULL, NULL, 0)) { + failures++; + pkglist_dl_error = 1; + opkg_msg(NOTICE, + "*** Failed to download the package list from %s\n\n", + url); + } else { + opkg_msg(NOTICE, + "Updated list of available packages in %s\n", + list_file_name); + } + free(url); #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN) - if (pkglist_dl_error == 0 && conf->check_signature) { - /* download detached signitures to verify the package lists */ - /* get the url for the sig file */ - if (src->extra_data) /* debian style? */ - sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, - "Packages.sig"); - else - sprintf_alloc(&url, "%s/%s", src->value, "Packages.sig"); - - /* create temporary file for it */ - char *tmp_file_name; - - /* Put the signature in the right place */ - sprintf_alloc (&tmp_file_name, "%s/%s.sig", lists_dir, src->name); - - err = opkg_download(url, tmp_file_name, NULL, NULL, 0); - if (err) { - failures++; - opkg_msg(NOTICE, "Signature file download failed.\n"); - } else { - err = opkg_verify_file (list_file_name, tmp_file_name); - if (err == 0) - opkg_msg(NOTICE, "Signature check passed.\n"); - else - opkg_msg(NOTICE, "Signature check failed.\n"); - } - if (err && !conf->force_signature) { - /* The signature was wrong so delete it */ - opkg_msg(NOTICE, "Remove wrong Signature file.\n"); - unlink (tmp_file_name); - unlink (list_file_name); - } - /* We shouldn't unlink the signature ! */ - // unlink (tmp_file_name); - free (tmp_file_name); - free (url); - } + if (pkglist_dl_error == 0 && conf->check_signature) { + /* download detached signitures to verify the package lists */ + /* get the url for the sig file */ + if (src->extra_data) /* debian style? */ + sprintf_alloc(&url, "%s/%s/%s", src->value, + src->extra_data, "Packages.sig"); + else + sprintf_alloc(&url, "%s/%s", src->value, + "Packages.sig"); + + /* create temporary file for it */ + char *tmp_file_name; + + /* Put the signature in the right place */ + sprintf_alloc(&tmp_file_name, "%s/%s.sig", lists_dir, + src->name); + + err = opkg_download(url, tmp_file_name, NULL, NULL, 0); + if (err) { + failures++; + opkg_msg(NOTICE, + "Signature file download failed.\n"); + } else { + err = + opkg_verify_file(list_file_name, + tmp_file_name); + if (err == 0) + opkg_msg(NOTICE, + "Signature check passed.\n"); + else + opkg_msg(NOTICE, + "Signature check failed.\n"); + } + if (err && !conf->force_signature) { + /* The signature was wrong so delete it */ + opkg_msg(NOTICE, + "Remove wrong Signature file.\n"); + unlink(tmp_file_name); + unlink(list_file_name); + } + /* We shouldn't unlink the signature ! */ + // unlink (tmp_file_name); + free(tmp_file_name); + free(url); + } #else - // Do nothing + // Do nothing #endif - free(list_file_name); - } - rmdir (tmp); - free (tmp); - free(lists_dir); + free(list_file_name); + } + rmdir(tmp); + free(tmp); + free(lists_dir); - return failures; + return failures; } - -struct opkg_intercept -{ - char *oldpath; - char *statedir; +struct opkg_intercept { + char *oldpath; + char *statedir; }; typedef struct opkg_intercept *opkg_intercept_t; -static opkg_intercept_t -opkg_prep_intercepts(void) +static opkg_intercept_t opkg_prep_intercepts(void) { - opkg_intercept_t ctx; - char *newpath; + opkg_intercept_t ctx; + char *newpath; + + ctx = xcalloc(1, sizeof(*ctx)); + ctx->oldpath = xstrdup(getenv("PATH")); + sprintf_alloc(&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath); + sprintf_alloc(&ctx->statedir, "%s/opkg-intercept-XXXXXX", + conf->tmp_dir); + + if (mkdtemp(ctx->statedir) == NULL) { + opkg_perror(ERROR, "Failed to make temp dir %s", ctx->statedir); + free(ctx->oldpath); + free(ctx->statedir); + free(newpath); + free(ctx); + return NULL; + } + + setenv("OPKG_INTERCEPT_DIR", ctx->statedir, 1); + setenv("PATH", newpath, 1); + free(newpath); - ctx = xcalloc(1, sizeof (*ctx)); - ctx->oldpath = xstrdup(getenv("PATH")); - sprintf_alloc(&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath); - sprintf_alloc(&ctx->statedir, "%s/opkg-intercept-XXXXXX", conf->tmp_dir); + return ctx; +} + +static int opkg_finalize_intercepts(opkg_intercept_t ctx) +{ + DIR *dir; + int err = 0; - if (mkdtemp(ctx->statedir) == NULL) { - opkg_perror(ERROR,"Failed to make temp dir %s", ctx->statedir); + setenv("PATH", ctx->oldpath, 1); free(ctx->oldpath); - free(ctx->statedir); - free(newpath); - free(ctx); - return NULL; - } - setenv("OPKG_INTERCEPT_DIR", ctx->statedir, 1); - setenv("PATH", newpath, 1); - free(newpath); + dir = opendir(ctx->statedir); + if (dir) { + struct dirent *de; + while (de = readdir(dir), de != NULL) { + char *path; - return ctx; -} + if (de->d_name[0] == '.') + continue; -static int -opkg_finalize_intercepts(opkg_intercept_t ctx) -{ - DIR *dir; - int err = 0; - - setenv ("PATH", ctx->oldpath, 1); - free (ctx->oldpath); - - dir = opendir (ctx->statedir); - if (dir) { - struct dirent *de; - while (de = readdir (dir), de != NULL) { - char *path; - - if (de->d_name[0] == '.') - continue; - - sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name); - if (access (path, X_OK) == 0) { - const char *argv[] = {"sh", "-c", path, NULL}; - xsystem (argv); - } - free (path); - } - closedir(dir); - } else - opkg_perror(ERROR, "Failed to open dir %s", ctx->statedir); + sprintf_alloc(&path, "%s/%s", ctx->statedir, + de->d_name); + if (access(path, X_OK) == 0) { + const char *argv[] = { "sh", "-c", path, NULL }; + xsystem(argv); + } + free(path); + } + closedir(dir); + } else + opkg_perror(ERROR, "Failed to open dir %s", ctx->statedir); - rm_r(ctx->statedir); - free (ctx->statedir); - free (ctx); + rm_r(ctx->statedir); + free(ctx->statedir); + free(ctx); - return err; + return err; } /* For package pkg do the following: If it is already visited, return. If not, @@ -273,454 +280,461 @@ opkg_finalize_intercepts(opkg_intercept_t ctx) pkg_vec ordered will finally contain the ordered set of packages. */ static int -opkg_recurse_pkgs_in_order(pkg_t *pkg, pkg_vec_t *all, - pkg_vec_t *visited, pkg_vec_t *ordered) +opkg_recurse_pkgs_in_order(pkg_t * pkg, pkg_vec_t * all, + pkg_vec_t * visited, pkg_vec_t * ordered) { - int j,k,l,m; - int count; - pkg_t *dep; - compound_depend_t * compound_depend; - depend_t ** possible_satisfiers; - abstract_pkg_t *abpkg; - abstract_pkg_t **dependents; - - /* If it's just an available package, that is, not installed and not even - unpacked, skip it */ - /* XXX: This is probably an overkill, since a state_status != SS_UNPACKED - would do here. However, if there is an intermediate node (pkg) that is - configured and installed between two unpacked packages, the latter - won't be properly reordered, unless all installed/unpacked pkgs are - checked */ - if (pkg->state_status == SS_NOT_INSTALLED) - return 0; - - /* If the package has already been visited (by this function), skip it */ - for(j = 0; j < visited->len; j++) - if ( ! strcmp(visited->pkgs[j]->name, pkg->name)) { - opkg_msg(DEBUG, "pkg %s already visited, skipping.\n", pkg->name); - return 0; - } - - pkg_vec_insert(visited, pkg); - - count = pkg->pre_depends_count + pkg->depends_count + \ - pkg->recommends_count + pkg->suggests_count; - - opkg_msg(DEBUG, "pkg %s.\n", pkg->name); - - /* Iterate over all the dependencies of pkg. For each one, find a package - that is either installed or unpacked and satisfies this dependency. - (there should only be one such package per dependency installed or - unpacked). Then recurse to the dependency package */ - for (j=0; j < count ; j++) { - compound_depend = &pkg->depends[j]; - possible_satisfiers = compound_depend->possibilities; - for (k=0; k < compound_depend->possibility_count ; k++) { - abpkg = possible_satisfiers[k]->pkg; - dependents = abpkg->provided_by->pkgs; - l = 0; - if (dependents != NULL) - while (l < abpkg->provided_by->len && dependents[l] != NULL) { - opkg_msg(DEBUG, "Descending on pkg %s.\n", - dependents [l]->name); - - /* find whether dependent l is installed or unpacked, - * and then find which package in the list satisfies it */ - for(m = 0; m < all->len; m++) { - dep = all->pkgs[m]; - if ( dep->state_status != SS_NOT_INSTALLED) - if ( ! strcmp(dep->name, dependents[l]->name)) { - opkg_recurse_pkgs_in_order(dep, all, - visited, ordered); - /* Stop the outer loop */ - l = abpkg->provided_by->len; - /* break from the inner loop */ - break; - } - } - l++; - } - } - } - - /* When all recursions from this node down, are over, and all - dependencies have been added in proper order in the ordered array, add - also the package pkg to ordered array */ - pkg_vec_insert(ordered, pkg); - - return 0; + int j, k, l, m; + int count; + pkg_t *dep; + compound_depend_t *compound_depend; + depend_t **possible_satisfiers; + abstract_pkg_t *abpkg; + abstract_pkg_t **dependents; + + /* If it's just an available package, that is, not installed and not even + unpacked, skip it */ + /* XXX: This is probably an overkill, since a state_status != SS_UNPACKED + would do here. However, if there is an intermediate node (pkg) that is + configured and installed between two unpacked packages, the latter + won't be properly reordered, unless all installed/unpacked pkgs are + checked */ + if (pkg->state_status == SS_NOT_INSTALLED) + return 0; + + /* If the package has already been visited (by this function), skip it */ + for (j = 0; j < visited->len; j++) + if (!strcmp(visited->pkgs[j]->name, pkg->name)) { + opkg_msg(DEBUG, "pkg %s already visited, skipping.\n", + pkg->name); + return 0; + } + + pkg_vec_insert(visited, pkg); + + count = pkg->pre_depends_count + pkg->depends_count + + pkg->recommends_count + pkg->suggests_count; + + opkg_msg(DEBUG, "pkg %s.\n", pkg->name); + + /* Iterate over all the dependencies of pkg. For each one, find a package + that is either installed or unpacked and satisfies this dependency. + (there should only be one such package per dependency installed or + unpacked). Then recurse to the dependency package */ + for (j = 0; j < count; j++) { + compound_depend = &pkg->depends[j]; + possible_satisfiers = compound_depend->possibilities; + for (k = 0; k < compound_depend->possibility_count; k++) { + abpkg = possible_satisfiers[k]->pkg; + dependents = abpkg->provided_by->pkgs; + l = 0; + if (dependents != NULL) + while (l < abpkg->provided_by->len + && dependents[l] != NULL) { + opkg_msg(DEBUG, + "Descending on pkg %s.\n", + dependents[l]->name); + + /* find whether dependent l is installed or unpacked, + * and then find which package in the list satisfies it */ + for (m = 0; m < all->len; m++) { + dep = all->pkgs[m]; + if (dep->state_status != + SS_NOT_INSTALLED) + if (!strcmp + (dep->name, + dependents[l]-> + name)) { + opkg_recurse_pkgs_in_order + (dep, all, + visited, + ordered); + /* Stop the outer loop */ + l = abpkg-> + provided_by-> + len; + /* break from the inner loop */ + break; + } + } + l++; + } + } + } + + /* When all recursions from this node down, are over, and all + dependencies have been added in proper order in the ordered array, add + also the package pkg to ordered array */ + pkg_vec_insert(ordered, pkg); + + return 0; } -static int -opkg_configure_packages(char *pkg_name) +static int opkg_configure_packages(char *pkg_name) { - pkg_vec_t *all, *ordered, *visited; - int i; - pkg_t *pkg; - opkg_intercept_t ic; - int r, err = 0; - - opkg_msg(INFO, "Configuring unpacked packages.\n"); - - all = pkg_vec_alloc(); - - pkg_hash_fetch_available(all); - - /* Reorder pkgs in order to be configured according to the Depends: tag - order */ - opkg_msg(INFO, "Reordering packages before configuring them...\n"); - ordered = pkg_vec_alloc(); - visited = pkg_vec_alloc(); - for(i = 0; i < all->len; i++) { - pkg = all->pkgs[i]; - opkg_recurse_pkgs_in_order(pkg, all, visited, ordered); - } - - ic = opkg_prep_intercepts(); - if (ic == NULL) { - err = -1; - goto error; - } - - for(i = 0; i < ordered->len; i++) { - pkg = ordered->pkgs[i]; - - if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) - continue; - - if (pkg->state_status == SS_UNPACKED) { - opkg_msg(NOTICE, "Configuring %s.\n", pkg->name); - r = opkg_configure(pkg); - if (r == 0) { - pkg->state_status = SS_INSTALLED; - pkg->parent->state_status = SS_INSTALLED; - pkg->state_flag &= ~SF_PREFER; - opkg_state_changed++; - } else { - err = -1; - } - } - } - - if (opkg_finalize_intercepts (ic)) - err = -1; + pkg_vec_t *all, *ordered, *visited; + int i; + pkg_t *pkg; + opkg_intercept_t ic; + int r, err = 0; + + opkg_msg(INFO, "Configuring unpacked packages.\n"); + + all = pkg_vec_alloc(); + + pkg_hash_fetch_available(all); + + /* Reorder pkgs in order to be configured according to the Depends: tag + order */ + opkg_msg(INFO, "Reordering packages before configuring them...\n"); + ordered = pkg_vec_alloc(); + visited = pkg_vec_alloc(); + for (i = 0; i < all->len; i++) { + pkg = all->pkgs[i]; + opkg_recurse_pkgs_in_order(pkg, all, visited, ordered); + } + + ic = opkg_prep_intercepts(); + if (ic == NULL) { + err = -1; + goto error; + } + + for (i = 0; i < ordered->len; i++) { + pkg = ordered->pkgs[i]; + + if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) + continue; + + if (pkg->state_status == SS_UNPACKED) { + opkg_msg(NOTICE, "Configuring %s.\n", pkg->name); + r = opkg_configure(pkg); + if (r == 0) { + pkg->state_status = SS_INSTALLED; + pkg->parent->state_status = SS_INSTALLED; + pkg->state_flag &= ~SF_PREFER; + opkg_state_changed++; + } else { + err = -1; + } + } + } + + if (opkg_finalize_intercepts(ic)) + err = -1; error: - pkg_vec_free(all); - pkg_vec_free(ordered); - pkg_vec_free(visited); + pkg_vec_free(all); + pkg_vec_free(ordered); + pkg_vec_free(visited); - return err; + return err; } -static int -opkg_remove_cmd(int argc, char **argv); +static int opkg_remove_cmd(int argc, char **argv); -static int -opkg_install_cmd(int argc, char **argv) +static int opkg_install_cmd(int argc, char **argv) { - int i; - char *arg; - int err = 0; - - if (conf->force_reinstall) { - int saved_force_depends = conf->force_depends; - conf->force_depends = 1; - (void)opkg_remove_cmd(argc, argv); - conf->force_depends = saved_force_depends; - conf->force_reinstall = 0; - } - - signal(SIGINT, sigint_handler); - - /* - * Now scan through package names and install - */ - for (i=0; i < argc; i++) { - arg = argv[i]; - - opkg_msg(DEBUG2, "%s\n", arg); - if (opkg_prepare_url_for_install(arg, &argv[i])) - return -1; - } - pkg_info_preinstall_check(); - - for (i=0; i < argc; i++) { - arg = argv[i]; - if (opkg_install_by_name(arg)) { - opkg_msg(ERROR, "Cannot install package %s.\n", arg); - err = -1; - } - } - - if (opkg_configure_packages(NULL)) - err = -1; - - write_status_files_if_changed(); - - return err; + int i; + char *arg; + int err = 0; + + if (conf->force_reinstall) { + int saved_force_depends = conf->force_depends; + conf->force_depends = 1; + (void)opkg_remove_cmd(argc, argv); + conf->force_depends = saved_force_depends; + conf->force_reinstall = 0; + } + + signal(SIGINT, sigint_handler); + + /* + * Now scan through package names and install + */ + for (i = 0; i < argc; i++) { + arg = argv[i]; + + opkg_msg(DEBUG2, "%s\n", arg); + if (opkg_prepare_url_for_install(arg, &argv[i])) + return -1; + } + pkg_info_preinstall_check(); + + for (i = 0; i < argc; i++) { + arg = argv[i]; + if (opkg_install_by_name(arg)) { + opkg_msg(ERROR, "Cannot install package %s.\n", arg); + err = -1; + } + } + + if (opkg_configure_packages(NULL)) + err = -1; + + write_status_files_if_changed(); + + return err; } -static int -opkg_upgrade_cmd(int argc, char **argv) +static int opkg_upgrade_cmd(int argc, char **argv) { - int i; - pkg_t *pkg; - int err = 0; - - signal(SIGINT, sigint_handler); - - if (argc) { - for (i=0; i < argc; i++) { - char *arg = argv[i]; - - if (opkg_prepare_url_for_install(arg, &arg)) - return -1; - } - pkg_info_preinstall_check(); - - for (i=0; i < argc; i++) { - char *arg = argv[i]; - if (conf->restrict_to_default_dest) { - pkg = pkg_hash_fetch_installed_by_name_dest(argv[i], - conf->default_dest); - if (pkg == NULL) { - opkg_msg(NOTICE, "Package %s not installed in %s.\n", - argv[i], conf->default_dest->name); - continue; - } - } else { - pkg = pkg_hash_fetch_installed_by_name(argv[i]); - } - if (pkg) { - if (opkg_upgrade_pkg(pkg)) - err = -1; - } else { - if (opkg_install_by_name(arg)) - err = -1; - } - } - } - - if (opkg_configure_packages(NULL)) - err = -1; - - write_status_files_if_changed(); - - return err; + int i; + pkg_t *pkg; + int err = 0; + + signal(SIGINT, sigint_handler); + + if (argc) { + for (i = 0; i < argc; i++) { + char *arg = argv[i]; + + if (opkg_prepare_url_for_install(arg, &arg)) + return -1; + } + pkg_info_preinstall_check(); + + for (i = 0; i < argc; i++) { + char *arg = argv[i]; + if (conf->restrict_to_default_dest) { + pkg = + pkg_hash_fetch_installed_by_name_dest(argv + [i], + conf-> + default_dest); + if (pkg == NULL) { + opkg_msg(NOTICE, + "Package %s not installed in %s.\n", + argv[i], + conf->default_dest->name); + continue; + } + } else { + pkg = pkg_hash_fetch_installed_by_name(argv[i]); + } + if (pkg) { + if (opkg_upgrade_pkg(pkg)) + err = -1; + } else { + if (opkg_install_by_name(arg)) + err = -1; + } + } + } + + if (opkg_configure_packages(NULL)) + err = -1; + + write_status_files_if_changed(); + + return err; } -static int -opkg_download_cmd(int argc, char **argv) +static int opkg_download_cmd(int argc, char **argv) { - int i, err = 0; - char *arg; - pkg_t *pkg; - - pkg_info_preinstall_check(); - for (i = 0; i < argc; i++) { - arg = argv[i]; - - pkg = pkg_hash_fetch_best_installation_candidate_by_name(arg); - if (pkg == NULL) { - opkg_msg(ERROR, "Cannot find package %s.\n", arg); - continue; - } - - if (opkg_download_pkg(pkg, ".")) - err = -1; - - if (err) { - opkg_msg(ERROR, "Failed to download %s.\n", pkg->name); - } else { - opkg_msg(NOTICE, "Downloaded %s as %s.\n", - pkg->name, pkg->local_filename); - } - } - - return err; -} + int i, err = 0; + char *arg; + pkg_t *pkg; + pkg_info_preinstall_check(); + for (i = 0; i < argc; i++) { + arg = argv[i]; -static int -opkg_list_find_cmd(int argc, char **argv, int use_desc) + pkg = pkg_hash_fetch_best_installation_candidate_by_name(arg); + if (pkg == NULL) { + opkg_msg(ERROR, "Cannot find package %s.\n", arg); + continue; + } + + if (opkg_download_pkg(pkg, ".")) + err = -1; + + if (err) { + opkg_msg(ERROR, "Failed to download %s.\n", pkg->name); + } else { + opkg_msg(NOTICE, "Downloaded %s as %s.\n", + pkg->name, pkg->local_filename); + } + } + + return err; +} + +static int opkg_list_find_cmd(int argc, char **argv, int use_desc) { - int i; - pkg_vec_t *available; - pkg_t *pkg; - char *pkg_name = NULL; - - if (argc > 0) { - pkg_name = argv[0]; - } - available = pkg_vec_alloc(); - pkg_hash_fetch_available(available); - pkg_vec_sort(available, pkg_compare_names); - for (i=0; i < available->len; i++) { - pkg = available->pkgs[i]; - /* if we have package name or pattern and pkg does not match, then skip it */ - if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase) && - (!use_desc || !pkg->description || fnmatch(pkg_name, pkg->description, conf->nocase))) - continue; - print_pkg(pkg); - } - pkg_vec_free(available); - - return 0; + int i; + pkg_vec_t *available; + pkg_t *pkg; + char *pkg_name = NULL; + + if (argc > 0) { + pkg_name = argv[0]; + } + available = pkg_vec_alloc(); + pkg_hash_fetch_available(available); + pkg_vec_sort(available, pkg_compare_names); + for (i = 0; i < available->len; i++) { + pkg = available->pkgs[i]; + /* if we have package name or pattern and pkg does not match, then skip it */ + if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase) && + (!use_desc || !pkg->description + || fnmatch(pkg_name, pkg->description, conf->nocase))) + continue; + print_pkg(pkg); + } + pkg_vec_free(available); + + return 0; } -static int -opkg_list_cmd(int argc, char **argv) +static int opkg_list_cmd(int argc, char **argv) { return opkg_list_find_cmd(argc, argv, 0); } -static int -opkg_find_cmd(int argc, char **argv) +static int opkg_find_cmd(int argc, char **argv) { return opkg_list_find_cmd(argc, argv, 1); } - -static int -opkg_list_installed_cmd(int argc, char **argv) +static int opkg_list_installed_cmd(int argc, char **argv) { - int i ; - pkg_vec_t *available; - pkg_t *pkg; - char *pkg_name = NULL; - - if (argc > 0) { - pkg_name = argv[0]; - } - available = pkg_vec_alloc(); - pkg_hash_fetch_all_installed(available); - pkg_vec_sort(available, pkg_compare_names); - for (i=0; i < available->len; i++) { - pkg = available->pkgs[i]; - /* if we have package name or pattern and pkg does not match, then skip it */ - if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) - continue; - print_pkg(pkg); - } - - pkg_vec_free(available); - - return 0; + int i; + pkg_vec_t *available; + pkg_t *pkg; + char *pkg_name = NULL; + + if (argc > 0) { + pkg_name = argv[0]; + } + available = pkg_vec_alloc(); + pkg_hash_fetch_all_installed(available); + pkg_vec_sort(available, pkg_compare_names); + for (i = 0; i < available->len; i++) { + pkg = available->pkgs[i]; + /* if we have package name or pattern and pkg does not match, then skip it */ + if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) + continue; + print_pkg(pkg); + } + + pkg_vec_free(available); + + return 0; } -static int -opkg_list_changed_conffiles_cmd(int argc, char **argv) +static int opkg_list_changed_conffiles_cmd(int argc, char **argv) { - int i ; - pkg_vec_t *available; - pkg_t *pkg; - char *pkg_name = NULL; - conffile_list_elt_t *iter; - conffile_t *cf; - - if (argc > 0) { - pkg_name = argv[0]; - } - available = pkg_vec_alloc(); - pkg_hash_fetch_all_installed(available); - pkg_vec_sort(available, pkg_compare_names); - for (i=0; i < available->len; i++) { - pkg = available->pkgs[i]; - /* if we have package name or pattern and pkg does not match, then skip it */ - if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) - continue; - if (nv_pair_list_empty(&pkg->conffiles)) - continue; - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - cf = (conffile_t *)iter->data; - if (cf->name && cf->value && conffile_has_been_modified(cf)) - printf("%s\n", cf->name); - } - } - pkg_vec_free(available); - return 0; + int i; + pkg_vec_t *available; + pkg_t *pkg; + char *pkg_name = NULL; + conffile_list_elt_t *iter; + conffile_t *cf; + + if (argc > 0) { + pkg_name = argv[0]; + } + available = pkg_vec_alloc(); + pkg_hash_fetch_all_installed(available); + pkg_vec_sort(available, pkg_compare_names); + for (i = 0; i < available->len; i++) { + pkg = available->pkgs[i]; + /* if we have package name or pattern and pkg does not match, then skip it */ + if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) + continue; + if (nv_pair_list_empty(&pkg->conffiles)) + continue; + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + cf = (conffile_t *) iter->data; + if (cf->name && cf->value + && conffile_has_been_modified(cf)) + printf("%s\n", cf->name); + } + } + pkg_vec_free(available); + return 0; } -static int -opkg_list_upgradable_cmd(int argc, char **argv) +static int opkg_list_upgradable_cmd(int argc, char **argv) { - struct active_list *head = prepare_upgrade_list(); - struct active_list *node=NULL; - pkg_t *_old_pkg, *_new_pkg; - char *old_v, *new_v; - for (node = active_list_next(head, head); node;node = active_list_next(head,node)) { - _old_pkg = list_entry(node, pkg_t, list); - _new_pkg = pkg_hash_fetch_best_installation_candidate_by_name(_old_pkg->name); - if (_new_pkg == NULL) - continue; - old_v = pkg_version_str_alloc(_old_pkg); - new_v = pkg_version_str_alloc(_new_pkg); - printf("%s - %s - %s\n", _old_pkg->name, old_v, new_v); - free(old_v); - free(new_v); - } - active_list_head_delete(head); - return 0; + struct active_list *head = prepare_upgrade_list(); + struct active_list *node = NULL; + pkg_t *_old_pkg, *_new_pkg; + char *old_v, *new_v; + for (node = active_list_next(head, head); node; + node = active_list_next(head, node)) { + _old_pkg = list_entry(node, pkg_t, list); + _new_pkg = + pkg_hash_fetch_best_installation_candidate_by_name + (_old_pkg->name); + if (_new_pkg == NULL) + continue; + old_v = pkg_version_str_alloc(_old_pkg); + new_v = pkg_version_str_alloc(_new_pkg); + printf("%s - %s - %s\n", _old_pkg->name, old_v, new_v); + free(old_v); + free(new_v); + } + active_list_head_delete(head); + return 0; } -static int -opkg_info_status_cmd(int argc, char **argv, int installed_only) +static int opkg_info_status_cmd(int argc, char **argv, int installed_only) { - int i; - pkg_vec_t *available; - pkg_t *pkg; - char *pkg_name = NULL; - - if (argc > 0) { - pkg_name = argv[0]; - } - - available = pkg_vec_alloc(); - if (installed_only) - pkg_hash_fetch_all_installed(available); - else - pkg_hash_fetch_available(available); - - for (i=0; i < available->len; i++) { - pkg = available->pkgs[i]; - if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) { - continue; - } - - pkg_formatted_info(stdout, pkg); - - if (conf->verbosity >= NOTICE) { - conffile_list_elt_t *iter; - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - conffile_t *cf = (conffile_t *)iter->data; - int modified = conffile_has_been_modified(cf); - if (cf->value) - opkg_msg(INFO, "conffile=%s md5sum=%s modified=%d.\n", - cf->name, cf->value, modified); - } - } - } - pkg_vec_free(available); - - return 0; + int i; + pkg_vec_t *available; + pkg_t *pkg; + char *pkg_name = NULL; + + if (argc > 0) { + pkg_name = argv[0]; + } + + available = pkg_vec_alloc(); + if (installed_only) + pkg_hash_fetch_all_installed(available); + else + pkg_hash_fetch_available(available); + + for (i = 0; i < available->len; i++) { + pkg = available->pkgs[i]; + if (pkg_name && fnmatch(pkg_name, pkg->name, conf->nocase)) { + continue; + } + + pkg_formatted_info(stdout, pkg); + + if (conf->verbosity >= NOTICE) { + conffile_list_elt_t *iter; + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + conffile_t *cf = (conffile_t *) iter->data; + int modified = conffile_has_been_modified(cf); + if (cf->value) + opkg_msg(INFO, + "conffile=%s md5sum=%s modified=%d.\n", + cf->name, cf->value, modified); + } + } + } + pkg_vec_free(available); + + return 0; } -static int -opkg_info_cmd(int argc, char **argv) +static int opkg_info_cmd(int argc, char **argv) { - return opkg_info_status_cmd(argc, argv, 0); + return opkg_info_status_cmd(argc, argv, 0); } -static int -opkg_status_cmd(int argc, char **argv) +static int opkg_status_cmd(int argc, char **argv) { - return opkg_info_status_cmd(argc, argv, 1); + return opkg_info_status_cmd(argc, argv, 1); } -static int -opkg_configure_cmd(int argc, char **argv) +static int opkg_configure_cmd(int argc, char **argv) { int err; char *pkg_name = NULL; @@ -735,140 +749,149 @@ opkg_configure_cmd(int argc, char **argv) return err; } -static int -opkg_remove_cmd(int argc, char **argv) +static int opkg_remove_cmd(int argc, char **argv) { - int i, a, done, err = 0; - pkg_t *pkg; - pkg_t *pkg_to_remove; - pkg_vec_t *available; - - done = 0; - - signal(SIGINT, sigint_handler); - - pkg_info_preinstall_check(); - - available = pkg_vec_alloc(); - pkg_hash_fetch_all_installed(available); - - for (i=0; ilen; a++) { - pkg = available->pkgs[a]; - if (fnmatch(argv[i], pkg->name, conf->nocase)) { - continue; - } - if (conf->restrict_to_default_dest) { - pkg_to_remove = pkg_hash_fetch_installed_by_name_dest( - pkg->name, - conf->default_dest); - } else { - pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name); - } - - if (pkg_to_remove == NULL) { - opkg_msg(ERROR, "Package %s is not installed.\n", pkg->name); - continue; - } - if (pkg->state_status == SS_NOT_INSTALLED) { - opkg_msg(ERROR, "Package %s not installed.\n", pkg->name); - continue; - } - - if (opkg_remove_pkg(pkg_to_remove, 0)) - err = -1; - else - done = 1; - } - } - - pkg_vec_free(available); - - if (done == 0) - opkg_msg(NOTICE, "No packages removed.\n"); - - write_status_files_if_changed(); - return err; + int i, a, done, err = 0; + pkg_t *pkg; + pkg_t *pkg_to_remove; + pkg_vec_t *available; + + done = 0; + + signal(SIGINT, sigint_handler); + + pkg_info_preinstall_check(); + + available = pkg_vec_alloc(); + pkg_hash_fetch_all_installed(available); + + for (i = 0; i < argc; i++) { + for (a = 0; a < available->len; a++) { + pkg = available->pkgs[a]; + if (fnmatch(argv[i], pkg->name, conf->nocase)) { + continue; + } + if (conf->restrict_to_default_dest) { + pkg_to_remove = + pkg_hash_fetch_installed_by_name_dest(pkg-> + name, + conf-> + default_dest); + } else { + pkg_to_remove = + pkg_hash_fetch_installed_by_name(pkg->name); + } + + if (pkg_to_remove == NULL) { + opkg_msg(ERROR, + "Package %s is not installed.\n", + pkg->name); + continue; + } + if (pkg->state_status == SS_NOT_INSTALLED) { + opkg_msg(ERROR, "Package %s not installed.\n", + pkg->name); + continue; + } + + if (opkg_remove_pkg(pkg_to_remove, 0)) + err = -1; + else + done = 1; + } + } + + pkg_vec_free(available); + + if (done == 0) + opkg_msg(NOTICE, "No packages removed.\n"); + + write_status_files_if_changed(); + return err; } -static int -opkg_flag_cmd(int argc, char **argv) +static int opkg_flag_cmd(int argc, char **argv) { - int i; - pkg_t *pkg; - const char *flags = argv[0]; - - signal(SIGINT, sigint_handler); - - for (i=1; i < argc; i++) { - if (conf->restrict_to_default_dest) { - pkg = pkg_hash_fetch_installed_by_name_dest(argv[i], - conf->default_dest); - } else { - pkg = pkg_hash_fetch_installed_by_name(argv[i]); - } - - if (pkg == NULL) { - opkg_msg(ERROR, "Package %s is not installed.\n", argv[i]); - continue; - } - if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)|| - ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) { - pkg->state_flag = pkg_state_flag_from_str(flags); - } - - /* - * Useful if a package is installed in an offline_root, and - * should be configured by opkg-cl configure at a later date. - */ - if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){ - pkg->state_status = pkg_state_status_from_str(flags); - } - - opkg_state_changed++; - opkg_msg(NOTICE, "Setting flags for package %s to %s.\n", - pkg->name, flags); - } - - write_status_files_if_changed(); - return 0; + int i; + pkg_t *pkg; + const char *flags = argv[0]; + + signal(SIGINT, sigint_handler); + + for (i = 1; i < argc; i++) { + if (conf->restrict_to_default_dest) { + pkg = pkg_hash_fetch_installed_by_name_dest(argv[i], + conf-> + default_dest); + } else { + pkg = pkg_hash_fetch_installed_by_name(argv[i]); + } + + if (pkg == NULL) { + opkg_msg(ERROR, "Package %s is not installed.\n", + argv[i]); + continue; + } + if ((strcmp(flags, "hold") == 0) + || (strcmp(flags, "noprune") == 0) + || (strcmp(flags, "user") == 0) + || (strcmp(flags, "ok") == 0)) { + pkg->state_flag = pkg_state_flag_from_str(flags); + } + + /* + * Useful if a package is installed in an offline_root, and + * should be configured by opkg-cl configure at a later date. + */ + if ((strcmp(flags, "installed") == 0) + || (strcmp(flags, "unpacked") == 0)) { + pkg->state_status = pkg_state_status_from_str(flags); + } + + opkg_state_changed++; + opkg_msg(NOTICE, "Setting flags for package %s to %s.\n", + pkg->name, flags); + } + + write_status_files_if_changed(); + return 0; } -static int -opkg_files_cmd(int argc, char **argv) +static int opkg_files_cmd(int argc, char **argv) { - pkg_t *pkg; - str_list_t *files; - str_list_elt_t *iter; - char *pkg_version; + pkg_t *pkg; + str_list_t *files; + str_list_elt_t *iter; + char *pkg_version; - if (argc < 1) { - return -1; - } + if (argc < 1) { + return -1; + } - pkg = pkg_hash_fetch_installed_by_name(argv[0]); - if (pkg == NULL) { - opkg_msg(ERROR, "Package %s not installed.\n", argv[0]); - return 0; - } + pkg = pkg_hash_fetch_installed_by_name(argv[0]); + if (pkg == NULL) { + opkg_msg(ERROR, "Package %s not installed.\n", argv[0]); + return 0; + } - files = pkg_get_installed_files(pkg); - pkg_version = pkg_version_str_alloc(pkg); + files = pkg_get_installed_files(pkg); + pkg_version = pkg_version_str_alloc(pkg); - printf("Package %s (%s) is installed on %s and has the following files:\n", - pkg->name, pkg_version, pkg->dest->name); + printf + ("Package %s (%s) is installed on %s and has the following files:\n", + pkg->name, pkg_version, pkg->dest->name); - for (iter=str_list_first(files); iter; iter=str_list_next(files, iter)) - printf("%s\n", (char *)iter->data); + for (iter = str_list_first(files); iter; + iter = str_list_next(files, iter)) + printf("%s\n", (char *)iter->data); - free(pkg_version); - pkg_free_installed_files(pkg); + free(pkg_version); + pkg_free_installed_files(pkg); - return 0; + return 0; } -static int -opkg_depends_cmd(int argc, char **argv) +static int opkg_depends_cmd(int argc, char **argv) { int i, j, k; int depends_count; @@ -881,66 +904,65 @@ opkg_depends_cmd(int argc, char **argv) available_pkgs = pkg_vec_alloc(); if (conf->query_all) - pkg_hash_fetch_available(available_pkgs); + pkg_hash_fetch_available(available_pkgs); else - pkg_hash_fetch_all_installed(available_pkgs); + pkg_hash_fetch_all_installed(available_pkgs); - for (i=0; ilen; j++) { + for (i = 0; i < argc; i++) { + for (j = 0; j < available_pkgs->len; j++) { pkg = available_pkgs->pkgs[j]; if (fnmatch(argv[i], pkg->name, conf->nocase) != 0) continue; depends_count = pkg->depends_count + - pkg->pre_depends_count + - pkg->recommends_count + - pkg->suggests_count; + pkg->pre_depends_count + + pkg->recommends_count + pkg->suggests_count; opkg_msg(NOTICE, "%s depends on:\n", pkg->name); - for (k=0; kdepends[k]; if (cdep->type != DEPEND) - continue; + continue; str = pkg_depend_str(pkg, k); opkg_msg(NOTICE, "\t%s\n", str); free(str); } - } + } } pkg_vec_free(available_pkgs); return 0; } -static int -pkg_mark_provides(pkg_t *pkg) +static int pkg_mark_provides(pkg_t * pkg) { - int provides_count = pkg->provides_count; - abstract_pkg_t **provides = pkg->provides; - int i; - pkg->parent->state_flag |= SF_MARKED; - for (i = 0; i < provides_count; i++) { - provides[i]->state_flag |= SF_MARKED; - } - return 0; + int provides_count = pkg->provides_count; + abstract_pkg_t **provides = pkg->provides; + int i; + pkg->parent->state_flag |= SF_MARKED; + for (i = 0; i < provides_count; i++) { + provides[i]->state_flag |= SF_MARKED; + } + return 0; } enum what_field_type { - WHATDEPENDS, - WHATCONFLICTS, - WHATPROVIDES, - WHATREPLACES, - WHATRECOMMENDS, - WHATSUGGESTS + WHATDEPENDS, + WHATCONFLICTS, + WHATPROVIDES, + WHATREPLACES, + WHATRECOMMENDS, + WHATSUGGESTS }; static int -opkg_what_depends_conflicts_cmd(enum depend_type what_field_type, int recursive, int argc, char **argv) +opkg_what_depends_conflicts_cmd(enum depend_type what_field_type, int recursive, + int argc, char **argv) { depend_t *possibility; compound_depend_t *cdep; @@ -952,90 +974,101 @@ opkg_what_depends_conflicts_cmd(enum depend_type what_field_type, int recursive, char *ver; switch (what_field_type) { - case DEPEND: rel_str = "depends on"; break; - case CONFLICTS: rel_str = "conflicts with"; break; - case SUGGEST: rel_str = "suggests"; break; - case RECOMMEND: rel_str = "recommends"; break; - default: return -1; + case DEPEND: + rel_str = "depends on"; + break; + case CONFLICTS: + rel_str = "conflicts with"; + break; + case SUGGEST: + rel_str = "suggests"; + break; + case RECOMMEND: + rel_str = "recommends"; + break; + default: + return -1; } available_pkgs = pkg_vec_alloc(); if (conf->query_all) - pkg_hash_fetch_available(available_pkgs); + pkg_hash_fetch_available(available_pkgs); else - pkg_hash_fetch_all_installed(available_pkgs); + pkg_hash_fetch_all_installed(available_pkgs); /* mark the root set */ pkg_vec_clear_marks(available_pkgs); opkg_msg(NOTICE, "Root set:\n"); for (i = 0; i < argc; i++) - pkg_vec_mark_if_matches(available_pkgs, argv[i]); + pkg_vec_mark_if_matches(available_pkgs, argv[i]); for (i = 0; i < available_pkgs->len; i++) { - pkg = available_pkgs->pkgs[i]; - if (pkg->state_flag & SF_MARKED) { - /* mark the parent (abstract) package */ - pkg_mark_provides(pkg); - opkg_msg(NOTICE, " %s\n", pkg->name); - } + pkg = available_pkgs->pkgs[i]; + if (pkg->state_flag & SF_MARKED) { + /* mark the parent (abstract) package */ + pkg_mark_provides(pkg); + opkg_msg(NOTICE, " %s\n", pkg->name); + } } opkg_msg(NOTICE, "What %s root set\n", rel_str); do { changed = 0; - for (j=0; jlen; j++) { + for (j = 0; j < available_pkgs->len; j++) { pkg = available_pkgs->pkgs[j]; count = ((what_field_type == CONFLICTS) ? pkg->conflicts_count : pkg->pre_depends_count + pkg->depends_count + - pkg->recommends_count + - pkg->suggests_count); + pkg->recommends_count + pkg->suggests_count); /* skip this package if it is already marked */ if (pkg->parent->state_flag & SF_MARKED) continue; - for (k=0; kconflicts[k] - : &pkg->depends[k]; + ? &pkg->conflicts[k] + : &pkg->depends[k]; if (what_field_type != cdep->type) continue; - for (l=0; lpossibility_count; l++) { + for (l = 0; l < cdep->possibility_count; l++) { possibility = cdep->possibilities[l]; if ((possibility->pkg->state_flag - & SF_MARKED) - != SF_MARKED) + & SF_MARKED) + != SF_MARKED) continue; /* mark the depending package so we - * won't visit it again */ + * won't visit it again */ pkg->state_flag |= SF_MARKED; pkg_mark_provides(pkg); changed++; ver = pkg_version_str_alloc(pkg); - opkg_msg(NOTICE, "\t%s %s\t%s %s", - pkg->name, - ver, - rel_str, - possibility->pkg->name); + opkg_msg(NOTICE, "\t%s %s\t%s %s", + pkg->name, + ver, + rel_str, + possibility->pkg->name); free(ver); if (possibility->version) { opkg_msg(NOTICE, " (%s%s)", - constraint_to_str(possibility->constraint), - possibility->version); + constraint_to_str + (possibility-> + constraint), + possibility->version); } - if (!pkg_dependence_satisfiable(possibility)) + if (!pkg_dependence_satisfiable + (possibility)) opkg_msg(NOTICE, - " unsatisfiable"); + " unsatisfiable"); opkg_message(NOTICE, "\n"); goto next_package; } @@ -1050,207 +1083,242 @@ next_package: return 0; } -static int -opkg_whatdepends_recursively_cmd(int argc, char **argv) +static int opkg_whatdepends_recursively_cmd(int argc, char **argv) { - return opkg_what_depends_conflicts_cmd(DEPEND, 1, argc, argv); + return opkg_what_depends_conflicts_cmd(DEPEND, 1, argc, argv); } -static int -opkg_whatdepends_cmd(int argc, char **argv) +static int opkg_whatdepends_cmd(int argc, char **argv) { - return opkg_what_depends_conflicts_cmd(DEPEND, 0, argc, argv); + return opkg_what_depends_conflicts_cmd(DEPEND, 0, argc, argv); } -static int -opkg_whatsuggests_cmd(int argc, char **argv) +static int opkg_whatsuggests_cmd(int argc, char **argv) { - return opkg_what_depends_conflicts_cmd(SUGGEST, 0, argc, argv); + return opkg_what_depends_conflicts_cmd(SUGGEST, 0, argc, argv); } -static int -opkg_whatrecommends_cmd(int argc, char **argv) +static int opkg_whatrecommends_cmd(int argc, char **argv) { - return opkg_what_depends_conflicts_cmd(RECOMMEND, 0, argc, argv); + return opkg_what_depends_conflicts_cmd(RECOMMEND, 0, argc, argv); } -static int -opkg_whatconflicts_cmd(int argc, char **argv) +static int opkg_whatconflicts_cmd(int argc, char **argv) { - return opkg_what_depends_conflicts_cmd(CONFLICTS, 0, argc, argv); + return opkg_what_depends_conflicts_cmd(CONFLICTS, 0, argc, argv); } static int -opkg_what_provides_replaces_cmd(enum what_field_type what_field_type, int argc, char **argv) +opkg_what_provides_replaces_cmd(enum what_field_type what_field_type, int argc, + char **argv) { - if (argc > 0) { - pkg_vec_t *available_pkgs = pkg_vec_alloc(); - const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces"); - int i; - - pkg_info_preinstall_check(); - - if (conf->query_all) - pkg_hash_fetch_available(available_pkgs); - else - pkg_hash_fetch_all_installed(available_pkgs); - for (i = 0; i < argc; i++) { - const char *target = argv[i]; - int j; - - opkg_msg(NOTICE, "What %s %s\n", - rel_str, target); - for (j = 0; j < available_pkgs->len; j++) { - pkg_t *pkg = available_pkgs->pkgs[j]; - int k; - int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count; - for (k = 0; k < count; k++) { - abstract_pkg_t *apkg = - ((what_field_type == WHATPROVIDES) - ? pkg->provides[k] - : pkg->replaces[k]); - if (fnmatch(target, apkg->name, conf->nocase) == 0) { - opkg_msg(NOTICE, " %s", pkg->name); - if ((conf->nocase ? strcasecmp(target, apkg->name) : strcmp(target, apkg->name)) != 0) - opkg_msg(NOTICE, "\t%s %s\n", - rel_str, apkg->name); - opkg_message(NOTICE, "\n"); - } - } - } - } - pkg_vec_free(available_pkgs); - } - return 0; + if (argc > 0) { + pkg_vec_t *available_pkgs = pkg_vec_alloc(); + const char *rel_str = + (what_field_type == WHATPROVIDES ? "provides" : "replaces"); + int i; + + pkg_info_preinstall_check(); + + if (conf->query_all) + pkg_hash_fetch_available(available_pkgs); + else + pkg_hash_fetch_all_installed(available_pkgs); + for (i = 0; i < argc; i++) { + const char *target = argv[i]; + int j; + + opkg_msg(NOTICE, "What %s %s\n", rel_str, target); + for (j = 0; j < available_pkgs->len; j++) { + pkg_t *pkg = available_pkgs->pkgs[j]; + int k; + int count = + (what_field_type == + WHATPROVIDES) ? pkg->provides_count : pkg-> + replaces_count; + for (k = 0; k < count; k++) { + abstract_pkg_t *apkg = + ((what_field_type == WHATPROVIDES) + ? pkg->provides[k] + : pkg->replaces[k]); + if (fnmatch + (target, apkg->name, + conf->nocase) == 0) { + opkg_msg(NOTICE, " %s", + pkg->name); + if ((conf-> + nocase ? strcasecmp(target, + apkg-> + name) : + strcmp(target, + apkg->name)) != 0) + opkg_msg(NOTICE, + "\t%s %s\n", + rel_str, + apkg->name); + opkg_message(NOTICE, "\n"); + } + } + } + } + pkg_vec_free(available_pkgs); + } + return 0; } -static int -opkg_whatprovides_cmd(int argc, char **argv) +static int opkg_whatprovides_cmd(int argc, char **argv) { - return opkg_what_provides_replaces_cmd(WHATPROVIDES, argc, argv); + return opkg_what_provides_replaces_cmd(WHATPROVIDES, argc, argv); } -static int -opkg_whatreplaces_cmd(int argc, char **argv) +static int opkg_whatreplaces_cmd(int argc, char **argv) { - return opkg_what_provides_replaces_cmd(WHATREPLACES, argc, argv); + return opkg_what_provides_replaces_cmd(WHATREPLACES, argc, argv); } -static int -opkg_search_cmd(int argc, char **argv) +static int opkg_search_cmd(int argc, char **argv) { - int i; + int i; - pkg_vec_t *installed; - pkg_t *pkg; - str_list_t *installed_files; - str_list_elt_t *iter; - char *installed_file; + pkg_vec_t *installed; + pkg_t *pkg; + str_list_t *installed_files; + str_list_elt_t *iter; + char *installed_file; - if (argc < 1) { - return -1; - } + if (argc < 1) { + return -1; + } - installed = pkg_vec_alloc(); - pkg_hash_fetch_all_installed(installed); - pkg_vec_sort(installed, pkg_compare_names); + installed = pkg_vec_alloc(); + pkg_hash_fetch_all_installed(installed); + pkg_vec_sort(installed, pkg_compare_names); - for (i=0; i < installed->len; i++) { - pkg = installed->pkgs[i]; + for (i = 0; i < installed->len; i++) { + pkg = installed->pkgs[i]; - installed_files = pkg_get_installed_files(pkg); + installed_files = pkg_get_installed_files(pkg); - for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) { - installed_file = (char *)iter->data; - if (fnmatch(argv[0], installed_file, conf->nocase)==0) - print_pkg(pkg); - } + for (iter = str_list_first(installed_files); iter; + iter = str_list_next(installed_files, iter)) { + installed_file = (char *)iter->data; + if (fnmatch(argv[0], installed_file, conf->nocase) == 0) + print_pkg(pkg); + } - pkg_free_installed_files(pkg); - } + pkg_free_installed_files(pkg); + } - pkg_vec_free(installed); + pkg_vec_free(installed); - return 0; + return 0; } -static int -opkg_compare_versions_cmd(int argc, char **argv) +static int opkg_compare_versions_cmd(int argc, char **argv) { - if (argc == 3) { - /* this is a bit gross */ - struct pkg p1, p2; - parse_version(&p1, argv[0]); - parse_version(&p2, argv[2]); - return pkg_version_satisfied(&p1, &p2, argv[1]); - } else { - opkg_msg(ERROR, - "opkg compare_versions \n" - " is one of <= >= << >> =\n"); - return -1; - } + if (argc == 3) { + /* this is a bit gross */ + struct pkg p1, p2; + parse_version(&p1, argv[0]); + parse_version(&p2, argv[2]); + return pkg_version_satisfied(&p1, &p2, argv[1]); + } else { + opkg_msg(ERROR, + "opkg compare_versions \n" + " is one of <= >= << >> =\n"); + return -1; + } } -static int -opkg_print_architecture_cmd(int argc, char **argv) +static int opkg_print_architecture_cmd(int argc, char **argv) { - nv_pair_list_elt_t *l; + nv_pair_list_elt_t *l; - list_for_each_entry(l, &conf->arch_list.head, node) { - nv_pair_t *nv = (nv_pair_t *)l->data; - printf("arch %s %s\n", nv->name, nv->value); - } - return 0; + list_for_each_entry(l, &conf->arch_list.head, node) { + nv_pair_t *nv = (nv_pair_t *) l->data; + printf("arch %s %s\n", nv->name, nv->value); + } + return 0; } - /* XXX: CLEANUP: The usage strings should be incorporated into this array for easier maintenance */ static opkg_cmd_t cmds[] = { - {"update", 0, (opkg_cmd_fun_t)opkg_update_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"upgrade", 1, (opkg_cmd_fun_t)opkg_upgrade_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"list", 0, (opkg_cmd_fun_t)opkg_list_cmd, PFM_SOURCE}, - {"list_installed", 0, (opkg_cmd_fun_t)opkg_list_installed_cmd, PFM_SOURCE}, - {"list-installed", 0, (opkg_cmd_fun_t)opkg_list_installed_cmd, PFM_SOURCE}, - {"list_upgradable", 0, (opkg_cmd_fun_t)opkg_list_upgradable_cmd, PFM_SOURCE}, - {"list-upgradable", 0, (opkg_cmd_fun_t)opkg_list_upgradable_cmd, PFM_SOURCE}, - {"list_changed_conffiles", 0, (opkg_cmd_fun_t)opkg_list_changed_conffiles_cmd, PFM_SOURCE}, - {"list-changed-conffiles", 0, (opkg_cmd_fun_t)opkg_list_changed_conffiles_cmd, PFM_SOURCE}, - {"info", 0, (opkg_cmd_fun_t)opkg_info_cmd, 0}, - {"flag", 1, (opkg_cmd_fun_t)opkg_flag_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"status", 0, (opkg_cmd_fun_t)opkg_status_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"install", 1, (opkg_cmd_fun_t)opkg_install_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"remove", 1, (opkg_cmd_fun_t)opkg_remove_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"configure", 0, (opkg_cmd_fun_t)opkg_configure_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"files", 1, (opkg_cmd_fun_t)opkg_files_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"search", 1, (opkg_cmd_fun_t)opkg_search_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"find", 1, (opkg_cmd_fun_t)opkg_find_cmd, PFM_SOURCE}, - {"download", 1, (opkg_cmd_fun_t)opkg_download_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"compare_versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"compare-versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"print-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"print_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"print-installation-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"print_installation_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"depends", 1, (opkg_cmd_fun_t)opkg_depends_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatdepends", 1, (opkg_cmd_fun_t)opkg_whatdepends_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatdependsrec", 1, (opkg_cmd_fun_t)opkg_whatdepends_recursively_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatrecommends", 1, (opkg_cmd_fun_t)opkg_whatrecommends_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatsuggests", 1, (opkg_cmd_fun_t)opkg_whatsuggests_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatprovides", 1, (opkg_cmd_fun_t)opkg_whatprovides_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatreplaces", 1, (opkg_cmd_fun_t)opkg_whatreplaces_cmd, PFM_DESCRIPTION|PFM_SOURCE}, - {"whatconflicts", 1, (opkg_cmd_fun_t)opkg_whatconflicts_cmd, PFM_DESCRIPTION|PFM_SOURCE}, + {"update", 0, (opkg_cmd_fun_t) opkg_update_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"upgrade", 1, (opkg_cmd_fun_t) opkg_upgrade_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"list", 0, (opkg_cmd_fun_t) opkg_list_cmd, PFM_SOURCE}, + {"list_installed", 0, (opkg_cmd_fun_t) opkg_list_installed_cmd, + PFM_SOURCE}, + {"list-installed", 0, (opkg_cmd_fun_t) opkg_list_installed_cmd, + PFM_SOURCE}, + {"list_upgradable", 0, (opkg_cmd_fun_t) opkg_list_upgradable_cmd, + PFM_SOURCE}, + {"list-upgradable", 0, (opkg_cmd_fun_t) opkg_list_upgradable_cmd, + PFM_SOURCE}, + {"list_changed_conffiles", 0, + (opkg_cmd_fun_t) opkg_list_changed_conffiles_cmd, PFM_SOURCE}, + {"list-changed-conffiles", 0, + (opkg_cmd_fun_t) opkg_list_changed_conffiles_cmd, PFM_SOURCE}, + {"info", 0, (opkg_cmd_fun_t) opkg_info_cmd, 0}, + {"flag", 1, (opkg_cmd_fun_t) opkg_flag_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"status", 0, (opkg_cmd_fun_t) opkg_status_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"install", 1, (opkg_cmd_fun_t) opkg_install_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"remove", 1, (opkg_cmd_fun_t) opkg_remove_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"configure", 0, (opkg_cmd_fun_t) opkg_configure_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"files", 1, (opkg_cmd_fun_t) opkg_files_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"search", 1, (opkg_cmd_fun_t) opkg_search_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"find", 1, (opkg_cmd_fun_t) opkg_find_cmd, PFM_SOURCE}, + {"download", 1, (opkg_cmd_fun_t) opkg_download_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"compare_versions", 1, (opkg_cmd_fun_t) opkg_compare_versions_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"compare-versions", 1, (opkg_cmd_fun_t) opkg_compare_versions_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"print-architecture", 0, (opkg_cmd_fun_t) opkg_print_architecture_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"print_architecture", 0, (opkg_cmd_fun_t) opkg_print_architecture_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"print-installation-architecture", 0, + (opkg_cmd_fun_t) opkg_print_architecture_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"print_installation_architecture", 0, + (opkg_cmd_fun_t) opkg_print_architecture_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"depends", 1, (opkg_cmd_fun_t) opkg_depends_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatdepends", 1, (opkg_cmd_fun_t) opkg_whatdepends_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatdependsrec", 1, (opkg_cmd_fun_t) opkg_whatdepends_recursively_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatrecommends", 1, (opkg_cmd_fun_t) opkg_whatrecommends_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatsuggests", 1, (opkg_cmd_fun_t) opkg_whatsuggests_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatprovides", 1, (opkg_cmd_fun_t) opkg_whatprovides_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatreplaces", 1, (opkg_cmd_fun_t) opkg_whatreplaces_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, + {"whatconflicts", 1, (opkg_cmd_fun_t) opkg_whatconflicts_cmd, + PFM_DESCRIPTION | PFM_SOURCE}, }; -opkg_cmd_t * -opkg_cmd_find(const char *name) +opkg_cmd_t *opkg_cmd_find(const char *name) { int i; opkg_cmd_t *cmd; int num_cmds = sizeof(cmds) / sizeof(opkg_cmd_t); - for (i=0; i < num_cmds; i++) { + for (i = 0; i < num_cmds; i++) { cmd = &cmds[i]; if (strcmp(name, cmd->name) == 0) return cmd; @@ -1259,8 +1327,7 @@ opkg_cmd_find(const char *name) return NULL; } -int -opkg_cmd_exec(opkg_cmd_t *cmd, int argc, const char **argv) +int opkg_cmd_exec(opkg_cmd_t * cmd, int argc, const char **argv) { - return (cmd->fun)(argc, argv); + return (cmd->fun) (argc, argv); } diff --git a/libopkg/opkg_cmd.h b/libopkg/opkg_cmd.h index 9ca42ff..e23d773 100644 --- a/libopkg/opkg_cmd.h +++ b/libopkg/opkg_cmd.h @@ -18,19 +18,18 @@ #ifndef OPKG_CMD_H #define OPKG_CMD_H -typedef int (*opkg_cmd_fun_t)(int argc, const char **argv); - -struct opkg_cmd -{ - const char *name; - int requires_args; - opkg_cmd_fun_t fun; - unsigned int pfm; /* package field mask */ +typedef int (*opkg_cmd_fun_t) (int argc, const char **argv); + +struct opkg_cmd { + const char *name; + int requires_args; + opkg_cmd_fun_t fun; + unsigned int pfm; /* package field mask */ }; typedef struct opkg_cmd opkg_cmd_t; opkg_cmd_t *opkg_cmd_find(const char *name); -int opkg_cmd_exec(opkg_cmd_t *cmd, int argc, const char **argv); +int opkg_cmd_exec(opkg_cmd_t * cmd, int argc, const char **argv); extern int opkg_state_changed; #endif diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c index 3a99935..d77d5dd 100644 --- a/libopkg/opkg_conf.c +++ b/libopkg/opkg_conf.c @@ -45,389 +45,425 @@ opkg_conf_t *conf = &_conf; * Config file options */ opkg_option_t options[] = { - { "cache", OPKG_OPT_TYPE_STRING, &_conf.cache}, - { "force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults }, - { "force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer }, - { "force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends }, - { "force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite }, - { "force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade }, - { "force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall }, - { "force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space }, - { "force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall }, - { "force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum }, - { "check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature }, - { "ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy }, - { "http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy }, - { "no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy }, - { "test", OPKG_OPT_TYPE_BOOL, &_conf.noaction }, - { "noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction }, - { "download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only }, - { "nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps }, - { "nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase }, - { "offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root }, - { "overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root }, - { "proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd }, - { "proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user }, - { "query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all }, - { "size", OPKG_OPT_TYPE_BOOL, &_conf.size }, - { "tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir }, - { "verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity }, + {"cache", OPKG_OPT_TYPE_STRING, &_conf.cache}, + {"force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults}, + {"force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer}, + {"force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends}, + {"force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite}, + {"force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade}, + {"force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall}, + {"force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space}, + {"force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall}, + {"force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum}, + {"check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature}, + {"ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy}, + {"http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy}, + {"no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy}, + {"test", OPKG_OPT_TYPE_BOOL, &_conf.noaction}, + {"noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction}, + {"download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only}, + {"nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps}, + {"nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase}, + {"offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root}, + {"overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root}, + {"proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd}, + {"proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user}, + {"query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all}, + {"size", OPKG_OPT_TYPE_BOOL, &_conf.size}, + {"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir}, + {"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity}, #if defined(HAVE_OPENSSL) - { "signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file }, - { "signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path }, + {"signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file}, + {"signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path}, #endif #if defined(HAVE_PATHFINDER) - { "check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path }, + {"check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path}, #endif #if defined(HAVE_SSLCURL) && defined(HAVE_CURL) - { "ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine }, - { "ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert }, - { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type }, - { "ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key }, - { "ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type }, - { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd }, - { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file }, - { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path }, - { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &_conf.ssl_dont_verify_peer }, + {"ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine}, + {"ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert}, + {"ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type}, + {"ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key}, + {"ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type}, + {"ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd}, + {"ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file}, + {"ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path}, + {"ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, + &_conf.ssl_dont_verify_peer}, #endif - { NULL, 0, NULL } + {NULL, 0, NULL} }; -static int -resolve_pkg_dest_list(void) +static int resolve_pkg_dest_list(void) { - nv_pair_list_elt_t *iter; - nv_pair_t *nv_pair; - pkg_dest_t *dest; - char *root_dir; - - for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter; - iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) { - nv_pair = (nv_pair_t *)iter->data; - - if (conf->offline_root) { - sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value); - } else { - root_dir = xstrdup(nv_pair->value); - } - - dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir); - free(root_dir); - - if (conf->default_dest == NULL) - conf->default_dest = dest; - - if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) { - conf->default_dest = dest; - conf->restrict_to_default_dest = 1; - } - } - - if (conf->dest_str && !conf->restrict_to_default_dest) { - opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str); - return -1; - } - - return 0; + nv_pair_list_elt_t *iter; + nv_pair_t *nv_pair; + pkg_dest_t *dest; + char *root_dir; + + for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter; + iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) { + nv_pair = (nv_pair_t *) iter->data; + + if (conf->offline_root) { + sprintf_alloc(&root_dir, "%s%s", conf->offline_root, + nv_pair->value); + } else { + root_dir = xstrdup(nv_pair->value); + } + + dest = + pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, + root_dir, conf->lists_dir); + free(root_dir); + + if (conf->default_dest == NULL) + conf->default_dest = dest; + + if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) { + conf->default_dest = dest; + conf->restrict_to_default_dest = 1; + } + } + + if (conf->dest_str && !conf->restrict_to_default_dest) { + opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str); + return -1; + } + + return 0; } -static int -opkg_conf_set_option(const char *name, const char *value) +static int opkg_conf_set_option(const char *name, const char *value) { - int i = 0; - - while (options[i].name) { - if (strcmp(options[i].name, name) == 0) { - switch (options[i].type) { - case OPKG_OPT_TYPE_BOOL: - if (*(int *)options[i].value) { - opkg_msg(ERROR, "Duplicate boolean option %s, " - "leaving this option on.\n", name); - return 0; - } - *((int * const)options[i].value) = 1; - return 0; - case OPKG_OPT_TYPE_INT: - if (value) { - if (*(int *)options[i].value) { - opkg_msg(ERROR, "Duplicate option %s, " - "using first seen value \"%d\".\n", - name, *((int *)options[i].value)); - return 0; - } - *((int * const)options[i].value) = atoi(value); - return 0; - } else { - opkg_msg(ERROR, "Option %s needs an argument\n", - name); - return -1; - } - case OPKG_OPT_TYPE_STRING: - if (value) { - if (*(char **)options[i].value) { - opkg_msg(ERROR, "Duplicate option %s, " - "using first seen value \"%s\".\n", - name, *((char **)options[i].value)); - return 0; - } - *((char ** const)options[i].value) = xstrdup(value); - return 0; - } else { - opkg_msg(ERROR, "Option %s needs an argument\n", - name); - return -1; - } - } - } - i++; - } - - opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value); - return -1; + int i = 0; + + while (options[i].name) { + if (strcmp(options[i].name, name) == 0) { + switch (options[i].type) { + case OPKG_OPT_TYPE_BOOL: + if (*(int *)options[i].value) { + opkg_msg(ERROR, + "Duplicate boolean option %s, " + "leaving this option on.\n", + name); + return 0; + } + *((int *const)options[i].value) = 1; + return 0; + case OPKG_OPT_TYPE_INT: + if (value) { + if (*(int *)options[i].value) { + opkg_msg(ERROR, + "Duplicate option %s, " + "using first seen value \"%d\".\n", + name, + *((int *)options[i]. + value)); + return 0; + } + *((int *const)options[i].value) = + atoi(value); + return 0; + } else { + opkg_msg(ERROR, + "Option %s needs an argument\n", + name); + return -1; + } + case OPKG_OPT_TYPE_STRING: + if (value) { + if (*(char **)options[i].value) { + opkg_msg(ERROR, + "Duplicate option %s, " + "using first seen value \"%s\".\n", + name, + *((char **)options[i]. + value)); + return 0; + } + *((char **const)options[i].value) = + xstrdup(value); + return 0; + } else { + opkg_msg(ERROR, + "Option %s needs an argument\n", + name); + return -1; + } + } + } + i++; + } + + opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value); + return -1; } static int opkg_conf_parse_file(const char *filename, - pkg_src_list_t *pkg_src_list, - pkg_src_list_t *dist_src_list) + pkg_src_list_t * pkg_src_list, + pkg_src_list_t * dist_src_list) { - int line_num = 0; - int err = 0; - FILE *file; - regex_t valid_line_re, comment_re; + int line_num = 0; + int err = 0; + FILE *file; + regex_t valid_line_re, comment_re; #define regmatch_size 14 - regmatch_t regmatch[regmatch_size]; - - file = fopen(filename, "r"); - if (file == NULL) { - opkg_perror(ERROR, "Failed to open %s", filename); - err = -1; - goto err0; - } - - opkg_msg(INFO, "Loading conf file %s.\n", filename); - - err = xregcomp(&comment_re, - "^[[:space:]]*(#.*|[[:space:]]*)$", - REG_EXTENDED); - if (err) - goto err1; - - err = xregcomp(&valid_line_re, - "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))" - "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))" - "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))" - "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$", - REG_EXTENDED); - if (err) - goto err2; - - while(1) { - char *line; - char *type, *name, *value, *extra; - - line_num++; - - line = file_read_line_alloc(file); - if (line == NULL) - break; - - if (regexec(&comment_re, line, 0, 0, 0) == 0) - goto NEXT_LINE; - - if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) { - opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n", - filename, line_num, line); - goto NEXT_LINE; - } - - /* This has to be so ugly to deal with optional quotation marks */ - if (regmatch[2].rm_so > 0) { - type = xstrndup(line + regmatch[2].rm_so, - regmatch[2].rm_eo - regmatch[2].rm_so); - } else { - type = xstrndup(line + regmatch[3].rm_so, - regmatch[3].rm_eo - regmatch[3].rm_so); - } - - if (regmatch[5].rm_so > 0) { - name = xstrndup(line + regmatch[5].rm_so, - regmatch[5].rm_eo - regmatch[5].rm_so); - } else { - name = xstrndup(line + regmatch[6].rm_so, - regmatch[6].rm_eo - regmatch[6].rm_so); - } - - if (regmatch[8].rm_so > 0) { - value = xstrndup(line + regmatch[8].rm_so, - regmatch[8].rm_eo - regmatch[8].rm_so); - } else { - value = xstrndup(line + regmatch[9].rm_so, - regmatch[9].rm_eo - regmatch[9].rm_so); - } - - extra = NULL; - if (regmatch[11].rm_so > 0) { - if (regmatch[13].rm_so > 0 && regmatch[13].rm_so!=regmatch[13].rm_eo ) - extra = xstrndup (line + regmatch[11].rm_so, - regmatch[13].rm_eo - regmatch[11].rm_so); - else - extra = xstrndup (line + regmatch[11].rm_so, - regmatch[11].rm_eo - regmatch[11].rm_so); - } - - if (regmatch[13].rm_so!=regmatch[13].rm_eo && strncmp(type, "dist", 4)!=0) { - opkg_msg(ERROR, "%s:%d: Ignoring config line with trailing garbage: `%s'\n", - filename, line_num, line); - } else { - - /* We use the conf->tmp_dest_list below instead of - conf->pkg_dest_list because we might encounter an - offline_root option later and that would invalidate the - directories we would have computed in - pkg_dest_list_init. (We do a similar thing with - tmp_src_nv_pair_list for sake of symmetry.) */ - if (strcmp(type, "option") == 0) { - opkg_conf_set_option(name, value); - } else if (strcmp(type, "dist") == 0) { - if (!nv_pair_list_find((nv_pair_list_t*) dist_src_list, name)) { - pkg_src_list_append (dist_src_list, name, value, extra, 0); - } else { - opkg_msg(ERROR, "Duplicate dist declaration (%s %s). " - "Skipping.\n", name, value); - } - } else if (strcmp(type, "dist/gz") == 0) { - if (!nv_pair_list_find((nv_pair_list_t*) dist_src_list, name)) { - pkg_src_list_append (dist_src_list, name, value, extra, 1); - } else { - opkg_msg(ERROR, "Duplicate dist declaration (%s %s). " - "Skipping.\n", name, value); - } - } else if (strcmp(type, "src") == 0) { - if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) { - pkg_src_list_append (pkg_src_list, name, value, extra, 0); - } else { - opkg_msg(ERROR, "Duplicate src declaration (%s %s). " - "Skipping.\n", name, value); - } - } else if (strcmp(type, "src/gz") == 0) { - if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) { - pkg_src_list_append (pkg_src_list, name, value, extra, 1); - } else { - opkg_msg(ERROR, "Duplicate src declaration (%s %s). " - "Skipping.\n", name, value); - } - } else if (strcmp(type, "dest") == 0) { - nv_pair_list_append(&conf->tmp_dest_list, name, value); - } else if (strcmp(type, "lists_dir") == 0) { - conf->lists_dir = xstrdup(value); - } else if (strcmp(type, "arch") == 0) { - opkg_msg(INFO, "Supported arch %s priority (%s)\n", name, value); - if (!value) { - opkg_msg(NOTICE, "No priority given for architecture %s," - "defaulting to 10\n", name); - value = xstrdup("10"); - } - nv_pair_list_append(&conf->arch_list, name, value); - } else { - opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n", - filename, line_num, line); - } - - } - - free(type); - free(name); - free(value); - if (extra) - free(extra); + regmatch_t regmatch[regmatch_size]; + + file = fopen(filename, "r"); + if (file == NULL) { + opkg_perror(ERROR, "Failed to open %s", filename); + err = -1; + goto err0; + } + + opkg_msg(INFO, "Loading conf file %s.\n", filename); + + err = xregcomp(&comment_re, + "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED); + if (err) + goto err1; + + err = xregcomp(&valid_line_re, + "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))" + "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))" + "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))" + "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$", + REG_EXTENDED); + if (err) + goto err2; + + while (1) { + char *line; + char *type, *name, *value, *extra; + + line_num++; + + line = file_read_line_alloc(file); + if (line == NULL) + break; + + if (regexec(&comment_re, line, 0, 0, 0) == 0) + goto NEXT_LINE; + + if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == + REG_NOMATCH) { + opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n", + filename, line_num, line); + goto NEXT_LINE; + } + + /* This has to be so ugly to deal with optional quotation marks */ + if (regmatch[2].rm_so > 0) { + type = xstrndup(line + regmatch[2].rm_so, + regmatch[2].rm_eo - regmatch[2].rm_so); + } else { + type = xstrndup(line + regmatch[3].rm_so, + regmatch[3].rm_eo - regmatch[3].rm_so); + } + + if (regmatch[5].rm_so > 0) { + name = xstrndup(line + regmatch[5].rm_so, + regmatch[5].rm_eo - regmatch[5].rm_so); + } else { + name = xstrndup(line + regmatch[6].rm_so, + regmatch[6].rm_eo - regmatch[6].rm_so); + } + + if (regmatch[8].rm_so > 0) { + value = xstrndup(line + regmatch[8].rm_so, + regmatch[8].rm_eo - regmatch[8].rm_so); + } else { + value = xstrndup(line + regmatch[9].rm_so, + regmatch[9].rm_eo - regmatch[9].rm_so); + } + + extra = NULL; + if (regmatch[11].rm_so > 0) { + if (regmatch[13].rm_so > 0 + && regmatch[13].rm_so != regmatch[13].rm_eo) + extra = + xstrndup(line + regmatch[11].rm_so, + regmatch[13].rm_eo - + regmatch[11].rm_so); + else + extra = xstrndup(line + regmatch[11].rm_so, + regmatch[11].rm_eo - + regmatch[11].rm_so); + } + + if (regmatch[13].rm_so != regmatch[13].rm_eo + && strncmp(type, "dist", 4) != 0) { + opkg_msg(ERROR, + "%s:%d: Ignoring config line with trailing garbage: `%s'\n", + filename, line_num, line); + } else { + + /* We use the conf->tmp_dest_list below instead of + conf->pkg_dest_list because we might encounter an + offline_root option later and that would invalidate the + directories we would have computed in + pkg_dest_list_init. (We do a similar thing with + tmp_src_nv_pair_list for sake of symmetry.) */ + if (strcmp(type, "option") == 0) { + opkg_conf_set_option(name, value); + } else if (strcmp(type, "dist") == 0) { + if (!nv_pair_list_find + ((nv_pair_list_t *) dist_src_list, name)) { + pkg_src_list_append(dist_src_list, name, + value, extra, 0); + } else { + opkg_msg(ERROR, + "Duplicate dist declaration (%s %s). " + "Skipping.\n", name, value); + } + } else if (strcmp(type, "dist/gz") == 0) { + if (!nv_pair_list_find + ((nv_pair_list_t *) dist_src_list, name)) { + pkg_src_list_append(dist_src_list, name, + value, extra, 1); + } else { + opkg_msg(ERROR, + "Duplicate dist declaration (%s %s). " + "Skipping.\n", name, value); + } + } else if (strcmp(type, "src") == 0) { + if (!nv_pair_list_find + ((nv_pair_list_t *) pkg_src_list, name)) { + pkg_src_list_append(pkg_src_list, name, + value, extra, 0); + } else { + opkg_msg(ERROR, + "Duplicate src declaration (%s %s). " + "Skipping.\n", name, value); + } + } else if (strcmp(type, "src/gz") == 0) { + if (!nv_pair_list_find + ((nv_pair_list_t *) pkg_src_list, name)) { + pkg_src_list_append(pkg_src_list, name, + value, extra, 1); + } else { + opkg_msg(ERROR, + "Duplicate src declaration (%s %s). " + "Skipping.\n", name, value); + } + } else if (strcmp(type, "dest") == 0) { + nv_pair_list_append(&conf->tmp_dest_list, name, + value); + } else if (strcmp(type, "lists_dir") == 0) { + conf->lists_dir = xstrdup(value); + } else if (strcmp(type, "arch") == 0) { + opkg_msg(INFO, + "Supported arch %s priority (%s)\n", + name, value); + if (!value) { + opkg_msg(NOTICE, + "No priority given for architecture %s," + "defaulting to 10\n", name); + value = xstrdup("10"); + } + nv_pair_list_append(&conf->arch_list, name, + value); + } else { + opkg_msg(ERROR, + "%s:%d: Ignoring invalid line: `%s'\n", + filename, line_num, line); + } + + } + + free(type); + free(name); + free(value); + if (extra) + free(extra); NEXT_LINE: - free(line); - } + free(line); + } - regfree(&valid_line_re); + regfree(&valid_line_re); err2: - regfree(&comment_re); + regfree(&comment_re); err1: - if (fclose(file) == EOF) { - opkg_perror(ERROR, "Couldn't close %s", filename); - err = -1; - } + if (fclose(file) == EOF) { + opkg_perror(ERROR, "Couldn't close %s", filename); + err = -1; + } err0: - return err; + return err; } -int -opkg_conf_write_status_files(void) +int opkg_conf_write_status_files(void) { - pkg_dest_list_elt_t *iter; - pkg_dest_t *dest; - pkg_vec_t *all; - pkg_t *pkg; - int i, ret = 0; - - if (conf->noaction) - return 0; - - list_for_each_entry(iter, &conf->pkg_dest_list.head, node) { - dest = (pkg_dest_t *)iter->data; - - dest->status_fp = fopen(dest->status_file_name, "w"); - if (dest->status_fp == NULL && errno != EROFS) { - opkg_perror(ERROR, "Can't open status file %s", - dest->status_file_name); - ret = -1; - } - } - - all = pkg_vec_alloc(); - pkg_hash_fetch_available(all); - - for(i = 0; i < all->len; i++) { - pkg = all->pkgs[i]; - /* We don't need most uninstalled packages in the status file */ - if (pkg->state_status == SS_NOT_INSTALLED - && (pkg->state_want == SW_UNKNOWN - || (pkg->state_want == SW_DEINSTALL - && pkg->state_flag != SF_HOLD) - || pkg->state_want == SW_PURGE)) { - continue; - } - if (pkg->dest == NULL) { - opkg_msg(ERROR, "Internal error: package %s has a NULL dest\n", - pkg->name); - continue; - } - if (pkg->dest->status_fp) - pkg_print_status(pkg, pkg->dest->status_fp); - } - - pkg_vec_free(all); - - list_for_each_entry(iter, &conf->pkg_dest_list.head, node) { - dest = (pkg_dest_t *)iter->data; - if (dest->status_fp && fclose(dest->status_fp) == EOF) { - opkg_perror(ERROR, "Couldn't close %s", dest->status_file_name); - ret = -1; - } - } - - return ret; -} + pkg_dest_list_elt_t *iter; + pkg_dest_t *dest; + pkg_vec_t *all; + pkg_t *pkg; + int i, ret = 0; + + if (conf->noaction) + return 0; + list_for_each_entry(iter, &conf->pkg_dest_list.head, node) { + dest = (pkg_dest_t *) iter->data; -char * -root_filename_alloc(char *filename) + dest->status_fp = fopen(dest->status_file_name, "w"); + if (dest->status_fp == NULL && errno != EROFS) { + opkg_perror(ERROR, "Can't open status file %s", + dest->status_file_name); + ret = -1; + } + } + + all = pkg_vec_alloc(); + pkg_hash_fetch_available(all); + + for (i = 0; i < all->len; i++) { + pkg = all->pkgs[i]; + /* We don't need most uninstalled packages in the status file */ + if (pkg->state_status == SS_NOT_INSTALLED + && (pkg->state_want == SW_UNKNOWN + || (pkg->state_want == SW_DEINSTALL + && pkg->state_flag != SF_HOLD) + || pkg->state_want == SW_PURGE)) { + continue; + } + if (pkg->dest == NULL) { + opkg_msg(ERROR, + "Internal error: package %s has a NULL dest\n", + pkg->name); + continue; + } + if (pkg->dest->status_fp) + pkg_print_status(pkg, pkg->dest->status_fp); + } + + pkg_vec_free(all); + + list_for_each_entry(iter, &conf->pkg_dest_list.head, node) { + dest = (pkg_dest_t *) iter->data; + if (dest->status_fp && fclose(dest->status_fp) == EOF) { + opkg_perror(ERROR, "Couldn't close %s", + dest->status_file_name); + ret = -1; + } + } + + return ret; +} + +char *root_filename_alloc(char *filename) { char *root_filename; sprintf_alloc(&root_filename, "%s%s", - (conf->offline_root ? conf->offline_root : ""), filename); + (conf->offline_root ? conf->offline_root : ""), filename); return root_filename; } -static int -glob_errfunc(const char *epath, int eerrno) +static int glob_errfunc(const char *epath, int eerrno) { if (eerrno == ENOENT) /* If leading dir does not exist, we get GLOB_NOMATCH. */ @@ -437,8 +473,7 @@ glob_errfunc(const char *epath, int eerrno) return 0; } -int -opkg_conf_init(void) +int opkg_conf_init(void) { pkg_src_list_init(&conf->pkg_src_list); pkg_src_list_init(&conf->dist_src_list); @@ -449,8 +484,7 @@ opkg_conf_init(void) return 0; } -int -opkg_conf_load(void) +int opkg_conf_load(void) { int i, glob_ret; char *tmp, *tmp_dir_base, **tmp_val; @@ -473,17 +507,20 @@ opkg_conf_load(void) goto err0; } if (opkg_conf_parse_file(conf->conf_file, - &conf->pkg_src_list, &conf->dist_src_list)) + &conf->pkg_src_list, + &conf->dist_src_list)) goto err1; } if (conf->offline_root) - sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root); + sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", + conf->offline_root); else { const char *conf_file_dir = getenv("OPKG_CONF_DIR"); if (conf_file_dir == NULL) conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR; - sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir); + sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", + conf_file_dir); } memset(&globbuf, 0, sizeof(globbuf)); @@ -499,10 +536,11 @@ opkg_conf_load(void) for (i = 0; i < globbuf.gl_pathc; i++) { if (globbuf.gl_pathv[i]) if (conf->conf_file && - !strcmp(conf->conf_file, globbuf.gl_pathv[i])) + !strcmp(conf->conf_file, globbuf.gl_pathv[i])) continue; - if ( opkg_conf_parse_file(globbuf.gl_pathv[i], - &conf->pkg_src_list, &conf->dist_src_list)<0) { + if (opkg_conf_parse_file(globbuf.gl_pathv[i], + &conf->pkg_src_list, + &conf->dist_src_list) < 0) { globfree(&globbuf); goto err1; } @@ -511,9 +549,10 @@ opkg_conf_load(void) globfree(&globbuf); if (conf->offline_root) - sprintf_alloc (&lock_file, "%s/%s", conf->offline_root, OPKGLOCKFILE); + sprintf_alloc(&lock_file, "%s/%s", conf->offline_root, + OPKGLOCKFILE); else - sprintf_alloc (&lock_file, "%s", OPKGLOCKFILE); + sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE); lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP); if (lock_fd == -1) { @@ -521,11 +560,11 @@ opkg_conf_load(void) goto err2; } - if (lockf(lock_fd, F_TLOCK, (off_t)0) == -1) { + if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) { opkg_perror(ERROR, "Could not lock %s", lock_file); if (close(lock_fd) == -1) opkg_perror(ERROR, "Couldn't close descriptor %d (%s)", - lock_fd, lock_file); + lock_fd, lock_file); lock_fd = -1; goto err2; } @@ -536,8 +575,8 @@ opkg_conf_load(void) tmp_dir_base = getenv("TMPDIR"); sprintf_alloc(&tmp, "%s/%s", - tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE, - OPKG_CONF_TMP_DIR_SUFFIX); + tmp_dir_base ? tmp_dir_base : + OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX); if (conf->tmp_dir) free(conf->tmp_dir); conf->tmp_dir = mkdtemp(tmp); @@ -547,14 +586,17 @@ opkg_conf_load(void) } pkg_hash_init(); - hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN); - hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN/16); + hash_table_init("file-hash", &conf->file_hash, + OPKG_CONF_DEFAULT_HASH_LEN); + hash_table_init("obs-file-hash", &conf->obs_file_hash, + OPKG_CONF_DEFAULT_HASH_LEN / 16); if (conf->lists_dir == NULL) conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR); if (conf->offline_root) { - sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir); + sprintf_alloc(&tmp, "%s/%s", conf->offline_root, + conf->lists_dir); free(conf->lists_dir); conf->lists_dir = tmp; } @@ -569,8 +611,8 @@ opkg_conf_load(void) /* Even if there is no conf file, we'll need at least one dest. */ if (nv_pair_list_empty(&conf->tmp_dest_list)) { nv_pair_list_append(&conf->tmp_dest_list, - OPKG_CONF_DEFAULT_DEST_NAME, - OPKG_CONF_DEFAULT_DEST_ROOT_DIR); + OPKG_CONF_DEFAULT_DEST_NAME, + OPKG_CONF_DEFAULT_DEST_ROOT_DIR); } if (resolve_pkg_dest_list()) @@ -580,7 +622,6 @@ opkg_conf_load(void) return 0; - err4: free(conf->lists_dir); @@ -591,12 +632,12 @@ err4: if (rmdir(conf->tmp_dir) == -1) opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir); err3: - if (lockf(lock_fd, F_ULOCK, (off_t)0) == -1) + if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1) opkg_perror(ERROR, "Couldn't unlock %s", lock_file); if (close(lock_fd) == -1) opkg_perror(ERROR, "Couldn't close descriptor %d (%s)", - lock_fd, lock_file); + lock_fd, lock_file); if (unlink(lock_file) == -1) opkg_perror(ERROR, "Couldn't unlink %s", lock_file); err2: @@ -610,7 +651,7 @@ err1: pkg_dest_list_deinit(&conf->pkg_dest_list); nv_pair_list_deinit(&conf->arch_list); - for (i=0; options[i].name; i++) { + for (i = 0; options[i].name; i++) { if (options[i].type == OPKG_OPT_TYPE_STRING) { tmp_val = (char **)options[i].value; if (*tmp_val) { @@ -629,8 +670,7 @@ err0: return -1; } -void -opkg_conf_deinit(void) +void opkg_conf_deinit(void) { int i; char **tmp; @@ -652,7 +692,7 @@ opkg_conf_deinit(void) pkg_dest_list_deinit(&conf->pkg_dest_list); nv_pair_list_deinit(&conf->arch_list); - for (i=0; options[i].name; i++) { + for (i = 0; options[i].name; i++) { if (options[i].type == OPKG_OPT_TYPE_STRING) { tmp = (char **)options[i].value; if (*tmp) { @@ -673,12 +713,12 @@ opkg_conf_deinit(void) hash_table_deinit(&conf->obs_file_hash); if (lock_fd != -1) { - if (lockf(lock_fd, F_ULOCK, (off_t)0) == -1) + if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1) opkg_perror(ERROR, "Couldn't unlock %s", lock_file); if (close(lock_fd) == -1) opkg_perror(ERROR, "Couldn't close descriptor %d (%s)", - lock_fd, lock_file); + lock_fd, lock_file); } diff --git a/libopkg/opkg_conf.h b/libopkg/opkg_conf.h index ab629ea..e811337 100644 --- a/libopkg/opkg_conf.h +++ b/libopkg/opkg_conf.h @@ -24,7 +24,7 @@ extern opkg_conf_t *conf; #include "config.h" #include -#include /* FNM_CASEFOLD */ +#include /* FNM_CASEFOLD */ #include "hash_table.h" #include "pkg_src_list.h" @@ -43,101 +43,100 @@ extern opkg_conf_t *conf; #define OPKG_CONF_DEFAULT_HASH_LEN 1024 -struct opkg_conf -{ - pkg_src_list_t pkg_src_list; - pkg_src_list_t dist_src_list; - pkg_dest_list_t pkg_dest_list; - pkg_dest_list_t tmp_dest_list; - nv_pair_list_t arch_list; - - int restrict_to_default_dest; - pkg_dest_t *default_dest; - char *dest_str; - - char *conf_file; - - char *tmp_dir; - char *lists_dir; - - unsigned int pfm; /* package field mask */ - - /* For libopkg users to capture messages. */ - void (*opkg_vmessage)(int, const char *fmt, va_list ap); - - /* options */ - int autoremove; - int force_depends; - int force_defaults; - int force_maintainer; - int force_overwrite; - int force_downgrade; - int force_reinstall; - int force_space; - int force_removal_of_dependent_packages; - int force_removal_of_essential_packages; - int force_postinstall; - int force_remove; - int force_checksum; - int check_signature; - int force_signature; - int nodeps; /* do not follow dependencies */ - int nocase; /* perform case insensitive matching */ - char *offline_root; - char *overlay_root; - int query_all; - int verbosity; - int noaction; - int size; - int download_only; - char *cache; +struct opkg_conf { + pkg_src_list_t pkg_src_list; + pkg_src_list_t dist_src_list; + pkg_dest_list_t pkg_dest_list; + pkg_dest_list_t tmp_dest_list; + nv_pair_list_t arch_list; + + int restrict_to_default_dest; + pkg_dest_t *default_dest; + char *dest_str; + + char *conf_file; + + char *tmp_dir; + char *lists_dir; + + unsigned int pfm; /* package field mask */ + + /* For libopkg users to capture messages. */ + void (*opkg_vmessage) (int, const char *fmt, va_list ap); + + /* options */ + int autoremove; + int force_depends; + int force_defaults; + int force_maintainer; + int force_overwrite; + int force_downgrade; + int force_reinstall; + int force_space; + int force_removal_of_dependent_packages; + int force_removal_of_essential_packages; + int force_postinstall; + int force_remove; + int force_checksum; + int check_signature; + int force_signature; + int nodeps; /* do not follow dependencies */ + int nocase; /* perform case insensitive matching */ + char *offline_root; + char *overlay_root; + int query_all; + int verbosity; + int noaction; + int size; + int download_only; + char *cache; #ifdef HAVE_SSLCURL - /* some options could be used by - * wget if curl support isn't builtin - * If someone want to try... - */ - char *ssl_engine; - char *ssl_cert; - char *ssl_cert_type; - char *ssl_key; - char *ssl_key_type; - char *ssl_key_passwd; - char *ssl_ca_file; - char *ssl_ca_path; - int ssl_dont_verify_peer; + /* some options could be used by + * wget if curl support isn't builtin + * If someone want to try... + */ + char *ssl_engine; + char *ssl_cert; + char *ssl_cert_type; + char *ssl_key; + char *ssl_key_type; + char *ssl_key_passwd; + char *ssl_ca_file; + char *ssl_ca_path; + int ssl_dont_verify_peer; #endif #ifdef HAVE_PATHFINDER - int check_x509_path; + int check_x509_path; #endif - /* proxy options */ - char *http_proxy; - char *ftp_proxy; - char *no_proxy; - char *proxy_user; - char *proxy_passwd; + /* proxy options */ + char *http_proxy; + char *ftp_proxy; + char *no_proxy; + char *proxy_user; + char *proxy_passwd; - char *signature_ca_file; - char *signature_ca_path; + char *signature_ca_file; + char *signature_ca_path; - hash_table_t pkg_hash; - hash_table_t file_hash; - hash_table_t obs_file_hash; + hash_table_t pkg_hash; + hash_table_t file_hash; + hash_table_t obs_file_hash; }; enum opkg_option_type { - OPKG_OPT_TYPE_BOOL, - OPKG_OPT_TYPE_INT, - OPKG_OPT_TYPE_STRING + OPKG_OPT_TYPE_BOOL, + OPKG_OPT_TYPE_INT, + OPKG_OPT_TYPE_STRING }; typedef enum opkg_option_type opkg_option_type_t; typedef struct opkg_option opkg_option_t; struct opkg_option { - const char *name; - const opkg_option_type_t type; - void * const value; + const char *name; + const opkg_option_type_t type; + void *const value; }; int opkg_conf_init(void); diff --git a/libopkg/opkg_configure.c b/libopkg/opkg_configure.c index 719da5a..dc05f1e 100644 --- a/libopkg/opkg_configure.c +++ b/libopkg/opkg_configure.c @@ -22,23 +22,21 @@ #include "opkg_message.h" #include "opkg_cmd.h" -int -opkg_configure(pkg_t *pkg) +int opkg_configure(pkg_t * pkg) { - int err; + int err; - /* DPKG_INCOMPATIBILITY: - dpkg actually does some conffile handling here, rather than at the - end of opkg_install(). Do we care? */ - /* DPKG_INCOMPATIBILITY: - dpkg actually includes a version number to this script call */ + /* DPKG_INCOMPATIBILITY: + dpkg actually does some conffile handling here, rather than at the + end of opkg_install(). Do we care? */ + /* DPKG_INCOMPATIBILITY: + dpkg actually includes a version number to this script call */ - err = pkg_run_script(pkg, "postinst", "configure"); - if (err) { - opkg_msg(ERROR, "%s.postinst returned %d.\n", pkg->name, err); - return err; - } + err = pkg_run_script(pkg, "postinst", "configure"); + if (err) { + opkg_msg(ERROR, "%s.postinst returned %d.\n", pkg->name, err); + return err; + } - return 0; + return 0; } - diff --git a/libopkg/opkg_configure.h b/libopkg/opkg_configure.h index ff01ff3..f13afd3 100644 --- a/libopkg/opkg_configure.h +++ b/libopkg/opkg_configure.h @@ -20,6 +20,6 @@ #include "pkg.h" -int opkg_configure(pkg_t *pkg); +int opkg_configure(pkg_t * pkg); #endif diff --git a/libopkg/opkg_download.c b/libopkg/opkg_download.c index 3f86462..260790b 100644 --- a/libopkg/opkg_download.c +++ b/libopkg/opkg_download.c @@ -75,289 +75,290 @@ static CURL *curl = NULL; static CURL *opkg_curl_init(curl_progress_func cb, void *data); #endif -static int -str_starts_with(const char *str, const char *prefix) +static int str_starts_with(const char *str, const char *prefix) { - return (strncmp(str, prefix, strlen(prefix)) == 0); + return (strncmp(str, prefix, strlen(prefix)) == 0); } int opkg_download(const char *src, const char *dest_file_name, - curl_progress_func cb, void *data, const short hide_error) + curl_progress_func cb, void *data, const short hide_error) { - int err = 0; + int err = 0; - char *src_basec = xstrdup(src); - char *src_base = basename(src_basec); - char *tmp_file_location; + char *src_basec = xstrdup(src); + char *src_base = basename(src_basec); + char *tmp_file_location; - opkg_msg(NOTICE,"Downloading %s\n", src); + opkg_msg(NOTICE, "Downloading %s\n", src); - if (str_starts_with(src, "file:")) { - const char *file_src = src + 5; - opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name); - err = file_copy(file_src, dest_file_name); - opkg_msg(INFO, "Done.\n"); - free(src_basec); - return err; - } + if (str_starts_with(src, "file:")) { + const char *file_src = src + 5; + opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name); + err = file_copy(file_src, dest_file_name); + opkg_msg(INFO, "Done.\n"); + free(src_basec); + return err; + } - sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base); - free(src_basec); - err = unlink(tmp_file_location); - if (err && errno != ENOENT) { - opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location); - free(tmp_file_location); - return -1; - } - - if (conf->http_proxy) { - opkg_msg(DEBUG, "Setting environment variable: http_proxy = %s.\n", - conf->http_proxy); - setenv("http_proxy", conf->http_proxy, 1); - } - if (conf->ftp_proxy) { - opkg_msg(DEBUG, "Setting environment variable: ftp_proxy = %s.\n", - conf->ftp_proxy); - setenv("ftp_proxy", conf->ftp_proxy, 1); - } - if (conf->no_proxy) { - opkg_msg(DEBUG,"Setting environment variable: no_proxy = %s.\n", - conf->no_proxy); - setenv("no_proxy", conf->no_proxy, 1); - } + sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base); + free(src_basec); + err = unlink(tmp_file_location); + if (err && errno != ENOENT) { + opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location); + free(tmp_file_location); + return -1; + } -#ifdef HAVE_CURL - CURLcode res; - FILE * file = fopen (tmp_file_location, "w"); - - curl = opkg_curl_init (cb, data); - if (curl) - { - curl_easy_setopt (curl, CURLOPT_URL, src); - curl_easy_setopt (curl, CURLOPT_WRITEDATA, file); - - res = curl_easy_perform (curl); - fclose (file); - if (res) - { - long error_code; - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code); - opkg_msg(hide_error?DEBUG2:ERROR, "Failed to download %s: %s.\n", - src, curl_easy_strerror(res)); - free(tmp_file_location); - return -1; + if (conf->http_proxy) { + opkg_msg(DEBUG, + "Setting environment variable: http_proxy = %s.\n", + conf->http_proxy); + setenv("http_proxy", conf->http_proxy, 1); } + if (conf->ftp_proxy) { + opkg_msg(DEBUG, + "Setting environment variable: ftp_proxy = %s.\n", + conf->ftp_proxy); + setenv("ftp_proxy", conf->ftp_proxy, 1); + } + if (conf->no_proxy) { + opkg_msg(DEBUG, + "Setting environment variable: no_proxy = %s.\n", + conf->no_proxy); + setenv("no_proxy", conf->no_proxy, 1); + } +#ifdef HAVE_CURL + CURLcode res; + FILE *file = fopen(tmp_file_location, "w"); + + curl = opkg_curl_init(cb, data); + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, src); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); + + res = curl_easy_perform(curl); + fclose(file); + if (res) { + long error_code; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, + &error_code); + opkg_msg(hide_error ? DEBUG2 : ERROR, + "Failed to download %s: %s.\n", src, + curl_easy_strerror(res)); + free(tmp_file_location); + return -1; + } - } - else - { - free(tmp_file_location); - return -1; - } + } else { + free(tmp_file_location); + return -1; + } #else - { - int res; - const char *argv[8]; - int i = 0; - - argv[i++] = "wget"; - argv[i++] = "-q"; - if (conf->http_proxy || conf->ftp_proxy) { - argv[i++] = "-Y"; - argv[i++] = "on"; - } - argv[i++] = "-O"; - argv[i++] = tmp_file_location; - argv[i++] = src; - argv[i++] = NULL; - res = xsystem(argv); - - if (res) { - opkg_msg(ERROR, "Failed to download %s, wget returned %d.\n", src, res); - if (res == 4) - opkg_msg(ERROR, "Check your network settings and connectivity.\n\n"); - free(tmp_file_location); - return -1; - } - } + { + int res; + const char *argv[8]; + int i = 0; + + argv[i++] = "wget"; + argv[i++] = "-q"; + if (conf->http_proxy || conf->ftp_proxy) { + argv[i++] = "-Y"; + argv[i++] = "on"; + } + argv[i++] = "-O"; + argv[i++] = tmp_file_location; + argv[i++] = src; + argv[i++] = NULL; + res = xsystem(argv); + + if (res) { + opkg_msg(ERROR, + "Failed to download %s, wget returned %d.\n", + src, res); + if (res == 4) + opkg_msg(ERROR, + "Check your network settings and connectivity.\n\n"); + free(tmp_file_location); + return -1; + } + } #endif - err = file_move(tmp_file_location, dest_file_name); + err = file_move(tmp_file_location, dest_file_name); - free(tmp_file_location); + free(tmp_file_location); - return err; + return err; } static int opkg_download_cache(const char *src, const char *dest_file_name, - curl_progress_func cb, void *data) + curl_progress_func cb, void *data) { - char *cache_name = xstrdup(src); - char *cache_location, *p; - int err = 0; - - if (!conf->cache || str_starts_with(src, "file:")) { - err = opkg_download(src, dest_file_name, cb, data, 0); - goto out1; - } - - if(!file_is_dir(conf->cache)){ - opkg_msg(ERROR, "%s is not a directory.\n", - conf->cache); - err = 1; - goto out1; - } - - for (p = cache_name; *p; p++) - if (*p == '/') - *p = ','; /* looks nicer than | or # */ - - sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name); - if (file_exists(cache_location)) - opkg_msg(NOTICE, "Copying %s.\n", cache_location); - else { - /* cache file with funky name not found, try simple name */ - free(cache_name); - char *filename = strrchr(dest_file_name,'/'); - if (filename) - cache_name = xstrdup(filename+1); // strip leading '/' - else - cache_name = xstrdup(dest_file_name); - free(cache_location); - sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name); - if (file_exists(cache_location)) - opkg_msg(NOTICE, "Copying %s.\n", cache_location); - else { - err = opkg_download(src, cache_location, cb, data, 0); - if (err) { - (void) unlink(cache_location); - goto out2; - } - } - } - - err = file_copy(cache_location, dest_file_name); + char *cache_name = xstrdup(src); + char *cache_location, *p; + int err = 0; + if (!conf->cache || str_starts_with(src, "file:")) { + err = opkg_download(src, dest_file_name, cb, data, 0); + goto out1; + } + + if (!file_is_dir(conf->cache)) { + opkg_msg(ERROR, "%s is not a directory.\n", conf->cache); + err = 1; + goto out1; + } + + for (p = cache_name; *p; p++) + if (*p == '/') + *p = ','; /* looks nicer than | or # */ + + sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name); + if (file_exists(cache_location)) + opkg_msg(NOTICE, "Copying %s.\n", cache_location); + else { + /* cache file with funky name not found, try simple name */ + free(cache_name); + char *filename = strrchr(dest_file_name, '/'); + if (filename) + cache_name = xstrdup(filename + 1); // strip leading '/' + else + cache_name = xstrdup(dest_file_name); + free(cache_location); + sprintf_alloc(&cache_location, "%s/%s", conf->cache, + cache_name); + if (file_exists(cache_location)) + opkg_msg(NOTICE, "Copying %s.\n", cache_location); + else { + err = opkg_download(src, cache_location, cb, data, 0); + if (err) { + (void)unlink(cache_location); + goto out2; + } + } + } + + err = file_copy(cache_location, dest_file_name); out2: - free(cache_location); + free(cache_location); out1: - free(cache_name); - return err; + free(cache_name); + return err; } -int -opkg_download_pkg(pkg_t *pkg, const char *dir) +int opkg_download_pkg(pkg_t * pkg, const char *dir) { - int err; - char *url; - char *stripped_filename; - - if (pkg->src == NULL) { - opkg_msg(ERROR, "Package %s is not available from any configured src.\n", - pkg->name); - return -1; - } - if (pkg->filename == NULL) { - opkg_msg(ERROR, "Package %s does not have a valid filename field.\n", - pkg->name); - return -1; - } - - sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename); - - /* The pkg->filename might be something like - "../../foo.opk". While this is correct, and exactly what we - want to use to construct url above, here we actually need to - use just the filename part, without any directory. */ - - stripped_filename = strrchr(pkg->filename, '/'); - if ( ! stripped_filename ) - stripped_filename = pkg->filename; - - sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename); - - err = opkg_download_cache(url, pkg->local_filename, NULL, NULL); - free(url); - - return err; + int err; + char *url; + char *stripped_filename; + + if (pkg->src == NULL) { + opkg_msg(ERROR, + "Package %s is not available from any configured src.\n", + pkg->name); + return -1; + } + if (pkg->filename == NULL) { + opkg_msg(ERROR, + "Package %s does not have a valid filename field.\n", + pkg->name); + return -1; + } + + sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename); + + /* The pkg->filename might be something like + "../../foo.opk". While this is correct, and exactly what we + want to use to construct url above, here we actually need to + use just the filename part, without any directory. */ + + stripped_filename = strrchr(pkg->filename, '/'); + if (!stripped_filename) + stripped_filename = pkg->filename; + + sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename); + + err = opkg_download_cache(url, pkg->local_filename, NULL, NULL); + free(url); + + return err; } /* * Downloads file from url, installs in package database, return package name. */ -int -opkg_prepare_url_for_install(const char *url, char **namep) +int opkg_prepare_url_for_install(const char *url, char **namep) { - int err = 0; - pkg_t *pkg; - - pkg = pkg_new(); - - if (str_starts_with(url, "http://") - || str_starts_with(url, "ftp://")) { - char *tmp_file; - char *file_basec = xstrdup(url); - char *file_base = basename(file_basec); - - sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base); - err = opkg_download(url, tmp_file, NULL, NULL, 0); - if (err) - return err; - - err = pkg_init_from_file(pkg, tmp_file); - if (err) - return err; - - free(tmp_file); - free(file_basec); - - } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0 - || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0 - || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) { - - err = pkg_init_from_file(pkg, url); - if (err) - return err; - opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n", - pkg->name, pkg->local_filename); - pkg->provided_by_hand = 1; - - } else { - pkg_deinit(pkg); - free(pkg); - return 0; - } - - pkg->dest = conf->default_dest; - pkg->state_want = SW_INSTALL; - pkg->state_flag |= SF_PREFER; - hash_insert_pkg(pkg, 1); - - if (namep) { - *namep = xstrdup(pkg->name); - } - return 0; + int err = 0; + pkg_t *pkg; + + pkg = pkg_new(); + + if (str_starts_with(url, "http://") + || str_starts_with(url, "ftp://")) { + char *tmp_file; + char *file_basec = xstrdup(url); + char *file_base = basename(file_basec); + + sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base); + err = opkg_download(url, tmp_file, NULL, NULL, 0); + if (err) + return err; + + err = pkg_init_from_file(pkg, tmp_file); + if (err) + return err; + + free(tmp_file); + free(file_basec); + + } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0 + || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0 + || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) { + + err = pkg_init_from_file(pkg, url); + if (err) + return err; + opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n", + pkg->name, pkg->local_filename); + pkg->provided_by_hand = 1; + + } else { + pkg_deinit(pkg); + free(pkg); + return 0; + } + + pkg->dest = conf->default_dest; + pkg->state_want = SW_INSTALL; + pkg->state_flag |= SF_PREFER; + hash_insert_pkg(pkg, 1); + + if (namep) { + *namep = xstrdup(pkg->name); + } + return 0; } -int -opkg_verify_file (char *text_file, char *sig_file) +int opkg_verify_file(char *text_file, char *sig_file) { #if defined HAVE_USIGN int status = -1; int pid; - if (conf->check_signature == 0 ) - return 0; + if (conf->check_signature == 0) + return 0; pid = fork(); if (pid < 0) return -1; if (!pid) { - execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file, text_file, NULL); + execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file, + text_file, NULL); exit(255); } @@ -367,337 +368,343 @@ opkg_verify_file (char *text_file, char *sig_file) return 0; #elif defined HAVE_GPGME - if (conf->check_signature == 0 ) - return 0; - int status = -1; - gpgme_ctx_t ctx; - gpgme_data_t sig, text, key; - gpgme_error_t err; - gpgme_verify_result_t result; - gpgme_signature_t s; - char *trusted_path = NULL; - - gpgme_check_version (NULL); - - err = gpgme_new (&ctx); - - if (err) - return -1; - - sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg"); - err = gpgme_data_new_from_file (&key, trusted_path, 1); - free (trusted_path); - if (err) - { - return -1; - } - err = gpgme_op_import (ctx, key); - if (err) - { - gpgme_data_release (key); - return -1; - } - gpgme_data_release (key); - - err = gpgme_data_new_from_file (&sig, sig_file, 1); - if (err) - { - gpgme_release (ctx); - return -1; - } - - err = gpgme_data_new_from_file (&text, text_file, 1); - if (err) - { - gpgme_data_release (sig); - gpgme_release (ctx); - return -1; - } - - err = gpgme_op_verify (ctx, sig, text, NULL); - - result = gpgme_op_verify_result (ctx); - if (!result) - return -1; - - /* see if any of the signitures matched */ - s = result->signatures; - while (s) - { - status = gpg_err_code (s->status); - if (status == GPG_ERR_NO_ERROR) - break; - s = s->next; - } - - - gpgme_data_release (sig); - gpgme_data_release (text); - gpgme_release (ctx); - - return status; + if (conf->check_signature == 0) + return 0; + int status = -1; + gpgme_ctx_t ctx; + gpgme_data_t sig, text, key; + gpgme_error_t err; + gpgme_verify_result_t result; + gpgme_signature_t s; + char *trusted_path = NULL; + + gpgme_check_version(NULL); + + err = gpgme_new(&ctx); + + if (err) + return -1; + + sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, + "/etc/opkg/trusted.gpg"); + err = gpgme_data_new_from_file(&key, trusted_path, 1); + free(trusted_path); + if (err) { + return -1; + } + err = gpgme_op_import(ctx, key); + if (err) { + gpgme_data_release(key); + return -1; + } + gpgme_data_release(key); + + err = gpgme_data_new_from_file(&sig, sig_file, 1); + if (err) { + gpgme_release(ctx); + return -1; + } + + err = gpgme_data_new_from_file(&text, text_file, 1); + if (err) { + gpgme_data_release(sig); + gpgme_release(ctx); + return -1; + } + + err = gpgme_op_verify(ctx, sig, text, NULL); + + result = gpgme_op_verify_result(ctx); + if (!result) + return -1; + + /* see if any of the signitures matched */ + s = result->signatures; + while (s) { + status = gpg_err_code(s->status); + if (status == GPG_ERR_NO_ERROR) + break; + s = s->next; + } + + gpgme_data_release(sig); + gpgme_data_release(text); + gpgme_release(ctx); + + return status; #elif defined HAVE_OPENSSL - X509_STORE *store = NULL; - PKCS7 *p7 = NULL; - BIO *in = NULL, *indata = NULL; - - // Sig check failed by default ! - int status = -1; - - openssl_init(); - - // Set-up the key store - if(!(store = setup_verify(conf->signature_ca_file, conf->signature_ca_path))){ - opkg_msg(ERROR, "Can't open CA certificates.\n"); - goto verify_file_end; - } - - // Open a BIO to read the sig file - if (!(in = BIO_new_file(sig_file, "rb"))){ - opkg_msg(ERROR, "Can't open signature file %s.\n", sig_file); - goto verify_file_end; - } - - // Read the PKCS7 block contained in the sig file - p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); - if(!p7){ - opkg_msg(ERROR, "Can't read signature file %s (Corrupted ?).\n", - sig_file); - goto verify_file_end; - } + X509_STORE *store = NULL; + PKCS7 *p7 = NULL; + BIO *in = NULL, *indata = NULL; + + // Sig check failed by default ! + int status = -1; + + openssl_init(); + + // Set-up the key store + if (! + (store = + setup_verify(conf->signature_ca_file, conf->signature_ca_path))) { + opkg_msg(ERROR, "Can't open CA certificates.\n"); + goto verify_file_end; + } + // Open a BIO to read the sig file + if (!(in = BIO_new_file(sig_file, "rb"))) { + opkg_msg(ERROR, "Can't open signature file %s.\n", sig_file); + goto verify_file_end; + } + // Read the PKCS7 block contained in the sig file + p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); + if (!p7) { + opkg_msg(ERROR, "Can't read signature file %s (Corrupted ?).\n", + sig_file); + goto verify_file_end; + } #if defined(HAVE_PATHFINDER) - if(conf->check_x509_path){ - if(!pkcs7_pathfinder_verify_signers(p7)){ - opkg_msg(ERROR, "pkcs7_pathfinder_verify_signers: " - "Path verification failed.\n"); - goto verify_file_end; + if (conf->check_x509_path) { + if (!pkcs7_pathfinder_verify_signers(p7)) { + opkg_msg(ERROR, "pkcs7_pathfinder_verify_signers: " + "Path verification failed.\n"); + goto verify_file_end; + } } - } #endif - // Open the Package file to authenticate - if (!(indata = BIO_new_file(text_file, "rb"))){ - opkg_msg(ERROR, "Can't open file %s.\n", text_file); - goto verify_file_end; - } - - // Let's verify the autenticity ! - if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1){ - // Get Off My Lawn! - opkg_msg(ERROR, "Verification failure.\n"); - }else{ - // Victory ! - status = 0; - } + // Open the Package file to authenticate + if (!(indata = BIO_new_file(text_file, "rb"))) { + opkg_msg(ERROR, "Can't open file %s.\n", text_file); + goto verify_file_end; + } + // Let's verify the autenticity ! + if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1) { + // Get Off My Lawn! + opkg_msg(ERROR, "Verification failure.\n"); + } else { + // Victory ! + status = 0; + } verify_file_end: - BIO_free(in); - BIO_free(indata); - PKCS7_free(p7); - X509_STORE_free(store); + BIO_free(in); + BIO_free(indata); + PKCS7_free(p7); + X509_STORE_free(store); - return status; + return status; #else - /* mute `unused variable' warnings. */ - (void) sig_file; - (void) text_file; - (void) conf; - return 0; + /* mute `unused variable' warnings. */ + (void)sig_file; + (void)text_file; + (void)conf; + return 0; #endif } - #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL) -static void openssl_init(void){ - static int init = 0; - - if(!init){ - OPENSSL_config(NULL); - OpenSSL_add_all_algorithms(); - ERR_load_crypto_strings(); - init = 1; - } +static void openssl_init(void) +{ + static int init = 0; + + if (!init) { + OPENSSL_config(NULL); + OpenSSL_add_all_algorithms(); + ERR_load_crypto_strings(); + init = 1; + } } #endif - #if defined HAVE_OPENSSL -static X509_STORE * -setup_verify(char *CAfile, char *CApath) +static X509_STORE *setup_verify(char *CAfile, char *CApath) { - X509_STORE *store = NULL; - X509_LOOKUP *lookup = NULL; - - if(!(store = X509_STORE_new())){ - // Something bad is happening... - goto end; - } - - // adds the X509 file lookup method - lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file()); - if (lookup == NULL){ - goto end; - } - - // Autenticating against one CA file - if (CAfile) { - if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) { - // Invalid CA => Bye bye - opkg_msg(ERROR, "Error loading file %s.\n", CAfile); - goto end; - } - } else { - X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT); - } - - // Now look into CApath directory if supplied - lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir()); - if (lookup == NULL){ - goto end; - } - - if (CApath) { - if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) { - opkg_msg(ERROR, "Error loading directory %s.\n", CApath); - goto end; - } - } else { - X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT); - } - - // All right ! - ERR_clear_error(); - return store; + X509_STORE *store = NULL; + X509_LOOKUP *lookup = NULL; + + if (!(store = X509_STORE_new())) { + // Something bad is happening... + goto end; + } + // adds the X509 file lookup method + lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); + if (lookup == NULL) { + goto end; + } + // Autenticating against one CA file + if (CAfile) { + if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) { + // Invalid CA => Bye bye + opkg_msg(ERROR, "Error loading file %s.\n", CAfile); + goto end; + } + } else { + X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); + } + + // Now look into CApath directory if supplied + lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir()); + if (lookup == NULL) { + goto end; + } + + if (CApath) { + if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) { + opkg_msg(ERROR, "Error loading directory %s.\n", + CApath); + goto end; + } + } else { + X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); + } + + // All right ! + ERR_clear_error(); + return store; end: - X509_STORE_free(store); - return NULL; + X509_STORE_free(store); + return NULL; } #endif #ifdef HAVE_CURL -void opkg_curl_cleanup(void){ - if(curl != NULL){ - curl_easy_cleanup (curl); - curl = NULL; - } +void opkg_curl_cleanup(void) +{ + if (curl != NULL) { + curl_easy_cleanup(curl); + curl = NULL; + } } -static CURL * -opkg_curl_init(curl_progress_func cb, void *data) +static CURL *opkg_curl_init(curl_progress_func cb, void *data) { - if(curl == NULL){ - curl = curl_easy_init(); + if (curl == NULL) { + curl = curl_easy_init(); #ifdef HAVE_SSLCURL - openssl_init(); + openssl_init(); + + if (conf->ssl_engine) { + + /* use crypto engine */ + if (curl_easy_setopt + (curl, CURLOPT_SSLENGINE, + conf->ssl_engine) != CURLE_OK) { + opkg_msg(ERROR, + "Can't set crypto engine '%s'.\n", + conf->ssl_engine); + + opkg_curl_cleanup(); + return NULL; + } + /* set the crypto engine as default */ + if (curl_easy_setopt + (curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) { + opkg_msg(ERROR, + "Can't set crypto engine '%s' as default.\n", + conf->ssl_engine); + + opkg_curl_cleanup(); + return NULL; + } + } + + /* cert & key can only be in PEM case in the same file */ + if (conf->ssl_key_passwd) { + if (curl_easy_setopt + (curl, CURLOPT_SSLKEYPASSWD, + conf->ssl_key_passwd) != CURLE_OK) { + opkg_msg(DEBUG, + "Failed to set key password.\n"); + } + } - if (conf->ssl_engine) { - - /* use crypto engine */ - if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, conf->ssl_engine) != CURLE_OK){ - opkg_msg(ERROR, "Can't set crypto engine '%s'.\n", - conf->ssl_engine); - - opkg_curl_cleanup(); - return NULL; - } - /* set the crypto engine as default */ - if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK){ - opkg_msg(ERROR, "Can't set crypto engine '%s' as default.\n", - conf->ssl_engine); - - opkg_curl_cleanup(); - return NULL; - } - } - - /* cert & key can only be in PEM case in the same file */ - if(conf->ssl_key_passwd){ - if (curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, conf->ssl_key_passwd) != CURLE_OK) - { - opkg_msg(DEBUG, "Failed to set key password.\n"); - } - } - - /* sets the client certificate and its type */ - if(conf->ssl_cert_type){ - if (curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, conf->ssl_cert_type) != CURLE_OK) - { - opkg_msg(DEBUG, "Failed to set certificate format.\n"); - } - } - /* SSL cert name isn't mandatory */ - if(conf->ssl_cert){ - curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert); - } - - /* sets the client key and its type */ - if(conf->ssl_key_type){ - if (curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, conf->ssl_key_type) != CURLE_OK) - { - opkg_msg(DEBUG, "Failed to set key format.\n"); - } - } - if(conf->ssl_key){ - if (curl_easy_setopt(curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK) - { - opkg_msg(DEBUG, "Failed to set key.\n"); - } - } - - /* Should we verify the peer certificate ? */ - if(conf->ssl_dont_verify_peer){ - /* - * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10) - */ - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - }else{ + /* sets the client certificate and its type */ + if (conf->ssl_cert_type) { + if (curl_easy_setopt + (curl, CURLOPT_SSLCERTTYPE, + conf->ssl_cert_type) != CURLE_OK) { + opkg_msg(DEBUG, + "Failed to set certificate format.\n"); + } + } + /* SSL cert name isn't mandatory */ + if (conf->ssl_cert) { + curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert); + } + + /* sets the client key and its type */ + if (conf->ssl_key_type) { + if (curl_easy_setopt + (curl, CURLOPT_SSLKEYTYPE, + conf->ssl_key_type) != CURLE_OK) { + opkg_msg(DEBUG, "Failed to set key format.\n"); + } + } + if (conf->ssl_key) { + if (curl_easy_setopt + (curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK) { + opkg_msg(DEBUG, "Failed to set key.\n"); + } + } + + /* Should we verify the peer certificate ? */ + if (conf->ssl_dont_verify_peer) { + /* + * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10) + */ + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); + } else { #ifdef HAVE_PATHFINDER - if(conf->check_x509_path){ - if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){ - opkg_msg(DEBUG, "Failed to set ssl path verification callback.\n"); - }else{ - curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL); + if (conf->check_x509_path) { + if (curl_easy_setopt + (curl, CURLOPT_SSL_CTX_FUNCTION, + curl_ssl_ctx_function) != CURLE_OK) { + opkg_msg(DEBUG, + "Failed to set ssl path verification callback.\n"); + } else { + curl_easy_setopt(curl, + CURLOPT_SSL_CTX_DATA, + NULL); + } + } +#endif + } + + /* certification authority file and/or path */ + if (conf->ssl_ca_file) { + curl_easy_setopt(curl, CURLOPT_CAINFO, + conf->ssl_ca_file); + } + if (conf->ssl_ca_path) { + curl_easy_setopt(curl, CURLOPT_CAPATH, + conf->ssl_ca_path); } - } #endif - } - /* certification authority file and/or path */ - if(conf->ssl_ca_file){ - curl_easy_setopt(curl, CURLOPT_CAINFO, conf->ssl_ca_file); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); + if (conf->http_proxy || conf->ftp_proxy) { + char *userpwd; + sprintf_alloc(&userpwd, "%s:%s", conf->proxy_user, + conf->proxy_passwd); + curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd); + free(userpwd); + } } - if(conf->ssl_ca_path){ - curl_easy_setopt(curl, CURLOPT_CAPATH, conf->ssl_ca_path); + + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, (cb == NULL)); + if (cb) { + curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, data); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb); } -#endif - curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1); - if (conf->http_proxy || conf->ftp_proxy) - { - char *userpwd; - sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user, - conf->proxy_passwd); - curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd); - free (userpwd); - } - } - - curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL)); - if (cb) - { - curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data); - curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb); - } - - return curl; + return curl; } #endif diff --git a/libopkg/opkg_download.h b/libopkg/opkg_download.h index 91b990e..ac67ca4 100644 --- a/libopkg/opkg_download.h +++ b/libopkg/opkg_download.h @@ -21,18 +21,19 @@ #include "config.h" #include "pkg.h" -typedef void (*opkg_download_progress_callback)(int percent, char *url); -typedef int (*curl_progress_func)(void *data, double t, double d, double ultotal, double ulnow); +typedef void (*opkg_download_progress_callback) (int percent, char *url); +typedef int (*curl_progress_func) (void *data, double t, double d, + double ultotal, double ulnow); - -int opkg_download(const char *src, const char *dest_file_name, curl_progress_func cb, void *data, const short hide_error); -int opkg_download_pkg(pkg_t *pkg, const char *dir); +int opkg_download(const char *src, const char *dest_file_name, + curl_progress_func cb, void *data, const short hide_error); +int opkg_download_pkg(pkg_t * pkg, const char *dir); /* * Downloads file from url, installs in package database, return package name. */ int opkg_prepare_url_for_install(const char *url, char **namep); -int opkg_verify_file (char *text_file, char *sig_file); +int opkg_verify_file(char *text_file, char *sig_file); #ifdef HAVE_CURL void opkg_curl_cleanup(void); #endif diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c index ad1169f..76b658d 100644 --- a/libopkg/opkg_install.c +++ b/libopkg/opkg_install.c @@ -42,155 +42,155 @@ #include "xsystem.h" #include "libbb/libbb.h" -static int -satisfy_dependencies_for(pkg_t *pkg) +static int satisfy_dependencies_for(pkg_t * pkg) { - int i, err; - pkg_vec_t *depends = pkg_vec_alloc(); - pkg_t *dep; - char **tmp, **unresolved = NULL; - int ndepends; - - ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends, - &unresolved); - - if (unresolved) { - opkg_msg(ERROR, "Cannot satisfy the following dependencies for %s:\n", - pkg->name); - tmp = unresolved; - while (*unresolved) { - opkg_message(ERROR, "\t%s", *unresolved); - free(*unresolved); - unresolved++; - } - free(tmp); - opkg_message(ERROR, "\n"); - if (! conf->force_depends) { - opkg_msg(INFO, - "This could mean that your package list is out of date or that the packages\n" - "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n" - "of this problem try again with the '-force-depends' option.\n"); - pkg_vec_free(depends); - return -1; - } - } - - if (ndepends <= 0) { - pkg_vec_free(depends); - return 0; - } - - /* Mark packages as to-be-installed */ - for (i=0; i < depends->len; i++) { - /* Dependencies should be installed the same place as pkg */ - if (depends->pkgs[i]->dest == NULL) { - depends->pkgs[i]->dest = pkg->dest; - } - depends->pkgs[i]->state_want = SW_INSTALL; - } - - for (i = 0; i < depends->len; i++) { - dep = depends->pkgs[i]; - /* The package was uninstalled when we started, but another - dep earlier in this loop may have depended on it and pulled - it in, so check first. */ - if ((dep->state_status != SS_INSTALLED) - && (dep->state_status != SS_UNPACKED)) { - opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n"); - err = opkg_install_pkg(dep, 0); - /* mark this package as having been automatically installed to - * satisfy a dependancy */ - dep->auto_installed = 1; - if (err) { - pkg_vec_free(depends); - return err; - } - } - } - - pkg_vec_free(depends); - - return 0; + int i, err; + pkg_vec_t *depends = pkg_vec_alloc(); + pkg_t *dep; + char **tmp, **unresolved = NULL; + int ndepends; + + ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends, + &unresolved); + + if (unresolved) { + opkg_msg(ERROR, + "Cannot satisfy the following dependencies for %s:\n", + pkg->name); + tmp = unresolved; + while (*unresolved) { + opkg_message(ERROR, "\t%s", *unresolved); + free(*unresolved); + unresolved++; + } + free(tmp); + opkg_message(ERROR, "\n"); + if (!conf->force_depends) { + opkg_msg(INFO, + "This could mean that your package list is out of date or that the packages\n" + "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n" + "of this problem try again with the '-force-depends' option.\n"); + pkg_vec_free(depends); + return -1; + } + } + + if (ndepends <= 0) { + pkg_vec_free(depends); + return 0; + } + + /* Mark packages as to-be-installed */ + for (i = 0; i < depends->len; i++) { + /* Dependencies should be installed the same place as pkg */ + if (depends->pkgs[i]->dest == NULL) { + depends->pkgs[i]->dest = pkg->dest; + } + depends->pkgs[i]->state_want = SW_INSTALL; + } + + for (i = 0; i < depends->len; i++) { + dep = depends->pkgs[i]; + /* The package was uninstalled when we started, but another + dep earlier in this loop may have depended on it and pulled + it in, so check first. */ + if ((dep->state_status != SS_INSTALLED) + && (dep->state_status != SS_UNPACKED)) { + opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n"); + err = opkg_install_pkg(dep, 0); + /* mark this package as having been automatically installed to + * satisfy a dependancy */ + dep->auto_installed = 1; + if (err) { + pkg_vec_free(depends); + return err; + } + } + } + + pkg_vec_free(depends); + + return 0; } -static int -check_conflicts_for(pkg_t *pkg) +static int check_conflicts_for(pkg_t * pkg) { - int i; - pkg_vec_t *conflicts = NULL; - message_level_t level; - - if (conf->force_depends) { - level = NOTICE; - } else { - level = ERROR; - } - - if (!conf->force_depends) - conflicts = pkg_hash_fetch_conflicts(pkg); - - if (conflicts) { - opkg_msg(level, "The following packages conflict with %s:\n", - pkg->name); - i = 0; - while (i < conflicts->len) - opkg_msg(level, "\t%s", conflicts->pkgs[i++]->name); - opkg_message(level, "\n"); - pkg_vec_free(conflicts); - return -1; - } - return 0; + int i; + pkg_vec_t *conflicts = NULL; + message_level_t level; + + if (conf->force_depends) { + level = NOTICE; + } else { + level = ERROR; + } + + if (!conf->force_depends) + conflicts = pkg_hash_fetch_conflicts(pkg); + + if (conflicts) { + opkg_msg(level, "The following packages conflict with %s:\n", + pkg->name); + i = 0; + while (i < conflicts->len) + opkg_msg(level, "\t%s", conflicts->pkgs[i++]->name); + opkg_message(level, "\n"); + pkg_vec_free(conflicts); + return -1; + } + return 0; } -static int -update_file_ownership(pkg_t *new_pkg, pkg_t *old_pkg) +static int update_file_ownership(pkg_t * new_pkg, pkg_t * old_pkg) { - str_list_t *new_list, *old_list; - str_list_elt_t *iter, *niter; - - new_list = pkg_get_installed_files(new_pkg); - if (new_list == NULL) - return -1; - - for (iter = str_list_first(new_list), niter = str_list_next(new_list, iter); - iter; - iter = niter, niter = str_list_next(new_list, niter)) { - char *new_file = (char *)iter->data; - pkg_t *owner = file_hash_get_file_owner(new_file); - pkg_t *obs = hash_table_get(&conf->obs_file_hash, new_file); - - opkg_msg(DEBUG2, "%s: new_pkg=%s wants file %s, from owner=%s\n", - __func__, new_pkg->name, new_file, owner?owner->name:""); - - if (!owner || (owner == old_pkg) || obs) - file_hash_set_file_owner(new_file, new_pkg); - } - - if (old_pkg) { - old_list = pkg_get_installed_files(old_pkg); - if (old_list == NULL) { - pkg_free_installed_files(new_pkg); - return -1; - } - - for (iter = str_list_first(old_list), niter = str_list_next(old_list, iter); - iter; - iter = niter, niter = str_list_next(old_list, niter)) { - char *old_file = (char *)iter->data; - pkg_t *owner = file_hash_get_file_owner(old_file); - if (!owner || (owner == old_pkg)) { - /* obsolete */ - hash_table_insert(&conf->obs_file_hash, old_file, old_pkg); - } - } - pkg_free_installed_files(old_pkg); - } - pkg_free_installed_files(new_pkg); - return 0; + str_list_t *new_list, *old_list; + str_list_elt_t *iter, *niter; + + new_list = pkg_get_installed_files(new_pkg); + if (new_list == NULL) + return -1; + + for (iter = str_list_first(new_list), niter = + str_list_next(new_list, iter); iter; + iter = niter, niter = str_list_next(new_list, niter)) { + char *new_file = (char *)iter->data; + pkg_t *owner = file_hash_get_file_owner(new_file); + pkg_t *obs = hash_table_get(&conf->obs_file_hash, new_file); + + opkg_msg(DEBUG2, + "%s: new_pkg=%s wants file %s, from owner=%s\n", + __func__, new_pkg->name, new_file, + owner ? owner->name : ""); + + if (!owner || (owner == old_pkg) || obs) + file_hash_set_file_owner(new_file, new_pkg); + } + + if (old_pkg) { + old_list = pkg_get_installed_files(old_pkg); + if (old_list == NULL) { + pkg_free_installed_files(new_pkg); + return -1; + } + + for (iter = str_list_first(old_list), niter = + str_list_next(old_list, iter); iter; + iter = niter, niter = str_list_next(old_list, niter)) { + char *old_file = (char *)iter->data; + pkg_t *owner = file_hash_get_file_owner(old_file); + if (!owner || (owner == old_pkg)) { + /* obsolete */ + hash_table_insert(&conf->obs_file_hash, + old_file, old_pkg); + } + } + pkg_free_installed_files(old_pkg); + } + pkg_free_installed_files(new_pkg); + return 0; } -static int -verify_pkg_installable(pkg_t *pkg) +static int verify_pkg_installable(pkg_t * pkg) { unsigned long kbs_available, pkg_size_kbs; char *root_dir = NULL; @@ -199,8 +199,7 @@ verify_pkg_installable(pkg_t *pkg) if (conf->force_space || pkg->installed_size == 0) return 0; - if (pkg->dest) - { + if (pkg->dest) { if (!strcmp(pkg->dest->name, "root") && conf->overlay_root && !stat(conf->overlay_root, &s) && (s.st_mode & S_IFDIR)) root_dir = conf->overlay_root; @@ -213,142 +212,139 @@ verify_pkg_installable(pkg_t *pkg) kbs_available = get_available_kbytes(root_dir); - pkg_size_kbs = (pkg->installed_size + 1023)/1024; + pkg_size_kbs = (pkg->installed_size + 1023) / 1024; if (pkg_size_kbs >= kbs_available) { opkg_msg(ERROR, "Only have %ldkb available on filesystem %s, " - "pkg %s needs %ld\n", - kbs_available, root_dir, pkg->name, pkg_size_kbs); + "pkg %s needs %ld\n", + kbs_available, root_dir, pkg->name, pkg_size_kbs); return -1; } - return 0; + return 0; } -static int -unpack_pkg_control_files(pkg_t *pkg) +static int unpack_pkg_control_files(pkg_t * pkg) { - int err; - char *conffiles_file_name; - char *root_dir; - FILE *conffiles_file; - - sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name); - - pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir); - if (pkg->tmp_unpack_dir == NULL) { - opkg_perror(ERROR, "Failed to create temporary directory '%s'", - pkg->tmp_unpack_dir); - return -1; - } - - err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir); - if (err) { - return err; - } - - /* XXX: CLEANUP: There might be a cleaner place to read in the - conffiles. Seems like I should be able to get everything to go - through pkg_init_from_file. If so, maybe it would make sense to - move all of unpack_pkg_control_files to that function. */ - - /* Don't need to re-read conffiles if we already have it */ - if (!nv_pair_list_empty(&pkg->conffiles)) { - return 0; - } - - sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir); - if (! file_exists(conffiles_file_name)) { - free(conffiles_file_name); - return 0; - } - - conffiles_file = fopen(conffiles_file_name, "r"); - if (conffiles_file == NULL) { - opkg_perror(ERROR, "Failed to open %s", conffiles_file_name); - free(conffiles_file_name); - return -1; - } - free(conffiles_file_name); - - while (1) { - char *cf_name; - char *cf_name_in_dest; - int i; - - cf_name = file_read_line_alloc(conffiles_file); - if (cf_name == NULL) { - break; - } - if (cf_name[0] == '\0') { - continue; - } - for (i = strlen(cf_name) - 1; - (i >= 0) && (cf_name[i] == ' ' || cf_name[i] == '\t'); - i-- - ) { - cf_name[i] = '\0'; - } - - /* Prepend dest->root_dir to conffile name. - Take pains to avoid multiple slashes. */ - root_dir = pkg->dest->root_dir; - if (conf->offline_root) - /* skip the offline_root prefix */ - root_dir = pkg->dest->root_dir + strlen(conf->offline_root); - sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir, - cf_name[0] == '/' ? (cf_name + 1) : cf_name); - - /* Can't get an md5sum now, (file isn't extracted yet). - We'll wait until resolve_conffiles */ - conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL); - - free(cf_name); - free(cf_name_in_dest); - } - - fclose(conffiles_file); - - return 0; + int err; + char *conffiles_file_name; + char *root_dir; + FILE *conffiles_file; + + sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, + pkg->name); + + pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir); + if (pkg->tmp_unpack_dir == NULL) { + opkg_perror(ERROR, "Failed to create temporary directory '%s'", + pkg->tmp_unpack_dir); + return -1; + } + + err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir); + if (err) { + return err; + } + + /* XXX: CLEANUP: There might be a cleaner place to read in the + conffiles. Seems like I should be able to get everything to go + through pkg_init_from_file. If so, maybe it would make sense to + move all of unpack_pkg_control_files to that function. */ + + /* Don't need to re-read conffiles if we already have it */ + if (!nv_pair_list_empty(&pkg->conffiles)) { + return 0; + } + + sprintf_alloc(&conffiles_file_name, "%s/conffiles", + pkg->tmp_unpack_dir); + if (!file_exists(conffiles_file_name)) { + free(conffiles_file_name); + return 0; + } + + conffiles_file = fopen(conffiles_file_name, "r"); + if (conffiles_file == NULL) { + opkg_perror(ERROR, "Failed to open %s", conffiles_file_name); + free(conffiles_file_name); + return -1; + } + free(conffiles_file_name); + + while (1) { + char *cf_name; + char *cf_name_in_dest; + int i; + + cf_name = file_read_line_alloc(conffiles_file); + if (cf_name == NULL) { + break; + } + if (cf_name[0] == '\0') { + continue; + } + for (i = strlen(cf_name) - 1; + (i >= 0) && (cf_name[i] == ' ' || cf_name[i] == '\t'); + i--) { + cf_name[i] = '\0'; + } + + /* Prepend dest->root_dir to conffile name. + Take pains to avoid multiple slashes. */ + root_dir = pkg->dest->root_dir; + if (conf->offline_root) + /* skip the offline_root prefix */ + root_dir = + pkg->dest->root_dir + strlen(conf->offline_root); + sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir, + cf_name[0] == '/' ? (cf_name + 1) : cf_name); + + /* Can't get an md5sum now, (file isn't extracted yet). + We'll wait until resolve_conffiles */ + conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL); + + free(cf_name); + free(cf_name_in_dest); + } + + fclose(conffiles_file); + + return 0; } /* * Remove packages which were auto_installed due to a dependency by old_pkg, * which are no longer a dependency in the new (upgraded) pkg. */ -static int -pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg) +static int pkg_remove_orphan_dependent(pkg_t * pkg, pkg_t * old_pkg) { - int i, j, k, l, found,r, err = 0; + int i, j, k, l, found, r, err = 0; int n_deps; pkg_t *p; struct compound_depend *cd0, *cd1; - abstract_pkg_t **dependents; + abstract_pkg_t **dependents; int count0 = old_pkg->pre_depends_count + - old_pkg->depends_count + - old_pkg->recommends_count + - old_pkg->suggests_count; + old_pkg->depends_count + + old_pkg->recommends_count + old_pkg->suggests_count; int count1 = pkg->pre_depends_count + - pkg->depends_count + - pkg->recommends_count + - pkg->suggests_count; + pkg->depends_count + pkg->recommends_count + pkg->suggests_count; - for (i=0; idepends[i]; if (cd0->type != DEPEND) continue; - for (j=0; jpossibility_count; j++) { + for (j = 0; j < cd0->possibility_count; j++) { found = 0; - for (k=0; kdepends[k]; if (cd1->type != DEPEND) continue; - for (l=0; lpossibility_count; l++) { + for (l = 0; l < cd1->possibility_count; l++) { if (cd0->possibilities[j] - == cd1->possibilities[l]) { + == cd1->possibilities[l]) { found = 1; break; } @@ -363,8 +359,9 @@ pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg) /* * old_pkg has a dependency that pkg does not. */ - p = pkg_hash_fetch_installed_by_name( - cd0->possibilities[j]->pkg->name); + p = pkg_hash_fetch_installed_by_name(cd0-> + possibilities[j]-> + pkg->name); if (!p) continue; @@ -373,12 +370,11 @@ pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg) continue; n_deps = pkg_has_installed_dependents(p, &dependents); - n_deps--; /* don't count old_pkg */ + n_deps--; /* don't count old_pkg */ if (n_deps == 0) { opkg_msg(NOTICE, "%s was autoinstalled and is " - "now orphaned, removing.\n", - p->name); + "now orphaned, removing.\n", p->name); /* p has one installed dependency (old_pkg), * which we need to ignore during removal. */ @@ -389,9 +385,9 @@ pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg) err = r; } else opkg_msg(INFO, "%s was autoinstalled and is " - "still required by %d " - "installed packages.\n", - p->name, n_deps); + "still required by %d " + "installed packages.\n", + p->name, n_deps); } } @@ -401,1199 +397,1183 @@ pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg) /* returns number of installed replacees */ static int -pkg_get_installed_replacees(pkg_t *pkg, pkg_vec_t *installed_replacees) +pkg_get_installed_replacees(pkg_t * pkg, pkg_vec_t * installed_replacees) { - abstract_pkg_t **replaces = pkg->replaces; - int replaces_count = pkg->replaces_count; - int i, j; - for (i = 0; i < replaces_count; i++) { - abstract_pkg_t *ab_pkg = replaces[i]; - pkg_vec_t *pkg_vec = ab_pkg->pkgs; - if (pkg_vec) { - for (j = 0; j < pkg_vec->len; j++) { - pkg_t *replacee = pkg_vec->pkgs[j]; - if (!pkg_conflicts(pkg, replacee)) - continue; - if (replacee->state_status == SS_INSTALLED) { - pkg_vec_insert(installed_replacees, replacee); - } - } - } - } - return installed_replacees->len; + abstract_pkg_t **replaces = pkg->replaces; + int replaces_count = pkg->replaces_count; + int i, j; + for (i = 0; i < replaces_count; i++) { + abstract_pkg_t *ab_pkg = replaces[i]; + pkg_vec_t *pkg_vec = ab_pkg->pkgs; + if (pkg_vec) { + for (j = 0; j < pkg_vec->len; j++) { + pkg_t *replacee = pkg_vec->pkgs[j]; + if (!pkg_conflicts(pkg, replacee)) + continue; + if (replacee->state_status == SS_INSTALLED) { + pkg_vec_insert(installed_replacees, + replacee); + } + } + } + } + return installed_replacees->len; } -static int -pkg_remove_installed_replacees(pkg_vec_t *replacees) +static int pkg_remove_installed_replacees(pkg_vec_t * replacees) { - int i; - int replaces_count = replacees->len; - for (i = 0; i < replaces_count; i++) { - pkg_t *replacee = replacees->pkgs[i]; - int err; - replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */ - err = opkg_remove_pkg(replacee, 0); - if (err) - return err; - } - return 0; + int i; + int replaces_count = replacees->len; + for (i = 0; i < replaces_count; i++) { + pkg_t *replacee = replacees->pkgs[i]; + int err; + replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */ + err = opkg_remove_pkg(replacee, 0); + if (err) + return err; + } + return 0; } /* to unwind the removal: make sure they are installed */ -static int -pkg_remove_installed_replacees_unwind(pkg_vec_t *replacees) +static int pkg_remove_installed_replacees_unwind(pkg_vec_t * replacees) { - int i, err; - int replaces_count = replacees->len; - for (i = 0; i < replaces_count; i++) { - pkg_t *replacee = replacees->pkgs[i]; - if (replacee->state_status != SS_INSTALLED) { - opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n"); - err = opkg_install_pkg(replacee, 0); - if (err) - return err; - } - } - return 0; + int i, err; + int replaces_count = replacees->len; + for (i = 0; i < replaces_count; i++) { + pkg_t *replacee = replacees->pkgs[i]; + if (replacee->state_status != SS_INSTALLED) { + opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n"); + err = opkg_install_pkg(replacee, 0); + if (err) + return err; + } + } + return 0; } /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */ static int -opkg_install_check_downgrade(pkg_t *pkg, pkg_t *old_pkg, int message) +opkg_install_check_downgrade(pkg_t * pkg, pkg_t * old_pkg, int message) { - if (old_pkg) { - char message_out[15]; - char *old_version = pkg_version_str_alloc(old_pkg); - char *new_version = pkg_version_str_alloc(pkg); - int cmp = pkg_compare_versions(old_pkg, pkg); - int rc = 0; - - memset(message_out,'\x0',15); - strncpy (message_out,"Upgrading ",strlen("Upgrading ")); - if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */ - cmp = -1 ; /* then we force opkg to downgrade */ - strncpy (message_out,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */ - /* reinstall, and some check could fail asking the "force-reinstall" option */ - } - - if (cmp > 0) { - if(!conf->download_only) - opkg_msg(NOTICE, - "Not downgrading package %s on %s from %s to %s.\n", - old_pkg->name, old_pkg->dest->name, old_version, new_version); - rc = 1; - } else if (cmp < 0) { - if(!conf->download_only) - opkg_msg(NOTICE, "%s%s on %s from %s to %s...\n", - message_out, pkg->name, old_pkg->dest->name, old_version, new_version); - pkg->dest = old_pkg->dest; - rc = 0; - } else /* cmp == 0 */ { - if(!conf->download_only) - opkg_msg(NOTICE, "%s (%s) already install on %s.\n", - pkg->name, new_version, old_pkg->dest->name); - rc = 1; - } - free(old_version); - free(new_version); - return rc; - } else { - char message_out[15] ; - memset(message_out,'\x0',15); - if ( message ) - strncpy( message_out,"Upgrading ",strlen("Upgrading ") ); - else - strncpy( message_out,"Installing ",strlen("Installing ") ); - char *version = pkg_version_str_alloc(pkg); - - if(!conf->download_only) - opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out, - pkg->name, version, pkg->dest->name); - free(version); - } - return 0; -} + if (old_pkg) { + char message_out[15]; + char *old_version = pkg_version_str_alloc(old_pkg); + char *new_version = pkg_version_str_alloc(pkg); + int cmp = pkg_compare_versions(old_pkg, pkg); + int rc = 0; + + memset(message_out, '\x0', 15); + strncpy(message_out, "Upgrading ", strlen("Upgrading ")); + if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */ + cmp = -1; /* then we force opkg to downgrade */ + strncpy(message_out, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */ + /* reinstall, and some check could fail asking the "force-reinstall" option */ + } + if (cmp > 0) { + if (!conf->download_only) + opkg_msg(NOTICE, + "Not downgrading package %s on %s from %s to %s.\n", + old_pkg->name, old_pkg->dest->name, + old_version, new_version); + rc = 1; + } else if (cmp < 0) { + if (!conf->download_only) + opkg_msg(NOTICE, + "%s%s on %s from %s to %s...\n", + message_out, pkg->name, + old_pkg->dest->name, old_version, + new_version); + pkg->dest = old_pkg->dest; + rc = 0; + } else { /* cmp == 0 */ + + if (!conf->download_only) + opkg_msg(NOTICE, + "%s (%s) already install on %s.\n", + pkg->name, new_version, + old_pkg->dest->name); + rc = 1; + } + free(old_version); + free(new_version); + return rc; + } else { + char message_out[15]; + memset(message_out, '\x0', 15); + if (message) + strncpy(message_out, "Upgrading ", + strlen("Upgrading ")); + else + strncpy(message_out, "Installing ", + strlen("Installing ")); + char *version = pkg_version_str_alloc(pkg); + + if (!conf->download_only) + opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out, + pkg->name, version, pkg->dest->name); + free(version); + } + return 0; +} -static int -prerm_upgrade_old_pkg(pkg_t *pkg, pkg_t *old_pkg) +static int prerm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg) { - /* DPKG_INCOMPATIBILITY: - dpkg does some things here that we don't do yet. Do we care? + /* DPKG_INCOMPATIBILITY: + dpkg does some things here that we don't do yet. Do we care? - 1. If a version of the package is already installed, call + 1. If a version of the package is already installed, call old-prerm upgrade new-version - 2. If the script runs but exits with a non-zero exit status + 2. If the script runs but exits with a non-zero exit status new-prerm failed-upgrade old-version Error unwind, for both the above cases: old-postinst abort-upgrade new-version - */ - int err; - char *script_args; - char *new_version; - - if (!old_pkg || !pkg) - return 0; - - new_version = pkg_version_str_alloc(pkg); - - sprintf_alloc(&script_args, "upgrade %s", new_version); - free(new_version); - err = pkg_run_script(old_pkg, "prerm", script_args); - free(script_args); - if (err != 0) { - opkg_msg(ERROR, "prerm script for package \"%s\" failed\n", - old_pkg->name); - return -1; - } - return 0; + */ + int err; + char *script_args; + char *new_version; + + if (!old_pkg || !pkg) + return 0; + + new_version = pkg_version_str_alloc(pkg); + + sprintf_alloc(&script_args, "upgrade %s", new_version); + free(new_version); + err = pkg_run_script(old_pkg, "prerm", script_args); + free(script_args); + if (err != 0) { + opkg_msg(ERROR, "prerm script for package \"%s\" failed\n", + old_pkg->name); + return -1; + } + return 0; } -static int -prerm_upgrade_old_pkg_unwind(pkg_t *pkg, pkg_t *old_pkg) +static int prerm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg) { - /* DPKG_INCOMPATIBILITY: - dpkg does some things here that we don't do yet. Do we care? - (See prerm_upgrade_old_package for details) - */ - return 0; + /* DPKG_INCOMPATIBILITY: + dpkg does some things here that we don't do yet. Do we care? + (See prerm_upgrade_old_package for details) + */ + return 0; } -static int -prerm_deconfigure_conflictors(pkg_t *pkg, pkg_vec_t *conflictors) +static int prerm_deconfigure_conflictors(pkg_t * pkg, pkg_vec_t * conflictors) { - /* DPKG_INCOMPATIBILITY: - dpkg does some things here that we don't do yet. Do we care? - 2. If a 'conflicting' package is being removed at the same time: - 1. If any packages depended on that conflicting package and - --auto-deconfigure is specified, call, for each such package: - deconfigured's-prerm deconfigure \ - in-favour package-being-installed version \ - removing conflicting-package version - Error unwind: - deconfigured's-postinst abort-deconfigure \ - in-favour package-being-installed-but-failed version \ - removing conflicting-package version - - The deconfigured packages are marked as requiring - configuration, so that if --install is used they will be - configured again if possible. - 2. To prepare for removal of the conflicting package, call: - conflictor's-prerm remove in-favour package new-version - Error unwind: - conflictor's-postinst abort-remove in-favour package new-version - */ - return 0; + /* DPKG_INCOMPATIBILITY: + dpkg does some things here that we don't do yet. Do we care? + 2. If a 'conflicting' package is being removed at the same time: + 1. If any packages depended on that conflicting package and + --auto-deconfigure is specified, call, for each such package: + deconfigured's-prerm deconfigure \ + in-favour package-being-installed version \ + removing conflicting-package version + Error unwind: + deconfigured's-postinst abort-deconfigure \ + in-favour package-being-installed-but-failed version \ + removing conflicting-package version + + The deconfigured packages are marked as requiring + configuration, so that if --install is used they will be + configured again if possible. + 2. To prepare for removal of the conflicting package, call: + conflictor's-prerm remove in-favour package new-version + Error unwind: + conflictor's-postinst abort-remove in-favour package new-version + */ + return 0; } static int -prerm_deconfigure_conflictors_unwind(pkg_t *pkg, pkg_vec_t *conflictors) +prerm_deconfigure_conflictors_unwind(pkg_t * pkg, pkg_vec_t * conflictors) { - /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't - do yet. Do we care? (See prerm_deconfigure_conflictors for - details) */ - return 0; + /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't + do yet. Do we care? (See prerm_deconfigure_conflictors for + details) */ + return 0; } -static int -preinst_configure(pkg_t *pkg, pkg_t *old_pkg) +static int preinst_configure(pkg_t * pkg, pkg_t * old_pkg) { - int err; - char *preinst_args; - - if (old_pkg) { - char *old_version = pkg_version_str_alloc(old_pkg); - sprintf_alloc(&preinst_args, "upgrade %s", old_version); - free(old_version); - } else if (pkg->state_status == SS_CONFIG_FILES) { - char *pkg_version = pkg_version_str_alloc(pkg); - sprintf_alloc(&preinst_args, "install %s", pkg_version); - free(pkg_version); - } else { - preinst_args = xstrdup("install"); - } - - err = pkg_run_script(pkg, "preinst", preinst_args); - if (err) { - opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name); - return -1; - } - - free(preinst_args); - - return 0; + int err; + char *preinst_args; + + if (old_pkg) { + char *old_version = pkg_version_str_alloc(old_pkg); + sprintf_alloc(&preinst_args, "upgrade %s", old_version); + free(old_version); + } else if (pkg->state_status == SS_CONFIG_FILES) { + char *pkg_version = pkg_version_str_alloc(pkg); + sprintf_alloc(&preinst_args, "install %s", pkg_version); + free(pkg_version); + } else { + preinst_args = xstrdup("install"); + } + + err = pkg_run_script(pkg, "preinst", preinst_args); + if (err) { + opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name); + return -1; + } + + free(preinst_args); + + return 0; } -static int -preinst_configure_unwind(pkg_t *pkg, pkg_t *old_pkg) +static int preinst_configure_unwind(pkg_t * pkg, pkg_t * old_pkg) { - /* DPKG_INCOMPATIBILITY: - dpkg does the following error unwind, should we? - pkg->postrm abort-upgrade old-version - OR pkg->postrm abort-install old-version - OR pkg->postrm abort-install - */ - return 0; + /* DPKG_INCOMPATIBILITY: + dpkg does the following error unwind, should we? + pkg->postrm abort-upgrade old-version + OR pkg->postrm abort-install old-version + OR pkg->postrm abort-install + */ + return 0; } -static char * -backup_filename_alloc(const char *file_name) +static char *backup_filename_alloc(const char *file_name) { - char *backup; + char *backup; - sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX); + sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX); - return backup; + return backup; } - -static int -backup_make_backup(const char *file_name) +static int backup_make_backup(const char *file_name) { - int err; - char *backup; + int err; + char *backup; - backup = backup_filename_alloc(file_name); - err = file_copy(file_name, backup); - if (err) { - opkg_msg(ERROR, "Failed to copy %s to %s\n", - file_name, backup); - } + backup = backup_filename_alloc(file_name); + err = file_copy(file_name, backup); + if (err) { + opkg_msg(ERROR, "Failed to copy %s to %s\n", file_name, backup); + } - free(backup); + free(backup); - return err; + return err; } -static int -backup_exists_for(const char *file_name) +static int backup_exists_for(const char *file_name) { - int ret; - char *backup; + int ret; + char *backup; - backup = backup_filename_alloc(file_name); + backup = backup_filename_alloc(file_name); - ret = file_exists(backup); + ret = file_exists(backup); - free(backup); + free(backup); - return ret; + return ret; } -static int -backup_remove(const char *file_name) +static int backup_remove(const char *file_name) { - char *backup; + char *backup; - backup = backup_filename_alloc(file_name); - unlink(backup); - free(backup); + backup = backup_filename_alloc(file_name); + unlink(backup); + free(backup); - return 0; + return 0; } -static int -backup_modified_conffiles(pkg_t *pkg, pkg_t *old_pkg) +static int backup_modified_conffiles(pkg_t * pkg, pkg_t * old_pkg) { - int err; - conffile_list_elt_t *iter; - conffile_t *cf; - - if (conf->noaction) return 0; - - /* Backup all modified conffiles */ - if (old_pkg) { - for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) { - char *cf_name; - - cf = iter->data; - cf_name = root_filename_alloc(cf->name); - - /* Don't worry if the conffile is just plain gone */ - if (file_exists(cf_name) && conffile_has_been_modified(cf)) { - err = backup_make_backup(cf_name); - if (err) { - return err; - } - } - free(cf_name); - } - } - - /* Backup all conffiles that were not conffiles in old_pkg */ - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - char *cf_name; - cf = (conffile_t *)iter->data; - cf_name = root_filename_alloc(cf->name); - /* Ignore if this was a conffile in old_pkg as well */ - if (pkg_get_conffile(old_pkg, cf->name)) { - continue; - } - - if (file_exists(cf_name) && (! backup_exists_for(cf_name))) { - err = backup_make_backup(cf_name); - if (err) { - return err; - } - } - free(cf_name); - } - - return 0; + int err; + conffile_list_elt_t *iter; + conffile_t *cf; + + if (conf->noaction) + return 0; + + /* Backup all modified conffiles */ + if (old_pkg) { + for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; + iter = nv_pair_list_next(&old_pkg->conffiles, iter)) { + char *cf_name; + + cf = iter->data; + cf_name = root_filename_alloc(cf->name); + + /* Don't worry if the conffile is just plain gone */ + if (file_exists(cf_name) + && conffile_has_been_modified(cf)) { + err = backup_make_backup(cf_name); + if (err) { + return err; + } + } + free(cf_name); + } + } + + /* Backup all conffiles that were not conffiles in old_pkg */ + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + char *cf_name; + cf = (conffile_t *) iter->data; + cf_name = root_filename_alloc(cf->name); + /* Ignore if this was a conffile in old_pkg as well */ + if (pkg_get_conffile(old_pkg, cf->name)) { + continue; + } + + if (file_exists(cf_name) && (!backup_exists_for(cf_name))) { + err = backup_make_backup(cf_name); + if (err) { + return err; + } + } + free(cf_name); + } + + return 0; } -static int -backup_modified_conffiles_unwind(pkg_t *pkg, pkg_t *old_pkg) +static int backup_modified_conffiles_unwind(pkg_t * pkg, pkg_t * old_pkg) { - conffile_list_elt_t *iter; + conffile_list_elt_t *iter; - if (old_pkg) { - for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) { - backup_remove(((nv_pair_t *)iter->data)->name); - } - } + if (old_pkg) { + for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; + iter = nv_pair_list_next(&old_pkg->conffiles, iter)) { + backup_remove(((nv_pair_t *) iter->data)->name); + } + } - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - backup_remove(((nv_pair_t *)iter->data)->name); - } + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + backup_remove(((nv_pair_t *) iter->data)->name); + } - return 0; + return 0; } - -static int -check_data_file_clashes(pkg_t *pkg, pkg_t *old_pkg) +static int check_data_file_clashes(pkg_t * pkg, pkg_t * old_pkg) { - /* DPKG_INCOMPATIBILITY: - opkg takes a slightly different approach than dpkg at this - point. dpkg installs each file in the new package while - creating a backup for any file that is replaced, (so that it - can unwind if necessary). To avoid complexity and redundant - storage, opkg doesn't do any installation until later, (at the - point at which dpkg removes the backups. - - But, we do have to check for data file clashes, since after - installing a package with a file clash, removing either of the - packages involved in the clash has the potential to break the - other package. - */ - str_list_t *files_list; - str_list_elt_t *iter, *niter; - char *filename; - int clashes = 0; - - files_list = pkg_get_installed_files(pkg); - if (files_list == NULL) - return -1; - - for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); - iter; - iter = niter, niter = str_list_next(files_list, iter)) { - filename = (char *) iter->data; - if (file_exists(filename) && (! file_is_dir(filename))) { - pkg_t *owner; - pkg_t *obs; - - if (backup_exists_for(filename)) { - continue; - } - - /* Pre-existing files are OK if force-overwrite was asserted. */ - if (conf->force_overwrite) { - /* but we need to change who owns this file */ - file_hash_set_file_owner(filename, pkg); - continue; - } - - owner = file_hash_get_file_owner(filename); - - /* Pre-existing files are OK if owned by the pkg being upgraded. */ - if (owner && old_pkg) { - if (strcmp(owner->name, old_pkg->name) == 0) { - continue; - } - } - - /* Pre-existing files are OK if owned by a package replaced by new pkg. */ - if (owner) { - opkg_msg(DEBUG2, "Checking replaces for %s in package %s\n", - filename, owner->name); - if (pkg_replaces(pkg, owner)) { - continue; - } + /* DPKG_INCOMPATIBILITY: + opkg takes a slightly different approach than dpkg at this + point. dpkg installs each file in the new package while + creating a backup for any file that is replaced, (so that it + can unwind if necessary). To avoid complexity and redundant + storage, opkg doesn't do any installation until later, (at the + point at which dpkg removes the backups. + + But, we do have to check for data file clashes, since after + installing a package with a file clash, removing either of the + packages involved in the clash has the potential to break the + other package. + */ + str_list_t *files_list; + str_list_elt_t *iter, *niter; + char *filename; + int clashes = 0; + + files_list = pkg_get_installed_files(pkg); + if (files_list == NULL) + return -1; + + for (iter = str_list_first(files_list), niter = + str_list_next(files_list, iter); iter; + iter = niter, niter = str_list_next(files_list, iter)) { + filename = (char *)iter->data; + if (file_exists(filename) && (!file_is_dir(filename))) { + pkg_t *owner; + pkg_t *obs; + + if (backup_exists_for(filename)) { + continue; + } + + /* Pre-existing files are OK if force-overwrite was asserted. */ + if (conf->force_overwrite) { + /* but we need to change who owns this file */ + file_hash_set_file_owner(filename, pkg); + continue; + } + + owner = file_hash_get_file_owner(filename); + + /* Pre-existing files are OK if owned by the pkg being upgraded. */ + if (owner && old_pkg) { + if (strcmp(owner->name, old_pkg->name) == 0) { + continue; + } + } + + /* Pre-existing files are OK if owned by a package replaced by new pkg. */ + if (owner) { + opkg_msg(DEBUG2, + "Checking replaces for %s in package %s\n", + filename, owner->name); + if (pkg_replaces(pkg, owner)) { + continue; + } /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar ) then it's ok to overwrite. */ - if (strcmp(owner->name,pkg->name)==0){ - opkg_msg(INFO, "Replacing pre-existing file %s" - " owned by package %s\n", - filename, owner->name); - continue; - } - } - - /* Pre-existing files are OK if they are obsolete */ - obs = hash_table_get(&conf->obs_file_hash, filename); - if (obs) { - opkg_msg(INFO, "Pre-exiting file %s is obsolete." - " obs_pkg=%s\n", - filename, obs->name); - continue; - } - - /* We have found a clash. */ - opkg_msg(ERROR, "Package %s wants to install file %s\n" - "\tBut that file is already provided by package ", - pkg->name, filename); - if (owner) { - opkg_message(ERROR, "%s\n", owner->name); - } else { - opkg_message(ERROR, "\n" - "Please move this file out of the way and try again.\n"); - } - clashes++; - } - } - pkg_free_installed_files(pkg); - - return clashes; + if (strcmp(owner->name, pkg->name) == 0) { + opkg_msg(INFO, + "Replacing pre-existing file %s" + " owned by package %s\n", + filename, owner->name); + continue; + } + } + + /* Pre-existing files are OK if they are obsolete */ + obs = hash_table_get(&conf->obs_file_hash, filename); + if (obs) { + opkg_msg(INFO, + "Pre-exiting file %s is obsolete." + " obs_pkg=%s\n", filename, obs->name); + continue; + } + + /* We have found a clash. */ + opkg_msg(ERROR, "Package %s wants to install file %s\n" + "\tBut that file is already provided by package ", + pkg->name, filename); + if (owner) { + opkg_message(ERROR, "%s\n", owner->name); + } else { + opkg_message(ERROR, "\n" + "Please move this file out of the way and try again.\n"); + } + clashes++; + } + } + pkg_free_installed_files(pkg); + + return clashes; } /* * XXX: This function sucks, as does the below comment. */ -static int -check_data_file_clashes_change(pkg_t *pkg, pkg_t *old_pkg) +static int check_data_file_clashes_change(pkg_t * pkg, pkg_t * old_pkg) { - /* Basically that's the worst hack I could do to be able to change ownership of - file list, but, being that we have no way to unwind the mods, due to structure - of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much. - What we do here is change the ownership of file in hash if a replace ( or similar events - happens ) - Only the action that are needed to change name should be considered. - @@@ To change after 1.0 release. - */ - str_list_t *files_list; - str_list_elt_t *iter, *niter; - - char *root_filename = NULL; - - files_list = pkg_get_installed_files(pkg); - if (files_list == NULL) - return -1; - - for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); - iter; - iter = niter, niter = str_list_next(files_list, niter)) { - char *filename = (char *) iter->data; - if (root_filename) { - free(root_filename); - root_filename = NULL; - } - root_filename = root_filename_alloc(filename); - if (file_exists(root_filename) && (! file_is_dir(root_filename))) { - pkg_t *owner; - - owner = file_hash_get_file_owner(filename); - - if (conf->force_overwrite) { - /* but we need to change who owns this file */ - file_hash_set_file_owner(filename, pkg); - continue; - } - - - /* Pre-existing files are OK if owned by a package replaced by new pkg. */ - if (owner) { - if (pkg_replaces(pkg, owner)) { + /* Basically that's the worst hack I could do to be able to change ownership of + file list, but, being that we have no way to unwind the mods, due to structure + of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much. + What we do here is change the ownership of file in hash if a replace ( or similar events + happens ) + Only the action that are needed to change name should be considered. + @@@ To change after 1.0 release. + */ + str_list_t *files_list; + str_list_elt_t *iter, *niter; + + char *root_filename = NULL; + + files_list = pkg_get_installed_files(pkg); + if (files_list == NULL) + return -1; + + for (iter = str_list_first(files_list), niter = + str_list_next(files_list, iter); iter; + iter = niter, niter = str_list_next(files_list, niter)) { + char *filename = (char *)iter->data; + if (root_filename) { + free(root_filename); + root_filename = NULL; + } + root_filename = root_filename_alloc(filename); + if (file_exists(root_filename) && (!file_is_dir(root_filename))) { + pkg_t *owner; + + owner = file_hash_get_file_owner(filename); + + if (conf->force_overwrite) { + /* but we need to change who owns this file */ + file_hash_set_file_owner(filename, pkg); + continue; + } + + /* Pre-existing files are OK if owned by a package replaced by new pkg. */ + if (owner) { + if (pkg_replaces(pkg, owner)) { /* It's now time to change the owner of that file. It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */ - opkg_msg(INFO, "Replacing pre-existing file %s " - "owned by package %s\n", - filename, owner->name); - file_hash_set_file_owner(filename, pkg); - continue; - } - } - - } - } - if (root_filename) { - free(root_filename); - root_filename = NULL; - } - pkg_free_installed_files(pkg); - - return 0; + opkg_msg(INFO, + "Replacing pre-existing file %s " + "owned by package %s\n", + filename, owner->name); + file_hash_set_file_owner(filename, pkg); + continue; + } + } + + } + } + if (root_filename) { + free(root_filename); + root_filename = NULL; + } + pkg_free_installed_files(pkg); + + return 0; } -static int -check_data_file_clashes_unwind(pkg_t *pkg, pkg_t *old_pkg) +static int check_data_file_clashes_unwind(pkg_t * pkg, pkg_t * old_pkg) { - /* Nothing to do since check_data_file_clashes doesn't change state */ - return 0; + /* Nothing to do since check_data_file_clashes doesn't change state */ + return 0; } -static int -postrm_upgrade_old_pkg(pkg_t *pkg, pkg_t *old_pkg) +static int postrm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg) { - /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we? - 1. If the package is being upgraded, call + /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we? + 1. If the package is being upgraded, call old-postrm upgrade new-version - 2. If this fails, attempt: + 2. If this fails, attempt: new-postrm failed-upgrade old-version - Error unwind, for both cases: + Error unwind, for both cases: old-preinst abort-upgrade new-version */ - int err; - char *script_args; - char *new_version; - - if (!old_pkg || !pkg) - return 0; - - new_version = pkg_version_str_alloc(pkg); - - sprintf_alloc(&script_args, "upgrade %s", new_version); - free(new_version); - err = pkg_run_script(old_pkg, "postrm", script_args); - free(script_args); - if (err != 0) { - opkg_msg(ERROR, "postrm script for package \"%s\" failed\n", - old_pkg->name); - return -1; - } - return 0; + int err; + char *script_args; + char *new_version; + + if (!old_pkg || !pkg) + return 0; + + new_version = pkg_version_str_alloc(pkg); + + sprintf_alloc(&script_args, "upgrade %s", new_version); + free(new_version); + err = pkg_run_script(old_pkg, "postrm", script_args); + free(script_args); + if (err != 0) { + opkg_msg(ERROR, "postrm script for package \"%s\" failed\n", + old_pkg->name); + return -1; + } + return 0; } -static int -postrm_upgrade_old_pkg_unwind(pkg_t *pkg, pkg_t *old_pkg) +static int postrm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg) { - /* DPKG_INCOMPATIBILITY: - dpkg does some things here that we don't do yet. Do we care? - (See postrm_upgrade_old_pkg for details) - */ - return 0; + /* DPKG_INCOMPATIBILITY: + dpkg does some things here that we don't do yet. Do we care? + (See postrm_upgrade_old_pkg for details) + */ + return 0; } -static int -remove_obsolesced_files(pkg_t *pkg, pkg_t *old_pkg) +static int remove_obsolesced_files(pkg_t * pkg, pkg_t * old_pkg) { - int err = 0; - str_list_t *old_files; - str_list_elt_t *of; - str_list_t *new_files; - str_list_elt_t *nf; - hash_table_t new_files_table; - - old_files = pkg_get_installed_files(old_pkg); - if (old_files == NULL) - return -1; - - new_files = pkg_get_installed_files(pkg); - if (new_files == NULL) { - pkg_free_installed_files(old_pkg); - return -1; - } - - new_files_table.entries = NULL; - hash_table_init("new_files" , &new_files_table, 20); - for (nf = str_list_first(new_files); nf; nf = str_list_next(new_files, nf)) { - if (nf && nf->data) - hash_table_insert(&new_files_table, nf->data, nf->data); - } - - for (of = str_list_first(old_files); of; of = str_list_next(old_files, of)) { - pkg_t *owner; - char *old, *new; - old = (char *)of->data; - new = (char *) hash_table_get (&new_files_table, old); - if (new) - continue; - - if (file_is_dir(old)) { - continue; - } - owner = file_hash_get_file_owner(old); - if (owner != old_pkg) { - /* in case obsolete file no longer belongs to old_pkg */ - continue; - } - - /* old file is obsolete */ - opkg_msg(NOTICE, "Removing obsolete file %s.\n", old); - if (!conf->noaction) { - err = unlink(old); - if (err) { - opkg_perror(ERROR, "unlinking %s failed", old); - } - } - } - - hash_table_deinit(&new_files_table); - pkg_free_installed_files(old_pkg); - pkg_free_installed_files(pkg); - - return err; + int err = 0; + str_list_t *old_files; + str_list_elt_t *of; + str_list_t *new_files; + str_list_elt_t *nf; + hash_table_t new_files_table; + + old_files = pkg_get_installed_files(old_pkg); + if (old_files == NULL) + return -1; + + new_files = pkg_get_installed_files(pkg); + if (new_files == NULL) { + pkg_free_installed_files(old_pkg); + return -1; + } + + new_files_table.entries = NULL; + hash_table_init("new_files", &new_files_table, 20); + for (nf = str_list_first(new_files); nf; + nf = str_list_next(new_files, nf)) { + if (nf && nf->data) + hash_table_insert(&new_files_table, nf->data, nf->data); + } + + for (of = str_list_first(old_files); of; + of = str_list_next(old_files, of)) { + pkg_t *owner; + char *old, *new; + old = (char *)of->data; + new = (char *)hash_table_get(&new_files_table, old); + if (new) + continue; + + if (file_is_dir(old)) { + continue; + } + owner = file_hash_get_file_owner(old); + if (owner != old_pkg) { + /* in case obsolete file no longer belongs to old_pkg */ + continue; + } + + /* old file is obsolete */ + opkg_msg(NOTICE, "Removing obsolete file %s.\n", old); + if (!conf->noaction) { + err = unlink(old); + if (err) { + opkg_perror(ERROR, "unlinking %s failed", old); + } + } + } + + hash_table_deinit(&new_files_table); + pkg_free_installed_files(old_pkg); + pkg_free_installed_files(pkg); + + return err; } -static int -install_maintainer_scripts(pkg_t *pkg, pkg_t *old_pkg) +static int install_maintainer_scripts(pkg_t * pkg, pkg_t * old_pkg) { - int ret; - char *prefix; - - sprintf_alloc(&prefix, "%s.", pkg->name); - ret = pkg_extract_control_files_to_dir_with_prefix(pkg, - pkg->dest->info_dir, - prefix); - free(prefix); - return ret; + int ret; + char *prefix; + + sprintf_alloc(&prefix, "%s.", pkg->name); + ret = pkg_extract_control_files_to_dir_with_prefix(pkg, + pkg->dest->info_dir, + prefix); + free(prefix); + return ret; } -static int -remove_disappeared(pkg_t *pkg) +static int remove_disappeared(pkg_t * pkg) { - /* DPKG_INCOMPATIBILITY: - This is a fairly sophisticated dpkg operation. Shall we - skip it? */ - - /* Any packages all of whose files have been overwritten during the - installation, and which aren't required for dependencies, are - considered to have been removed. For each such package - 1. disappearer's-postrm disappear overwriter overwriter-version - 2. The package's maintainer scripts are removed - 3. It is noted in the status database as being in a sane state, - namely not installed (any conffiles it may have are ignored, + /* DPKG_INCOMPATIBILITY: + This is a fairly sophisticated dpkg operation. Shall we + skip it? */ + + /* Any packages all of whose files have been overwritten during the + installation, and which aren't required for dependencies, are + considered to have been removed. For each such package + 1. disappearer's-postrm disappear overwriter overwriter-version + 2. The package's maintainer scripts are removed + 3. It is noted in the status database as being in a sane state, + namely not installed (any conffiles it may have are ignored, rather than being removed by dpkg). Note that disappearing packages do not have their prerm called, because dpkg doesn't know in advance that the package is going to vanish. - */ - return 0; + */ + return 0; } -static int -install_data_files(pkg_t *pkg) +static int install_data_files(pkg_t * pkg) { - int err; - - /* opkg takes a slightly different approach to data file backups - than dpkg. Rather than removing backups at this point, we - actually do the data file installation now. See comments in - check_data_file_clashes() for more details. */ - - opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir); - err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir); - if (err) { - return err; - } - - /* The "Essential" control field may only be present in the control - * file and not in the Packages list. Ensure we capture it regardless. - * - * XXX: This should be fixed outside of opkg, in the Package list. - */ - set_flags_from_control(pkg) ; - - opkg_msg(DEBUG, "Calling pkg_write_filelist.\n"); - err = pkg_write_filelist(pkg); - if (err) - return err; - - /* XXX: FEATURE: opkg should identify any files which existed - before installation and which were overwritten, (see - check_data_file_clashes()). What it must do is remove any such - files from the filelist of the old package which provided the - file. Otherwise, if the old package were removed at some point - it would break the new package. Removing the new package will - also break the old one, but this cannot be helped since the old - package's file has already been deleted. This is the importance - of check_data_file_clashes(), and only allowing opkg to install - a clashing package with a user force. */ - - return 0; + int err; + + /* opkg takes a slightly different approach to data file backups + than dpkg. Rather than removing backups at this point, we + actually do the data file installation now. See comments in + check_data_file_clashes() for more details. */ + + opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir); + err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir); + if (err) { + return err; + } + + /* The "Essential" control field may only be present in the control + * file and not in the Packages list. Ensure we capture it regardless. + * + * XXX: This should be fixed outside of opkg, in the Package list. + */ + set_flags_from_control(pkg); + + opkg_msg(DEBUG, "Calling pkg_write_filelist.\n"); + err = pkg_write_filelist(pkg); + if (err) + return err; + + /* XXX: FEATURE: opkg should identify any files which existed + before installation and which were overwritten, (see + check_data_file_clashes()). What it must do is remove any such + files from the filelist of the old package which provided the + file. Otherwise, if the old package were removed at some point + it would break the new package. Removing the new package will + also break the old one, but this cannot be helped since the old + package's file has already been deleted. This is the importance + of check_data_file_clashes(), and only allowing opkg to install + a clashing package with a user force. */ + + return 0; } -static int -resolve_conffiles(pkg_t *pkg) +static int resolve_conffiles(pkg_t * pkg) { - conffile_list_elt_t *iter; - conffile_t *cf; - char *cf_backup; - char *chksum; + conffile_list_elt_t *iter; + conffile_t *cf; + char *cf_backup; + char *chksum; - if (conf->noaction) return 0; + if (conf->noaction) + return 0; - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - char *root_filename; - cf = (conffile_t *)iter->data; - root_filename = root_filename_alloc(cf->name); + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + char *root_filename; + cf = (conffile_t *) iter->data; + root_filename = root_filename_alloc(cf->name); - /* Might need to initialize the md5sum for each conffile */ - if (cf->value == NULL) { - cf->value = file_sha256sum_alloc(root_filename); - } + /* Might need to initialize the md5sum for each conffile */ + if (cf->value == NULL) { + cf->value = file_sha256sum_alloc(root_filename); + } - if (!file_exists(root_filename)) { - free(root_filename); - continue; - } + if (!file_exists(root_filename)) { + free(root_filename); + continue; + } - cf_backup = backup_filename_alloc(root_filename); + cf_backup = backup_filename_alloc(root_filename); - if (file_exists(cf_backup)) { - /* Let's compute md5 to test if files are changed */ + if (file_exists(cf_backup)) { + /* Let's compute md5 to test if files are changed */ #ifdef HAVE_MD5 - if(cf->value && strlen(cf->value) > 33) { - chksum = file_sha256sum_alloc(cf_backup); - } else { - chksum = file_md5sum_alloc(cf_backup); - } + if (cf->value && strlen(cf->value) > 33) { + chksum = file_sha256sum_alloc(cf_backup); + } else { + chksum = file_md5sum_alloc(cf_backup); + } #else - chksum = file_sha256sum_alloc(cf_backup); + chksum = file_sha256sum_alloc(cf_backup); #endif - if (chksum && cf->value && strcmp(cf->value,chksum) != 0 ) { - if (conf->force_maintainer) { - opkg_msg(NOTICE, "Conffile %s using maintainer's setting.\n", - cf_backup); - } else { - char *new_conffile; - sprintf_alloc(&new_conffile, "%s-opkg", root_filename); - opkg_msg(ERROR, "Existing conffile %s " - "is different from the conffile in the new package." - " The new conffile will be placed at %s.\n", - root_filename, new_conffile); - rename(root_filename, new_conffile); - rename(cf_backup, root_filename); - free(new_conffile); - } - } - unlink(cf_backup); - if (chksum) - free(chksum); - } - - free(cf_backup); - free(root_filename); - } - - return 0; -} + if (chksum && cf->value + && strcmp(cf->value, chksum) != 0) { + if (conf->force_maintainer) { + opkg_msg(NOTICE, + "Conffile %s using maintainer's setting.\n", + cf_backup); + } else { + char *new_conffile; + sprintf_alloc(&new_conffile, "%s-opkg", + root_filename); + opkg_msg(ERROR, + "Existing conffile %s " + "is different from the conffile in the new package." + " The new conffile will be placed at %s.\n", + root_filename, new_conffile); + rename(root_filename, new_conffile); + rename(cf_backup, root_filename); + free(new_conffile); + } + } + unlink(cf_backup); + if (chksum) + free(chksum); + } + + free(cf_backup); + free(root_filename); + } + return 0; +} -int -opkg_install_by_name(const char *pkg_name) +int opkg_install_by_name(const char *pkg_name) { - int cmp; - pkg_t *old, *new; - char *old_version, *new_version; - - old = pkg_hash_fetch_installed_by_name(pkg_name); - if (old) - opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n", - old->version); - - new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name); - if (new == NULL) { - opkg_msg(NOTICE, "Unknown package '%s'.\n", pkg_name); - return -1; - } - - opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:"); - if ( old ) - opkg_message(DEBUG2, " old %s ", old->version); - opkg_message(DEBUG2, " new %s\n", new->version); - - new->state_flag |= SF_USER; - if (old) { - old_version = pkg_version_str_alloc(old); - new_version = pkg_version_str_alloc(new); - - cmp = pkg_compare_versions(old, new); - if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */ - opkg_msg(DEBUG, "Forcing downgrade\n"); - cmp = -1 ; /* then we force opkg to downgrade */ - /* We need to use a value < 0 because in the 0 case we are asking to */ - /* reinstall, and some check could fail asking the "force-reinstall" option */ - } - opkg_msg(DEBUG, "Comparing visible versions of pkg %s:" - "\n\t%s is installed " - "\n\t%s is available " - "\n\t%d was comparison result\n", - pkg_name, old_version, new_version, cmp); - if (cmp == 0) { - opkg_msg(NOTICE, - "Package %s (%s) installed in %s is up to date.\n", - old->name, old_version, old->dest->name); - free(old_version); - free(new_version); - return 0; - } else if (cmp > 0) { - opkg_msg(NOTICE, - "Not downgrading package %s on %s from %s to %s.\n", - old->name, old->dest->name, old_version, new_version); - free(old_version); - free(new_version); - return 0; - } else if (cmp < 0) { - new->dest = old->dest; - old->state_want = SW_DEINSTALL; - } - free(old_version); - free(new_version); - } - - opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n"); - return opkg_install_pkg(new, 0); + int cmp; + pkg_t *old, *new; + char *old_version, *new_version; + + old = pkg_hash_fetch_installed_by_name(pkg_name); + if (old) + opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n", + old->version); + + new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name); + if (new == NULL) { + opkg_msg(NOTICE, "Unknown package '%s'.\n", pkg_name); + return -1; + } + + opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:"); + if (old) + opkg_message(DEBUG2, " old %s ", old->version); + opkg_message(DEBUG2, " new %s\n", new->version); + + new->state_flag |= SF_USER; + if (old) { + old_version = pkg_version_str_alloc(old); + new_version = pkg_version_str_alloc(new); + + cmp = pkg_compare_versions(old, new); + if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */ + opkg_msg(DEBUG, "Forcing downgrade\n"); + cmp = -1; /* then we force opkg to downgrade */ + /* We need to use a value < 0 because in the 0 case we are asking to */ + /* reinstall, and some check could fail asking the "force-reinstall" option */ + } + opkg_msg(DEBUG, "Comparing visible versions of pkg %s:" + "\n\t%s is installed " + "\n\t%s is available " + "\n\t%d was comparison result\n", + pkg_name, old_version, new_version, cmp); + if (cmp == 0) { + opkg_msg(NOTICE, + "Package %s (%s) installed in %s is up to date.\n", + old->name, old_version, old->dest->name); + free(old_version); + free(new_version); + return 0; + } else if (cmp > 0) { + opkg_msg(NOTICE, + "Not downgrading package %s on %s from %s to %s.\n", + old->name, old->dest->name, old_version, + new_version); + free(old_version); + free(new_version); + return 0; + } else if (cmp < 0) { + new->dest = old->dest; + old->state_want = SW_DEINSTALL; + } + free(old_version); + free(new_version); + } + + opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n"); + return opkg_install_pkg(new, 0); } /** * @brief Really install a pkg_t */ -int -opkg_install_pkg(pkg_t *pkg, int from_upgrade) +int opkg_install_pkg(pkg_t * pkg, int from_upgrade) { - int err = 0; - int message = 0; - pkg_t *old_pkg = NULL; - pkg_vec_t *replacees; - abstract_pkg_t *ab_pkg = NULL; - int old_state_flag; - char* file_md5; + int err = 0; + int message = 0; + pkg_t *old_pkg = NULL; + pkg_vec_t *replacees; + abstract_pkg_t *ab_pkg = NULL; + int old_state_flag; + char *file_md5; #ifdef HAVE_SHA256 - char* file_sha256; + char *file_sha256; #endif - sigset_t newset, oldset; - - if ( from_upgrade ) - message = 1; /* Coming from an upgrade, and should change the output message */ - - opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n"); - - if (!pkg_arch_supported(pkg)) { - opkg_msg(ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n", - pkg->architecture, pkg->name); - return -1; - } - if (pkg->state_status == SS_INSTALLED && conf->nodeps == 0) { - err = satisfy_dependencies_for(pkg); - if (err) - return -1; - - opkg_msg(NOTICE, "Package %s is already installed on %s.\n", - pkg->name, pkg->dest->name); - return 0; - } - - if (pkg->dest == NULL) { - pkg->dest = conf->default_dest; - } - - old_pkg = pkg_hash_fetch_installed_by_name(pkg->name); - - err = opkg_install_check_downgrade(pkg, old_pkg, message); - if (err) - return -1; - - pkg->state_want = SW_INSTALL; - if (old_pkg){ - old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependencies */ - } - - err = check_conflicts_for(pkg); - if (err) - return -1; - - /* this setup is to remove the upgrade scenario in the end when - installing pkg A, A deps B & B deps on A. So both B and A are - installed. Then A's installation is started resulting in an - uncecessary upgrade */ - if (pkg->state_status == SS_INSTALLED) - return 0; - - err = verify_pkg_installable(pkg); - if (err) - return -1; - - if (pkg->local_filename == NULL) { - if(!conf->cache && conf->download_only){ - char cwd[4096]; - if(getcwd(cwd, sizeof(cwd)) != NULL) - err = opkg_download_pkg(pkg, cwd); - else - return -1; - } else { - err = opkg_download_pkg(pkg, conf->tmp_dir); - } - if (err) { - opkg_msg(ERROR, "Failed to download %s. " - "Perhaps you need to run 'opkg update'?\n", - pkg->name); - return -1; - } - } - - /* check that the repository is valid */ - #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN) - char *list_file_name, *sig_file_name, *lists_dir; - - /* check to ensure the package has come from a repository */ - if (conf->check_signature && pkg->src) - { - sprintf_alloc (&lists_dir, "%s", - (conf->restrict_to_default_dest) - ? conf->default_dest->lists_dir - : conf->lists_dir); - sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name); - sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name); - - if (file_exists (sig_file_name)) - { - if (opkg_verify_file (list_file_name, sig_file_name)){ - opkg_msg(ERROR, "Failed to verify the signature of %s.\n", - list_file_name); - if (!conf->force_signature) - return -1; - } - }else{ - opkg_msg(ERROR, "Signature file is missing for %s. " - "Perhaps you need to run 'opkg update'?\n", - pkg->name); - if (!conf->force_signature) - return -1; - } + sigset_t newset, oldset; + + if (from_upgrade) + message = 1; /* Coming from an upgrade, and should change the output message */ + + opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n"); - free (lists_dir); - free (list_file_name); - free (sig_file_name); - } - #endif + if (!pkg_arch_supported(pkg)) { + opkg_msg(ERROR, + "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n", + pkg->architecture, pkg->name); + return -1; + } + if (pkg->state_status == SS_INSTALLED && conf->nodeps == 0) { + err = satisfy_dependencies_for(pkg); + if (err) + return -1; + + opkg_msg(NOTICE, "Package %s is already installed on %s.\n", + pkg->name, pkg->dest->name); + return 0; + } + + if (pkg->dest == NULL) { + pkg->dest = conf->default_dest; + } + + old_pkg = pkg_hash_fetch_installed_by_name(pkg->name); + + err = opkg_install_check_downgrade(pkg, old_pkg, message); + if (err) + return -1; + + pkg->state_want = SW_INSTALL; + if (old_pkg) { + old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependencies */ + } + + err = check_conflicts_for(pkg); + if (err) + return -1; + + /* this setup is to remove the upgrade scenario in the end when + installing pkg A, A deps B & B deps on A. So both B and A are + installed. Then A's installation is started resulting in an + uncecessary upgrade */ + if (pkg->state_status == SS_INSTALLED) + return 0; + + err = verify_pkg_installable(pkg); + if (err) + return -1; + + if (pkg->local_filename == NULL) { + if (!conf->cache && conf->download_only) { + char cwd[4096]; + if (getcwd(cwd, sizeof(cwd)) != NULL) + err = opkg_download_pkg(pkg, cwd); + else + return -1; + } else { + err = opkg_download_pkg(pkg, conf->tmp_dir); + } + if (err) { + opkg_msg(ERROR, "Failed to download %s. " + "Perhaps you need to run 'opkg update'?\n", + pkg->name); + return -1; + } + } + + /* check that the repository is valid */ +#if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN) + char *list_file_name, *sig_file_name, *lists_dir; + + /* check to ensure the package has come from a repository */ + if (conf->check_signature && pkg->src) { + sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest) + ? conf->default_dest->lists_dir + : conf->lists_dir); + sprintf_alloc(&list_file_name, "%s/%s", lists_dir, + pkg->src->name); + sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir, + pkg->src->name); + + if (file_exists(sig_file_name)) { + if (opkg_verify_file(list_file_name, sig_file_name)) { + opkg_msg(ERROR, + "Failed to verify the signature of %s.\n", + list_file_name); + if (!conf->force_signature) + return -1; + } + } else { + opkg_msg(ERROR, "Signature file is missing for %s. " + "Perhaps you need to run 'opkg update'?\n", + pkg->name); + if (!conf->force_signature) + return -1; + } + + free(lists_dir); + free(list_file_name); + free(sig_file_name); + } +#endif #ifdef HAVE_MD5 - /* Check for md5 values */ - if (pkg->md5sum) - { - file_md5 = file_md5sum_alloc(pkg->local_filename); - if (file_md5 && strcmp(file_md5, pkg->md5sum)) - { - if (!conf->force_checksum) - { - opkg_msg(ERROR, "Package %s md5sum mismatch. " - "Either the opkg or the package index are corrupt. " - "Try 'opkg update'.\n", - pkg->name); - free(file_md5); - return -1; - } - else - { - opkg_msg(NOTICE, "Ignored %s md5sum mismatch.\n", pkg->name); - } - } - if (file_md5) - free(file_md5); - } + /* Check for md5 values */ + if (pkg->md5sum) { + file_md5 = file_md5sum_alloc(pkg->local_filename); + if (file_md5 && strcmp(file_md5, pkg->md5sum)) { + if (!conf->force_checksum) { + opkg_msg(ERROR, "Package %s md5sum mismatch. " + "Either the opkg or the package index are corrupt. " + "Try 'opkg update'.\n", pkg->name); + free(file_md5); + return -1; + } else { + opkg_msg(NOTICE, + "Ignored %s md5sum mismatch.\n", + pkg->name); + } + } + if (file_md5) + free(file_md5); + } #endif #ifdef HAVE_SHA256 - /* Check for sha256 value */ - if(pkg->sha256sum) - { - file_sha256 = file_sha256sum_alloc(pkg->local_filename); - if (file_sha256 && strcmp(file_sha256, pkg->sha256sum)) - { - if (!conf->force_checksum) - { - opkg_msg(ERROR, - "Package %s sha256sum mismatch. " - "Either the opkg or the package index are corrupt. " - "Try 'opkg update'.\n", - pkg->name); - free(file_sha256); - return -1; - } - else - { - opkg_msg(NOTICE, - "Ignored %s sha256sum mismatch.\n", - pkg->name); - } - } - if (file_sha256) - free(file_sha256); - } + /* Check for sha256 value */ + if (pkg->sha256sum) { + file_sha256 = file_sha256sum_alloc(pkg->local_filename); + if (file_sha256 && strcmp(file_sha256, pkg->sha256sum)) { + if (!conf->force_checksum) { + opkg_msg(ERROR, + "Package %s sha256sum mismatch. " + "Either the opkg or the package index are corrupt. " + "Try 'opkg update'.\n", pkg->name); + free(file_sha256); + return -1; + } else { + opkg_msg(NOTICE, + "Ignored %s sha256sum mismatch.\n", + pkg->name); + } + } + if (file_sha256) + free(file_sha256); + } #endif - if(conf->download_only) { - if (conf->nodeps == 0) { - err = satisfy_dependencies_for(pkg); - if (err) - return -1; - } - return 0; - } - - if (pkg->tmp_unpack_dir == NULL) { - if (unpack_pkg_control_files(pkg) == -1) { - opkg_msg(ERROR, "Failed to unpack control files from %s.\n", - pkg->local_filename); - return -1; - } - } - - err = update_file_ownership(pkg, old_pkg); - if (err) - return -1; - - if (conf->nodeps == 0) { - err = satisfy_dependencies_for(pkg); - if (err) + if (conf->download_only) { + if (conf->nodeps == 0) { + err = satisfy_dependencies_for(pkg); + if (err) + return -1; + } + return 0; + } + + if (pkg->tmp_unpack_dir == NULL) { + if (unpack_pkg_control_files(pkg) == -1) { + opkg_msg(ERROR, + "Failed to unpack control files from %s.\n", + pkg->local_filename); + return -1; + } + } + + err = update_file_ownership(pkg, old_pkg); + if (err) return -1; - if (pkg->state_status == SS_UNPACKED) - /* Circular dependency has installed it for us. */ + + if (conf->nodeps == 0) { + err = satisfy_dependencies_for(pkg); + if (err) + return -1; + if (pkg->state_status == SS_UNPACKED) + /* Circular dependency has installed it for us. */ + return 0; + } + + replacees = pkg_vec_alloc(); + pkg_get_installed_replacees(pkg, replacees); + + /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */ + + sigemptyset(&newset); + sigaddset(&newset, SIGINT); + sigprocmask(SIG_BLOCK, &newset, &oldset); + + opkg_state_changed++; + pkg->state_flag |= SF_FILELIST_CHANGED; + + if (old_pkg) { + pkg_remove_orphan_dependent(pkg, old_pkg); + old_pkg->is_upgrade = 1; + pkg->is_upgrade = 1; + } + /* XXX: BUG: we really should treat replacement more like an upgrade + * Instead, we're going to remove the replacees + */ + err = pkg_remove_installed_replacees(replacees); + if (err) + goto UNWIND_REMOVE_INSTALLED_REPLACEES; + + err = prerm_upgrade_old_pkg(pkg, old_pkg); + if (err) + goto UNWIND_PRERM_UPGRADE_OLD_PKG; + + err = prerm_deconfigure_conflictors(pkg, replacees); + if (err) + goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS; + + err = preinst_configure(pkg, old_pkg); + if (err) + goto UNWIND_PREINST_CONFIGURE; + + err = backup_modified_conffiles(pkg, old_pkg); + if (err) + goto UNWIND_BACKUP_MODIFIED_CONFFILES; + + err = check_data_file_clashes(pkg, old_pkg); + if (err) + goto UNWIND_CHECK_DATA_FILE_CLASHES; + + err = postrm_upgrade_old_pkg(pkg, old_pkg); + if (err) + goto UNWIND_POSTRM_UPGRADE_OLD_PKG; + + if (conf->noaction) return 0; - } - - replacees = pkg_vec_alloc(); - pkg_get_installed_replacees(pkg, replacees); - - /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */ - - sigemptyset(&newset); - sigaddset(&newset, SIGINT); - sigprocmask(SIG_BLOCK, &newset, &oldset); - - opkg_state_changed++; - pkg->state_flag |= SF_FILELIST_CHANGED; - - if (old_pkg) { - pkg_remove_orphan_dependent(pkg, old_pkg); - old_pkg->is_upgrade = 1; - pkg->is_upgrade = 1; - } - /* XXX: BUG: we really should treat replacement more like an upgrade - * Instead, we're going to remove the replacees - */ - err = pkg_remove_installed_replacees(replacees); - if (err) - goto UNWIND_REMOVE_INSTALLED_REPLACEES; - - err = prerm_upgrade_old_pkg(pkg, old_pkg); - if (err) - goto UNWIND_PRERM_UPGRADE_OLD_PKG; - - err = prerm_deconfigure_conflictors(pkg, replacees); - if (err) - goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS; - - err = preinst_configure(pkg, old_pkg); - if (err) - goto UNWIND_PREINST_CONFIGURE; - - err = backup_modified_conffiles(pkg, old_pkg); - if (err) - goto UNWIND_BACKUP_MODIFIED_CONFFILES; - - err = check_data_file_clashes(pkg, old_pkg); - if (err) - goto UNWIND_CHECK_DATA_FILE_CLASHES; - - err = postrm_upgrade_old_pkg(pkg, old_pkg); - if (err) - goto UNWIND_POSTRM_UPGRADE_OLD_PKG; - - if (conf->noaction) - return 0; - - /* point of no return: no unwinding after this */ - if (old_pkg) { - old_pkg->state_want = SW_DEINSTALL; - - if (old_pkg->state_flag & SF_NOPRUNE) { - opkg_msg(INFO, "Not removing obsolesced files because " - "package %s marked noprune.\n", - old_pkg->name); - } else { - opkg_msg(INFO, "Removing obsolesced files for %s\n", - old_pkg->name); - if (remove_obsolesced_files(pkg, old_pkg)) { - opkg_msg(ERROR, "Failed to determine " - "obsolete files from previously " - "installed %s\n", old_pkg->name); - } - } - - /* removing files from old package, to avoid ghost files */ - remove_data_files_and_list(old_pkg); - remove_maintainer_scripts(old_pkg); - } - - - opkg_msg(INFO, "%s maintainer scripts.\n", (pkg->is_upgrade) ? ("Upgrading") : ("Installing")); - if (install_maintainer_scripts(pkg, old_pkg)) { + + /* point of no return: no unwinding after this */ + if (old_pkg) { + old_pkg->state_want = SW_DEINSTALL; + + if (old_pkg->state_flag & SF_NOPRUNE) { + opkg_msg(INFO, "Not removing obsolesced files because " + "package %s marked noprune.\n", old_pkg->name); + } else { + opkg_msg(INFO, "Removing obsolesced files for %s\n", + old_pkg->name); + if (remove_obsolesced_files(pkg, old_pkg)) { + opkg_msg(ERROR, "Failed to determine " + "obsolete files from previously " + "installed %s\n", old_pkg->name); + } + } + + /* removing files from old package, to avoid ghost files */ + remove_data_files_and_list(old_pkg); + remove_maintainer_scripts(old_pkg); + } + + opkg_msg(INFO, "%s maintainer scripts.\n", + (pkg->is_upgrade) ? ("Upgrading") : ("Installing")); + if (install_maintainer_scripts(pkg, old_pkg)) { opkg_msg(ERROR, "Failed to extract maintainer scripts for %s." - " Package debris may remain!\n", - pkg->name); + " Package debris may remain!\n", pkg->name); goto pkg_is_hosed; - } + } - /* the following just returns 0 */ - remove_disappeared(pkg); + /* the following just returns 0 */ + remove_disappeared(pkg); - opkg_msg(INFO, "Installing data files for %s.\n", pkg->name); + opkg_msg(INFO, "Installing data files for %s.\n", pkg->name); - if (install_data_files(pkg)) { + if (install_data_files(pkg)) { opkg_msg(ERROR, "Failed to extract data files for %s. " - "Package debris may remain!\n", - pkg->name); + "Package debris may remain!\n", pkg->name); goto pkg_is_hosed; - } + } - err = check_data_file_clashes_change(pkg, old_pkg); - if (err) { + err = check_data_file_clashes_change(pkg, old_pkg); + if (err) { opkg_msg(ERROR, "check_data_file_clashes_change() failed for " - "for files belonging to %s.\n", - pkg->name); - } - - opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name); - resolve_conffiles(pkg); - - pkg->state_status = SS_UNPACKED; - old_state_flag = pkg->state_flag; - pkg->state_flag &= ~SF_PREFER; - opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n", - pkg->name, old_state_flag, pkg->state_flag); - - if (old_pkg) - old_pkg->state_status = SS_NOT_INSTALLED; - - time(&pkg->installed_time); - - ab_pkg = pkg->parent; - if (ab_pkg) - ab_pkg->state_status = pkg->state_status; - - sigprocmask(SIG_UNBLOCK, &newset, &oldset); - pkg_vec_free (replacees); - return 0; - - - UNWIND_POSTRM_UPGRADE_OLD_PKG: - postrm_upgrade_old_pkg_unwind(pkg, old_pkg); - UNWIND_CHECK_DATA_FILE_CLASHES: - check_data_file_clashes_unwind(pkg, old_pkg); - UNWIND_BACKUP_MODIFIED_CONFFILES: - backup_modified_conffiles_unwind(pkg, old_pkg); - UNWIND_PREINST_CONFIGURE: - preinst_configure_unwind(pkg, old_pkg); - UNWIND_PRERM_DECONFIGURE_CONFLICTORS: - prerm_deconfigure_conflictors_unwind(pkg, replacees); - UNWIND_PRERM_UPGRADE_OLD_PKG: - prerm_upgrade_old_pkg_unwind(pkg, old_pkg); - UNWIND_REMOVE_INSTALLED_REPLACEES: - pkg_remove_installed_replacees_unwind(replacees); + "for files belonging to %s.\n", pkg->name); + } + + opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name); + resolve_conffiles(pkg); + + pkg->state_status = SS_UNPACKED; + old_state_flag = pkg->state_flag; + pkg->state_flag &= ~SF_PREFER; + opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n", + pkg->name, old_state_flag, pkg->state_flag); + + if (old_pkg) + old_pkg->state_status = SS_NOT_INSTALLED; + + time(&pkg->installed_time); + + ab_pkg = pkg->parent; + if (ab_pkg) + ab_pkg->state_status = pkg->state_status; + + sigprocmask(SIG_UNBLOCK, &newset, &oldset); + pkg_vec_free(replacees); + return 0; + +UNWIND_POSTRM_UPGRADE_OLD_PKG: + postrm_upgrade_old_pkg_unwind(pkg, old_pkg); +UNWIND_CHECK_DATA_FILE_CLASHES: + check_data_file_clashes_unwind(pkg, old_pkg); +UNWIND_BACKUP_MODIFIED_CONFFILES: + backup_modified_conffiles_unwind(pkg, old_pkg); +UNWIND_PREINST_CONFIGURE: + preinst_configure_unwind(pkg, old_pkg); +UNWIND_PRERM_DECONFIGURE_CONFLICTORS: + prerm_deconfigure_conflictors_unwind(pkg, replacees); +UNWIND_PRERM_UPGRADE_OLD_PKG: + prerm_upgrade_old_pkg_unwind(pkg, old_pkg); +UNWIND_REMOVE_INSTALLED_REPLACEES: + pkg_remove_installed_replacees_unwind(replacees); pkg_is_hosed: - sigprocmask(SIG_UNBLOCK, &newset, &oldset); + sigprocmask(SIG_UNBLOCK, &newset, &oldset); - pkg_vec_free (replacees); - return -1; + pkg_vec_free(replacees); + return -1; } diff --git a/libopkg/opkg_install.h b/libopkg/opkg_install.h index eaffff8..ff7ff13 100644 --- a/libopkg/opkg_install.h +++ b/libopkg/opkg_install.h @@ -22,6 +22,6 @@ #include "opkg_conf.h" int opkg_install_by_name(const char *pkg_name); -int opkg_install_pkg(pkg_t *pkg, int from_upgrading); +int opkg_install_pkg(pkg_t * pkg, int from_upgrading); #endif diff --git a/libopkg/opkg_message.c b/libopkg/opkg_message.c index 2862ea6..4cc49c3 100644 --- a/libopkg/opkg_message.c +++ b/libopkg/opkg_message.c @@ -27,12 +27,11 @@ struct errlist { static struct errlist *error_list_head, *error_list_tail; -static void -push_error_list(char *msg) +static void push_error_list(char *msg) { struct errlist *e; - e = xcalloc(1, sizeof(struct errlist)); + e = xcalloc(1, sizeof(struct errlist)); e->errmsg = xstrdup(msg); e->next = NULL; @@ -44,8 +43,7 @@ push_error_list(char *msg) } } -void -free_error_list(void) +void free_error_list(void) { struct errlist *err, *err_tmp; @@ -58,8 +56,7 @@ free_error_list(void) } } -void -print_error_list(void) +void print_error_list(void) { struct errlist *err = error_list_head; @@ -73,8 +70,7 @@ print_error_list(void) } } -void -opkg_message (message_level_t level, const char *fmt, ...) +void opkg_message(message_level_t level, const char *fmt, ...) { va_list ap; @@ -83,13 +79,13 @@ opkg_message (message_level_t level, const char *fmt, ...) if (conf->opkg_vmessage) { /* Pass the message to libopkg users. */ - va_start (ap, fmt); + va_start(ap, fmt); conf->opkg_vmessage(level, fmt, ap); - va_end (ap); + va_end(ap); return; } - va_start (ap, fmt); + va_start(ap, fmt); if (level == ERROR) { #define MSG_LEN 4096 @@ -98,24 +94,22 @@ opkg_message (message_level_t level, const char *fmt, ...) ret = vsnprintf(msg, MSG_LEN, fmt, ap); if (ret < 0) { fprintf(stderr, "%s: encountered an output or encoding" - " error during vsnprintf.\n", - __FUNCTION__); - va_end (ap); + " error during vsnprintf.\n", __FUNCTION__); + va_end(ap); exit(EXIT_FAILURE); } if (ret >= MSG_LEN) { fprintf(stderr, "%s: Message truncated.\n", - __FUNCTION__); + __FUNCTION__); } push_error_list(msg); } else { if (vprintf(fmt, ap) < 0) { fprintf(stderr, "%s: encountered an output or encoding" - " error during vprintf.\n", - __FUNCTION__); + " error during vprintf.\n", __FUNCTION__); exit(EXIT_FAILURE); } } - va_end (ap); + va_end(ap); } diff --git a/libopkg/opkg_message.h b/libopkg/opkg_message.h index 4fa2a0b..4110ab9 100644 --- a/libopkg/opkg_message.h +++ b/libopkg/opkg_message.h @@ -21,17 +21,17 @@ #include typedef enum { - ERROR, /* error conditions */ - NOTICE, /* normal but significant condition */ - INFO, /* informational message */ - DEBUG, /* debug level message */ - DEBUG2, /* more debug level message */ + ERROR, /* error conditions */ + NOTICE, /* normal but significant condition */ + INFO, /* informational message */ + DEBUG, /* debug level message */ + DEBUG2, /* more debug level message */ } message_level_t; void free_error_list(void); void print_error_list(void); void opkg_message(message_level_t level, const char *fmt, ...) - __attribute__ ((format (printf, 2, 3))); + __attribute__ ((format(printf, 2, 3))); #define opkg_msg(l, fmt, args...) \ do { \ diff --git a/libopkg/opkg_pathfinder.c b/libopkg/opkg_pathfinder.c index bf7dab6..98a6dd4 100644 --- a/libopkg/opkg_pathfinder.c +++ b/libopkg/opkg_pathfinder.c @@ -31,70 +31,71 @@ * validation on a certificate using pathfinder. * */ -static int pathfinder_verify_callback(X509_STORE_CTX *ctx, void *arg) +static int pathfinder_verify_callback(X509_STORE_CTX * ctx, void *arg) { - char *errmsg; - const char *hex = "0123456789ABCDEF"; - size_t size = i2d_X509(ctx->cert, NULL); - unsigned char *keybuf, *iend; - iend = keybuf = xmalloc(size); - i2d_X509(ctx->cert, &iend); - char *certdata_str = xmalloc(size * 2 + 1); - unsigned char *cp = keybuf; - char *certdata_str_i = certdata_str; - while (cp < iend) - { - unsigned char ch = *cp++; - *certdata_str_i++ = hex[(ch >> 4) & 0xf]; - *certdata_str_i++ = hex[ch & 0xf]; - } - *certdata_str_i = 0; - free(keybuf); - - const char *policy = "2.5.29.32.0"; // anyPolicy - int validated = pathfinder_dbus_verify(certdata_str, policy, 0, 0, &errmsg); - - if (!validated) - opkg_msg(ERROR, "Path verification failed: %s.\n", errmsg); - - free(certdata_str); - free(errmsg); - - return validated; + char *errmsg; + const char *hex = "0123456789ABCDEF"; + size_t size = i2d_X509(ctx->cert, NULL); + unsigned char *keybuf, *iend; + iend = keybuf = xmalloc(size); + i2d_X509(ctx->cert, &iend); + char *certdata_str = xmalloc(size * 2 + 1); + unsigned char *cp = keybuf; + char *certdata_str_i = certdata_str; + while (cp < iend) { + unsigned char ch = *cp++; + *certdata_str_i++ = hex[(ch >> 4) & 0xf]; + *certdata_str_i++ = hex[ch & 0xf]; + } + *certdata_str_i = 0; + free(keybuf); + + const char *policy = "2.5.29.32.0"; // anyPolicy + int validated = + pathfinder_dbus_verify(certdata_str, policy, 0, 0, &errmsg); + + if (!validated) + opkg_msg(ERROR, "Path verification failed: %s.\n", errmsg); + + free(certdata_str); + free(errmsg); + + return validated; } #endif #if defined(HAVE_OPENSSL) -int pkcs7_pathfinder_verify_signers(PKCS7* p7) +int pkcs7_pathfinder_verify_signers(PKCS7 * p7) { - STACK_OF(X509) *signers; - int i, ret = 1; /* signers are verified by default */ + STACK_OF(X509) * signers; + int i, ret = 1; /* signers are verified by default */ - signers = PKCS7_get0_signers(p7, NULL, 0); + signers = PKCS7_get0_signers(p7, NULL, 0); - for(i = 0; i < sk_X509_num(signers); i++){ - X509_STORE_CTX ctx = { - .cert = sk_X509_value(signers, i), - }; + for (i = 0; i < sk_X509_num(signers); i++) { + X509_STORE_CTX ctx = { + .cert = sk_X509_value(signers, i), + }; - if(!pathfinder_verify_callback(&ctx, NULL)){ - /* Signer isn't verified ! goto jail; */ - ret = 0; - break; + if (!pathfinder_verify_callback(&ctx, NULL)) { + /* Signer isn't verified ! goto jail; */ + ret = 0; + break; + } } - } - sk_X509_free(signers); - return ret; + sk_X509_free(signers); + return ret; } #endif #if defined(HAVE_SSLCURL) -CURLcode curl_ssl_ctx_function(CURL * curl, void * sslctx, void * parm) { +CURLcode curl_ssl_ctx_function(CURL * curl, void *sslctx, void *parm) +{ - SSL_CTX * ctx = (SSL_CTX *) sslctx; - SSL_CTX_set_cert_verify_callback(ctx, pathfinder_verify_callback, parm); + SSL_CTX *ctx = (SSL_CTX *) sslctx; + SSL_CTX_set_cert_verify_callback(ctx, pathfinder_verify_callback, parm); - return CURLE_OK ; + return CURLE_OK; } #endif diff --git a/libopkg/opkg_pathfinder.h b/libopkg/opkg_pathfinder.h index 89bebbd..33d1b10 100644 --- a/libopkg/opkg_pathfinder.h +++ b/libopkg/opkg_pathfinder.h @@ -1,6 +1,5 @@ /* opkg_pathfinder.h - the opkg package management system - Copyright (C) 2009 Camille Moncelier This program is free software; you can redistribute it and/or @@ -20,12 +19,11 @@ #include "config.h" #if defined(HAVE_OPENSSL) -int pkcs7_pathfinder_verify_signers(PKCS7* p7); +int pkcs7_pathfinder_verify_signers(PKCS7 * p7); #endif #if defined(HAVE_SSLCURL) -CURLcode curl_ssl_ctx_function(CURL * curl, void * sslctx, void * parm); +CURLcode curl_ssl_ctx_function(CURL * curl, void *sslctx, void *parm); #endif - #endif diff --git a/libopkg/opkg_remove.c b/libopkg/opkg_remove.c index 1ff270d..41d98d9 100644 --- a/libopkg/opkg_remove.c +++ b/libopkg/opkg_remove.c @@ -30,143 +30,144 @@ * Returns number of the number of packages depending on the packages provided by this package. * Every package implicitly provides itself. */ -int -pkg_has_installed_dependents(pkg_t *pkg, abstract_pkg_t *** pdependents) +int pkg_has_installed_dependents(pkg_t * pkg, abstract_pkg_t *** pdependents) { - int nprovides = pkg->provides_count; - abstract_pkg_t **provides = pkg->provides; - unsigned int n_installed_dependents = 0; - int i; - for (i = 0; i < nprovides; i++) { - abstract_pkg_t *providee = provides[i]; - abstract_pkg_t **dependers = providee->depended_upon_by; - abstract_pkg_t *dep_ab_pkg; - if (dependers == NULL) - continue; - while ((dep_ab_pkg = *dependers++) != NULL) { - if (dep_ab_pkg->state_status == SS_INSTALLED){ - n_installed_dependents++; - } - } - - } - /* if caller requested the set of installed dependents */ - if (pdependents) { - int p = 0; - abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *)); - - *pdependents = dependents; - for (i = 0; i < nprovides; i++) { - abstract_pkg_t *providee = provides[i]; - abstract_pkg_t **dependers = providee->depended_upon_by; - abstract_pkg_t *dep_ab_pkg; - if (dependers == NULL) - continue; - while ((dep_ab_pkg = *dependers++) != NULL) { - if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) { - dependents[p++] = dep_ab_pkg; - dep_ab_pkg->state_flag |= SF_MARKED; - } - } - } - dependents[p] = NULL; - /* now clear the marks */ - for (i = 0; i < p; i++) { - abstract_pkg_t *dep_ab_pkg = dependents[i]; - dep_ab_pkg->state_flag &= ~SF_MARKED; - } - } - return n_installed_dependents; + int nprovides = pkg->provides_count; + abstract_pkg_t **provides = pkg->provides; + unsigned int n_installed_dependents = 0; + int i; + for (i = 0; i < nprovides; i++) { + abstract_pkg_t *providee = provides[i]; + abstract_pkg_t **dependers = providee->depended_upon_by; + abstract_pkg_t *dep_ab_pkg; + if (dependers == NULL) + continue; + while ((dep_ab_pkg = *dependers++) != NULL) { + if (dep_ab_pkg->state_status == SS_INSTALLED) { + n_installed_dependents++; + } + } + + } + /* if caller requested the set of installed dependents */ + if (pdependents) { + int p = 0; + abstract_pkg_t **dependents = + xcalloc((n_installed_dependents + 1), + sizeof(abstract_pkg_t *)); + + *pdependents = dependents; + for (i = 0; i < nprovides; i++) { + abstract_pkg_t *providee = provides[i]; + abstract_pkg_t **dependers = providee->depended_upon_by; + abstract_pkg_t *dep_ab_pkg; + if (dependers == NULL) + continue; + while ((dep_ab_pkg = *dependers++) != NULL) { + if (dep_ab_pkg->state_status == SS_INSTALLED + && !(dep_ab_pkg->state_flag & SF_MARKED)) { + dependents[p++] = dep_ab_pkg; + dep_ab_pkg->state_flag |= SF_MARKED; + } + } + } + dependents[p] = NULL; + /* now clear the marks */ + for (i = 0; i < p; i++) { + abstract_pkg_t *dep_ab_pkg = dependents[i]; + dep_ab_pkg->state_flag &= ~SF_MARKED; + } + } + return n_installed_dependents; } -static int -opkg_remove_dependent_pkgs(pkg_t *pkg, abstract_pkg_t **dependents) +static int opkg_remove_dependent_pkgs(pkg_t * pkg, abstract_pkg_t ** dependents) { - int i; - int a; - int count; - pkg_vec_t *dependent_pkgs; - abstract_pkg_t * ab_pkg; - - if((ab_pkg = pkg->parent) == NULL){ - opkg_msg(ERROR, "Internal error: pkg %s isn't in hash table\n", - pkg->name); - return 0; - } - - if (dependents == NULL) - return 0; - - // here i am using the dependencies_checked - if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package - return 0; // has already been encountered in the process - // of marking packages for removal - Karthik - ab_pkg->dependencies_checked = 2; - - i = 0; - count = 1; - dependent_pkgs = pkg_vec_alloc(); - - while (dependents [i] != NULL) { - abstract_pkg_t *dep_ab_pkg = dependents[i]; - - if (dep_ab_pkg->dependencies_checked == 2){ - i++; - continue; - } - if (dep_ab_pkg->state_status == SS_INSTALLED) { - for (a = 0; a < dep_ab_pkg->pkgs->len; a++) { - pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a]; - if (dep_pkg->state_status == SS_INSTALLED) { - pkg_vec_insert(dependent_pkgs, dep_pkg); - count++; - } - } - } - i++; - /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs. - * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */ - } - - if (count == 1) { - pkg_vec_free(dependent_pkgs); - return 0; - } - - - int err=0; - for (i = 0; i < dependent_pkgs->len; i++) { - err = opkg_remove_pkg(dependent_pkgs->pkgs[i],0); - if (err) { - break; + int i; + int a; + int count; + pkg_vec_t *dependent_pkgs; + abstract_pkg_t *ab_pkg; + + if ((ab_pkg = pkg->parent) == NULL) { + opkg_msg(ERROR, "Internal error: pkg %s isn't in hash table\n", + pkg->name); + return 0; + } + + if (dependents == NULL) + return 0; + + // here i am using the dependencies_checked + if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package + return 0; // has already been encountered in the process + // of marking packages for removal - Karthik + ab_pkg->dependencies_checked = 2; + + i = 0; + count = 1; + dependent_pkgs = pkg_vec_alloc(); + + while (dependents[i] != NULL) { + abstract_pkg_t *dep_ab_pkg = dependents[i]; + + if (dep_ab_pkg->dependencies_checked == 2) { + i++; + continue; + } + if (dep_ab_pkg->state_status == SS_INSTALLED) { + for (a = 0; a < dep_ab_pkg->pkgs->len; a++) { + pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a]; + if (dep_pkg->state_status == SS_INSTALLED) { + pkg_vec_insert(dependent_pkgs, dep_pkg); + count++; + } + } + } + i++; + /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs. + * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */ } - } - pkg_vec_free(dependent_pkgs); - return err; + + if (count == 1) { + pkg_vec_free(dependent_pkgs); + return 0; + } + + int err = 0; + for (i = 0; i < dependent_pkgs->len; i++) { + err = opkg_remove_pkg(dependent_pkgs->pkgs[i], 0); + if (err) { + break; + } + } + pkg_vec_free(dependent_pkgs); + return err; } -static void -print_dependents_warning(pkg_t *pkg, abstract_pkg_t **dependents) +static void print_dependents_warning(pkg_t * pkg, abstract_pkg_t ** dependents) { - abstract_pkg_t *dep_ab_pkg; - opkg_msg(ERROR, "Package %s is depended upon by packages:\n", pkg->name); - while ((dep_ab_pkg = *dependents++) != NULL) { - if (dep_ab_pkg->state_status == SS_INSTALLED) - opkg_msg(ERROR, "\t%s\n", dep_ab_pkg->name); - } - opkg_msg(ERROR, "These might cease to work if package %s is removed.\n\n", - pkg->name); - opkg_msg(ERROR, "Force removal of this package with --force-depends.\n"); - opkg_msg(ERROR, "Force removal of this package and its dependents\n"); - opkg_msg(ERROR, "with --force-removal-of-dependent-packages.\n"); + abstract_pkg_t *dep_ab_pkg; + opkg_msg(ERROR, "Package %s is depended upon by packages:\n", + pkg->name); + while ((dep_ab_pkg = *dependents++) != NULL) { + if (dep_ab_pkg->state_status == SS_INSTALLED) + opkg_msg(ERROR, "\t%s\n", dep_ab_pkg->name); + } + opkg_msg(ERROR, + "These might cease to work if package %s is removed.\n\n", + pkg->name); + opkg_msg(ERROR, + "Force removal of this package with --force-depends.\n"); + opkg_msg(ERROR, "Force removal of this package and its dependents\n"); + opkg_msg(ERROR, "with --force-removal-of-dependent-packages.\n"); } /* * Find and remove packages that were autoinstalled and are orphaned * by the removal of pkg. */ -static int -remove_autoinstalled(pkg_t *pkg) +static int remove_autoinstalled(pkg_t * pkg) { int i, j; int err = 0; @@ -176,19 +177,17 @@ remove_autoinstalled(pkg_t *pkg) abstract_pkg_t **dependents; int count = pkg->pre_depends_count + - pkg->depends_count + - pkg->recommends_count + - pkg->suggests_count; + pkg->depends_count + pkg->recommends_count + pkg->suggests_count; - for (i=0; idepends[i]; if (cdep->type != PREDEPEND - && cdep->type != DEPEND - && cdep->type != RECOMMEND) + && cdep->type != DEPEND && cdep->type != RECOMMEND) continue; - for (j=0; jpossibility_count; j++) { - p = pkg_hash_fetch_installed_by_name( - cdep->possibilities[j]->pkg->name); + for (j = 0; j < cdep->possibility_count; j++) { + p = pkg_hash_fetch_installed_by_name(cdep-> + possibilities[j]-> + pkg->name); /* If the package is not installed, this could have * been a circular dependency and the package has @@ -202,17 +201,16 @@ remove_autoinstalled(pkg_t *pkg) n_deps = pkg_has_installed_dependents(p, &dependents); if (n_deps == 0) { - opkg_msg(NOTICE, "%s was autoinstalled and is " - "now orphaned, removing.\n", - p->name); + opkg_msg(NOTICE, "%s was autoinstalled and is " + "now orphaned, removing.\n", p->name); if (opkg_remove_pkg(p, 0) != 0) { err = -1; } } else opkg_msg(INFO, "%s was autoinstalled and is " - "still required by %d " - "installed packages.\n", - p->name, n_deps); + "still required by %d " + "installed packages.\n", + p->name, n_deps); if (dependents) free(dependents); @@ -222,211 +220,214 @@ remove_autoinstalled(pkg_t *pkg) return err; } -int -opkg_remove_pkg(pkg_t *pkg, int from_upgrade) +int opkg_remove_pkg(pkg_t * pkg, int from_upgrade) { - int err; - abstract_pkg_t *parent_pkg = NULL; + int err; + abstract_pkg_t *parent_pkg = NULL; /* * If called from an upgrade and not from a normal remove, * ignore the essential flag. */ - if (pkg->essential && !from_upgrade) { - if (conf->force_removal_of_essential_packages) { - opkg_msg(NOTICE, - "Removing essential package %s under your coercion.\n" - "\tIf your system breaks, you get to keep both pieces\n", - pkg->name); - } else { - opkg_msg(NOTICE, "Refusing to remove essential package %s.\n" - "\tRemoving an essential package may lead to an unusable system, but if\n" - "\tyou enjoy that kind of pain, you can force opkg to proceed against\n" - "\tits will with the option: --force-removal-of-essential-packages\n", - pkg->name); - return -1; - } - } - - if ((parent_pkg = pkg->parent) == NULL) - return 0; - - /* only attempt to remove dependent installed packages if - * force_depends is not specified or the package is being - * replaced. - */ - if (!conf->force_depends - && !(pkg->state_flag & SF_REPLACE)) { - abstract_pkg_t **dependents; - int has_installed_dependents = - pkg_has_installed_dependents(pkg, &dependents); - - if (has_installed_dependents) { - /* - * if this package is depended upon by others, then either we should - * not remove it or we should remove it and all of its dependents - */ - - if (!conf->force_removal_of_dependent_packages) { - print_dependents_warning(pkg, dependents); - free(dependents); - return -1; - } - - /* remove packages depending on this package - Karthik */ - err = opkg_remove_dependent_pkgs(pkg, dependents); - if (err) { - free(dependents); - return err; - } - } - if (dependents) - free(dependents); - } - - if (from_upgrade == 0) { - opkg_msg(NOTICE, "Removing package %s from %s...\n", + if (pkg->essential && !from_upgrade) { + if (conf->force_removal_of_essential_packages) { + opkg_msg(NOTICE, + "Removing essential package %s under your coercion.\n" + "\tIf your system breaks, you get to keep both pieces\n", + pkg->name); + } else { + opkg_msg(NOTICE, + "Refusing to remove essential package %s.\n" + "\tRemoving an essential package may lead to an unusable system, but if\n" + "\tyou enjoy that kind of pain, you can force opkg to proceed against\n" + "\tits will with the option: --force-removal-of-essential-packages\n", + pkg->name); + return -1; + } + } + + if ((parent_pkg = pkg->parent) == NULL) + return 0; + + /* only attempt to remove dependent installed packages if + * force_depends is not specified or the package is being + * replaced. + */ + if (!conf->force_depends && !(pkg->state_flag & SF_REPLACE)) { + abstract_pkg_t **dependents; + int has_installed_dependents = + pkg_has_installed_dependents(pkg, &dependents); + + if (has_installed_dependents) { + /* + * if this package is depended upon by others, then either we should + * not remove it or we should remove it and all of its dependents + */ + + if (!conf->force_removal_of_dependent_packages) { + print_dependents_warning(pkg, dependents); + free(dependents); + return -1; + } + + /* remove packages depending on this package - Karthik */ + err = opkg_remove_dependent_pkgs(pkg, dependents); + if (err) { + free(dependents); + return err; + } + } + if (dependents) + free(dependents); + } + + if (from_upgrade == 0) { + opkg_msg(NOTICE, "Removing package %s from %s...\n", pkg->name, pkg->dest->name); - } - pkg->state_flag |= SF_FILELIST_CHANGED; - - pkg->state_want = SW_DEINSTALL; - opkg_state_changed++; - - if (pkg_run_script(pkg, "prerm", "remove") != 0) { - if (!conf->force_remove) { - opkg_msg(ERROR, "not removing package \"%s\", " - "prerm script failed\n", pkg->name); - opkg_msg(NOTICE, "You can force removal of packages with failed " - "prerm scripts with the option: \n" - "\t--force-remove\n"); - return -1; - } - } - - /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It - maintains an empty filelist rather than deleting it. That seems - like a big pain, and I don't see that that should make a big - difference, but for anyone who wants tighter compatibility, - feel free to fix this. */ - remove_data_files_and_list(pkg); - - err = pkg_run_script(pkg, "postrm", "remove"); - - remove_maintainer_scripts(pkg); - pkg->state_status = SS_NOT_INSTALLED; - - if (parent_pkg) - parent_pkg->state_status = SS_NOT_INSTALLED; - - /* remove autoinstalled packages that are orphaned by the removal of this one */ - if (conf->autoremove) { - if (remove_autoinstalled(pkg) != 0) { - err = -1; - } - } - return err; + } + pkg->state_flag |= SF_FILELIST_CHANGED; + + pkg->state_want = SW_DEINSTALL; + opkg_state_changed++; + + if (pkg_run_script(pkg, "prerm", "remove") != 0) { + if (!conf->force_remove) { + opkg_msg(ERROR, "not removing package \"%s\", " + "prerm script failed\n", pkg->name); + opkg_msg(NOTICE, + "You can force removal of packages with failed " + "prerm scripts with the option: \n" + "\t--force-remove\n"); + return -1; + } + } + + /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It + maintains an empty filelist rather than deleting it. That seems + like a big pain, and I don't see that that should make a big + difference, but for anyone who wants tighter compatibility, + feel free to fix this. */ + remove_data_files_and_list(pkg); + + err = pkg_run_script(pkg, "postrm", "remove"); + + remove_maintainer_scripts(pkg); + pkg->state_status = SS_NOT_INSTALLED; + + if (parent_pkg) + parent_pkg->state_status = SS_NOT_INSTALLED; + + /* remove autoinstalled packages that are orphaned by the removal of this one */ + if (conf->autoremove) { + if (remove_autoinstalled(pkg) != 0) { + err = -1; + } + } + return err; } -void -remove_data_files_and_list(pkg_t *pkg) +void remove_data_files_and_list(pkg_t * pkg) { - str_list_t installed_dirs; - str_list_t *installed_files; - str_list_elt_t *iter; - char *file_name; - conffile_t *conffile; - int removed_a_dir; - pkg_t *owner; - int rootdirlen = 0; - - installed_files = pkg_get_installed_files(pkg); - if (installed_files == NULL) { - opkg_msg(ERROR, "Failed to determine installed " - "files for %s. None removed.\n", pkg->name); - return; - } - - str_list_init(&installed_dirs); - - /* don't include trailing slash */ - if (conf->offline_root) - rootdirlen = strlen(conf->offline_root); - - for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) { - file_name = (char *)iter->data; - - owner = file_hash_get_file_owner(file_name); - if (owner != pkg) - /* File may have been claimed by another package. */ - continue; - - if (file_is_dir(file_name)) { - str_list_append(&installed_dirs, file_name); - continue; - } - - conffile = pkg_get_conffile(pkg, file_name+rootdirlen); - if (conffile) { - if (conffile_has_been_modified(conffile)) { - opkg_msg(NOTICE, "Not deleting modified conffile %s.\n", - file_name); - continue; - } - } - - if (!conf->noaction) { - opkg_msg(INFO, "Deleting %s.\n", file_name); - unlink(file_name); - } else - opkg_msg(INFO, "Not deleting %s. (noaction)\n", - file_name); - - file_hash_remove(file_name); - } - - /* Remove empty directories */ - if (!conf->noaction) { - do { - removed_a_dir = 0; - for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) { - file_name = (char *)iter->data; - - if (rmdir(file_name) == 0) { - opkg_msg(INFO, "Deleting %s.\n", file_name); - removed_a_dir = 1; - str_list_remove(&installed_dirs, &iter); - } - } - } while (removed_a_dir); - } - - pkg_free_installed_files(pkg); - pkg_remove_installed_files_list(pkg); - - /* Don't print warning for dirs that are provided by other packages */ - for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) { - file_name = (char *)iter->data; - - owner = file_hash_get_file_owner(file_name); - if (owner) { - free(iter->data); - iter->data = NULL; - str_list_remove(&installed_dirs, &iter); - } - } - - /* cleanup */ - while (!void_list_empty(&installed_dirs)) { - iter = str_list_pop(&installed_dirs); - free(iter->data); - free(iter); - } - str_list_deinit(&installed_dirs); + str_list_t installed_dirs; + str_list_t *installed_files; + str_list_elt_t *iter; + char *file_name; + conffile_t *conffile; + int removed_a_dir; + pkg_t *owner; + int rootdirlen = 0; + + installed_files = pkg_get_installed_files(pkg); + if (installed_files == NULL) { + opkg_msg(ERROR, "Failed to determine installed " + "files for %s. None removed.\n", pkg->name); + return; + } + + str_list_init(&installed_dirs); + + /* don't include trailing slash */ + if (conf->offline_root) + rootdirlen = strlen(conf->offline_root); + + for (iter = str_list_first(installed_files); iter; + iter = str_list_next(installed_files, iter)) { + file_name = (char *)iter->data; + + owner = file_hash_get_file_owner(file_name); + if (owner != pkg) + /* File may have been claimed by another package. */ + continue; + + if (file_is_dir(file_name)) { + str_list_append(&installed_dirs, file_name); + continue; + } + + conffile = pkg_get_conffile(pkg, file_name + rootdirlen); + if (conffile) { + if (conffile_has_been_modified(conffile)) { + opkg_msg(NOTICE, + "Not deleting modified conffile %s.\n", + file_name); + continue; + } + } + + if (!conf->noaction) { + opkg_msg(INFO, "Deleting %s.\n", file_name); + unlink(file_name); + } else + opkg_msg(INFO, "Not deleting %s. (noaction)\n", + file_name); + + file_hash_remove(file_name); + } + + /* Remove empty directories */ + if (!conf->noaction) { + do { + removed_a_dir = 0; + for (iter = str_list_first(&installed_dirs); iter; + iter = str_list_next(&installed_dirs, iter)) { + file_name = (char *)iter->data; + + if (rmdir(file_name) == 0) { + opkg_msg(INFO, "Deleting %s.\n", + file_name); + removed_a_dir = 1; + str_list_remove(&installed_dirs, &iter); + } + } + } while (removed_a_dir); + } + + pkg_free_installed_files(pkg); + pkg_remove_installed_files_list(pkg); + + /* Don't print warning for dirs that are provided by other packages */ + for (iter = str_list_first(&installed_dirs); iter; + iter = str_list_next(&installed_dirs, iter)) { + file_name = (char *)iter->data; + + owner = file_hash_get_file_owner(file_name); + if (owner) { + free(iter->data); + iter->data = NULL; + str_list_remove(&installed_dirs, &iter); + } + } + + /* cleanup */ + while (!void_list_empty(&installed_dirs)) { + iter = str_list_pop(&installed_dirs); + free(iter->data); + free(iter); + } + str_list_deinit(&installed_dirs); } -void -remove_maintainer_scripts(pkg_t *pkg) +void remove_maintainer_scripts(pkg_t * pkg) { int i, err; char *globpattern; @@ -435,8 +436,7 @@ remove_maintainer_scripts(pkg_t *pkg) if (conf->noaction) return; - sprintf_alloc(&globpattern, "%s/%s.*", - pkg->dest->info_dir, pkg->name); + sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name); err = glob(globpattern, 0, NULL, &globbuf); free(globpattern); diff --git a/libopkg/opkg_remove.h b/libopkg/opkg_remove.h index f0c45d2..08760b9 100644 --- a/libopkg/opkg_remove.h +++ b/libopkg/opkg_remove.h @@ -21,10 +21,9 @@ #include "pkg.h" #include "opkg_conf.h" -int opkg_remove_pkg(pkg_t *pkg,int message); -int pkg_has_installed_dependents(pkg_t *pkg, abstract_pkg_t *** pdependents); -void remove_data_files_and_list(pkg_t *pkg); -void remove_maintainer_scripts(pkg_t *pkg); - +int opkg_remove_pkg(pkg_t * pkg, int message); +int pkg_has_installed_dependents(pkg_t * pkg, abstract_pkg_t *** pdependents); +void remove_data_files_and_list(pkg_t * pkg); +void remove_maintainer_scripts(pkg_t * pkg); #endif diff --git a/libopkg/opkg_upgrade.c b/libopkg/opkg_upgrade.c index 10b8217..e4eca11 100644 --- a/libopkg/opkg_upgrade.c +++ b/libopkg/opkg_upgrade.c @@ -23,67 +23,67 @@ #include "opkg_upgrade.h" #include "opkg_message.h" -int -opkg_upgrade_pkg(pkg_t *old) +int opkg_upgrade_pkg(pkg_t * old) { - pkg_t *new; - int cmp; - char *old_version, *new_version; - - if (old->state_flag & SF_HOLD) { - opkg_msg(NOTICE, "Not upgrading package %s which is marked " - "hold (flags=%#x).\n", old->name, old->state_flag); - return 0; - } - - new = pkg_hash_fetch_best_installation_candidate_by_name(old->name); - if (new == NULL) { - old_version = pkg_version_str_alloc(old); - opkg_msg(NOTICE, "Assuming locally installed package %s (%s) " - "is up to date.\n", old->name, old_version); - free(old_version); - return 0; - } - - old_version = pkg_version_str_alloc(old); - new_version = pkg_version_str_alloc(new); - - cmp = pkg_compare_versions(old, new); - opkg_msg(DEBUG, "Comparing visible versions of pkg %s:" - "\n\t%s is installed " - "\n\t%s is available " - "\n\t%d was comparison result\n", - old->name, old_version, new_version, cmp); - if (cmp == 0) { - opkg_msg(INFO, "Package %s (%s) installed in %s is up to date.\n", - old->name, old_version, old->dest->name); - free(old_version); - free(new_version); - return 0; - } else if (cmp > 0) { - opkg_msg(NOTICE, "Not downgrading package %s on %s from %s to %s.\n", - old->name, old->dest->name, old_version, new_version); - free(old_version); - free(new_version); - return 0; - } else if (cmp < 0) { - new->dest = old->dest; - old->state_want = SW_DEINSTALL; - } - - free(old_version); - free(new_version); - new->state_flag |= SF_USER; - return opkg_install_pkg(new,1); -} + pkg_t *new; + int cmp; + char *old_version, *new_version; + + if (old->state_flag & SF_HOLD) { + opkg_msg(NOTICE, "Not upgrading package %s which is marked " + "hold (flags=%#x).\n", old->name, old->state_flag); + return 0; + } + + new = pkg_hash_fetch_best_installation_candidate_by_name(old->name); + if (new == NULL) { + old_version = pkg_version_str_alloc(old); + opkg_msg(NOTICE, "Assuming locally installed package %s (%s) " + "is up to date.\n", old->name, old_version); + free(old_version); + return 0; + } + old_version = pkg_version_str_alloc(old); + new_version = pkg_version_str_alloc(new); + + cmp = pkg_compare_versions(old, new); + opkg_msg(DEBUG, "Comparing visible versions of pkg %s:" + "\n\t%s is installed " + "\n\t%s is available " + "\n\t%d was comparison result\n", + old->name, old_version, new_version, cmp); + if (cmp == 0) { + opkg_msg(INFO, + "Package %s (%s) installed in %s is up to date.\n", + old->name, old_version, old->dest->name); + free(old_version); + free(new_version); + return 0; + } else if (cmp > 0) { + opkg_msg(NOTICE, + "Not downgrading package %s on %s from %s to %s.\n", + old->name, old->dest->name, old_version, new_version); + free(old_version); + free(new_version); + return 0; + } else if (cmp < 0) { + new->dest = old->dest; + old->state_want = SW_DEINSTALL; + } + + free(old_version); + free(new_version); + new->state_flag |= SF_USER; + return opkg_install_pkg(new, 1); +} static void pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry, - void *data) + void *data) { - struct active_list *head = (struct active_list *) data; - abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry; + struct active_list *head = (struct active_list *)data; + abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry; pkg_vec_t *pkg_vec = ab_pkg->pkgs; int j; @@ -93,38 +93,41 @@ pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry, for (j = 0; j < pkg_vec->len; j++) { pkg_t *pkg = pkg_vec->pkgs[j]; if (pkg->state_status == SS_INSTALLED - || pkg->state_status == SS_UNPACKED) + || pkg->state_status == SS_UNPACKED) active_list_add(head, &pkg->list); } } -struct active_list * -prepare_upgrade_list(void) +struct active_list *prepare_upgrade_list(void) { - struct active_list *head = active_list_head_new(); - struct active_list *all = active_list_head_new(); - struct active_list *node=NULL; + struct active_list *head = active_list_head_new(); + struct active_list *all = active_list_head_new(); + struct active_list *node = NULL; - /* ensure all data is valid */ - pkg_info_preinstall_check(); + /* ensure all data is valid */ + pkg_info_preinstall_check(); - hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper, all); - for (node=active_list_next(all,all); node; node = active_list_next(all, node)) { - pkg_t *old, *new; - int cmp; + hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper, + all); + for (node = active_list_next(all, all); node; + node = active_list_next(all, node)) { + pkg_t *old, *new; + int cmp; - old = list_entry(node, pkg_t, list); - new = pkg_hash_fetch_best_installation_candidate_by_name(old->name); + old = list_entry(node, pkg_t, list); + new = + pkg_hash_fetch_best_installation_candidate_by_name(old-> + name); - if (new == NULL) - continue; + if (new == NULL) + continue; - cmp = pkg_compare_versions(old, new); + cmp = pkg_compare_versions(old, new); - if ( cmp < 0 ) { - node = active_list_move_node(all, head, &old->list); - } - } - active_list_head_delete(all); - return head; + if (cmp < 0) { + node = active_list_move_node(all, head, &old->list); + } + } + active_list_head_delete(all); + return head; } diff --git a/libopkg/opkg_upgrade.h b/libopkg/opkg_upgrade.h index 6545fa8..775a453 100644 --- a/libopkg/opkg_upgrade.h +++ b/libopkg/opkg_upgrade.h @@ -16,7 +16,7 @@ #define OPKG_UPGRADE_H #include "active_list.h" -int opkg_upgrade_pkg(pkg_t *old); -struct active_list * prepare_upgrade_list (void); +int opkg_upgrade_pkg(pkg_t * old); +struct active_list *prepare_upgrade_list(void); #endif diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index ebe4fa8..e16ca3a 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -20,59 +20,54 @@ #include "libbb/libbb.h" -unsigned long -get_available_kbytes(char * filesystem) +unsigned long get_available_kbytes(char *filesystem) { - struct statvfs f; - - if (statvfs(filesystem, &f) == -1) { - opkg_perror(ERROR, "Failed to statvfs for %s", filesystem); - return 0; - } - - // Actually ((sfs.f_bavail * sfs.f_frsize) / 1024) - // and here we try to avoid overflow. - if (f.f_frsize >= 1024) - return (f.f_bavail * (f.f_frsize / 1024)); - else if (f.f_frsize > 0) - return f.f_bavail / (1024 / f.f_frsize); - - opkg_msg(ERROR, "Unknown block size for target filesystem.\n"); - - return 0; + struct statvfs f; + + if (statvfs(filesystem, &f) == -1) { + opkg_perror(ERROR, "Failed to statvfs for %s", filesystem); + return 0; + } + // Actually ((sfs.f_bavail * sfs.f_frsize) / 1024) + // and here we try to avoid overflow. + if (f.f_frsize >= 1024) + return (f.f_bavail * (f.f_frsize / 1024)); + else if (f.f_frsize > 0) + return f.f_bavail / (1024 / f.f_frsize); + + opkg_msg(ERROR, "Unknown block size for target filesystem.\n"); + + return 0; } /* something to remove whitespace, a hash pooper */ char *trim_xstrdup(const char *src) { - const char *end; + const char *end; - /* remove it from the front */ - while(src && - isspace(*src) && - *src) - src++; + /* remove it from the front */ + while (src && isspace(*src) && *src) + src++; - end = src + (strlen(src) - 1); + end = src + (strlen(src) - 1); - /* and now from the back */ - while((end > src) && - isspace(*end)) - end--; + /* and now from the back */ + while ((end > src) && isspace(*end)) + end--; - end++; + end++; - /* xstrndup will NULL terminate for us */ - return xstrndup(src, end-src); + /* xstrndup will NULL terminate for us */ + return xstrndup(src, end - src); } int line_is_blank(const char *line) { - const char *s; + const char *s; - for (s = line; *s; s++) { - if (!isspace(*s)) - return 0; - } - return 1; + for (s = line; *s; s++) { + if (!isspace(*s)) + return 0; + } + return 1; } diff --git a/libopkg/opkg_utils.h b/libopkg/opkg_utils.h index 092d158..96e58eb 100644 --- a/libopkg/opkg_utils.h +++ b/libopkg/opkg_utils.h @@ -18,7 +18,7 @@ #ifndef OPKG_UTILS_H #define OPKG_UTILS_H -unsigned long get_available_kbytes(char * filesystem); +unsigned long get_available_kbytes(char *filesystem); char *trim_xstrdup(const char *line); int line_is_blank(const char *line); diff --git a/libopkg/parse_util.c b/libopkg/parse_util.c index 2aef7a9..4d453af 100644 --- a/libopkg/parse_util.c +++ b/libopkg/parse_util.c @@ -24,16 +24,14 @@ #include "parse_util.h" #include "pkg_parse.h" -int -is_field(const char *type, const char *line) +int is_field(const char *type, const char *line) { if (!strncmp(line, type, strlen(type))) return 1; return 0; } -char * -parse_simple(const char *type, const char *line) +char *parse_simple(const char *type, const char *line) { char *field = trim_xstrdup(line + strlen(type) + 1); if (strlen(field) == 0) { @@ -46,8 +44,8 @@ parse_simple(const char *type, const char *line) /* * Parse a comma separated string into an array. */ -char ** -parse_list(const char *raw, unsigned int *count, const char sep, int skip_field) +char **parse_list(const char *raw, unsigned int *count, const char sep, + int skip_field) { char **depends = NULL; const char *start, *end; @@ -55,9 +53,9 @@ parse_list(const char *raw, unsigned int *count, const char sep, int skip_field) /* skip past the "Field:" marker */ if (!skip_field) { - while (*raw && *raw != ':') + while (*raw && *raw != ':') + raw++; raw++; - raw++; } if (line_is_blank(raw)) { @@ -67,7 +65,7 @@ parse_list(const char *raw, unsigned int *count, const char sep, int skip_field) while (*raw) { depends = xrealloc(depends, sizeof(char *) * (line_count + 1)); - + while (isspace(*raw)) raw++; @@ -82,11 +80,11 @@ parse_list(const char *raw, unsigned int *count, const char sep, int skip_field) if (sep == ' ') end++; - depends[line_count] = xstrndup(start, end-start); + depends[line_count] = xstrndup(start, end - start); - line_count++; + line_count++; if (*raw == sep) - raw++; + raw++; } *count = line_count; @@ -94,8 +92,8 @@ parse_list(const char *raw, unsigned int *count, const char sep, int skip_field) } int -parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask, - char **buf0, size_t buf0len) +parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE * fp, + uint mask, char **buf0, size_t buf0len) { int ret, lineno; char *buf, *nl; @@ -113,9 +111,9 @@ parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint m if (ferror(fp)) { opkg_perror(ERROR, "fgets"); ret = -1; - } else if (strlen(*buf0) == buf0len-1) { + } else if (strlen(*buf0) == buf0len - 1) { opkg_msg(ERROR, "Missing new line character" - " at end of file!\n"); + " at end of file!\n"); parse_line(item, *buf0, mask); } break; @@ -123,21 +121,20 @@ parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint m nl = strchr(buf, '\n'); if (nl == NULL) { - if (strlen(buf) < buflen-1) { + if (strlen(buf) < buflen - 1) { /* * Line could be exactly buflen-1 long and * missing a newline, but we won't know until * fgets fails to read more data. */ opkg_msg(ERROR, "Missing new line character" - " at end of file!\n"); + " at end of file!\n"); parse_line(item, *buf0, mask); break; } if (buf0len >= EXCESSIVE_LINE_LEN) { opkg_msg(ERROR, "Excessively long line at " - "%d. Corrupt file?\n", - lineno); + "%d. Corrupt file?\n", lineno); ret = -1; break; } @@ -150,10 +147,10 @@ parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint m * |---------------------|---------------------| * buf0 buf */ - buflen = buf0len +1; + buflen = buf0len + 1; buf0len *= 2; *buf0 = xrealloc(*buf0, buf0len); - buf = *buf0 + buflen -2; + buf = *buf0 + buflen - 2; continue; } @@ -172,4 +169,3 @@ parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint m return ret; } - diff --git a/libopkg/parse_util.h b/libopkg/parse_util.h index 3263509..6b7d499 100644 --- a/libopkg/parse_util.h +++ b/libopkg/parse_util.h @@ -20,10 +20,11 @@ int is_field(const char *type, const char *line); char *parse_simple(const char *type, const char *line); -char **parse_list(const char *raw, unsigned int *count, const char sep, int skip_field); +char **parse_list(const char *raw, unsigned int *count, const char sep, + int skip_field); -typedef int (*parse_line_t)(void *, const char *, uint); -int parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask, - char **buf0, size_t buf0len); +typedef int (*parse_line_t) (void *, const char *, uint); +int parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE * fp, + uint mask, char **buf0, size_t buf0len); #endif diff --git a/libopkg/pkg.c b/libopkg/pkg.c index c75acb6..41aaff8 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -37,123 +37,117 @@ #include "opkg_conf.h" typedef struct enum_map enum_map_t; -struct enum_map -{ - unsigned int value; - const char *str; +struct enum_map { + unsigned int value; + const char *str; }; static const enum_map_t pkg_state_want_map[] = { - { SW_UNKNOWN, "unknown"}, - { SW_INSTALL, "install"}, - { SW_DEINSTALL, "deinstall"}, - { SW_PURGE, "purge"} + {SW_UNKNOWN, "unknown"}, + {SW_INSTALL, "install"}, + {SW_DEINSTALL, "deinstall"}, + {SW_PURGE, "purge"} }; static const enum_map_t pkg_state_flag_map[] = { - { SF_OK, "ok"}, - { SF_REINSTREQ, "reinstreq"}, - { SF_HOLD, "hold"}, - { SF_REPLACE, "replace"}, - { SF_NOPRUNE, "noprune"}, - { SF_PREFER, "prefer"}, - { SF_OBSOLETE, "obsolete"}, - { SF_USER, "user"}, + {SF_OK, "ok"}, + {SF_REINSTREQ, "reinstreq"}, + {SF_HOLD, "hold"}, + {SF_REPLACE, "replace"}, + {SF_NOPRUNE, "noprune"}, + {SF_PREFER, "prefer"}, + {SF_OBSOLETE, "obsolete"}, + {SF_USER, "user"}, }; static const enum_map_t pkg_state_status_map[] = { - { SS_NOT_INSTALLED, "not-installed" }, - { SS_UNPACKED, "unpacked" }, - { SS_HALF_CONFIGURED, "half-configured" }, - { SS_INSTALLED, "installed" }, - { SS_HALF_INSTALLED, "half-installed" }, - { SS_CONFIG_FILES, "config-files" }, - { SS_POST_INST_FAILED, "post-inst-failed" }, - { SS_REMOVAL_FAILED, "removal-failed" } + {SS_NOT_INSTALLED, "not-installed"}, + {SS_UNPACKED, "unpacked"}, + {SS_HALF_CONFIGURED, "half-configured"}, + {SS_INSTALLED, "installed"}, + {SS_HALF_INSTALLED, "half-installed"}, + {SS_CONFIG_FILES, "config-files"}, + {SS_POST_INST_FAILED, "post-inst-failed"}, + {SS_REMOVAL_FAILED, "removal-failed"} }; -static void -pkg_init(pkg_t *pkg) +static void pkg_init(pkg_t * pkg) { - pkg->name = NULL; - pkg->epoch = 0; - pkg->version = NULL; - pkg->revision = NULL; - pkg->dest = NULL; - pkg->src = NULL; - pkg->architecture = NULL; - pkg->maintainer = NULL; - pkg->section = NULL; - pkg->description = NULL; - pkg->state_want = SW_UNKNOWN; - pkg->state_flag = SF_OK; - pkg->state_status = SS_NOT_INSTALLED; - pkg->depends_str = NULL; - pkg->provides_str = NULL; - pkg->depends_count = 0; - pkg->depends = NULL; - pkg->suggests_str = NULL; - pkg->recommends_str = NULL; - pkg->suggests_count = 0; - pkg->recommends_count = 0; - - active_list_init(&pkg->list); - - pkg->conflicts = NULL; - pkg->conflicts_count = 0; - - pkg->replaces = NULL; - pkg->replaces_count = 0; - - pkg->pre_depends_count = 0; - pkg->pre_depends_str = NULL; - pkg->provides_count = 0; - pkg->provides = NULL; - pkg->filename = NULL; - pkg->local_filename = NULL; - pkg->tmp_unpack_dir = NULL; - pkg->md5sum = NULL; + pkg->name = NULL; + pkg->epoch = 0; + pkg->version = NULL; + pkg->revision = NULL; + pkg->dest = NULL; + pkg->src = NULL; + pkg->architecture = NULL; + pkg->maintainer = NULL; + pkg->section = NULL; + pkg->description = NULL; + pkg->state_want = SW_UNKNOWN; + pkg->state_flag = SF_OK; + pkg->state_status = SS_NOT_INSTALLED; + pkg->depends_str = NULL; + pkg->provides_str = NULL; + pkg->depends_count = 0; + pkg->depends = NULL; + pkg->suggests_str = NULL; + pkg->recommends_str = NULL; + pkg->suggests_count = 0; + pkg->recommends_count = 0; + + active_list_init(&pkg->list); + + pkg->conflicts = NULL; + pkg->conflicts_count = 0; + + pkg->replaces = NULL; + pkg->replaces_count = 0; + + pkg->pre_depends_count = 0; + pkg->pre_depends_str = NULL; + pkg->provides_count = 0; + pkg->provides = NULL; + pkg->filename = NULL; + pkg->local_filename = NULL; + pkg->tmp_unpack_dir = NULL; + pkg->md5sum = NULL; #if defined HAVE_SHA256 - pkg->sha256sum = NULL; + pkg->sha256sum = NULL; #endif - pkg->size = 0; - pkg->installed_size = 0; - pkg->priority = NULL; - pkg->source = NULL; - conffile_list_init(&pkg->conffiles); - pkg->installed_files = NULL; - pkg->installed_files_ref_cnt = 0; - pkg->essential = 0; - pkg->provided_by_hand = 0; + pkg->size = 0; + pkg->installed_size = 0; + pkg->priority = NULL; + pkg->source = NULL; + conffile_list_init(&pkg->conffiles); + pkg->installed_files = NULL; + pkg->installed_files_ref_cnt = 0; + pkg->essential = 0; + pkg->provided_by_hand = 0; } -pkg_t * -pkg_new(void) +pkg_t *pkg_new(void) { - pkg_t *pkg; + pkg_t *pkg; - pkg = xcalloc(1, sizeof(pkg_t)); - pkg_init(pkg); + pkg = xcalloc(1, sizeof(pkg_t)); + pkg_init(pkg); - return pkg; + return pkg; } -static void -compound_depend_deinit(compound_depend_t *depends) +static void compound_depend_deinit(compound_depend_t * depends) { - int i; - for (i = 0; i < depends->possibility_count; i++) - { - depend_t *d; - d = depends->possibilities[i]; - free (d->version); - free (d); - } - free (depends->possibilities); + int i; + for (i = 0; i < depends->possibility_count; i++) { + depend_t *d; + d = depends->possibilities[i]; + free(d->version); + free(d); + } + free(depends->possibilities); } -void -pkg_deinit(pkg_t *pkg) +void pkg_deinit(pkg_t * pkg) { int i; @@ -197,28 +191,27 @@ pkg_deinit(pkg_t *pkg) active_list_clear(&pkg->list); if (pkg->replaces) - free (pkg->replaces); + free(pkg->replaces); pkg->replaces = NULL; if (pkg->depends) { int count = pkg->pre_depends_count - + pkg->depends_count - + pkg->recommends_count - + pkg->suggests_count; + + pkg->depends_count + + pkg->recommends_count + pkg->suggests_count; - for (i=0; idepends[i]); - free (pkg->depends); + for (i = 0; i < count; i++) + compound_depend_deinit(&pkg->depends[i]); + free(pkg->depends); } if (pkg->conflicts) { - for (i=0; iconflicts_count; i++) - compound_depend_deinit (&pkg->conflicts[i]); - free (pkg->conflicts); + for (i = 0; i < pkg->conflicts_count; i++) + compound_depend_deinit(&pkg->conflicts[i]); + free(pkg->conflicts); } if (pkg->provides) - free (pkg->provides); + free(pkg->provides); pkg->pre_depends_count = 0; pkg->provides_count = 0; @@ -231,9 +224,9 @@ pkg_deinit(pkg_t *pkg) free(pkg->local_filename); pkg->local_filename = NULL; - /* CLEANUP: It'd be nice to pullin the cleanup function from - opkg_install.c here. See comment in - opkg_install.c:cleanup_temporary_files */ + /* CLEANUP: It'd be nice to pullin the cleanup function from + opkg_install.c here. See comment in + opkg_install.c:cleanup_temporary_files */ if (pkg->tmp_unpack_dir) free(pkg->tmp_unpack_dir); pkg->tmp_unpack_dir = NULL; @@ -259,19 +252,18 @@ pkg_deinit(pkg_t *pkg) conffile_list_deinit(&pkg->conffiles); /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so, - since if they are calling deinit, they should know. Maybe do an - assertion here instead? */ + since if they are calling deinit, they should know. Maybe do an + assertion here instead? */ pkg->installed_files_ref_cnt = 1; pkg_free_installed_files(pkg); pkg->essential = 0; if (pkg->tags) - free (pkg->tags); + free(pkg->tags); pkg->tags = NULL; } -int -pkg_init_from_file(pkg_t *pkg, const char *filename) +int pkg_init_from_file(pkg_t * pkg, const char *filename) { int fd, err = 0; FILE *control_file; @@ -283,8 +275,7 @@ pkg_init_from_file(pkg_t *pkg, const char *filename) tmp = xstrdup(filename); sprintf_alloc(&control_path, "%s/%s.control.XXXXXX", - conf->tmp_dir, - basename(tmp)); + conf->tmp_dir, basename(tmp)); free(tmp); fd = mkstemp(control_path); if (fd == -1) { @@ -304,7 +295,7 @@ pkg_init_from_file(pkg_t *pkg, const char *filename) err = pkg_extract_control_file_to_stream(pkg, control_file); if (err) { opkg_msg(ERROR, "Failed to extract control file from %s.\n", - filename); + filename); goto err2; } @@ -313,7 +304,7 @@ pkg_init_from_file(pkg_t *pkg, const char *filename) if ((err = pkg_parse_from_stream(pkg, control_file, 0))) { if (err == 1) { opkg_msg(ERROR, "Malformed package file %s.\n", - filename); + filename); } err = -1; } @@ -329,188 +320,187 @@ err0: } /* Merge any new information in newpkg into oldpkg */ -int -pkg_merge(pkg_t *oldpkg, pkg_t *newpkg) +int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg) { - if (oldpkg == newpkg) { - return 0; - } - - if (!oldpkg->auto_installed) - oldpkg->auto_installed = newpkg->auto_installed; - - if (!oldpkg->src) - oldpkg->src = newpkg->src; - if (!oldpkg->dest) - oldpkg->dest = newpkg->dest; - if (!oldpkg->architecture) - oldpkg->architecture = xstrdup(newpkg->architecture); - if (!oldpkg->arch_priority) - oldpkg->arch_priority = newpkg->arch_priority; - if (!oldpkg->section) - oldpkg->section = xstrdup(newpkg->section); - if(!oldpkg->maintainer) - oldpkg->maintainer = xstrdup(newpkg->maintainer); - if(!oldpkg->description) - oldpkg->description = xstrdup(newpkg->description); - - if (!oldpkg->depends_count && !oldpkg->pre_depends_count && !oldpkg->recommends_count && !oldpkg->suggests_count) { - oldpkg->depends_count = newpkg->depends_count; - newpkg->depends_count = 0; - - oldpkg->depends = newpkg->depends; - newpkg->depends = NULL; - - oldpkg->pre_depends_count = newpkg->pre_depends_count; - newpkg->pre_depends_count = 0; - - oldpkg->recommends_count = newpkg->recommends_count; - newpkg->recommends_count = 0; - - oldpkg->suggests_count = newpkg->suggests_count; - newpkg->suggests_count = 0; - } - - if (oldpkg->provides_count <= 1) { - oldpkg->provides_count = newpkg->provides_count; - newpkg->provides_count = 0; - - if (!oldpkg->provides) { - oldpkg->provides = newpkg->provides; - newpkg->provides = NULL; - } - } - - if (!oldpkg->conflicts_count) { - oldpkg->conflicts_count = newpkg->conflicts_count; - newpkg->conflicts_count = 0; - - oldpkg->conflicts = newpkg->conflicts; - newpkg->conflicts = NULL; - } - - if (!oldpkg->replaces_count) { - oldpkg->replaces_count = newpkg->replaces_count; - newpkg->replaces_count = 0; - - oldpkg->replaces = newpkg->replaces; - newpkg->replaces = NULL; - } - - if (!oldpkg->filename) - oldpkg->filename = xstrdup(newpkg->filename); - if (!oldpkg->local_filename) - oldpkg->local_filename = xstrdup(newpkg->local_filename); - if (!oldpkg->tmp_unpack_dir) - oldpkg->tmp_unpack_dir = xstrdup(newpkg->tmp_unpack_dir); - if (!oldpkg->md5sum) - oldpkg->md5sum = xstrdup(newpkg->md5sum); + if (oldpkg == newpkg) { + return 0; + } + + if (!oldpkg->auto_installed) + oldpkg->auto_installed = newpkg->auto_installed; + + if (!oldpkg->src) + oldpkg->src = newpkg->src; + if (!oldpkg->dest) + oldpkg->dest = newpkg->dest; + if (!oldpkg->architecture) + oldpkg->architecture = xstrdup(newpkg->architecture); + if (!oldpkg->arch_priority) + oldpkg->arch_priority = newpkg->arch_priority; + if (!oldpkg->section) + oldpkg->section = xstrdup(newpkg->section); + if (!oldpkg->maintainer) + oldpkg->maintainer = xstrdup(newpkg->maintainer); + if (!oldpkg->description) + oldpkg->description = xstrdup(newpkg->description); + + if (!oldpkg->depends_count && !oldpkg->pre_depends_count + && !oldpkg->recommends_count && !oldpkg->suggests_count) { + oldpkg->depends_count = newpkg->depends_count; + newpkg->depends_count = 0; + + oldpkg->depends = newpkg->depends; + newpkg->depends = NULL; + + oldpkg->pre_depends_count = newpkg->pre_depends_count; + newpkg->pre_depends_count = 0; + + oldpkg->recommends_count = newpkg->recommends_count; + newpkg->recommends_count = 0; + + oldpkg->suggests_count = newpkg->suggests_count; + newpkg->suggests_count = 0; + } + + if (oldpkg->provides_count <= 1) { + oldpkg->provides_count = newpkg->provides_count; + newpkg->provides_count = 0; + + if (!oldpkg->provides) { + oldpkg->provides = newpkg->provides; + newpkg->provides = NULL; + } + } + + if (!oldpkg->conflicts_count) { + oldpkg->conflicts_count = newpkg->conflicts_count; + newpkg->conflicts_count = 0; + + oldpkg->conflicts = newpkg->conflicts; + newpkg->conflicts = NULL; + } + + if (!oldpkg->replaces_count) { + oldpkg->replaces_count = newpkg->replaces_count; + newpkg->replaces_count = 0; + + oldpkg->replaces = newpkg->replaces; + newpkg->replaces = NULL; + } + + if (!oldpkg->filename) + oldpkg->filename = xstrdup(newpkg->filename); + if (!oldpkg->local_filename) + oldpkg->local_filename = xstrdup(newpkg->local_filename); + if (!oldpkg->tmp_unpack_dir) + oldpkg->tmp_unpack_dir = xstrdup(newpkg->tmp_unpack_dir); + if (!oldpkg->md5sum) + oldpkg->md5sum = xstrdup(newpkg->md5sum); #if defined HAVE_SHA256 - if (!oldpkg->sha256sum) - oldpkg->sha256sum = xstrdup(newpkg->sha256sum); + if (!oldpkg->sha256sum) + oldpkg->sha256sum = xstrdup(newpkg->sha256sum); #endif - if (!oldpkg->size) - oldpkg->size = newpkg->size; - if (!oldpkg->installed_size) - oldpkg->installed_size = newpkg->installed_size; - if (!oldpkg->priority) - oldpkg->priority = xstrdup(newpkg->priority); - if (!oldpkg->source) - oldpkg->source = xstrdup(newpkg->source); - - if (nv_pair_list_empty(&oldpkg->conffiles)){ - list_splice_init(&newpkg->conffiles.head, &oldpkg->conffiles.head); - } - - if (!oldpkg->installed_files){ - oldpkg->installed_files = newpkg->installed_files; - oldpkg->installed_files_ref_cnt = newpkg->installed_files_ref_cnt; - newpkg->installed_files = NULL; - } - - if (!oldpkg->essential) - oldpkg->essential = newpkg->essential; - - return 0; + if (!oldpkg->size) + oldpkg->size = newpkg->size; + if (!oldpkg->installed_size) + oldpkg->installed_size = newpkg->installed_size; + if (!oldpkg->priority) + oldpkg->priority = xstrdup(newpkg->priority); + if (!oldpkg->source) + oldpkg->source = xstrdup(newpkg->source); + + if (nv_pair_list_empty(&oldpkg->conffiles)) { + list_splice_init(&newpkg->conffiles.head, + &oldpkg->conffiles.head); + } + + if (!oldpkg->installed_files) { + oldpkg->installed_files = newpkg->installed_files; + oldpkg->installed_files_ref_cnt = + newpkg->installed_files_ref_cnt; + newpkg->installed_files = NULL; + } + + if (!oldpkg->essential) + oldpkg->essential = newpkg->essential; + + return 0; } -static void -abstract_pkg_init(abstract_pkg_t *ab_pkg) +static void abstract_pkg_init(abstract_pkg_t * ab_pkg) { - ab_pkg->provided_by = abstract_pkg_vec_alloc(); - ab_pkg->dependencies_checked = 0; - ab_pkg->state_status = SS_NOT_INSTALLED; + ab_pkg->provided_by = abstract_pkg_vec_alloc(); + ab_pkg->dependencies_checked = 0; + ab_pkg->state_status = SS_NOT_INSTALLED; } -abstract_pkg_t * -abstract_pkg_new(void) +abstract_pkg_t *abstract_pkg_new(void) { - abstract_pkg_t * ab_pkg; + abstract_pkg_t *ab_pkg; - ab_pkg = xcalloc(1, sizeof(abstract_pkg_t)); - abstract_pkg_init(ab_pkg); + ab_pkg = xcalloc(1, sizeof(abstract_pkg_t)); + abstract_pkg_init(ab_pkg); - return ab_pkg; + return ab_pkg; } -void -set_flags_from_control(pkg_t *pkg){ - char *file_name; - FILE *fp; +void set_flags_from_control(pkg_t * pkg) +{ + char *file_name; + FILE *fp; - sprintf_alloc(&file_name,"%s/%s.control", pkg->dest->info_dir, pkg->name); + sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir, + pkg->name); - fp = fopen(file_name, "r"); - if (fp == NULL) { - opkg_perror(ERROR, "Failed to open %s", file_name); - free(file_name); - return; - } + fp = fopen(file_name, "r"); + if (fp == NULL) { + opkg_perror(ERROR, "Failed to open %s", file_name); + free(file_name); + return; + } - free(file_name); + free(file_name); - if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) { - opkg_msg(DEBUG, "Unable to read control file for %s. May be empty.\n", - pkg->name); - } + if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) { + opkg_msg(DEBUG, + "Unable to read control file for %s. May be empty.\n", + pkg->name); + } - fclose(fp); + fclose(fp); - return; + return; } -static const char * -pkg_state_want_to_str(pkg_state_want_t sw) +static const char *pkg_state_want_to_str(pkg_state_want_t sw) { - int i; + int i; - for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) { - if (pkg_state_want_map[i].value == sw) { - return pkg_state_want_map[i].str; - } - } + for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) { + if (pkg_state_want_map[i].value == sw) { + return pkg_state_want_map[i].str; + } + } - opkg_msg(ERROR, "Internal error: state_want=%d\n", sw); - return ""; + opkg_msg(ERROR, "Internal error: state_want=%d\n", sw); + return ""; } -pkg_state_want_t -pkg_state_want_from_str(char *str) +pkg_state_want_t pkg_state_want_from_str(char *str) { - int i; + int i; - for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) { - if (strcmp(str, pkg_state_want_map[i].str) == 0) { - return pkg_state_want_map[i].value; - } - } + for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) { + if (strcmp(str, pkg_state_want_map[i].str) == 0) { + return pkg_state_want_map[i].value; + } + } - opkg_msg(ERROR, "Internal error: state_want=%s\n", str); - return SW_UNKNOWN; + opkg_msg(ERROR, "Internal error: state_want=%s\n", str); + return SW_UNKNOWN; } -static char * -pkg_state_flag_to_str(pkg_state_flag_t sf) +static char *pkg_state_flag_to_str(pkg_state_flag_t sf) { int i; unsigned int len; @@ -523,15 +513,15 @@ pkg_state_flag_to_str(pkg_state_flag_t sf) return xstrdup("ok"); len = 0; - for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { + for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { if (sf & pkg_state_flag_map[i].value) len += strlen(pkg_state_flag_map[i].str) + 1; } - str = xmalloc(len+1); + str = xmalloc(len + 1); str[0] = '\0'; - for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { + for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { if (sf & pkg_state_flag_map[i].value) { strncat(str, pkg_state_flag_map[i].str, len); strncat(str, ",", len); @@ -539,312 +529,319 @@ pkg_state_flag_to_str(pkg_state_flag_t sf) } len = strlen(str); - str[len-1] = '\0'; /* squash last comma */ + str[len - 1] = '\0'; /* squash last comma */ return str; } -pkg_state_flag_t -pkg_state_flag_from_str(const char *str) +pkg_state_flag_t pkg_state_flag_from_str(const char *str) { - int i; - int sf = SF_OK; - const char *sfname; - unsigned int sfname_len; - - if (strcmp(str, "ok") == 0) { - return SF_OK; - } - for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { - sfname = pkg_state_flag_map[i].str; - sfname_len = strlen(sfname); - if (strncmp(str, sfname, sfname_len) == 0) { - sf |= pkg_state_flag_map[i].value; - str += sfname_len; - if (str[0] == ',') { - str++; - } else { - break; - } - } - } - - return sf; + int i; + int sf = SF_OK; + const char *sfname; + unsigned int sfname_len; + + if (strcmp(str, "ok") == 0) { + return SF_OK; + } + for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) { + sfname = pkg_state_flag_map[i].str; + sfname_len = strlen(sfname); + if (strncmp(str, sfname, sfname_len) == 0) { + sf |= pkg_state_flag_map[i].value; + str += sfname_len; + if (str[0] == ',') { + str++; + } else { + break; + } + } + } + + return sf; } -static const char * -pkg_state_status_to_str(pkg_state_status_t ss) +static const char *pkg_state_status_to_str(pkg_state_status_t ss) { - int i; + int i; - for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) { - if (pkg_state_status_map[i].value == ss) { - return pkg_state_status_map[i].str; - } - } + for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) { + if (pkg_state_status_map[i].value == ss) { + return pkg_state_status_map[i].str; + } + } - opkg_msg(ERROR, "Internal error: state_status=%d\n", ss); - return ""; + opkg_msg(ERROR, "Internal error: state_status=%d\n", ss); + return ""; } -pkg_state_status_t -pkg_state_status_from_str(const char *str) +pkg_state_status_t pkg_state_status_from_str(const char *str) { - int i; + int i; - for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) { - if (strcmp(str, pkg_state_status_map[i].str) == 0) { - return pkg_state_status_map[i].value; - } - } + for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) { + if (strcmp(str, pkg_state_status_map[i].str) == 0) { + return pkg_state_status_map[i].value; + } + } - opkg_msg(ERROR, "Internal error: state_status=%s\n", str); - return SS_NOT_INSTALLED; + opkg_msg(ERROR, "Internal error: state_status=%s\n", str); + return SS_NOT_INSTALLED; } -void -pkg_formatted_field(FILE *fp, pkg_t *pkg, const char *field) +void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field) { - int i, j; - char *str; - int depends_count = pkg->pre_depends_count + - pkg->depends_count + - pkg->recommends_count + - pkg->suggests_count; - - if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) { - goto UNKNOWN_FMT_FIELD; - } - - switch (field[0]) - { - case 'a': - case 'A': - if (strcasecmp(field, "Architecture") == 0) { - if (pkg->architecture) { - fprintf(fp, "Architecture: %s\n", pkg->architecture); - } - } else if (strcasecmp(field, "Auto-Installed") == 0) { - if (pkg->auto_installed) - fprintf(fp, "Auto-Installed: yes\n"); - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 'c': - case 'C': - if (strcasecmp(field, "Conffiles") == 0) { - conffile_list_elt_t *iter; - - if (nv_pair_list_empty(&pkg->conffiles)) - return; - - fprintf(fp, "Conffiles:\n"); - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - if (((conffile_t *)iter->data)->name && ((conffile_t *)iter->data)->value) { - fprintf(fp, " %s %s\n", - ((conffile_t *)iter->data)->name, - ((conffile_t *)iter->data)->value); - } - } - } else if (strcasecmp(field, "Conflicts") == 0) { - struct depend *cdep; - if (pkg->conflicts_count) { - fprintf(fp, "Conflicts:"); - for(i = 0; i < pkg->conflicts_count; i++) { - cdep = pkg->conflicts[i].possibilities[0]; - fprintf(fp, "%s %s", i == 0 ? "" : ",", - cdep->pkg->name); - if (cdep->version) { - fprintf(fp, " (%s%s)", - constraint_to_str(cdep->constraint), - cdep->version); + int i, j; + char *str; + int depends_count = pkg->pre_depends_count + + pkg->depends_count + pkg->recommends_count + pkg->suggests_count; + + if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) { + goto UNKNOWN_FMT_FIELD; + } + + switch (field[0]) { + case 'a': + case 'A': + if (strcasecmp(field, "Architecture") == 0) { + if (pkg->architecture) { + fprintf(fp, "Architecture: %s\n", + pkg->architecture); + } + } else if (strcasecmp(field, "Auto-Installed") == 0) { + if (pkg->auto_installed) + fprintf(fp, "Auto-Installed: yes\n"); + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 'c': + case 'C': + if (strcasecmp(field, "Conffiles") == 0) { + conffile_list_elt_t *iter; + + if (nv_pair_list_empty(&pkg->conffiles)) + return; + + fprintf(fp, "Conffiles:\n"); + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + if (((conffile_t *) iter->data)->name + && ((conffile_t *) iter->data)->value) { + fprintf(fp, " %s %s\n", + ((conffile_t *) iter->data)-> + name, + ((conffile_t *) iter->data)-> + value); + } + } + } else if (strcasecmp(field, "Conflicts") == 0) { + struct depend *cdep; + if (pkg->conflicts_count) { + fprintf(fp, "Conflicts:"); + for (i = 0; i < pkg->conflicts_count; i++) { + cdep = + pkg->conflicts[i].possibilities[0]; + fprintf(fp, "%s %s", i == 0 ? "" : ",", + cdep->pkg->name); + if (cdep->version) { + fprintf(fp, " (%s%s)", + constraint_to_str(cdep-> + constraint), + cdep->version); + } + } + fprintf(fp, "\n"); + } + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 'd': + case 'D': + if (strcasecmp(field, "Depends") == 0) { + if (pkg->depends_count) { + fprintf(fp, "Depends:"); + for (j = 0, i = 0; i < depends_count; i++) { + if (pkg->depends[i].type != DEPEND) + continue; + str = pkg_depend_str(pkg, i); + fprintf(fp, "%s %s", j == 0 ? "" : ",", + str); + free(str); + j++; + } + fprintf(fp, "\n"); + } + } else if (strcasecmp(field, "Description") == 0) { + if (pkg->description) { + fprintf(fp, "Description: %s\n", + pkg->description); + } + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 'e': + case 'E': + if (pkg->essential) { + fprintf(fp, "Essential: yes\n"); + } + break; + case 'f': + case 'F': + if (pkg->filename) { + fprintf(fp, "Filename: %s\n", pkg->filename); + } + break; + case 'i': + case 'I': + if (strcasecmp(field, "Installed-Size") == 0) { + fprintf(fp, "Installed-Size: %ld\n", + pkg->installed_size); + } else if (strcasecmp(field, "Installed-Time") == 0 + && pkg->installed_time) { + fprintf(fp, "Installed-Time: %lu\n", + pkg->installed_time); + } + break; + case 'm': + case 'M': + if (strcasecmp(field, "Maintainer") == 0) { + if (pkg->maintainer) { + fprintf(fp, "Maintainer: %s\n", + pkg->maintainer); + } + } else if (strcasecmp(field, "MD5sum") == 0) { + if (pkg->md5sum) { + fprintf(fp, "MD5Sum: %s\n", pkg->md5sum); + } + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 'p': + case 'P': + if (strcasecmp(field, "Package") == 0) { + fprintf(fp, "Package: %s\n", pkg->name); + } else if (strcasecmp(field, "Priority") == 0) { + fprintf(fp, "Priority: %s\n", pkg->priority); + } else if (strcasecmp(field, "Provides") == 0) { + if (pkg->provides_count > 1) { + fprintf(fp, "Provides:"); + for (i = 1; i < pkg->provides_count; i++) { + fprintf(fp, "%s %s", i == 1 ? "" : ",", + pkg->provides[i]->name); + } + fprintf(fp, "\n"); + } + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 'r': + case 'R': + if (strcasecmp(field, "Replaces") == 0) { + if (pkg->replaces_count) { + fprintf(fp, "Replaces:"); + for (i = 0; i < pkg->replaces_count; i++) { + fprintf(fp, "%s %s", i == 0 ? "" : ",", + pkg->replaces[i]->name); + } + fprintf(fp, "\n"); + } + } else if (strcasecmp(field, "Recommends") == 0) { + if (pkg->recommends_count) { + fprintf(fp, "Recommends:"); + for (j = 0, i = 0; i < depends_count; i++) { + if (pkg->depends[i].type != RECOMMEND) + continue; + str = pkg_depend_str(pkg, i); + fprintf(fp, "%s %s", j == 0 ? "" : ",", + str); + free(str); + j++; + } + fprintf(fp, "\n"); + } + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 's': + case 'S': + if (strcasecmp(field, "Section") == 0) { + if (pkg->section) { + fprintf(fp, "Section: %s\n", pkg->section); } - } - fprintf(fp, "\n"); - } - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 'd': - case 'D': - if (strcasecmp(field, "Depends") == 0) { - if (pkg->depends_count) { - fprintf(fp, "Depends:"); - for (j=0, i=0; idepends[i].type != DEPEND) - continue; - str = pkg_depend_str(pkg, i); - fprintf(fp, "%s %s", j == 0 ? "" : ",", str); - free(str); - j++; - } - fprintf(fp, "\n"); - } - } else if (strcasecmp(field, "Description") == 0) { - if (pkg->description) { - fprintf(fp, "Description: %s\n", pkg->description); - } - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 'e': - case 'E': - if (pkg->essential) { - fprintf(fp, "Essential: yes\n"); - } - break; - case 'f': - case 'F': - if (pkg->filename) { - fprintf(fp, "Filename: %s\n", pkg->filename); - } - break; - case 'i': - case 'I': - if (strcasecmp(field, "Installed-Size") == 0) { - fprintf(fp, "Installed-Size: %ld\n", pkg->installed_size); - } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) { - fprintf(fp, "Installed-Time: %lu\n", pkg->installed_time); - } - break; - case 'm': - case 'M': - if (strcasecmp(field, "Maintainer") == 0) { - if (pkg->maintainer) { - fprintf(fp, "Maintainer: %s\n", pkg->maintainer); - } - } else if (strcasecmp(field, "MD5sum") == 0) { - if (pkg->md5sum) { - fprintf(fp, "MD5Sum: %s\n", pkg->md5sum); - } - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 'p': - case 'P': - if (strcasecmp(field, "Package") == 0) { - fprintf(fp, "Package: %s\n", pkg->name); - } else if (strcasecmp(field, "Priority") == 0) { - fprintf(fp, "Priority: %s\n", pkg->priority); - } else if (strcasecmp(field, "Provides") == 0) { - if (pkg->provides_count > 1) { - fprintf(fp, "Provides:"); - for(i = 1; i < pkg->provides_count; i++) { - fprintf(fp, "%s %s", i == 1 ? "" : ",", - pkg->provides[i]->name); - } - fprintf(fp, "\n"); - } - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 'r': - case 'R': - if (strcasecmp (field, "Replaces") == 0) { - if (pkg->replaces_count) { - fprintf(fp, "Replaces:"); - for (i = 0; i < pkg->replaces_count; i++) { - fprintf(fp, "%s %s", i == 0 ? "" : ",", - pkg->replaces[i]->name); - } - fprintf(fp, "\n"); - } - } else if (strcasecmp (field, "Recommends") == 0) { - if (pkg->recommends_count) { - fprintf(fp, "Recommends:"); - for (j=0, i=0; idepends[i].type != RECOMMEND) - continue; - str = pkg_depend_str(pkg, i); - fprintf(fp, "%s %s", j == 0 ? "" : ",", str); - free(str); - j++; - } - fprintf(fp, "\n"); - } - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 's': - case 'S': - if (strcasecmp(field, "Section") == 0) { - if (pkg->section) { - fprintf(fp, "Section: %s\n", pkg->section); - } #if defined HAVE_SHA256 - } else if (strcasecmp(field, "SHA256sum") == 0) { - if (pkg->sha256sum) { - fprintf(fp, "SHA256sum: %s\n", pkg->sha256sum); - } + } else if (strcasecmp(field, "SHA256sum") == 0) { + if (pkg->sha256sum) { + fprintf(fp, "SHA256sum: %s\n", pkg->sha256sum); + } #endif - } else if (strcasecmp(field, "Size") == 0) { - if (pkg->size) { - fprintf(fp, "Size: %ld\n", pkg->size); - } - } else if (strcasecmp(field, "Source") == 0) { - if (pkg->source) { - fprintf(fp, "Source: %s\n", pkg->source); - } - } else if (strcasecmp(field, "Status") == 0) { - char *pflag = pkg_state_flag_to_str(pkg->state_flag); - fprintf(fp, "Status: %s %s %s\n", - pkg_state_want_to_str(pkg->state_want), - pflag, - pkg_state_status_to_str(pkg->state_status)); - free(pflag); - } else if (strcasecmp(field, "Suggests") == 0) { - if (pkg->suggests_count) { - fprintf(fp, "Suggests:"); - for (j=0, i=0; idepends[i].type != SUGGEST) - continue; - str = pkg_depend_str(pkg, i); - fprintf(fp, "%s %s", j == 0 ? "" : ",", str); - free(str); - j++; - } - fprintf(fp, "\n"); - } - } else { - goto UNKNOWN_FMT_FIELD; - } - break; - case 't': - case 'T': - if (strcasecmp(field, "Tags") == 0) { - if (pkg->tags) { - fprintf(fp, "Tags: %s\n", pkg->tags); - } - } - break; - case 'v': - case 'V': - { - char *version = pkg_version_str_alloc(pkg); - if (version == NULL) - return; - fprintf(fp, "Version: %s\n", version); - free(version); - } - break; - default: - goto UNKNOWN_FMT_FIELD; - } - - return; + } else if (strcasecmp(field, "Size") == 0) { + if (pkg->size) { + fprintf(fp, "Size: %ld\n", pkg->size); + } + } else if (strcasecmp(field, "Source") == 0) { + if (pkg->source) { + fprintf(fp, "Source: %s\n", pkg->source); + } + } else if (strcasecmp(field, "Status") == 0) { + char *pflag = pkg_state_flag_to_str(pkg->state_flag); + fprintf(fp, "Status: %s %s %s\n", + pkg_state_want_to_str(pkg->state_want), + pflag, + pkg_state_status_to_str(pkg->state_status)); + free(pflag); + } else if (strcasecmp(field, "Suggests") == 0) { + if (pkg->suggests_count) { + fprintf(fp, "Suggests:"); + for (j = 0, i = 0; i < depends_count; i++) { + if (pkg->depends[i].type != SUGGEST) + continue; + str = pkg_depend_str(pkg, i); + fprintf(fp, "%s %s", j == 0 ? "" : ",", + str); + free(str); + j++; + } + fprintf(fp, "\n"); + } + } else { + goto UNKNOWN_FMT_FIELD; + } + break; + case 't': + case 'T': + if (strcasecmp(field, "Tags") == 0) { + if (pkg->tags) { + fprintf(fp, "Tags: %s\n", pkg->tags); + } + } + break; + case 'v': + case 'V': + { + char *version = pkg_version_str_alloc(pkg); + if (version == NULL) + return; + fprintf(fp, "Version: %s\n", version); + free(version); + } + break; + default: + goto UNKNOWN_FMT_FIELD; + } + + return; UNKNOWN_FMT_FIELD: - opkg_msg(ERROR, "Internal error: field=%s\n", field); + opkg_msg(ERROR, "Internal error: field=%s\n", field); } -void -pkg_formatted_info(FILE *fp, pkg_t *pkg) +void pkg_formatted_info(FILE * fp, pkg_t * pkg) { pkg_formatted_field(fp, pkg, "Package"); pkg_formatted_field(fp, pkg, "Version"); @@ -870,28 +867,27 @@ pkg_formatted_info(FILE *fp, pkg_t *pkg) fputs("\n", fp); } -void -pkg_print_status(pkg_t * pkg, FILE * file) +void pkg_print_status(pkg_t * pkg, FILE * file) { - if (pkg == NULL) { - return; - } - - pkg_formatted_field(file, pkg, "Package"); - pkg_formatted_field(file, pkg, "Version"); - pkg_formatted_field(file, pkg, "Depends"); - pkg_formatted_field(file, pkg, "Recommends"); - pkg_formatted_field(file, pkg, "Suggests"); - pkg_formatted_field(file, pkg, "Provides"); - pkg_formatted_field(file, pkg, "Replaces"); - pkg_formatted_field(file, pkg, "Conflicts"); - pkg_formatted_field(file, pkg, "Status"); - pkg_formatted_field(file, pkg, "Essential"); - pkg_formatted_field(file, pkg, "Architecture"); - pkg_formatted_field(file, pkg, "Conffiles"); - pkg_formatted_field(file, pkg, "Installed-Time"); - pkg_formatted_field(file, pkg, "Auto-Installed"); - fputs("\n", file); + if (pkg == NULL) { + return; + } + + pkg_formatted_field(file, pkg, "Package"); + pkg_formatted_field(file, pkg, "Version"); + pkg_formatted_field(file, pkg, "Depends"); + pkg_formatted_field(file, pkg, "Recommends"); + pkg_formatted_field(file, pkg, "Suggests"); + pkg_formatted_field(file, pkg, "Provides"); + pkg_formatted_field(file, pkg, "Replaces"); + pkg_formatted_field(file, pkg, "Conflicts"); + pkg_formatted_field(file, pkg, "Status"); + pkg_formatted_field(file, pkg, "Essential"); + pkg_formatted_field(file, pkg, "Architecture"); + pkg_formatted_field(file, pkg, "Conffiles"); + pkg_formatted_field(file, pkg, "Installed-Time"); + pkg_formatted_field(file, pkg, "Auto-Installed"); + fputs("\n", file); } /* @@ -908,152 +904,157 @@ pkg_print_status(pkg_t * pkg, FILE * file) : isalpha((x)) ? (x) \ : (x) + 256) -static int -verrevcmp(const char *val, const char *ref) { - if (!val) val= ""; - if (!ref) ref= ""; - - while (*val || *ref) { - int first_diff= 0; - - while ( (*val && !isdigit(*val)) || (*ref && !isdigit(*ref)) ) { - int vc= order(*val), rc= order(*ref); - if (vc != rc) return vc - rc; - val++; ref++; - } - - while ( *val == '0' ) val++; - while ( *ref == '0' ) ref++; - while (isdigit(*val) && isdigit(*ref)) { - if (!first_diff) first_diff= *val - *ref; - val++; ref++; - } - if (isdigit(*val)) return 1; - if (isdigit(*ref)) return -1; - if (first_diff) return first_diff; - } - return 0; +static int verrevcmp(const char *val, const char *ref) +{ + if (!val) + val = ""; + if (!ref) + ref = ""; + + while (*val || *ref) { + int first_diff = 0; + + while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) { + int vc = order(*val), rc = order(*ref); + if (vc != rc) + return vc - rc; + val++; + ref++; + } + + while (*val == '0') + val++; + while (*ref == '0') + ref++; + while (isdigit(*val) && isdigit(*ref)) { + if (!first_diff) + first_diff = *val - *ref; + val++; + ref++; + } + if (isdigit(*val)) + return 1; + if (isdigit(*ref)) + return -1; + if (first_diff) + return first_diff; + } + return 0; } -int -pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg) +int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg) { - int r; + int r; - if (pkg->epoch > ref_pkg->epoch) { - return 1; - } + if (pkg->epoch > ref_pkg->epoch) { + return 1; + } - if (pkg->epoch < ref_pkg->epoch) { - return -1; - } + if (pkg->epoch < ref_pkg->epoch) { + return -1; + } - r = verrevcmp(pkg->version, ref_pkg->version); - if (r) { - return r; - } + r = verrevcmp(pkg->version, ref_pkg->version); + if (r) { + return r; + } - r = verrevcmp(pkg->revision, ref_pkg->revision); - if (r) { - return r; - } + r = verrevcmp(pkg->revision, ref_pkg->revision); + if (r) { + return r; + } - return r; + return r; } - -int -pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op) +int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op) { - int r; + int r; - r = pkg_compare_versions(it, ref); + r = pkg_compare_versions(it, ref); - if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) { - return r <= 0; - } + if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) { + return r <= 0; + } - if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) { - return r >= 0; - } + if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) { + return r >= 0; + } - if (strcmp(op, "<<") == 0) { - return r < 0; - } + if (strcmp(op, "<<") == 0) { + return r < 0; + } - if (strcmp(op, ">>") == 0) { - return r > 0; - } + if (strcmp(op, ">>") == 0) { + return r > 0; + } - if (strcmp(op, "=") == 0) { - return r == 0; - } + if (strcmp(op, "=") == 0) { + return r == 0; + } - opkg_msg(ERROR, "Unknown operator: %s.\n", op); - return 0; + opkg_msg(ERROR, "Unknown operator: %s.\n", op); + return 0; } -int -pkg_name_version_and_architecture_compare(const void *p1, const void *p2) +int pkg_name_version_and_architecture_compare(const void *p1, const void *p2) { - const pkg_t *a = *(const pkg_t**) p1; - const pkg_t *b = *(const pkg_t**) p2; - int namecmp; - int vercmp; - if (!a->name || !b->name) { - opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n", - a->name, b->name); - return 0; - } - - namecmp = strcmp(a->name, b->name); - if (namecmp) - return namecmp; - vercmp = pkg_compare_versions(a, b); - if (vercmp) - return vercmp; - if (!a->arch_priority || !b->arch_priority) { - opkg_msg(ERROR, "Internal error: a->arch_priority=%i b->arch_priority=%i.\n", - a->arch_priority, b->arch_priority); - return 0; - } - if (a->arch_priority > b->arch_priority) - return 1; - if (a->arch_priority < b->arch_priority) - return -1; - return 0; + const pkg_t *a = *(const pkg_t **)p1; + const pkg_t *b = *(const pkg_t **)p2; + int namecmp; + int vercmp; + if (!a->name || !b->name) { + opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n", + a->name, b->name); + return 0; + } + + namecmp = strcmp(a->name, b->name); + if (namecmp) + return namecmp; + vercmp = pkg_compare_versions(a, b); + if (vercmp) + return vercmp; + if (!a->arch_priority || !b->arch_priority) { + opkg_msg(ERROR, + "Internal error: a->arch_priority=%i b->arch_priority=%i.\n", + a->arch_priority, b->arch_priority); + return 0; + } + if (a->arch_priority > b->arch_priority) + return 1; + if (a->arch_priority < b->arch_priority) + return -1; + return 0; } -int -abstract_pkg_name_compare(const void *p1, const void *p2) +int abstract_pkg_name_compare(const void *p1, const void *p2) { - const abstract_pkg_t *a = *(const abstract_pkg_t **)p1; - const abstract_pkg_t *b = *(const abstract_pkg_t **)p2; - if (!a->name || !b->name) { - opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n", - a->name, b->name); - return 0; - } - return strcmp(a->name, b->name); + const abstract_pkg_t *a = *(const abstract_pkg_t **)p1; + const abstract_pkg_t *b = *(const abstract_pkg_t **)p2; + if (!a->name || !b->name) { + opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n", + a->name, b->name); + return 0; + } + return strcmp(a->name, b->name); } - -char * -pkg_version_str_alloc(pkg_t *pkg) +char *pkg_version_str_alloc(pkg_t * pkg) { char *version; if (pkg->epoch) { if (pkg->revision) sprintf_alloc(&version, "%d:%s-%s", - pkg->epoch, pkg->version, pkg->revision); + pkg->epoch, pkg->version, pkg->revision); else sprintf_alloc(&version, "%d:%s", - pkg->epoch, pkg->version); + pkg->epoch, pkg->version); } else { if (pkg->revision) sprintf_alloc(&version, "%s-%s", - pkg->version, pkg->revision); + pkg->version, pkg->revision); else version = xstrdup(pkg->version); } @@ -1064,129 +1065,129 @@ pkg_version_str_alloc(pkg_t *pkg) /* * XXX: this should be broken into two functions */ -str_list_t * -pkg_get_installed_files(pkg_t *pkg) +str_list_t *pkg_get_installed_files(pkg_t * pkg) { - int err, fd; - char *list_file_name = NULL; - FILE *list_file = NULL; - char *line; - char *installed_file_name; - unsigned int rootdirlen = 0; - int list_from_package; - - pkg->installed_files_ref_cnt++; - - if (pkg->installed_files) { - return pkg->installed_files; - } - - pkg->installed_files = str_list_alloc(); - - /* - * For installed packages, look at the package.list file in the database. - * For uninstalled packages, get the file list directly from the package. - */ - if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) - list_from_package = 1; - else - list_from_package = 0; - - if (list_from_package) { - if (pkg->local_filename == NULL) { - return pkg->installed_files; - } - /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary - file. In other words, change deb_extract so that it can - simply return the file list as a char *[] rather than - insisting on writing it to a FILE * as it does now. */ - sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX", - conf->tmp_dir, pkg->name); - fd = mkstemp(list_file_name); - if (fd == -1) { - opkg_perror(ERROR, "Failed to make temp file %s.", - list_file_name); - free(list_file_name); - return pkg->installed_files; - } - list_file = fdopen(fd, "r+"); - if (list_file == NULL) { - opkg_perror(ERROR, "Failed to fdopen temp file %s.", - list_file_name); - close(fd); - unlink(list_file_name); - free(list_file_name); - return pkg->installed_files; - } - err = pkg_extract_data_file_names_to_stream(pkg, list_file); - if (err) { - opkg_msg(ERROR, "Error extracting file list from %s.\n", - pkg->local_filename); - fclose(list_file); - unlink(list_file_name); - free(list_file_name); - str_list_deinit(pkg->installed_files); - pkg->installed_files = NULL; - return NULL; - } - rewind(list_file); - } else { - sprintf_alloc(&list_file_name, "%s/%s.list", - pkg->dest->info_dir, pkg->name); - list_file = fopen(list_file_name, "r"); - if (list_file == NULL) { - opkg_perror(ERROR, "Failed to open %s", - list_file_name); - free(list_file_name); - return pkg->installed_files; - } - free(list_file_name); - } - - if (conf->offline_root) - rootdirlen = strlen(conf->offline_root); - - while (1) { - char *file_name; - - line = file_read_line_alloc(list_file); - if (line == NULL) { - break; - } - file_name = line; - - if (list_from_package) { - if (*file_name == '.') { - file_name++; - } - if (*file_name == '/') { - file_name++; - } - sprintf_alloc(&installed_file_name, "%s%s", - pkg->dest->root_dir, file_name); - } else { - if (conf->offline_root && - strncmp(conf->offline_root, file_name, rootdirlen)) { - sprintf_alloc(&installed_file_name, "%s%s", - conf->offline_root, file_name); - } else { - // already contains root_dir as header -> ABSOLUTE - sprintf_alloc(&installed_file_name, "%s", file_name); - } - } - str_list_append(pkg->installed_files, installed_file_name); - free(installed_file_name); - free(line); - } - - fclose(list_file); - - if (list_from_package) { - unlink(list_file_name); - free(list_file_name); - } - - return pkg->installed_files; + int err, fd; + char *list_file_name = NULL; + FILE *list_file = NULL; + char *line; + char *installed_file_name; + unsigned int rootdirlen = 0; + int list_from_package; + + pkg->installed_files_ref_cnt++; + + if (pkg->installed_files) { + return pkg->installed_files; + } + + pkg->installed_files = str_list_alloc(); + + /* + * For installed packages, look at the package.list file in the database. + * For uninstalled packages, get the file list directly from the package. + */ + if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) + list_from_package = 1; + else + list_from_package = 0; + + if (list_from_package) { + if (pkg->local_filename == NULL) { + return pkg->installed_files; + } + /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary + file. In other words, change deb_extract so that it can + simply return the file list as a char *[] rather than + insisting on writing it to a FILE * as it does now. */ + sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX", + conf->tmp_dir, pkg->name); + fd = mkstemp(list_file_name); + if (fd == -1) { + opkg_perror(ERROR, "Failed to make temp file %s.", + list_file_name); + free(list_file_name); + return pkg->installed_files; + } + list_file = fdopen(fd, "r+"); + if (list_file == NULL) { + opkg_perror(ERROR, "Failed to fdopen temp file %s.", + list_file_name); + close(fd); + unlink(list_file_name); + free(list_file_name); + return pkg->installed_files; + } + err = pkg_extract_data_file_names_to_stream(pkg, list_file); + if (err) { + opkg_msg(ERROR, "Error extracting file list from %s.\n", + pkg->local_filename); + fclose(list_file); + unlink(list_file_name); + free(list_file_name); + str_list_deinit(pkg->installed_files); + pkg->installed_files = NULL; + return NULL; + } + rewind(list_file); + } else { + sprintf_alloc(&list_file_name, "%s/%s.list", + pkg->dest->info_dir, pkg->name); + list_file = fopen(list_file_name, "r"); + if (list_file == NULL) { + opkg_perror(ERROR, "Failed to open %s", list_file_name); + free(list_file_name); + return pkg->installed_files; + } + free(list_file_name); + } + + if (conf->offline_root) + rootdirlen = strlen(conf->offline_root); + + while (1) { + char *file_name; + + line = file_read_line_alloc(list_file); + if (line == NULL) { + break; + } + file_name = line; + + if (list_from_package) { + if (*file_name == '.') { + file_name++; + } + if (*file_name == '/') { + file_name++; + } + sprintf_alloc(&installed_file_name, "%s%s", + pkg->dest->root_dir, file_name); + } else { + if (conf->offline_root && + strncmp(conf->offline_root, file_name, + rootdirlen)) { + sprintf_alloc(&installed_file_name, "%s%s", + conf->offline_root, file_name); + } else { + // already contains root_dir as header -> ABSOLUTE + sprintf_alloc(&installed_file_name, "%s", + file_name); + } + } + str_list_append(pkg->installed_files, installed_file_name); + free(installed_file_name); + free(line); + } + + fclose(list_file); + + if (list_from_package) { + unlink(list_file_name); + free(list_file_name); + } + + return pkg->installed_files; } /* XXX: CLEANUP: This function and it's counterpart, @@ -1194,28 +1195,26 @@ pkg_get_installed_files(pkg_t *pkg) convention. Nor the alloc/free convention. But, then again, neither of these conventions currrently fit the way these two functions work. */ -void -pkg_free_installed_files(pkg_t *pkg) +void pkg_free_installed_files(pkg_t * pkg) { - pkg->installed_files_ref_cnt--; + pkg->installed_files_ref_cnt--; - if (pkg->installed_files_ref_cnt > 0) - return; + if (pkg->installed_files_ref_cnt > 0) + return; - if (pkg->installed_files) { - str_list_purge(pkg->installed_files); - } + if (pkg->installed_files) { + str_list_purge(pkg->installed_files); + } - pkg->installed_files = NULL; + pkg->installed_files = NULL; } -void -pkg_remove_installed_files_list(pkg_t *pkg) +void pkg_remove_installed_files_list(pkg_t * pkg) { char *list_file_name; sprintf_alloc(&list_file_name, "%s/%s.list", - pkg->dest->info_dir, pkg->name); + pkg->dest->info_dir, pkg->name); if (!conf->noaction) (void)unlink(list_file_name); @@ -1223,178 +1222,179 @@ pkg_remove_installed_files_list(pkg_t *pkg) free(list_file_name); } -conffile_t * -pkg_get_conffile(pkg_t *pkg, const char *file_name) +conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name) { - conffile_list_elt_t *iter; - conffile_t *conffile; + conffile_list_elt_t *iter; + conffile_t *conffile; - if (pkg == NULL) { - return NULL; - } + if (pkg == NULL) { + return NULL; + } - for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) { - conffile = (conffile_t *)iter->data; + for (iter = nv_pair_list_first(&pkg->conffiles); iter; + iter = nv_pair_list_next(&pkg->conffiles, iter)) { + conffile = (conffile_t *) iter->data; - if (strcmp(conffile->name, file_name) == 0) { - return conffile; - } - } + if (strcmp(conffile->name, file_name) == 0) { + return conffile; + } + } - return NULL; + return NULL; } -int -pkg_run_script(pkg_t *pkg, const char *script, const char *args) +int pkg_run_script(pkg_t * pkg, const char *script, const char *args) { - int err; - char *path; - char *cmd; - - if (conf->noaction) - return 0; - - /* XXX: FEATURE: When conf->offline_root is set, we should run the - maintainer script within a chroot environment. */ - if (conf->offline_root && !conf->force_postinstall) { - opkg_msg(INFO, "Offline root mode: not running %s.%s.\n", - pkg->name, script); - return 0; - } - - /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages - have scripts in pkg->tmp_unpack_dir. */ - if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) { - if (pkg->dest == NULL) { - opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n", - pkg->name); - return -1; - } - sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, script); - } else { - if (pkg->tmp_unpack_dir == NULL) { - opkg_msg(ERROR, "Internal error: %s has a NULL tmp_unpack_dir.\n", - pkg->name); - return -1; - } - sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script); - } - - opkg_msg(INFO, "Running script %s.\n", path); - - setenv("PKG_ROOT", - pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, 1); + int err; + char *path; + char *cmd; + + if (conf->noaction) + return 0; + + /* XXX: FEATURE: When conf->offline_root is set, we should run the + maintainer script within a chroot environment. */ + if (conf->offline_root && !conf->force_postinstall) { + opkg_msg(INFO, "Offline root mode: not running %s.%s.\n", + pkg->name, script); + return 0; + } + + /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages + have scripts in pkg->tmp_unpack_dir. */ + if (pkg->state_status == SS_INSTALLED + || pkg->state_status == SS_UNPACKED) { + if (pkg->dest == NULL) { + opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n", + pkg->name); + return -1; + } + sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, + script); + } else { + if (pkg->tmp_unpack_dir == NULL) { + opkg_msg(ERROR, + "Internal error: %s has a NULL tmp_unpack_dir.\n", + pkg->name); + return -1; + } + sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script); + } + + opkg_msg(INFO, "Running script %s.\n", path); + + setenv("PKG_ROOT", + pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, + 1); if (pkg->is_upgrade) setenv("PKG_UPGRADE", "1", 1); else setenv("PKG_UPGRADE", "0", 1); + if (!file_exists(path)) { + free(path); + return 0; + } - if (! file_exists(path)) { - free(path); - return 0; - } - - sprintf_alloc(&cmd, "%s %s", path, args); - free(path); - { - const char *argv[] = {"sh", "-c", cmd, NULL}; - err = xsystem(argv); - } - free(cmd); + sprintf_alloc(&cmd, "%s %s", path, args); + free(path); + { + const char *argv[] = { "sh", "-c", cmd, NULL }; + err = xsystem(argv); + } + free(cmd); - if (err) { - opkg_msg(ERROR, "package \"%s\" %s script returned status %d.\n", - pkg->name, script, err); - return err; - } + if (err) { + opkg_msg(ERROR, + "package \"%s\" %s script returned status %d.\n", + pkg->name, script, err); + return err; + } - return 0; + return 0; } -int -pkg_arch_supported(pkg_t *pkg) +int pkg_arch_supported(pkg_t * pkg) { - nv_pair_list_elt_t *l; - - if (!pkg->architecture) - return 1; - - list_for_each_entry(l , &conf->arch_list.head, node) { - nv_pair_t *nv = (nv_pair_t *)l->data; - if (strcmp(nv->name, pkg->architecture) == 0) { - opkg_msg(DEBUG, "Arch %s (priority %s) supported for pkg %s.\n", - nv->name, nv->value, pkg->name); - return 1; - } - } - - opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n", - pkg->architecture, pkg->name); - return 0; + nv_pair_list_elt_t *l; + + if (!pkg->architecture) + return 1; + + list_for_each_entry(l, &conf->arch_list.head, node) { + nv_pair_t *nv = (nv_pair_t *) l->data; + if (strcmp(nv->name, pkg->architecture) == 0) { + opkg_msg(DEBUG, + "Arch %s (priority %s) supported for pkg %s.\n", + nv->name, nv->value, pkg->name); + return 1; + } + } + + opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n", + pkg->architecture, pkg->name); + return 0; } -void -pkg_info_preinstall_check(void) +void pkg_info_preinstall_check(void) { - int i; - pkg_vec_t *installed_pkgs = pkg_vec_alloc(); - - /* update the file owner data structure */ - opkg_msg(INFO, "Updating file owner list.\n"); - pkg_hash_fetch_all_installed(installed_pkgs); - for (i = 0; i < installed_pkgs->len; i++) { - pkg_t *pkg = installed_pkgs->pkgs[i]; - str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */ - str_list_elt_t *iter, *niter; - if (installed_files == NULL) { - opkg_msg(ERROR, "Failed to determine installed " - "files for pkg %s.\n", pkg->name); - break; - } - for (iter = str_list_first(installed_files), niter = str_list_next(installed_files, iter); - iter; - iter = niter, niter = str_list_next(installed_files, iter)) { - char *installed_file = (char *) iter->data; - file_hash_set_file_owner(installed_file, pkg); - } - pkg_free_installed_files(pkg); - } - pkg_vec_free(installed_pkgs); + int i; + pkg_vec_t *installed_pkgs = pkg_vec_alloc(); + + /* update the file owner data structure */ + opkg_msg(INFO, "Updating file owner list.\n"); + pkg_hash_fetch_all_installed(installed_pkgs); + for (i = 0; i < installed_pkgs->len; i++) { + pkg_t *pkg = installed_pkgs->pkgs[i]; + str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */ + str_list_elt_t *iter, *niter; + if (installed_files == NULL) { + opkg_msg(ERROR, "Failed to determine installed " + "files for pkg %s.\n", pkg->name); + break; + } + for (iter = str_list_first(installed_files), niter = + str_list_next(installed_files, iter); iter; + iter = niter, niter = + str_list_next(installed_files, iter)) { + char *installed_file = (char *)iter->data; + file_hash_set_file_owner(installed_file, pkg); + } + pkg_free_installed_files(pkg); + } + pkg_vec_free(installed_pkgs); } struct pkg_write_filelist_data { - pkg_t *pkg; - FILE *stream; + pkg_t *pkg; + FILE *stream; }; static void pkg_write_filelist_helper(const char *key, void *entry_, void *data_) { - struct pkg_write_filelist_data *data = data_; - pkg_t *entry = entry_; - if (entry == data->pkg) { - fprintf(data->stream, "%s\n", key); - } + struct pkg_write_filelist_data *data = data_; + pkg_t *entry = entry_; + if (entry == data->pkg) { + fprintf(data->stream, "%s\n", key); + } } -int -pkg_write_filelist(pkg_t *pkg) +int pkg_write_filelist(pkg_t * pkg) { struct pkg_write_filelist_data data; char *list_file_name; sprintf_alloc(&list_file_name, "%s/%s.list", - pkg->dest->info_dir, pkg->name); + pkg->dest->info_dir, pkg->name); opkg_msg(INFO, "Creating %s file for pkg %s.\n", - list_file_name, pkg->name); + list_file_name, pkg->name); data.stream = fopen(list_file_name, "w"); if (!data.stream) { - opkg_perror(ERROR, "Failed to open %s", - list_file_name); + opkg_perror(ERROR, "Failed to open %s", list_file_name); free(list_file_name); return -1; } @@ -1409,8 +1409,7 @@ pkg_write_filelist(pkg_t *pkg) return 0; } -int -pkg_write_changed_filelists(void) +int pkg_write_changed_filelists(void) { pkg_vec_t *installed_pkgs = pkg_vec_alloc(); int i, err, ret = 0; @@ -1430,7 +1429,7 @@ pkg_write_changed_filelists(void) } } - pkg_vec_free (installed_pkgs); + pkg_vec_free(installed_pkgs); return ret; } diff --git a/libopkg/pkg.h b/libopkg/pkg.h index be98c7b..ad4bba5 100644 --- a/libopkg/pkg.h +++ b/libopkg/pkg.h @@ -32,65 +32,61 @@ struct opkg_conf; - #define ARRAY_SIZE(array) sizeof(array) / sizeof((array)[0]) /* I think "Size" is currently the shortest field name */ #define PKG_MINIMUM_FIELD_NAME_LEN 4 -enum pkg_state_want -{ - SW_UNKNOWN = 1, - SW_INSTALL, - SW_DEINSTALL, - SW_PURGE, - SW_LAST_STATE_WANT +enum pkg_state_want { + SW_UNKNOWN = 1, + SW_INSTALL, + SW_DEINSTALL, + SW_PURGE, + SW_LAST_STATE_WANT }; typedef enum pkg_state_want pkg_state_want_t; -enum pkg_state_flag -{ - SF_OK = 0, - SF_REINSTREQ = 1, - SF_HOLD = 2, /* do not upgrade version */ - SF_REPLACE = 4, /* replace this package */ - SF_NOPRUNE = 8, /* do not remove obsolete files */ - SF_PREFER = 16, /* prefer this version */ - SF_OBSOLETE = 32, /* old package in upgrade pair */ - SF_MARKED = 64, /* temporary mark */ - SF_FILELIST_CHANGED = 128, /* needs filelist written */ - SF_USER = 256, - SF_LAST_STATE_FLAG +enum pkg_state_flag { + SF_OK = 0, + SF_REINSTREQ = 1, + SF_HOLD = 2, /* do not upgrade version */ + SF_REPLACE = 4, /* replace this package */ + SF_NOPRUNE = 8, /* do not remove obsolete files */ + SF_PREFER = 16, /* prefer this version */ + SF_OBSOLETE = 32, /* old package in upgrade pair */ + SF_MARKED = 64, /* temporary mark */ + SF_FILELIST_CHANGED = 128, /* needs filelist written */ + SF_USER = 256, + SF_LAST_STATE_FLAG }; typedef enum pkg_state_flag pkg_state_flag_t; #define SF_NONVOLATILE_FLAGS (SF_HOLD|SF_NOPRUNE|SF_PREFER|SF_OBSOLETE|SF_USER) -enum pkg_state_status -{ - SS_NOT_INSTALLED = 1, - SS_UNPACKED, - SS_HALF_CONFIGURED, - SS_INSTALLED, - SS_HALF_INSTALLED, - SS_CONFIG_FILES, - SS_POST_INST_FAILED, - SS_REMOVAL_FAILED, - SS_LAST_STATE_STATUS +enum pkg_state_status { + SS_NOT_INSTALLED = 1, + SS_UNPACKED, + SS_HALF_CONFIGURED, + SS_INSTALLED, + SS_HALF_INSTALLED, + SS_CONFIG_FILES, + SS_POST_INST_FAILED, + SS_REMOVAL_FAILED, + SS_LAST_STATE_STATUS }; typedef enum pkg_state_status pkg_state_status_t; -struct abstract_pkg{ - char * name; - int dependencies_checked; - pkg_vec_t * pkgs; - pkg_state_status_t state_status; - pkg_state_flag_t state_flag; +struct abstract_pkg { + char *name; + int dependencies_checked; + pkg_vec_t *pkgs; + pkg_state_status_t state_status; + pkg_state_flag_t state_flag; - /* XXX: This should be abstract_pkg_vec_t for consistency. */ - struct abstract_pkg ** depended_upon_by; + /* XXX: This should be abstract_pkg_vec_t for consistency. */ + struct abstract_pkg **depended_upon_by; - abstract_pkg_vec_t * provided_by; - abstract_pkg_vec_t * replaced_by; + abstract_pkg_vec_t *provided_by; + abstract_pkg_vec_t *replaced_by; }; #include "pkg_depends.h" @@ -115,118 +111,117 @@ struct abstract_pkg{ storage and use less memory. We might even do reference counting, but probably not since most often we only create new pkg_t structs, we don't often free them. */ -struct pkg -{ - char *name; - unsigned long epoch; - char *version; - char *revision; - pkg_src_t *src; - pkg_dest_t *dest; - char *architecture; - char *section; - char *maintainer; - char *description; - char *tags; - pkg_state_want_t state_want; - pkg_state_flag_t state_flag; - pkg_state_status_t state_status; - char **depends_str; - unsigned int depends_count; - char **pre_depends_str; - unsigned int pre_depends_count; - char **recommends_str; - unsigned int recommends_count; - char **suggests_str; - unsigned int suggests_count; - struct active_list list; /* Used for installing|upgrading */ - compound_depend_t * depends; - - char **conflicts_str; - compound_depend_t * conflicts; - unsigned int conflicts_count; - - char **replaces_str; - unsigned int replaces_count; - abstract_pkg_t ** replaces; - - char **provides_str; - unsigned int provides_count; - abstract_pkg_t ** provides; - - abstract_pkg_t *parent; - - char *filename; - char *local_filename; - char *tmp_unpack_dir; - char *md5sum; +struct pkg { + char *name; + unsigned long epoch; + char *version; + char *revision; + pkg_src_t *src; + pkg_dest_t *dest; + char *architecture; + char *section; + char *maintainer; + char *description; + char *tags; + pkg_state_want_t state_want; + pkg_state_flag_t state_flag; + pkg_state_status_t state_status; + char **depends_str; + unsigned int depends_count; + char **pre_depends_str; + unsigned int pre_depends_count; + char **recommends_str; + unsigned int recommends_count; + char **suggests_str; + unsigned int suggests_count; + struct active_list list; /* Used for installing|upgrading */ + compound_depend_t *depends; + + char **conflicts_str; + compound_depend_t *conflicts; + unsigned int conflicts_count; + + char **replaces_str; + unsigned int replaces_count; + abstract_pkg_t **replaces; + + char **provides_str; + unsigned int provides_count; + abstract_pkg_t **provides; + + abstract_pkg_t *parent; + + char *filename; + char *local_filename; + char *tmp_unpack_dir; + char *md5sum; #if defined HAVE_SHA256 - char *sha256sum; + char *sha256sum; #endif - unsigned long size; /* in bytes */ - unsigned long installed_size; /* in bytes */ - char *priority; - char *source; - conffile_list_t conffiles; - time_t installed_time; - /* As pointer for lazy evaluation */ - str_list_t *installed_files; - /* XXX: CLEANUP: I'd like to perhaps come up with a better - mechanism to avoid the problem here, (which is that the - installed_files list was being freed from an inner loop while - still being used within an outer loop. */ - int installed_files_ref_cnt; - int essential; - int arch_priority; + unsigned long size; /* in bytes */ + unsigned long installed_size; /* in bytes */ + char *priority; + char *source; + conffile_list_t conffiles; + time_t installed_time; + /* As pointer for lazy evaluation */ + str_list_t *installed_files; + /* XXX: CLEANUP: I'd like to perhaps come up with a better + mechanism to avoid the problem here, (which is that the + installed_files list was being freed from an inner loop while + still being used within an outer loop. */ + int installed_files_ref_cnt; + int essential; + int arch_priority; /* Adding this flag, to "force" opkg to choose a "provided_by_hand" package, if there are multiple choice */ - int provided_by_hand; + int provided_by_hand; - /* this flag specifies whether the package was installed to satisfy another - * package's dependancies */ - int auto_installed; - int is_upgrade; + /* this flag specifies whether the package was installed to satisfy another + * package's dependancies */ + int auto_installed; + int is_upgrade; }; pkg_t *pkg_new(void); -void pkg_deinit(pkg_t *pkg); -int pkg_init_from_file(pkg_t *pkg, const char *filename); +void pkg_deinit(pkg_t * pkg); +int pkg_init_from_file(pkg_t * pkg, const char *filename); abstract_pkg_t *abstract_pkg_new(void); /* * merges fields from newpkg into oldpkg. * Forcibly sets oldpkg state_status, state_want and state_flags */ -int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg); +int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg); -char *pkg_version_str_alloc(pkg_t *pkg); +char *pkg_version_str_alloc(pkg_t * pkg); -int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg); +int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg); int pkg_name_version_and_architecture_compare(const void *a, const void *b); int abstract_pkg_name_compare(const void *a, const void *b); -void pkg_formatted_info(FILE *fp, pkg_t *pkg); -void pkg_formatted_field(FILE *fp, pkg_t *pkg, const char *field); +void pkg_formatted_info(FILE * fp, pkg_t * pkg); +void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field); -void set_flags_from_control(pkg_t *pkg); +void set_flags_from_control(pkg_t * pkg); void pkg_print_status(pkg_t * pkg, FILE * file); -str_list_t *pkg_get_installed_files(pkg_t *pkg); -void pkg_free_installed_files(pkg_t *pkg); -void pkg_remove_installed_files_list(pkg_t *pkg); -conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name); -int pkg_run_script(pkg_t *pkg, const char *script, const char *args); +str_list_t *pkg_get_installed_files(pkg_t * pkg); +void pkg_free_installed_files(pkg_t * pkg); +void pkg_remove_installed_files_list(pkg_t * pkg); +conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name); +int pkg_run_script(pkg_t * pkg, const char *script, const char *args); /* enum mappings */ pkg_state_want_t pkg_state_want_from_str(char *str); pkg_state_flag_t pkg_state_flag_from_str(const char *str); pkg_state_status_t pkg_state_status_from_str(const char *str); -int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op); +int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op); -int pkg_arch_supported(pkg_t *pkg); +int pkg_arch_supported(pkg_t * pkg); void pkg_info_preinstall_check(void); -int pkg_write_filelist(pkg_t *pkg); +int pkg_write_filelist(pkg_t * pkg); int pkg_write_changed_filelists(void); #endif diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c index 1e14d1f..70c2922 100644 --- a/libopkg/pkg_depends.c +++ b/libopkg/pkg_depends.c @@ -26,237 +26,293 @@ #include "hash_table.h" #include "libbb/libbb.h" -static int parseDepends(compound_depend_t *compound_depend, char * depend_str); -static depend_t * depend_init(void); -static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx); -static char ** merge_unresolved(char ** oldstuff, char ** newstuff); +static int parseDepends(compound_depend_t * compound_depend, char *depend_str); +static depend_t *depend_init(void); +static char **add_unresolved_dep(pkg_t * pkg, char **the_lost, int ref_ndx); +static char **merge_unresolved(char **oldstuff, char **newstuff); static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg); -static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata) +static int pkg_installed_and_constraint_satisfied(pkg_t * pkg, void *cdata) { - depend_t *depend = (depend_t *)cdata; - if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg)) - return 1; - else - return 0; + depend_t *depend = (depend_t *) cdata; + if ((pkg->state_status == SS_INSTALLED + || pkg->state_status == SS_UNPACKED) + && version_constraints_satisfied(depend, pkg)) + return 1; + else + return 0; } -static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata) +static int pkg_constraint_satisfied(pkg_t * pkg, void *cdata) { - depend_t *depend = (depend_t *)cdata; - if (version_constraints_satisfied(depend, pkg)) - return 1; - else - return 0; + depend_t *depend = (depend_t *) cdata; + if (version_constraints_satisfied(depend, pkg)) + return 1; + else + return 0; } /* returns ndependencies or negative error value */ int -pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t *unsatisfied, - char *** unresolved) +pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t * unsatisfied, + char ***unresolved) { - pkg_t * satisfier_entry_pkg; - int i, j, k; - int count, found; - char ** the_lost; - abstract_pkg_t * ab_pkg; - - /* - * this is a setup to check for redundant/cyclic dependency checks, - * which are marked at the abstract_pkg level - */ - if (!(ab_pkg = pkg->parent)) { - opkg_msg(ERROR, "Internal error, with pkg %s.\n", pkg->name); - *unresolved = NULL; - return 0; - } - if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */ - *unresolved = NULL; - return 0; - } else { - ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */ - } - /**/ - - count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count; - if (!count){ - *unresolved = NULL; - return 0; - } - - the_lost = NULL; - - /* foreach dependency */ - for (i = 0; i < count; i++) { - compound_depend_t * compound_depend = &pkg->depends[i]; - depend_t ** possible_satisfiers = compound_depend->possibilities;; - found = 0; - satisfier_entry_pkg = NULL; - - if (compound_depend->type == GREEDY_DEPEND) { - /* foreach possible satisfier */ - for (j = 0; j < compound_depend->possibility_count; j++) { - /* foreach provided_by, which includes the abstract_pkg itself */ - abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg; - abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by; - int nposs = ab_provider_vec->len; - abstract_pkg_t **ab_providers = ab_provider_vec->pkgs; - int l; - for (l = 0; l < nposs; l++) { - pkg_vec_t *test_vec = ab_providers[l]->pkgs; - /* if no depends on this one, try the first package that Provides this one */ - if (!test_vec){ /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */ - continue; - } - - /* cruise this possiblity's pkg_vec looking for an installed version */ - for (k = 0; k < test_vec->len; k++) { - pkg_t *pkg_scout = test_vec->pkgs[k]; - /* not installed, and not already known about? */ - if ((pkg_scout->state_want != SW_INSTALL) - && !pkg_scout->parent->dependencies_checked - && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) { - char ** newstuff = NULL; - int rc; - pkg_vec_t *tmp_vec = pkg_vec_alloc (); - /* check for not-already-installed dependencies */ - rc = pkg_hash_fetch_unsatisfied_dependencies(pkg_scout, - tmp_vec, - &newstuff); - if (newstuff == NULL) { - int m; - int ok = 1; - for (m = 0; m < rc; m++) { - pkg_t *p = tmp_vec->pkgs[m]; - if (p->state_want == SW_INSTALL) + pkg_t *satisfier_entry_pkg; + int i, j, k; + int count, found; + char **the_lost; + abstract_pkg_t *ab_pkg; + + /* + * this is a setup to check for redundant/cyclic dependency checks, + * which are marked at the abstract_pkg level + */ + if (!(ab_pkg = pkg->parent)) { + opkg_msg(ERROR, "Internal error, with pkg %s.\n", pkg->name); + *unresolved = NULL; + return 0; + } + if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */ + *unresolved = NULL; + return 0; + } else { + ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */ + } + /**/ + count = + pkg->pre_depends_count + pkg->depends_count + + pkg->recommends_count + pkg->suggests_count; + if (!count) { + *unresolved = NULL; + return 0; + } + + the_lost = NULL; + + /* foreach dependency */ + for (i = 0; i < count; i++) { + compound_depend_t *compound_depend = &pkg->depends[i]; + depend_t **possible_satisfiers = + compound_depend->possibilities;; + found = 0; + satisfier_entry_pkg = NULL; + + if (compound_depend->type == GREEDY_DEPEND) { + /* foreach possible satisfier */ + for (j = 0; j < compound_depend->possibility_count; j++) { + /* foreach provided_by, which includes the abstract_pkg itself */ + abstract_pkg_t *abpkg = + possible_satisfiers[j]->pkg; + abstract_pkg_vec_t *ab_provider_vec = + abpkg->provided_by; + int nposs = ab_provider_vec->len; + abstract_pkg_t **ab_providers = + ab_provider_vec->pkgs; + int l; + for (l = 0; l < nposs; l++) { + pkg_vec_t *test_vec = + ab_providers[l]->pkgs; + /* if no depends on this one, try the first package that Provides this one */ + if (!test_vec) { /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */ continue; - opkg_msg(DEBUG, - "Not installing %s due" - " to requirement for %s.\n", - pkg_scout->name, - p->name); - ok = 0; - break; } - pkg_vec_free (tmp_vec); - if (ok) { - /* mark this one for installation */ - opkg_msg(NOTICE, - "Adding satisfier for greedy" - " dependence %s.\n", - pkg_scout->name); - pkg_vec_insert(unsatisfied, pkg_scout); + + /* cruise this possiblity's pkg_vec looking for an installed version */ + for (k = 0; k < test_vec->len; k++) { + pkg_t *pkg_scout = + test_vec->pkgs[k]; + /* not installed, and not already known about? */ + if ((pkg_scout->state_want != + SW_INSTALL) + && !pkg_scout->parent-> + dependencies_checked + && + !is_pkg_in_pkg_vec + (unsatisfied, pkg_scout)) { + char **newstuff = NULL; + int rc; + pkg_vec_t *tmp_vec = + pkg_vec_alloc(); + /* check for not-already-installed dependencies */ + rc = pkg_hash_fetch_unsatisfied_dependencies(pkg_scout, tmp_vec, &newstuff); + if (newstuff == NULL) { + int m; + int ok = 1; + for (m = 0; + m < rc; + m++) { + pkg_t *p + = + tmp_vec-> + pkgs + [m]; + if (p-> + state_want + == + SW_INSTALL) + continue; + opkg_msg + (DEBUG, + "Not installing %s due" + " to requirement for %s.\n", + pkg_scout-> + name, + p-> + name); + ok = 0; + break; + } + pkg_vec_free + (tmp_vec); + if (ok) { + /* mark this one for installation */ + opkg_msg + (NOTICE, + "Adding satisfier for greedy" + " dependence %s.\n", + pkg_scout-> + name); + pkg_vec_insert + (unsatisfied, + pkg_scout); + } + } else { + opkg_msg(DEBUG, + "Not installing %s due to " + "broken depends.\n", + pkg_scout-> + name); + free(newstuff); + } + } + } + } + } + + continue; + } + + /* foreach possible satisfier, look for installed package */ + for (j = 0; j < compound_depend->possibility_count; j++) { + /* foreach provided_by, which includes the abstract_pkg itself */ + depend_t *dependence_to_satisfy = + possible_satisfiers[j]; + abstract_pkg_t *satisfying_apkg = + possible_satisfiers[j]->pkg; + pkg_t *satisfying_pkg = + pkg_hash_fetch_best_installation_candidate + (satisfying_apkg, + pkg_installed_and_constraint_satisfied, + dependence_to_satisfy, 1); + /* Being that I can't test constraing in pkg_hash, I will test it here */ + if (satisfying_pkg != NULL) { + if (!pkg_installed_and_constraint_satisfied + (satisfying_pkg, dependence_to_satisfy)) { + satisfying_pkg = NULL; + } + } + opkg_msg(DEBUG, "satisfying_pkg=%p\n", satisfying_pkg); + if (satisfying_pkg != NULL) { + found = 1; + break; + } + + } + /* if nothing installed matches, then look for uninstalled satisfier */ + if (!found) { + /* foreach possible satisfier, look for installed package */ + for (j = 0; j < compound_depend->possibility_count; j++) { + /* foreach provided_by, which includes the abstract_pkg itself */ + depend_t *dependence_to_satisfy = + possible_satisfiers[j]; + abstract_pkg_t *satisfying_apkg = + possible_satisfiers[j]->pkg; + pkg_t *satisfying_pkg = + pkg_hash_fetch_best_installation_candidate + (satisfying_apkg, + pkg_constraint_satisfied, + dependence_to_satisfy, 1); + /* Being that I can't test constraing in pkg_hash, I will test it here too */ + if (satisfying_pkg != NULL) { + if (!pkg_constraint_satisfied + (satisfying_pkg, + dependence_to_satisfy)) { + satisfying_pkg = NULL; + } + } + + /* user request overrides package recommendation */ + if (satisfying_pkg != NULL + && (compound_depend->type == RECOMMEND + || compound_depend->type == SUGGEST) + && (satisfying_pkg->state_want == + SW_DEINSTALL + || satisfying_pkg->state_want == + SW_PURGE)) { + opkg_msg(NOTICE, + "%s: ignoring recommendation for " + "%s at user request\n", + pkg->name, + satisfying_pkg->name); + continue; + } + + opkg_msg(DEBUG, "satisfying_pkg=%p\n", + satisfying_pkg); + if (satisfying_pkg != NULL) { + satisfier_entry_pkg = satisfying_pkg; + break; + } + } + } + + /* we didn't find one, add something to the unsatisfied vector */ + if (!found) { + if (!satisfier_entry_pkg) { + /* failure to meet recommendations is not an error */ + if (compound_depend->type != RECOMMEND + && compound_depend->type != SUGGEST) + the_lost = + add_unresolved_dep(pkg, the_lost, + i); + else + opkg_msg(NOTICE, + "%s: unsatisfied recommendation for %s\n", + pkg->name, + compound_depend-> + possibilities[0]->pkg->name); + } else { + if (compound_depend->type == SUGGEST) { + /* just mention it politely */ + opkg_msg(NOTICE, + "package %s suggests installing %s\n", + pkg->name, + satisfier_entry_pkg->name); + } else { + char **newstuff = NULL; + + if (satisfier_entry_pkg != pkg && + !is_pkg_in_pkg_vec(unsatisfied, + satisfier_entry_pkg)) + { + pkg_vec_insert(unsatisfied, + satisfier_entry_pkg); + pkg_hash_fetch_unsatisfied_dependencies + (satisfier_entry_pkg, + unsatisfied, &newstuff); + the_lost = + merge_unresolved(the_lost, + newstuff); + if (newstuff) + free(newstuff); } - } else { - opkg_msg(DEBUG, - "Not installing %s due to " - "broken depends.\n", - pkg_scout->name); - free (newstuff); - } - } - } - } - } - - continue; - } - - /* foreach possible satisfier, look for installed package */ - for (j = 0; j < compound_depend->possibility_count; j++) { - /* foreach provided_by, which includes the abstract_pkg itself */ - depend_t *dependence_to_satisfy = possible_satisfiers[j]; - abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg; - pkg_t *satisfying_pkg = - pkg_hash_fetch_best_installation_candidate(satisfying_apkg, - pkg_installed_and_constraint_satisfied, - dependence_to_satisfy, 1); - /* Being that I can't test constraing in pkg_hash, I will test it here */ - if (satisfying_pkg != NULL) { - if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) { - satisfying_pkg = NULL; - } - } - opkg_msg(DEBUG, "satisfying_pkg=%p\n", satisfying_pkg); - if (satisfying_pkg != NULL) { - found = 1; - break; - } - - } - /* if nothing installed matches, then look for uninstalled satisfier */ - if (!found) { - /* foreach possible satisfier, look for installed package */ - for (j = 0; j < compound_depend->possibility_count; j++) { - /* foreach provided_by, which includes the abstract_pkg itself */ - depend_t *dependence_to_satisfy = possible_satisfiers[j]; - abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg; - pkg_t *satisfying_pkg = - pkg_hash_fetch_best_installation_candidate(satisfying_apkg, - pkg_constraint_satisfied, - dependence_to_satisfy, 1); - /* Being that I can't test constraing in pkg_hash, I will test it here too */ - if (satisfying_pkg != NULL) { - if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) { - satisfying_pkg = NULL; - } - } - - /* user request overrides package recommendation */ - if (satisfying_pkg != NULL - && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST) - && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) { - opkg_msg(NOTICE, "%s: ignoring recommendation for " - "%s at user request\n", - pkg->name, satisfying_pkg->name); - continue; - } - - opkg_msg(DEBUG, "satisfying_pkg=%p\n", satisfying_pkg); - if (satisfying_pkg != NULL) { - satisfier_entry_pkg = satisfying_pkg; - break; - } - } - } - - /* we didn't find one, add something to the unsatisfied vector */ - if (!found) { - if (!satisfier_entry_pkg) { - /* failure to meet recommendations is not an error */ - if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST) - the_lost = add_unresolved_dep(pkg, the_lost, i); - else - opkg_msg(NOTICE, - "%s: unsatisfied recommendation for %s\n", - pkg->name, - compound_depend->possibilities[0]->pkg->name); - } - else { - if (compound_depend->type == SUGGEST) { - /* just mention it politely */ - opkg_msg(NOTICE, "package %s suggests installing %s\n", - pkg->name, satisfier_entry_pkg->name); - } else { - char ** newstuff = NULL; - - if (satisfier_entry_pkg != pkg && - !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) { - pkg_vec_insert(unsatisfied, satisfier_entry_pkg); - pkg_hash_fetch_unsatisfied_dependencies(satisfier_entry_pkg, - unsatisfied, - &newstuff); - the_lost = merge_unresolved(the_lost, newstuff); - if (newstuff) - free(newstuff); - } - } - } - } - } - *unresolved = the_lost; - - return unsatisfied->len; + } + } + } + } + *unresolved = the_lost; + + return unsatisfied->len; } /*checking for conflicts !in replaces @@ -264,437 +320,452 @@ pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t *unsatisfied, really conflicts returns 0 if conflicts <> replaces or 1 if conflicts == replaces */ -static int -is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg) +static int is_pkg_a_replaces(pkg_t * pkg_scout, pkg_t * pkg) { - int i ; - int replaces_count = pkg->replaces_count; - abstract_pkg_t **replaces; + int i; + int replaces_count = pkg->replaces_count; + abstract_pkg_t **replaces; - if (pkg->replaces_count==0) // No replaces, it's surely a conflict - return 0; + if (pkg->replaces_count == 0) // No replaces, it's surely a conflict + return 0; - replaces = pkg->replaces; + replaces = pkg->replaces; - for (i = 0; i < replaces_count; i++) { - if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) { // Found - opkg_msg(DEBUG2, "Seems I've found a replace %s %s\n", - pkg_scout->name, pkg->replaces[i]->name); - return 1; - } - } - return 0; + for (i = 0; i < replaces_count; i++) { + if (strcmp(pkg_scout->name, pkg->replaces[i]->name) == 0) { // Found + opkg_msg(DEBUG2, "Seems I've found a replace %s %s\n", + pkg_scout->name, pkg->replaces[i]->name); + return 1; + } + } + return 0; } - -pkg_vec_t * pkg_hash_fetch_conflicts(pkg_t * pkg) +pkg_vec_t *pkg_hash_fetch_conflicts(pkg_t * pkg) { - pkg_vec_t * installed_conflicts, * test_vec; - compound_depend_t * conflicts; - depend_t ** possible_satisfiers; - depend_t * possible_satisfier; - int i, j, k; - int count; - abstract_pkg_t * ab_pkg; - pkg_t **pkg_scouts; - pkg_t *pkg_scout; - - /* - * this is a setup to check for redundant/cyclic dependency checks, - * which are marked at the abstract_pkg level - */ - if(!(ab_pkg = pkg->parent)){ - opkg_msg(ERROR, "Internal error: %s not in hash table\n", pkg->name); - return (pkg_vec_t *)NULL; - } - - conflicts = pkg->conflicts; - if(!conflicts){ - return (pkg_vec_t *)NULL; - } - installed_conflicts = pkg_vec_alloc(); - - count = pkg->conflicts_count; - - - - /* foreach conflict */ - for(i = 0; i < pkg->conflicts_count; i++){ - - possible_satisfiers = conflicts->possibilities; - - /* foreach possible satisfier */ - for(j = 0; j < conflicts->possibility_count; j++){ - possible_satisfier = possible_satisfiers[j]; - if (!possible_satisfier) - opkg_msg(ERROR, "Internal error: possible_satisfier=NULL\n"); - if (!possible_satisfier->pkg) - opkg_msg(ERROR, "Internal error: possible_satisfier->pkg=NULL\n"); - test_vec = possible_satisfier->pkg->pkgs; - if (test_vec) { - /* pkg_vec found, it is an actual package conflict - * cruise this possiblity's pkg_vec looking for an installed version */ - pkg_scouts = test_vec->pkgs; - for(k = 0; k < test_vec->len; k++){ - pkg_scout = pkg_scouts[k]; - if (!pkg_scout) { - opkg_msg(ERROR, "Internal error: pkg_scout=NULL\n"); - continue; - } - if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) && - version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){ - if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){ - pkg_vec_insert(installed_conflicts, pkg_scout); + pkg_vec_t *installed_conflicts, *test_vec; + compound_depend_t *conflicts; + depend_t **possible_satisfiers; + depend_t *possible_satisfier; + int i, j, k; + int count; + abstract_pkg_t *ab_pkg; + pkg_t **pkg_scouts; + pkg_t *pkg_scout; + + /* + * this is a setup to check for redundant/cyclic dependency checks, + * which are marked at the abstract_pkg level + */ + if (!(ab_pkg = pkg->parent)) { + opkg_msg(ERROR, "Internal error: %s not in hash table\n", + pkg->name); + return (pkg_vec_t *) NULL; + } + + conflicts = pkg->conflicts; + if (!conflicts) { + return (pkg_vec_t *) NULL; + } + installed_conflicts = pkg_vec_alloc(); + + count = pkg->conflicts_count; + + /* foreach conflict */ + for (i = 0; i < pkg->conflicts_count; i++) { + + possible_satisfiers = conflicts->possibilities; + + /* foreach possible satisfier */ + for (j = 0; j < conflicts->possibility_count; j++) { + possible_satisfier = possible_satisfiers[j]; + if (!possible_satisfier) + opkg_msg(ERROR, + "Internal error: possible_satisfier=NULL\n"); + if (!possible_satisfier->pkg) + opkg_msg(ERROR, + "Internal error: possible_satisfier->pkg=NULL\n"); + test_vec = possible_satisfier->pkg->pkgs; + if (test_vec) { + /* pkg_vec found, it is an actual package conflict + * cruise this possiblity's pkg_vec looking for an installed version */ + pkg_scouts = test_vec->pkgs; + for (k = 0; k < test_vec->len; k++) { + pkg_scout = pkg_scouts[k]; + if (!pkg_scout) { + opkg_msg(ERROR, + "Internal error: pkg_scout=NULL\n"); + continue; + } + if ((pkg_scout->state_status == + SS_INSTALLED + || pkg_scout->state_want == + SW_INSTALL) + && + version_constraints_satisfied + (possible_satisfier, pkg_scout) + && !is_pkg_a_replaces(pkg_scout, + pkg)) { + if (!is_pkg_in_pkg_vec + (installed_conflicts, + pkg_scout)) { + pkg_vec_insert + (installed_conflicts, + pkg_scout); + } + } + } } - } } - } + conflicts++; } - conflicts++; - } - if (installed_conflicts->len) - return installed_conflicts; - pkg_vec_free(installed_conflicts); - return (pkg_vec_t *)NULL; + if (installed_conflicts->len) + return installed_conflicts; + pkg_vec_free(installed_conflicts); + return (pkg_vec_t *) NULL; } int version_constraints_satisfied(depend_t * depends, pkg_t * pkg) { - pkg_t * temp; - int comparison; + pkg_t *temp; + int comparison; - if(depends->constraint == NONE) - return 1; + if (depends->constraint == NONE) + return 1; - temp = pkg_new(); + temp = pkg_new(); - parse_version(temp, depends->version); + parse_version(temp, depends->version); - comparison = pkg_compare_versions(pkg, temp); + comparison = pkg_compare_versions(pkg, temp); - free (temp->version); - free(temp); + free(temp->version); + free(temp); - if((depends->constraint == EARLIER) && - (comparison < 0)) - return 1; - else if((depends->constraint == LATER) && - (comparison > 0)) - return 1; - else if(comparison == 0) - return 1; - else if((depends->constraint == LATER_EQUAL) && - (comparison >= 0)) - return 1; - else if((depends->constraint == EARLIER_EQUAL) && - (comparison <= 0)) - return 1; + if ((depends->constraint == EARLIER) && (comparison < 0)) + return 1; + else if ((depends->constraint == LATER) && (comparison > 0)) + return 1; + else if (comparison == 0) + return 1; + else if ((depends->constraint == LATER_EQUAL) && (comparison >= 0)) + return 1; + else if ((depends->constraint == EARLIER_EQUAL) && (comparison <= 0)) + return 1; - return 0; + return 0; } -int pkg_dependence_satisfiable(depend_t *depend) +int pkg_dependence_satisfiable(depend_t * depend) { - abstract_pkg_t *apkg = depend->pkg; - abstract_pkg_vec_t *provider_apkgs = apkg->provided_by; - int n_providers = provider_apkgs->len; - abstract_pkg_t **apkgs = provider_apkgs->pkgs; - pkg_vec_t *pkg_vec; - int n_pkgs ; - int i; - int j; - - for (i = 0; i < n_providers; i++) { - abstract_pkg_t *papkg = apkgs[i]; - pkg_vec = papkg->pkgs; - if (pkg_vec) { - n_pkgs = pkg_vec->len; - for (j = 0; j < n_pkgs; j++) { - pkg_t *pkg = pkg_vec->pkgs[j]; - if (version_constraints_satisfied(depend, pkg)) { - return 1; - } - } - } - } - return 0; + abstract_pkg_t *apkg = depend->pkg; + abstract_pkg_vec_t *provider_apkgs = apkg->provided_by; + int n_providers = provider_apkgs->len; + abstract_pkg_t **apkgs = provider_apkgs->pkgs; + pkg_vec_t *pkg_vec; + int n_pkgs; + int i; + int j; + + for (i = 0; i < n_providers; i++) { + abstract_pkg_t *papkg = apkgs[i]; + pkg_vec = papkg->pkgs; + if (pkg_vec) { + n_pkgs = pkg_vec->len; + for (j = 0; j < n_pkgs; j++) { + pkg_t *pkg = pkg_vec->pkgs[j]; + if (version_constraints_satisfied(depend, pkg)) { + return 1; + } + } + } + } + return 0; } -int pkg_dependence_satisfied(depend_t *depend) +int pkg_dependence_satisfied(depend_t * depend) { - abstract_pkg_t *apkg = depend->pkg; - abstract_pkg_vec_t *provider_apkgs = apkg->provided_by; - int n_providers = provider_apkgs->len; - abstract_pkg_t **apkgs = provider_apkgs->pkgs; - int i; - int n_pkgs; - int j; - - for (i = 0; i < n_providers; i++) { - abstract_pkg_t *papkg = apkgs[i]; - pkg_vec_t *pkg_vec = papkg->pkgs; - if (pkg_vec) { - n_pkgs = pkg_vec->len; - for (j = 0; j < n_pkgs; j++) { - pkg_t *pkg = pkg_vec->pkgs[j]; - if (version_constraints_satisfied(depend, pkg)) { - if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) - return 1; - } - } - } - } - return 0; + abstract_pkg_t *apkg = depend->pkg; + abstract_pkg_vec_t *provider_apkgs = apkg->provided_by; + int n_providers = provider_apkgs->len; + abstract_pkg_t **apkgs = provider_apkgs->pkgs; + int i; + int n_pkgs; + int j; + + for (i = 0; i < n_providers; i++) { + abstract_pkg_t *papkg = apkgs[i]; + pkg_vec_t *pkg_vec = papkg->pkgs; + if (pkg_vec) { + n_pkgs = pkg_vec->len; + for (j = 0; j < n_pkgs; j++) { + pkg_t *pkg = pkg_vec->pkgs[j]; + if (version_constraints_satisfied(depend, pkg)) { + if (pkg->state_status == SS_INSTALLED + || pkg->state_status == SS_UNPACKED) + return 1; + } + } + } + } + return 0; } static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg) { - int i; - pkg_t ** pkgs = vec->pkgs; - - for(i = 0; i < vec->len; i++) - if((strcmp(pkg->name, (*(pkgs + i))->name) == 0) - && (pkg_compare_versions(pkg, *(pkgs + i)) == 0) - && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0)) - return 1; - return 0; + int i; + pkg_t **pkgs = vec->pkgs; + + for (i = 0; i < vec->len; i++) + if ((strcmp(pkg->name, (*(pkgs + i))->name) == 0) + && (pkg_compare_versions(pkg, *(pkgs + i)) == 0) + && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) + == 0)) + return 1; + return 0; } /** * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0 * otherwise. */ -int pkg_replaces(pkg_t *pkg, pkg_t *replacee) +int pkg_replaces(pkg_t * pkg, pkg_t * replacee) { - abstract_pkg_t **replaces = pkg->replaces; - int replaces_count = pkg->replaces_count; - int replacee_provides_count = replacee->provides_count; - int i, j; - for (i = 0; i < replaces_count; i++) { - abstract_pkg_t *abstract_replacee = replaces[i]; - for (j = 0; j < replacee_provides_count; j++) { - if (replacee->provides[j] == abstract_replacee) - return 1; - } - } - return 0; + abstract_pkg_t **replaces = pkg->replaces; + int replaces_count = pkg->replaces_count; + int replacee_provides_count = replacee->provides_count; + int i, j; + for (i = 0; i < replaces_count; i++) { + abstract_pkg_t *abstract_replacee = replaces[i]; + for (j = 0; j < replacee_provides_count; j++) { + if (replacee->provides[j] == abstract_replacee) + return 1; + } + } + return 0; } - /** * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0 * otherwise. */ -int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee) +int pkg_conflicts_abstract(pkg_t * pkg, abstract_pkg_t * conflictee) { - compound_depend_t *conflicts = pkg->conflicts; - int conflicts_count = pkg->conflicts_count; - int i, j; - for (i = 0; i < conflicts_count; i++) { - int possibility_count = conflicts[i].possibility_count; - struct depend **possibilities = conflicts[i].possibilities; - for (j = 0; j < possibility_count; j++) { - if (possibilities[j]->pkg == conflictee) { - return 1; - } - } - } - return 0; + compound_depend_t *conflicts = pkg->conflicts; + int conflicts_count = pkg->conflicts_count; + int i, j; + for (i = 0; i < conflicts_count; i++) { + int possibility_count = conflicts[i].possibility_count; + struct depend **possibilities = conflicts[i].possibilities; + for (j = 0; j < possibility_count; j++) { + if (possibilities[j]->pkg == conflictee) { + return 1; + } + } + } + return 0; } /** * pkg_conflicts returns 1 if pkg->conflicts contains one of * conflictee's provides and 0 otherwise. */ -int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee) +int pkg_conflicts(pkg_t * pkg, pkg_t * conflictee) { - compound_depend_t *conflicts = pkg->conflicts; - int conflicts_count = pkg->conflicts_count; - abstract_pkg_t **conflictee_provides = conflictee->provides; - int conflictee_provides_count = conflictee->provides_count; - int i, j, k; - int possibility_count; - struct depend **possibilities; - abstract_pkg_t *possibility ; - - for (i = 0; i < conflicts_count; i++) { - possibility_count = conflicts[i].possibility_count; - possibilities = conflicts[i].possibilities; - for (j = 0; j < possibility_count; j++) { - possibility = possibilities[j]->pkg; - for (k = 0; k < conflictee_provides_count; k++) { - if (possibility == conflictee_provides[k]) { - return 1; - } - } - } - } - return 0; + compound_depend_t *conflicts = pkg->conflicts; + int conflicts_count = pkg->conflicts_count; + abstract_pkg_t **conflictee_provides = conflictee->provides; + int conflictee_provides_count = conflictee->provides_count; + int i, j, k; + int possibility_count; + struct depend **possibilities; + abstract_pkg_t *possibility; + + for (i = 0; i < conflicts_count; i++) { + possibility_count = conflicts[i].possibility_count; + possibilities = conflicts[i].possibilities; + for (j = 0; j < possibility_count; j++) { + possibility = possibilities[j]->pkg; + for (k = 0; k < conflictee_provides_count; k++) { + if (possibility == conflictee_provides[k]) { + return 1; + } + } + } + } + return 0; } -static char ** merge_unresolved(char ** oldstuff, char ** newstuff) +static char **merge_unresolved(char **oldstuff, char **newstuff) { - int oldlen = 0, newlen = 0; - char ** result; - int i, j; + int oldlen = 0, newlen = 0; + char **result; + int i, j; - if(!newstuff) - return oldstuff; + if (!newstuff) + return oldstuff; - while(oldstuff && oldstuff[oldlen]) oldlen++; - while(newstuff && newstuff[newlen]) newlen++; + while (oldstuff && oldstuff[oldlen]) + oldlen++; + while (newstuff && newstuff[newlen]) + newlen++; - result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1)); + result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1)); - for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++) - *(result + i) = *(newstuff + j); + for (i = oldlen, j = 0; i < (oldlen + newlen); i++, j++) + *(result + i) = *(newstuff + j); - *(result + i) = NULL; + *(result + i) = NULL; - return result; + return result; } /* * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre) * this is null terminated, no count is carried around */ -char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx) +char **add_unresolved_dep(pkg_t * pkg, char **the_lost, int ref_ndx) { - int count; - char ** resized; + int count; + char **resized; - count = 0; - while(the_lost && the_lost[count]) count++; + count = 0; + while (the_lost && the_lost[count]) + count++; - count++; /* need one to hold the null */ - resized = xrealloc(the_lost, sizeof(char *) * (count + 1)); - resized[count - 1] = pkg_depend_str(pkg, ref_ndx); - resized[count] = NULL; + count++; /* need one to hold the null */ + resized = xrealloc(the_lost, sizeof(char *) * (count + 1)); + resized[count - 1] = pkg_depend_str(pkg, ref_ndx); + resized[count] = NULL; - return resized; + return resized; } void buildProvides(abstract_pkg_t * ab_pkg, pkg_t * pkg) { - int i; + int i; - /* every pkg provides itself */ - pkg->provides_count++; - abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg); - pkg->provides = xcalloc(pkg->provides_count, sizeof(abstract_pkg_t *)); - pkg->provides[0] = ab_pkg; + /* every pkg provides itself */ + pkg->provides_count++; + abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg); + pkg->provides = xcalloc(pkg->provides_count, sizeof(abstract_pkg_t *)); + pkg->provides[0] = ab_pkg; - for (i=1; iprovides_count; i++) { - abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name( - pkg->provides_str[i-1]); - free(pkg->provides_str[i-1]); + for (i = 1; i < pkg->provides_count; i++) { + abstract_pkg_t *provided_abpkg = + ensure_abstract_pkg_by_name(pkg->provides_str[i - 1]); + free(pkg->provides_str[i - 1]); - pkg->provides[i] = provided_abpkg; + pkg->provides[i] = provided_abpkg; - abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg); - } - if (pkg->provides_str) - free(pkg->provides_str); + abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg); + } + if (pkg->provides_str) + free(pkg->provides_str); } void buildConflicts(pkg_t * pkg) { - int i; - compound_depend_t * conflicts; - - if (!pkg->conflicts_count) - return; - - conflicts = pkg->conflicts = xcalloc(pkg->conflicts_count, sizeof(compound_depend_t)); - for (i = 0; i < pkg->conflicts_count; i++) { - conflicts->type = CONFLICTS; - parseDepends(conflicts, pkg->conflicts_str[i]); - free(pkg->conflicts_str[i]); - conflicts++; - } - if (pkg->conflicts_str) - free(pkg->conflicts_str); + int i; + compound_depend_t *conflicts; + + if (!pkg->conflicts_count) + return; + + conflicts = pkg->conflicts = + xcalloc(pkg->conflicts_count, sizeof(compound_depend_t)); + for (i = 0; i < pkg->conflicts_count; i++) { + conflicts->type = CONFLICTS; + parseDepends(conflicts, pkg->conflicts_str[i]); + free(pkg->conflicts_str[i]); + conflicts++; + } + if (pkg->conflicts_str) + free(pkg->conflicts_str); } void buildReplaces(abstract_pkg_t * ab_pkg, pkg_t * pkg) { - int i; + int i; - if (!pkg->replaces_count) - return; + if (!pkg->replaces_count) + return; - pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); + pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); - for(i = 0; i < pkg->replaces_count; i++){ - abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(pkg->replaces_str[i]); + for (i = 0; i < pkg->replaces_count; i++) { + abstract_pkg_t *old_abpkg = + ensure_abstract_pkg_by_name(pkg->replaces_str[i]); - pkg->replaces[i] = old_abpkg; - free(pkg->replaces_str[i]); + pkg->replaces[i] = old_abpkg; + free(pkg->replaces_str[i]); - if (!old_abpkg->replaced_by) - old_abpkg->replaced_by = abstract_pkg_vec_alloc(); - /* if a package pkg both replaces and conflicts old_abpkg, - * then add it to the replaced_by vector so that old_abpkg - * will be upgraded to ab_pkg automatically */ - if (pkg_conflicts_abstract(pkg, old_abpkg)) - abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg); - } + if (!old_abpkg->replaced_by) + old_abpkg->replaced_by = abstract_pkg_vec_alloc(); + /* if a package pkg both replaces and conflicts old_abpkg, + * then add it to the replaced_by vector so that old_abpkg + * will be upgraded to ab_pkg automatically */ + if (pkg_conflicts_abstract(pkg, old_abpkg)) + abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg); + } - if (pkg->replaces_str) - free(pkg->replaces_str); + if (pkg->replaces_str) + free(pkg->replaces_str); } void buildDepends(pkg_t * pkg) { - unsigned int count; - int i; - compound_depend_t * depends; - - if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count)) - return; - - depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t)); - - for(i = 0; i < pkg->pre_depends_count; i++){ - parseDepends(depends, pkg->pre_depends_str[i]); - free(pkg->pre_depends_str[i]); - depends->type = PREDEPEND; - depends++; - } - if (pkg->pre_depends_str) - free(pkg->pre_depends_str); - - for(i = 0; i < pkg->depends_count; i++){ - parseDepends(depends, pkg->depends_str[i]); - free(pkg->depends_str[i]); - depends++; - } - if (pkg->depends_str) - free(pkg->depends_str); - - for(i = 0; i < pkg->recommends_count; i++){ - parseDepends(depends, pkg->recommends_str[i]); - free(pkg->recommends_str[i]); - depends->type = RECOMMEND; - depends++; - } - if(pkg->recommends_str) - free(pkg->recommends_str); - - for(i = 0; i < pkg->suggests_count; i++){ - parseDepends(depends, pkg->suggests_str[i]); - free(pkg->suggests_str[i]); - depends->type = SUGGEST; - depends++; - } - if(pkg->suggests_str) - free(pkg->suggests_str); + unsigned int count; + int i; + compound_depend_t *depends; + + if (! + (count = + pkg->pre_depends_count + pkg->depends_count + + pkg->recommends_count + pkg->suggests_count)) + return; + + depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t)); + + for (i = 0; i < pkg->pre_depends_count; i++) { + parseDepends(depends, pkg->pre_depends_str[i]); + free(pkg->pre_depends_str[i]); + depends->type = PREDEPEND; + depends++; + } + if (pkg->pre_depends_str) + free(pkg->pre_depends_str); + + for (i = 0; i < pkg->depends_count; i++) { + parseDepends(depends, pkg->depends_str[i]); + free(pkg->depends_str[i]); + depends++; + } + if (pkg->depends_str) + free(pkg->depends_str); + + for (i = 0; i < pkg->recommends_count; i++) { + parseDepends(depends, pkg->recommends_str[i]); + free(pkg->recommends_str[i]); + depends->type = RECOMMEND; + depends++; + } + if (pkg->recommends_str) + free(pkg->recommends_str); + + for (i = 0; i < pkg->suggests_count; i++) { + parseDepends(depends, pkg->suggests_str[i]); + free(pkg->suggests_str[i]); + depends->type = SUGGEST; + depends++; + } + if (pkg->suggests_str) + free(pkg->suggests_str); } -const char* -constraint_to_str(enum version_constraint c) +const char *constraint_to_str(enum version_constraint c) { switch (c) { case NONE: @@ -702,13 +773,13 @@ constraint_to_str(enum version_constraint c) case EARLIER: return "< "; case EARLIER_EQUAL: - return "<= "; + return "<= "; case EQUAL: - return "= "; + return "= "; case LATER_EQUAL: - return ">= "; + return ">= "; case LATER: - return "> "; + return "> "; } return ""; @@ -718,8 +789,7 @@ constraint_to_str(enum version_constraint c) * Returns a printable string for pkg's dependency at the specified idx. The * resultant string must be passed to free() by the caller. */ -char * -pkg_depend_str(pkg_t *pkg, int idx) +char *pkg_depend_str(pkg_t * pkg, int idx) { int i; unsigned int len; @@ -731,26 +801,26 @@ pkg_depend_str(pkg_t *pkg, int idx) cdep = &pkg->depends[idx]; /* calculate string length */ - for (i=0; ipossibility_count; i++) { + for (i = 0; i < cdep->possibility_count; i++) { dep = cdep->possibilities[i]; if (i != 0) - len += 3; /* space, pipe, space */ + len += 3; /* space, pipe, space */ len += strlen(dep->pkg->name); if (dep->version) { - len += 2; /* space, left parenthesis */ - len += 3; /* constraint string (<=, >=, etc), space */ + len += 2; /* space, left parenthesis */ + len += 3; /* constraint string (<=, >=, etc), space */ len += strlen(dep->version); - len += 1; /* right parenthesis */ + len += 1; /* right parenthesis */ } } - str = xmalloc(len + 1); /* +1 for the NULL terminator */ + str = xmalloc(len + 1); /* +1 for the NULL terminator */ str[0] = '\0'; - for (i=0; ipossibility_count; i++) { + for (i = 0; i < cdep->possibility_count; i++) { dep = cdep->possibilities[i]; if (i != 0) @@ -771,41 +841,39 @@ pkg_depend_str(pkg_t *pkg, int idx) void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) { - compound_depend_t * depends; + compound_depend_t *depends; int count, othercount; int i, j; - abstract_pkg_t * ab_depend; - abstract_pkg_t ** temp; + abstract_pkg_t *ab_depend; + abstract_pkg_t **temp; count = pkg->pre_depends_count + - pkg->depends_count + - pkg->recommends_count + - pkg->suggests_count; + pkg->depends_count + pkg->recommends_count + pkg->suggests_count; for (i = 0; i < count; i++) { depends = &pkg->depends[i]; if (depends->type != PREDEPEND - && depends->type != DEPEND - && depends->type != RECOMMEND) + && depends->type != DEPEND && depends->type != RECOMMEND) continue; for (j = 0; j < depends->possibility_count; j++) { ab_depend = depends->possibilities[j]->pkg; if (!ab_depend->depended_upon_by) { ab_depend->depended_upon_by = - xcalloc(1, sizeof(abstract_pkg_t *)); + xcalloc(1, sizeof(abstract_pkg_t *)); } temp = ab_depend->depended_upon_by; othercount = 1; while (*temp) { - temp++; - othercount++; + temp++; + othercount++; } *temp = ab_pkg; ab_depend->depended_upon_by = - xrealloc(ab_depend->depended_upon_by, - (othercount + 1) * sizeof(abstract_pkg_t *)); + xrealloc(ab_depend->depended_upon_by, + (othercount + + 1) * sizeof(abstract_pkg_t *)); /* the array may have been moved by realloc */ temp = ab_depend->depended_upon_by + othercount; @@ -814,118 +882,109 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) } } -static depend_t * depend_init(void) +static depend_t *depend_init(void) { - depend_t * d = xcalloc(1, sizeof(depend_t)); - d->constraint = NONE; - d->version = NULL; - d->pkg = NULL; + depend_t *d = xcalloc(1, sizeof(depend_t)); + d->constraint = NONE; + d->version = NULL; + d->pkg = NULL; - return d; + return d; } -static int parseDepends(compound_depend_t *compound_depend, - char * depend_str) +static int parseDepends(compound_depend_t * compound_depend, char *depend_str) { - char * pkg_name, buffer[2048]; - unsigned int num_of_ors = 0; - int i; - char * src, * dest; - depend_t ** possibilities; - - /* first count the number of ored possibilities for satisfying dependency */ - src = depend_str; - while(*src) - if(*src++ == '|') - num_of_ors++; - - compound_depend->type = DEPEND; - - compound_depend->possibility_count = num_of_ors + 1; - possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *) ); - compound_depend->possibilities = possibilities; - - src = depend_str; - for(i = 0; i < num_of_ors + 1; i++){ - possibilities[i] = depend_init(); - /* gobble up just the name first */ - dest = buffer; - while(*src && - !isspace(*src) && - (*src != '(') && - (*src != '*') && - (*src != '|')) - *dest++ = *src++; - *dest = '\0'; - pkg_name = trim_xstrdup(buffer); - - /* now look at possible version info */ - - /* skip to next chars */ - if(isspace(*src)) - while(*src && isspace(*src)) src++; - - /* extract constraint and version */ - if(*src == '('){ - src++; - if(!strncmp(src, "<<", 2)){ - possibilities[i]->constraint = EARLIER; - src += 2; - } - else if(!strncmp(src, "<=", 2)){ - possibilities[i]->constraint = EARLIER_EQUAL; - src += 2; - } - else if(!strncmp(src, ">=", 2)){ - possibilities[i]->constraint = LATER_EQUAL; - src += 2; - } - else if(!strncmp(src, ">>", 2)){ - possibilities[i]->constraint = LATER; - src += 2; - } - else if(!strncmp(src, "=", 1)){ - possibilities[i]->constraint = EQUAL; - src++; - } - /* should these be here to support deprecated designations; dpkg does */ - else if(!strncmp(src, "<", 1)){ - possibilities[i]->constraint = EARLIER_EQUAL; - src++; - } - else if(!strncmp(src, ">", 1)){ - possibilities[i]->constraint = LATER_EQUAL; - src++; - } - - /* now we have any constraint, pass space to version string */ - while(isspace(*src)) src++; - - /* this would be the version string */ - dest = buffer; - while(*src && *src != ')') - *dest++ = *src++; - *dest = '\0'; - - possibilities[i]->version = trim_xstrdup(buffer); - } - /* hook up the dependency to its abstract pkg */ - possibilities[i]->pkg = ensure_abstract_pkg_by_name(pkg_name); - - free(pkg_name); - - /* now get past the ) and any possible | chars */ - while(*src && - (isspace(*src) || - (*src == ')') || - (*src == '|'))) - src++; - if (*src == '*') - { - compound_depend->type = GREEDY_DEPEND; - src++; - } - } - - return 0; + char *pkg_name, buffer[2048]; + unsigned int num_of_ors = 0; + int i; + char *src, *dest; + depend_t **possibilities; + + /* first count the number of ored possibilities for satisfying dependency */ + src = depend_str; + while (*src) + if (*src++ == '|') + num_of_ors++; + + compound_depend->type = DEPEND; + + compound_depend->possibility_count = num_of_ors + 1; + possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *)); + compound_depend->possibilities = possibilities; + + src = depend_str; + for (i = 0; i < num_of_ors + 1; i++) { + possibilities[i] = depend_init(); + /* gobble up just the name first */ + dest = buffer; + while (*src && + !isspace(*src) && + (*src != '(') && (*src != '*') && (*src != '|')) + *dest++ = *src++; + *dest = '\0'; + pkg_name = trim_xstrdup(buffer); + + /* now look at possible version info */ + + /* skip to next chars */ + if (isspace(*src)) + while (*src && isspace(*src)) + src++; + + /* extract constraint and version */ + if (*src == '(') { + src++; + if (!strncmp(src, "<<", 2)) { + possibilities[i]->constraint = EARLIER; + src += 2; + } else if (!strncmp(src, "<=", 2)) { + possibilities[i]->constraint = EARLIER_EQUAL; + src += 2; + } else if (!strncmp(src, ">=", 2)) { + possibilities[i]->constraint = LATER_EQUAL; + src += 2; + } else if (!strncmp(src, ">>", 2)) { + possibilities[i]->constraint = LATER; + src += 2; + } else if (!strncmp(src, "=", 1)) { + possibilities[i]->constraint = EQUAL; + src++; + } + /* should these be here to support deprecated designations; dpkg does */ + else if (!strncmp(src, "<", 1)) { + possibilities[i]->constraint = EARLIER_EQUAL; + src++; + } else if (!strncmp(src, ">", 1)) { + possibilities[i]->constraint = LATER_EQUAL; + src++; + } + + /* now we have any constraint, pass space to version string */ + while (isspace(*src)) + src++; + + /* this would be the version string */ + dest = buffer; + while (*src && *src != ')') + *dest++ = *src++; + *dest = '\0'; + + possibilities[i]->version = trim_xstrdup(buffer); + } + /* hook up the dependency to its abstract pkg */ + possibilities[i]->pkg = ensure_abstract_pkg_by_name(pkg_name); + + free(pkg_name); + + /* now get past the ) and any possible | chars */ + while (*src && + (isspace(*src) || (*src == ')') || (*src == '|'))) + src++; + if (*src == '*') { + compound_depend->type = GREEDY_DEPEND; + src++; + } + } + + return 0; } diff --git a/libopkg/pkg_depends.h b/libopkg/pkg_depends.h index 5d1f074..c1f7aa8 100644 --- a/libopkg/pkg_depends.h +++ b/libopkg/pkg_depends.h @@ -22,36 +22,36 @@ #include "pkg_hash.h" enum depend_type { - PREDEPEND, - DEPEND, - CONFLICTS, - GREEDY_DEPEND, - RECOMMEND, - SUGGEST + PREDEPEND, + DEPEND, + CONFLICTS, + GREEDY_DEPEND, + RECOMMEND, + SUGGEST }; typedef enum depend_type depend_type_t; enum version_constraint { - NONE, - EARLIER, - EARLIER_EQUAL, - EQUAL, - LATER_EQUAL, - LATER + NONE, + EARLIER, + EARLIER_EQUAL, + EQUAL, + LATER_EQUAL, + LATER }; typedef enum version_constraint version_constraint_t; -struct depend{ - version_constraint_t constraint; - char * version; - abstract_pkg_t * pkg; +struct depend { + version_constraint_t constraint; + char *version; + abstract_pkg_t *pkg; }; typedef struct depend depend_t; -struct compound_depend{ - depend_type_t type; - int possibility_count; - struct depend ** possibilities; +struct compound_depend { + depend_type_t type; + int possibility_count; + struct depend **possibilities; }; typedef struct compound_depend compound_depend_t; @@ -64,27 +64,28 @@ void buildDepends(pkg_t * pkg); * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0 * otherwise. */ -int pkg_replaces(pkg_t *pkg, pkg_t *replacee); +int pkg_replaces(pkg_t * pkg, pkg_t * replacee); /** * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee provides and 0 * otherwise. */ -int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflicts); +int pkg_conflicts_abstract(pkg_t * pkg, abstract_pkg_t * conflicts); /** * pkg_conflicts returns 1 if pkg->conflicts contains one of conflictee's provides and 0 * otherwise. */ -int pkg_conflicts(pkg_t *pkg, pkg_t *conflicts); +int pkg_conflicts(pkg_t * pkg, pkg_t * conflicts); -char *pkg_depend_str(pkg_t *pkg, int index); +char *pkg_depend_str(pkg_t * pkg, int index); void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg); int version_constraints_satisfied(depend_t * depends, pkg_t * pkg); -int pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t *depends, char *** unresolved); -pkg_vec_t * pkg_hash_fetch_conflicts(pkg_t * pkg); -int pkg_dependence_satisfiable(depend_t *depend); -int pkg_dependence_satisfied(depend_t *depend); -const char* constraint_to_str(enum version_constraint c); +int pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t * depends, + char ***unresolved); +pkg_vec_t *pkg_hash_fetch_conflicts(pkg_t * pkg); +int pkg_dependence_satisfiable(depend_t * depend); +int pkg_dependence_satisfied(depend_t * depend); +const char *constraint_to_str(enum version_constraint c); #endif diff --git a/libopkg/pkg_dest.c b/libopkg/pkg_dest.c index d56dd78..341d0a8 100644 --- a/libopkg/pkg_dest.c +++ b/libopkg/pkg_dest.c @@ -25,58 +25,59 @@ #include "opkg_defines.h" #include "libbb/libbb.h" -int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir) +int pkg_dest_init(pkg_dest_t * dest, const char *name, const char *root_dir, + const char *lists_dir) { - dest->name = xstrdup(name); + dest->name = xstrdup(name); - /* Guarantee that dest->root_dir ends with a '/' */ - if (root_dir[strlen(root_dir) -1] == '/') { - dest->root_dir = xstrdup(root_dir); - } else { - sprintf_alloc(&dest->root_dir, "%s/", root_dir); - } - file_mkdir_hier(dest->root_dir, 0755); + /* Guarantee that dest->root_dir ends with a '/' */ + if (root_dir[strlen(root_dir) - 1] == '/') { + dest->root_dir = xstrdup(root_dir); + } else { + sprintf_alloc(&dest->root_dir, "%s/", root_dir); + } + file_mkdir_hier(dest->root_dir, 0755); - sprintf_alloc(&dest->opkg_dir, "%s%s", - dest->root_dir, OPKG_STATE_DIR_PREFIX); - file_mkdir_hier(dest->opkg_dir, 0755); + sprintf_alloc(&dest->opkg_dir, "%s%s", + dest->root_dir, OPKG_STATE_DIR_PREFIX); + file_mkdir_hier(dest->opkg_dir, 0755); - if (lists_dir[0] == '/') - sprintf_alloc(&dest->lists_dir, "%s", lists_dir); - else - sprintf_alloc(&dest->lists_dir, "/%s", lists_dir); + if (lists_dir[0] == '/') + sprintf_alloc(&dest->lists_dir, "%s", lists_dir); + else + sprintf_alloc(&dest->lists_dir, "/%s", lists_dir); - file_mkdir_hier(dest->lists_dir, 0755); + file_mkdir_hier(dest->lists_dir, 0755); - sprintf_alloc(&dest->info_dir, "%s/%s", - dest->opkg_dir, OPKG_INFO_DIR_SUFFIX); - file_mkdir_hier(dest->info_dir, 0755); + sprintf_alloc(&dest->info_dir, "%s/%s", + dest->opkg_dir, OPKG_INFO_DIR_SUFFIX); + file_mkdir_hier(dest->info_dir, 0755); - sprintf_alloc(&dest->status_file_name, "%s/%s", - dest->opkg_dir, OPKG_STATUS_FILE_SUFFIX); + sprintf_alloc(&dest->status_file_name, "%s/%s", + dest->opkg_dir, OPKG_STATUS_FILE_SUFFIX); - return 0; + return 0; } -void pkg_dest_deinit(pkg_dest_t *dest) +void pkg_dest_deinit(pkg_dest_t * dest) { - free(dest->name); - dest->name = NULL; + free(dest->name); + dest->name = NULL; - free(dest->root_dir); - dest->root_dir = NULL; + free(dest->root_dir); + dest->root_dir = NULL; - free(dest->opkg_dir); - dest->opkg_dir = NULL; + free(dest->opkg_dir); + dest->opkg_dir = NULL; - free(dest->lists_dir); - dest->lists_dir = NULL; + free(dest->lists_dir); + dest->lists_dir = NULL; - free(dest->info_dir); - dest->info_dir = NULL; + free(dest->info_dir); + dest->info_dir = NULL; - free(dest->status_file_name); - dest->status_file_name = NULL; + free(dest->status_file_name); + dest->status_file_name = NULL; - dest->root_dir = NULL; + dest->root_dir = NULL; } diff --git a/libopkg/pkg_dest.h b/libopkg/pkg_dest.h index 4ad417e..613e69f 100644 --- a/libopkg/pkg_dest.h +++ b/libopkg/pkg_dest.h @@ -21,19 +21,18 @@ #include typedef struct pkg_dest pkg_dest_t; -struct pkg_dest -{ - char *name; - char *root_dir; - char *opkg_dir; - char *lists_dir; - char *info_dir; - char *status_file_name; - FILE *status_fp; +struct pkg_dest { + char *name; + char *root_dir; + char *opkg_dir; + char *lists_dir; + char *info_dir; + char *status_file_name; + FILE *status_fp; }; -int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char *lists_dir); -void pkg_dest_deinit(pkg_dest_t *dest); +int pkg_dest_init(pkg_dest_t * dest, const char *name, const char *root_dir, + const char *lists_dir); +void pkg_dest_deinit(pkg_dest_t * dest); #endif - diff --git a/libopkg/pkg_dest_list.c b/libopkg/pkg_dest_list.c index f5f2e1d..ebc80cc 100644 --- a/libopkg/pkg_dest_list.c +++ b/libopkg/pkg_dest_list.c @@ -22,56 +22,56 @@ #include "pkg_dest_list.h" #include "libbb/libbb.h" -void pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data) +void pkg_dest_list_elt_init(pkg_dest_list_elt_t * elt, pkg_dest_t * data) { - void_list_elt_init((void_list_elt_t *) elt, data); + void_list_elt_init((void_list_elt_t *) elt, data); } -void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt) +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t * elt) { - void_list_elt_deinit((void_list_elt_t *) elt); + void_list_elt_deinit((void_list_elt_t *) elt); } -void pkg_dest_list_init(pkg_dest_list_t *list) +void pkg_dest_list_init(pkg_dest_list_t * list) { - void_list_init((void_list_t *) list); + void_list_init((void_list_t *) list); } -void pkg_dest_list_deinit(pkg_dest_list_t *list) +void pkg_dest_list_deinit(pkg_dest_list_t * list) { - pkg_dest_list_elt_t *iter, *n; - pkg_dest_t *pkg_dest; - - list_for_each_entry_safe(iter, n, &list->head, node) { - pkg_dest = (pkg_dest_t *)iter->data; - pkg_dest_deinit(pkg_dest); - - /* malloced in pkg_dest_list_append */ - free(pkg_dest); - iter->data = NULL; - } - void_list_deinit((void_list_t *) list); + pkg_dest_list_elt_t *iter, *n; + pkg_dest_t *pkg_dest; + + list_for_each_entry_safe(iter, n, &list->head, node) { + pkg_dest = (pkg_dest_t *) iter->data; + pkg_dest_deinit(pkg_dest); + + /* malloced in pkg_dest_list_append */ + free(pkg_dest); + iter->data = NULL; + } + void_list_deinit((void_list_t *) list); } -pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name, - const char *root_dir,const char *lists_dir) +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t * list, const char *name, + const char *root_dir, const char *lists_dir) { - pkg_dest_t *pkg_dest; + pkg_dest_t *pkg_dest; - /* freed in pkg_dest_list_deinit */ - pkg_dest = xcalloc(1, sizeof(pkg_dest_t)); - pkg_dest_init(pkg_dest, name, root_dir,lists_dir); - void_list_append((void_list_t *) list, pkg_dest); + /* freed in pkg_dest_list_deinit */ + pkg_dest = xcalloc(1, sizeof(pkg_dest_t)); + pkg_dest_init(pkg_dest, name, root_dir, lists_dir); + void_list_append((void_list_t *) list, pkg_dest); - return pkg_dest; + return pkg_dest; } -void pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data) +void pkg_dest_list_push(pkg_dest_list_t * list, pkg_dest_t * data) { - void_list_push((void_list_t *) list, data); + void_list_push((void_list_t *) list, data); } -pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list) +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t * list) { - return (pkg_dest_list_elt_t *) void_list_pop((void_list_t *) list); + return (pkg_dest_list_elt_t *) void_list_pop((void_list_t *) list); } diff --git a/libopkg/pkg_dest_list.h b/libopkg/pkg_dest_list.h index 33aef19..2863f1d 100644 --- a/libopkg/pkg_dest_list.h +++ b/libopkg/pkg_dest_list.h @@ -24,16 +24,15 @@ typedef struct void_list_elt pkg_dest_list_elt_t; typedef struct void_list pkg_dest_list_t; -void pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data); -void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt); +void pkg_dest_list_elt_init(pkg_dest_list_elt_t * elt, pkg_dest_t * data); +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t * elt); -void pkg_dest_list_init(pkg_dest_list_t *list); -void pkg_dest_list_deinit(pkg_dest_list_t *list); +void pkg_dest_list_init(pkg_dest_list_t * list); +void pkg_dest_list_deinit(pkg_dest_list_t * list); -pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name, - const char *root_dir,const char* lists_dir); -void pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data); -pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list); +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t * list, const char *name, + const char *root_dir, const char *lists_dir); +void pkg_dest_list_push(pkg_dest_list_t * list, pkg_dest_t * data); +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t * list); #endif - diff --git a/libopkg/pkg_extract.c b/libopkg/pkg_extract.c index 0f21e40..8a541e3 100644 --- a/libopkg/pkg_extract.c +++ b/libopkg/pkg_extract.c @@ -22,20 +22,18 @@ #include "file_util.h" #include "sprintf_alloc.h" -int -pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream) +int pkg_extract_control_file_to_stream(pkg_t * pkg, FILE * stream) { int err; deb_extract(pkg->local_filename, stream, - extract_control_tar_gz - | extract_to_stream, - NULL, "control", &err); + extract_control_tar_gz + | extract_to_stream, NULL, "control", &err); return err; } int -pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir, - const char *prefix) +pkg_extract_control_files_to_dir_with_prefix(pkg_t * pkg, const char *dir, + const char *prefix) { int err; char *dir_with_prefix; @@ -43,57 +41,51 @@ pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir, sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix); deb_extract(pkg->local_filename, stderr, - extract_control_tar_gz - | extract_all_to_fs| extract_preserve_date - | extract_unconditional, - dir_with_prefix, NULL, &err); + extract_control_tar_gz + | extract_all_to_fs | extract_preserve_date + | extract_unconditional, dir_with_prefix, NULL, &err); free(dir_with_prefix); return err; } -int -pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir) +int pkg_extract_control_files_to_dir(pkg_t * pkg, const char *dir) { return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, ""); } - -int -pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir) +int pkg_extract_data_files_to_dir(pkg_t * pkg, const char *dir) { int err; deb_extract(pkg->local_filename, stderr, - extract_data_tar_gz - | extract_all_to_fs| extract_preserve_date - | extract_unconditional, - dir, NULL, &err); + extract_data_tar_gz + | extract_all_to_fs | extract_preserve_date + | extract_unconditional, dir, NULL, &err); return err; } -int -pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *stream) +int pkg_extract_data_file_names_to_stream(pkg_t * pkg, FILE * stream) { int err; - /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the - data file names with a '.' as the first character. I've taught - opkg how to cope with the presence or absence of the '.', but - this may trip up dpkg. + /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the + data file names with a '.' as the first character. I've taught + opkg how to cope with the presence or absence of the '.', but + this may trip up dpkg. - For all I know, this could actually be a bug in opkg-build. So, - I'll have to try installing some .debs and comparing the *.list - files. + For all I know, this could actually be a bug in opkg-build. So, + I'll have to try installing some .debs and comparing the *.list + files. - If we wanted to, we could workaround the deb_extract behavior - right here, by writing to a tmpfile, then munging things as we - wrote to the actual stream. */ + If we wanted to, we could workaround the deb_extract behavior + right here, by writing to a tmpfile, then munging things as we + wrote to the actual stream. */ deb_extract(pkg->local_filename, stream, - extract_quiet | extract_data_tar_gz | extract_list, - NULL, NULL, &err); + extract_quiet | extract_data_tar_gz | extract_list, + NULL, NULL, &err); return err; } diff --git a/libopkg/pkg_extract.h b/libopkg/pkg_extract.h index b83b41b..2a25a8b 100644 --- a/libopkg/pkg_extract.h +++ b/libopkg/pkg_extract.h @@ -20,12 +20,12 @@ #include "pkg.h" -int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream); -int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir); -int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, +int pkg_extract_control_file_to_stream(pkg_t * pkg, FILE * stream); +int pkg_extract_control_files_to_dir(pkg_t * pkg, const char *dir); +int pkg_extract_control_files_to_dir_with_prefix(pkg_t * pkg, const char *dir, const char *prefix); -int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir); -int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file); +int pkg_extract_data_files_to_dir(pkg_t * pkg, const char *dir); +int pkg_extract_data_file_names_to_stream(pkg_t * pkg, FILE * file); #endif diff --git a/libopkg/pkg_hash.c b/libopkg/pkg_hash.c index c6d7158..edc5188 100644 --- a/libopkg/pkg_hash.c +++ b/libopkg/pkg_hash.c @@ -30,15 +30,13 @@ #include "libbb/libbb.h" #include "libbb/gzip.h" -void -pkg_hash_init(void) +void pkg_hash_init(void) { hash_table_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN); } -static void -free_pkgs(const char *key, void *entry, void *data) +static void free_pkgs(const char *key, void *entry, void *data) { int i; abstract_pkg_t *ab_pkg; @@ -47,38 +45,36 @@ free_pkgs(const char *key, void *entry, void *data) * a list of packages that provide the abstract package. */ - ab_pkg = (abstract_pkg_t*) entry; + ab_pkg = (abstract_pkg_t *) entry; if (ab_pkg->pkgs) { for (i = 0; i < ab_pkg->pkgs->len; i++) { - pkg_deinit (ab_pkg->pkgs->pkgs[i]); - free (ab_pkg->pkgs->pkgs[i]); + pkg_deinit(ab_pkg->pkgs->pkgs[i]); + free(ab_pkg->pkgs->pkgs[i]); } } - abstract_pkg_vec_free (ab_pkg->provided_by); - abstract_pkg_vec_free (ab_pkg->replaced_by); - pkg_vec_free (ab_pkg->pkgs); - free (ab_pkg->depended_upon_by); - free (ab_pkg->name); - free (ab_pkg); + abstract_pkg_vec_free(ab_pkg->provided_by); + abstract_pkg_vec_free(ab_pkg->replaced_by); + pkg_vec_free(ab_pkg->pkgs); + free(ab_pkg->depended_upon_by); + free(ab_pkg->name); + free(ab_pkg); } -void -pkg_hash_deinit(void) +void pkg_hash_deinit(void) { hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL); hash_table_deinit(&conf->pkg_hash); } -int -dist_hash_add_from_file(const char *lists_dir, pkg_src_t *dist) +int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist) { nv_pair_list_elt_t *l; char *list_file, *subname; - list_for_each_entry(l , &conf->arch_list.head, node) { - nv_pair_t *nv = (nv_pair_t *)l->data; + list_for_each_entry(l, &conf->arch_list.head, node) { + nv_pair_t *nv = (nv_pair_t *) l->data; sprintf_alloc(&subname, "%s-%s", dist->name, nv->name); sprintf_alloc(&list_file, "%s/%s", lists_dir, subname); @@ -87,7 +83,8 @@ dist_hash_add_from_file(const char *lists_dir, pkg_src_t *dist) free(list_file); return -1; } - pkg_src_list_append (&conf->pkg_src_list, subname, dist->value, "__dummy__", 0); + pkg_src_list_append(&conf->pkg_src_list, subname, + dist->value, "__dummy__", 0); } free(list_file); @@ -96,10 +93,9 @@ dist_hash_add_from_file(const char *lists_dir, pkg_src_t *dist) return 0; } - int pkg_hash_add_from_file(const char *file_name, - pkg_src_t *src, pkg_dest_t *dest, int is_status_file) + pkg_src_t * src, pkg_dest_t * dest, int is_status_file) { pkg_t *pkg; FILE *fp; @@ -110,10 +106,9 @@ pkg_hash_add_from_file(const char *file_name, if (src && src->gzip) { fp = gzip_fdopen(&zh, file_name); - } - else { + } else { fp = fopen(file_name, "r"); - } + } if (fp == NULL) { opkg_perror(ERROR, "Failed to open %s", file_name); @@ -128,7 +123,7 @@ pkg_hash_add_from_file(const char *file_name, pkg->dest = dest; ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0, - &buf, len); + &buf, len); if (pkg->name == NULL) { /* probably just a blank line */ @@ -136,7 +131,7 @@ pkg_hash_add_from_file(const char *file_name, } if (ret) { - pkg_deinit (pkg); + pkg_deinit(pkg); free(pkg); if (ret == -1) break; @@ -149,8 +144,8 @@ pkg_hash_add_from_file(const char *file_name, if (!pkg->architecture || !pkg->arch_priority) { char *version_str = pkg_version_str_alloc(pkg); opkg_msg(NOTICE, "Package %s version %s has no " - "valid architecture, ignoring.\n", - pkg->name, version_str); + "valid architecture, ignoring.\n", + pkg->name, version_str); free(version_str); continue; } @@ -171,8 +166,7 @@ pkg_hash_add_from_file(const char *file_name, /* * Load in feed files from the cached "src" and/or "src/gz" locations. */ -int -pkg_hash_load_feeds(void) +int pkg_hash_load_feeds(void) { pkg_src_list_elt_t *iter; pkg_src_t *src, *subdist; @@ -181,12 +175,12 @@ pkg_hash_load_feeds(void) opkg_msg(INFO, "\n"); lists_dir = conf->restrict_to_default_dest ? - conf->default_dest->lists_dir : conf->lists_dir; + conf->default_dest->lists_dir : conf->lists_dir; for (iter = void_list_first(&conf->pkg_src_list); iter; - iter = void_list_next(&conf->pkg_src_list, iter)) { + iter = void_list_next(&conf->pkg_src_list, iter)) { - src = (pkg_src_t *)iter->data; + src = (pkg_src_t *) iter->data; sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name); @@ -205,8 +199,7 @@ pkg_hash_load_feeds(void) /* * Load in status files from the configured "dest"s. */ -int -pkg_hash_load_status_files(void) +int pkg_hash_load_status_files(void) { pkg_dest_list_elt_t *iter; pkg_dest_t *dest; @@ -214,12 +207,13 @@ pkg_hash_load_status_files(void) opkg_msg(INFO, "\n"); for (iter = void_list_first(&conf->pkg_dest_list); iter; - iter = void_list_next(&conf->pkg_dest_list, iter)) { + iter = void_list_next(&conf->pkg_dest_list, iter)) { - dest = (pkg_dest_t *)iter->data; + dest = (pkg_dest_t *) iter->data; if (file_exists(dest->status_file_name)) { - if (pkg_hash_add_from_file(dest->status_file_name, NULL, dest, 1)) + if (pkg_hash_add_from_file + (dest->status_file_name, NULL, dest, 1)) return -1; } } @@ -227,221 +221,235 @@ pkg_hash_load_status_files(void) return 0; } -static abstract_pkg_t * -abstract_pkg_fetch_by_name(const char * pkg_name) +static abstract_pkg_t *abstract_pkg_fetch_by_name(const char *pkg_name) { - return (abstract_pkg_t *)hash_table_get(&conf->pkg_hash, pkg_name); + return (abstract_pkg_t *) hash_table_get(&conf->pkg_hash, pkg_name); } -pkg_t * -pkg_hash_fetch_best_installation_candidate(abstract_pkg_t *apkg, - int (*constraint_fcn)(pkg_t *pkg, void *cdata), - void *cdata, int quiet) +pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg, + int (*constraint_fcn) (pkg_t * + pkg, + void + *cdata), + void *cdata, int quiet) { - int i, j; - int nprovides = 0; - int nmatching = 0; - int wrong_arch_found = 0; - pkg_vec_t *matching_pkgs; - abstract_pkg_vec_t *matching_apkgs; - abstract_pkg_vec_t *provided_apkg_vec; - abstract_pkg_t **provided_apkgs; - abstract_pkg_vec_t *providers; - pkg_t *latest_installed_parent = NULL; - pkg_t *latest_matching = NULL; - pkg_t *priorized_matching = NULL; - pkg_t *held_pkg = NULL; - pkg_t *good_pkg_by_name = NULL; - - if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0)) - return NULL; - - matching_pkgs = pkg_vec_alloc(); - matching_apkgs = abstract_pkg_vec_alloc(); - providers = abstract_pkg_vec_alloc(); - - opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name); - - provided_apkg_vec = apkg->provided_by; - nprovides = provided_apkg_vec->len; - provided_apkgs = provided_apkg_vec->pkgs; - if (nprovides > 1) - opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name, nprovides); - - /* accumulate all the providers */ - for (i = 0; i < nprovides; i++) { - abstract_pkg_t *provider_apkg = provided_apkgs[i]; - opkg_msg(DEBUG, "Adding %s to providers.\n", provider_apkg->name); - abstract_pkg_vec_insert(providers, provider_apkg); - } - nprovides = providers->len; - - for (i = 0; i < nprovides; i++) { - abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i); - abstract_pkg_t *replacement_apkg = NULL; - pkg_vec_t *vec; - - if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) { - replacement_apkg = provider_apkg->replaced_by->pkgs[0]; - if (provider_apkg->replaced_by->len > 1) { - opkg_msg(NOTICE, "Multiple replacers for %s, " - "using first one (%s).\n", - provider_apkg->name, replacement_apkg->name); - } - } - - if (replacement_apkg) - opkg_msg(DEBUG, "replacement_apkg=%s for provider_apkg=%s.\n", - replacement_apkg->name, provider_apkg->name); - - if (replacement_apkg && (replacement_apkg != provider_apkg)) { - if (abstract_pkg_vec_contains(providers, replacement_apkg)) - continue; - else - provider_apkg = replacement_apkg; - } - - if (!(vec = provider_apkg->pkgs)) { - opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n", - provider_apkg->name); - continue; - } - - - /* now check for supported architecture */ - { - int max_count = 0; - - /* count packages matching max arch priority and keep track of last one */ - for (j=0; jlen; j++) { - pkg_t *maybe = vec->pkgs[j]; - opkg_msg(DEBUG, "%s arch=%s arch_priority=%d version=%s.\n", - maybe->name, maybe->architecture, - maybe->arch_priority, maybe->version); - /* We make sure not to add the same package twice. Need to search for the reason why - they show up twice sometimes. */ - if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) { - max_count++; - abstract_pkg_vec_insert(matching_apkgs, maybe->parent); - pkg_vec_insert(matching_pkgs, maybe); - } - } - - if (vec->len > 0 && matching_pkgs->len < 1) - wrong_arch_found = 1; - } - } - - if (matching_pkgs->len < 1) { - if (wrong_arch_found) - opkg_msg(ERROR, "Packages for %s found, but" - " incompatible with the architectures configured\n", - apkg->name); - pkg_vec_free(matching_pkgs); - abstract_pkg_vec_free(matching_apkgs); - abstract_pkg_vec_free(providers); - return NULL; - } - - - if (matching_pkgs->len > 1) - pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare); - if (matching_apkgs->len > 1) - abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare); - - for (i = 0; i < matching_pkgs->len; i++) { - pkg_t *matching = matching_pkgs->pkgs[i]; - if (constraint_fcn(matching, cdata)) { - opkg_msg(DEBUG, "Candidate: %s %s.\n", - matching->name, matching->version) ; - good_pkg_by_name = matching; - /* It has been provided by hand, so it is what user want */ - if (matching->provided_by_hand == 1) - break; - } - } - - - for (i = 0; i < matching_pkgs->len; i++) { - pkg_t *matching = matching_pkgs->pkgs[i]; - latest_matching = matching; - if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED) - latest_installed_parent = matching; - if (matching->state_flag & (SF_HOLD|SF_PREFER)) { - if (held_pkg) - opkg_msg(NOTICE, "Multiple packages (%s and %s) providing" - " same name marked HOLD or PREFER. " - "Using latest.\n", - held_pkg->name, matching->name); - held_pkg = matching; - } - } - - if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) { - int prio = 0; - for (i = 0; i < matching_pkgs->len; i++) { - pkg_t *matching = matching_pkgs->pkgs[i]; - if (matching->arch_priority > prio) { - priorized_matching = matching; - prio = matching->arch_priority; - opkg_msg(DEBUG, "Match %s with priority %i.\n", - matching->name, prio); - } - } - - } - - if (conf->verbosity >= INFO && matching_apkgs->len > 1) { - opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n", - matching_pkgs->len, apkg->name); - for (i = 0; i < matching_pkgs->len; i++) { - pkg_t *matching = matching_pkgs->pkgs[i]; - opkg_msg(INFO, "%s %s %s\n", - matching->name, matching->version, - matching->architecture); - } - } - - nmatching = matching_apkgs->len; - - pkg_vec_free(matching_pkgs); - abstract_pkg_vec_free(matching_apkgs); - abstract_pkg_vec_free(providers); - - if (good_pkg_by_name) { /* We found a good candidate, we will install it */ - return good_pkg_by_name; - } - if (held_pkg) { - opkg_msg(INFO, "Using held package %s.\n", held_pkg->name); - return held_pkg; - } - if (latest_installed_parent) { - opkg_msg(INFO, "Using latest version of installed package %s.\n", - latest_installed_parent->name); - return latest_installed_parent; - } - if (priorized_matching) { - opkg_msg(INFO, "Using priorized matching %s %s %s.\n", - priorized_matching->name, priorized_matching->version, - priorized_matching->architecture); - return priorized_matching; - } - if (nmatching > 1) { - opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n", - nmatching); - return NULL; - } - if (latest_matching) { - opkg_msg(INFO, "Using latest matching %s %s %s.\n", - latest_matching->name, latest_matching->version, - latest_matching->architecture); - return latest_matching; - } - return NULL; + int i, j; + int nprovides = 0; + int nmatching = 0; + int wrong_arch_found = 0; + pkg_vec_t *matching_pkgs; + abstract_pkg_vec_t *matching_apkgs; + abstract_pkg_vec_t *provided_apkg_vec; + abstract_pkg_t **provided_apkgs; + abstract_pkg_vec_t *providers; + pkg_t *latest_installed_parent = NULL; + pkg_t *latest_matching = NULL; + pkg_t *priorized_matching = NULL; + pkg_t *held_pkg = NULL; + pkg_t *good_pkg_by_name = NULL; + + if (apkg == NULL || apkg->provided_by == NULL + || (apkg->provided_by->len == 0)) + return NULL; + + matching_pkgs = pkg_vec_alloc(); + matching_apkgs = abstract_pkg_vec_alloc(); + providers = abstract_pkg_vec_alloc(); + + opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name); + + provided_apkg_vec = apkg->provided_by; + nprovides = provided_apkg_vec->len; + provided_apkgs = provided_apkg_vec->pkgs; + if (nprovides > 1) + opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name, + nprovides); + + /* accumulate all the providers */ + for (i = 0; i < nprovides; i++) { + abstract_pkg_t *provider_apkg = provided_apkgs[i]; + opkg_msg(DEBUG, "Adding %s to providers.\n", + provider_apkg->name); + abstract_pkg_vec_insert(providers, provider_apkg); + } + nprovides = providers->len; + + for (i = 0; i < nprovides; i++) { + abstract_pkg_t *provider_apkg = + abstract_pkg_vec_get(providers, i); + abstract_pkg_t *replacement_apkg = NULL; + pkg_vec_t *vec; + + if (provider_apkg->replaced_by + && provider_apkg->replaced_by->len) { + replacement_apkg = provider_apkg->replaced_by->pkgs[0]; + if (provider_apkg->replaced_by->len > 1) { + opkg_msg(NOTICE, "Multiple replacers for %s, " + "using first one (%s).\n", + provider_apkg->name, + replacement_apkg->name); + } + } + + if (replacement_apkg) + opkg_msg(DEBUG, + "replacement_apkg=%s for provider_apkg=%s.\n", + replacement_apkg->name, provider_apkg->name); + + if (replacement_apkg && (replacement_apkg != provider_apkg)) { + if (abstract_pkg_vec_contains + (providers, replacement_apkg)) + continue; + else + provider_apkg = replacement_apkg; + } + + if (!(vec = provider_apkg->pkgs)) { + opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n", + provider_apkg->name); + continue; + } + + /* now check for supported architecture */ + { + int max_count = 0; + + /* count packages matching max arch priority and keep track of last one */ + for (j = 0; j < vec->len; j++) { + pkg_t *maybe = vec->pkgs[j]; + opkg_msg(DEBUG, + "%s arch=%s arch_priority=%d version=%s.\n", + maybe->name, maybe->architecture, + maybe->arch_priority, maybe->version); + /* We make sure not to add the same package twice. Need to search for the reason why + they show up twice sometimes. */ + if ((maybe->arch_priority > 0) + && + (!pkg_vec_contains(matching_pkgs, maybe))) { + max_count++; + abstract_pkg_vec_insert(matching_apkgs, + maybe->parent); + pkg_vec_insert(matching_pkgs, maybe); + } + } + + if (vec->len > 0 && matching_pkgs->len < 1) + wrong_arch_found = 1; + } + } + + if (matching_pkgs->len < 1) { + if (wrong_arch_found) + opkg_msg(ERROR, "Packages for %s found, but" + " incompatible with the architectures configured\n", + apkg->name); + pkg_vec_free(matching_pkgs); + abstract_pkg_vec_free(matching_apkgs); + abstract_pkg_vec_free(providers); + return NULL; + } + + if (matching_pkgs->len > 1) + pkg_vec_sort(matching_pkgs, + pkg_name_version_and_architecture_compare); + if (matching_apkgs->len > 1) + abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare); + + for (i = 0; i < matching_pkgs->len; i++) { + pkg_t *matching = matching_pkgs->pkgs[i]; + if (constraint_fcn(matching, cdata)) { + opkg_msg(DEBUG, "Candidate: %s %s.\n", + matching->name, matching->version); + good_pkg_by_name = matching; + /* It has been provided by hand, so it is what user want */ + if (matching->provided_by_hand == 1) + break; + } + } + + for (i = 0; i < matching_pkgs->len; i++) { + pkg_t *matching = matching_pkgs->pkgs[i]; + latest_matching = matching; + if (matching->parent->state_status == SS_INSTALLED + || matching->parent->state_status == SS_UNPACKED) + latest_installed_parent = matching; + if (matching->state_flag & (SF_HOLD | SF_PREFER)) { + if (held_pkg) + opkg_msg(NOTICE, + "Multiple packages (%s and %s) providing" + " same name marked HOLD or PREFER. " + "Using latest.\n", held_pkg->name, + matching->name); + held_pkg = matching; + } + } + + if (!good_pkg_by_name && !held_pkg && !latest_installed_parent + && matching_apkgs->len > 1 && !quiet) { + int prio = 0; + for (i = 0; i < matching_pkgs->len; i++) { + pkg_t *matching = matching_pkgs->pkgs[i]; + if (matching->arch_priority > prio) { + priorized_matching = matching; + prio = matching->arch_priority; + opkg_msg(DEBUG, "Match %s with priority %i.\n", + matching->name, prio); + } + } + + } + + if (conf->verbosity >= INFO && matching_apkgs->len > 1) { + opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n", + matching_pkgs->len, apkg->name); + for (i = 0; i < matching_pkgs->len; i++) { + pkg_t *matching = matching_pkgs->pkgs[i]; + opkg_msg(INFO, "%s %s %s\n", + matching->name, matching->version, + matching->architecture); + } + } + + nmatching = matching_apkgs->len; + + pkg_vec_free(matching_pkgs); + abstract_pkg_vec_free(matching_apkgs); + abstract_pkg_vec_free(providers); + + if (good_pkg_by_name) { /* We found a good candidate, we will install it */ + return good_pkg_by_name; + } + if (held_pkg) { + opkg_msg(INFO, "Using held package %s.\n", held_pkg->name); + return held_pkg; + } + if (latest_installed_parent) { + opkg_msg(INFO, + "Using latest version of installed package %s.\n", + latest_installed_parent->name); + return latest_installed_parent; + } + if (priorized_matching) { + opkg_msg(INFO, "Using priorized matching %s %s %s.\n", + priorized_matching->name, priorized_matching->version, + priorized_matching->architecture); + return priorized_matching; + } + if (nmatching > 1) { + opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n", + nmatching); + return NULL; + } + if (latest_matching) { + opkg_msg(INFO, "Using latest matching %s %s %s.\n", + latest_matching->name, latest_matching->version, + latest_matching->architecture); + return latest_matching; + } + return NULL; } -static int -pkg_name_constraint_fcn(pkg_t *pkg, void *cdata) +static int pkg_name_constraint_fcn(pkg_t * pkg, void *cdata) { const char *name = (const char *)cdata; @@ -451,19 +459,19 @@ pkg_name_constraint_fcn(pkg_t *pkg, void *cdata) return 0; } -static pkg_vec_t * -pkg_vec_fetch_by_name(const char *pkg_name) +static pkg_vec_t *pkg_vec_fetch_by_name(const char *pkg_name) { - abstract_pkg_t * ab_pkg; + abstract_pkg_t *ab_pkg; - if(!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name))) + if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name))) return NULL; if (ab_pkg->pkgs) return ab_pkg->pkgs; if (ab_pkg->provided_by) { - abstract_pkg_t *abpkg = abstract_pkg_vec_get(ab_pkg->provided_by, 0); + abstract_pkg_t *abpkg = + abstract_pkg_vec_get(ab_pkg->provided_by, 0); if (abpkg != NULL) return abpkg->pkgs; else @@ -473,9 +481,7 @@ pkg_vec_fetch_by_name(const char *pkg_name) return NULL; } - -pkg_t * -pkg_hash_fetch_best_installation_candidate_by_name(const char *name) +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(const char *name) { abstract_pkg_t *apkg = NULL; @@ -483,39 +489,38 @@ pkg_hash_fetch_best_installation_candidate_by_name(const char *name) return NULL; return pkg_hash_fetch_best_installation_candidate(apkg, - pkg_name_constraint_fcn, apkg->name, 0); + pkg_name_constraint_fcn, + apkg->name, 0); } - -pkg_t * -pkg_hash_fetch_by_name_version(const char *pkg_name, const char * version) +pkg_t *pkg_hash_fetch_by_name_version(const char *pkg_name, const char *version) { - pkg_vec_t * vec; + pkg_vec_t *vec; int i; char *version_str = NULL; - if(!(vec = pkg_vec_fetch_by_name(pkg_name))) + if (!(vec = pkg_vec_fetch_by_name(pkg_name))) return NULL; - for(i = 0; i < vec->len; i++) { + for (i = 0; i < vec->len; i++) { version_str = pkg_version_str_alloc(vec->pkgs[i]); - if(!strcmp(version_str, version)) { + if (!strcmp(version_str, version)) { free(version_str); break; } free(version_str); } - if(i == vec->len) + if (i == vec->len) return NULL; return vec->pkgs[i]; } -pkg_t * -pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, pkg_dest_t *dest) +pkg_t *pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, + pkg_dest_t * dest) { - pkg_vec_t * vec; + pkg_vec_t *vec; int i; if (!(vec = pkg_vec_fetch_by_name(pkg_name))) { @@ -523,19 +528,18 @@ pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, pkg_dest_t *dest) } for (i = 0; i < vec->len; i++) - if((vec->pkgs[i]->state_status == SS_INSTALLED - || vec->pkgs[i]->state_status == SS_UNPACKED) - && vec->pkgs[i]->dest == dest) { + if ((vec->pkgs[i]->state_status == SS_INSTALLED + || vec->pkgs[i]->state_status == SS_UNPACKED) + && vec->pkgs[i]->dest == dest) { return vec->pkgs[i]; - } + } return NULL; } -pkg_t * -pkg_hash_fetch_installed_by_name(const char *pkg_name) +pkg_t *pkg_hash_fetch_installed_by_name(const char *pkg_name) { - pkg_vec_t * vec; + pkg_vec_t *vec; int i; if (!(vec = pkg_vec_fetch_by_name(pkg_name))) { @@ -544,21 +548,20 @@ pkg_hash_fetch_installed_by_name(const char *pkg_name) for (i = 0; i < vec->len; i++) { if (vec->pkgs[i]->state_status == SS_INSTALLED - || vec->pkgs[i]->state_status == SS_UNPACKED) { + || vec->pkgs[i]->state_status == SS_UNPACKED) { return vec->pkgs[i]; - } + } } return NULL; } - static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data) { int j; - abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry; - pkg_vec_t *all = (pkg_vec_t *)data; + abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry; + pkg_vec_t *all = (pkg_vec_t *) data; pkg_vec_t *pkg_vec = ab_pkg->pkgs; if (!pkg_vec) @@ -570,18 +573,18 @@ pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data) } } -void -pkg_hash_fetch_available(pkg_vec_t *all) +void pkg_hash_fetch_available(pkg_vec_t * all) { hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper, - all); + all); } static void -pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data) +pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, + void *data) { - abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry; - pkg_vec_t *all = (pkg_vec_t *)data; + abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry; + pkg_vec_t *all = (pkg_vec_t *) data; pkg_vec_t *pkg_vec = ab_pkg->pkgs; int j; @@ -591,23 +594,21 @@ pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *dat for (j = 0; j < pkg_vec->len; j++) { pkg_t *pkg = pkg_vec->pkgs[j]; if (pkg->state_status == SS_INSTALLED - || pkg->state_status == SS_UNPACKED) + || pkg->state_status == SS_UNPACKED) pkg_vec_insert(all, pkg); } } -void -pkg_hash_fetch_all_installed(pkg_vec_t *all) +void pkg_hash_fetch_all_installed(pkg_vec_t * all) { hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper, - all); + all); } /* * This assumes that the abstract pkg doesn't exist. */ -static abstract_pkg_t * -add_new_abstract_pkg_by_name(const char *pkg_name) +static abstract_pkg_t *add_new_abstract_pkg_by_name(const char *pkg_name) { abstract_pkg_t *ab_pkg; @@ -619,11 +620,9 @@ add_new_abstract_pkg_by_name(const char *pkg_name) return ab_pkg; } - -abstract_pkg_t * -ensure_abstract_pkg_by_name(const char *pkg_name) +abstract_pkg_t *ensure_abstract_pkg_by_name(const char *pkg_name) { - abstract_pkg_t * ab_pkg; + abstract_pkg_t *ab_pkg; if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name))) ab_pkg = add_new_abstract_pkg_by_name(pkg_name); @@ -631,10 +630,9 @@ ensure_abstract_pkg_by_name(const char *pkg_name) return ab_pkg; } -void -hash_insert_pkg(pkg_t *pkg, int set_status) +void hash_insert_pkg(pkg_t * pkg, int set_status) { - abstract_pkg_t * ab_pkg; + abstract_pkg_t *ab_pkg; ab_pkg = ensure_abstract_pkg_by_name(pkg->name); if (!ab_pkg->pkgs) @@ -663,8 +661,7 @@ hash_insert_pkg(pkg_t *pkg, int set_status) pkg->parent = ab_pkg; } -static const char * -strip_offline_root(const char *file_name) +static const char *strip_offline_root(const char *file_name) { unsigned int len; @@ -677,27 +674,24 @@ strip_offline_root(const char *file_name) return file_name; } -void -file_hash_remove(const char *file_name) +void file_hash_remove(const char *file_name) { file_name = strip_offline_root(file_name); hash_table_remove(&conf->file_hash, file_name); } -pkg_t * -file_hash_get_file_owner(const char *file_name) +pkg_t *file_hash_get_file_owner(const char *file_name) { file_name = strip_offline_root(file_name); return hash_table_get(&conf->file_hash, file_name); } -void -file_hash_set_file_owner(const char *file_name, pkg_t *owning_pkg) +void file_hash_set_file_owner(const char *file_name, pkg_t * owning_pkg) { pkg_t *old_owning_pkg; int file_name_len = strlen(file_name); - if (file_name[file_name_len -1] == '/') + if (file_name[file_name_len - 1] == '/') return; file_name = strip_offline_root(file_name); diff --git a/libopkg/pkg_hash.h b/libopkg/pkg_hash.h index b3cf3d1..d879278 100644 --- a/libopkg/pkg_hash.h +++ b/libopkg/pkg_hash.h @@ -23,34 +23,36 @@ #include "pkg_dest.h" #include "hash_table.h" - void pkg_hash_init(void); void pkg_hash_deinit(void); -void pkg_hash_fetch_available(pkg_vec_t *available); +void pkg_hash_fetch_available(pkg_vec_t * available); -int dist_hash_add_from_file(const char *file_name, pkg_src_t *dist); -int pkg_hash_add_from_file(const char *file_name, pkg_src_t *src, - pkg_dest_t *dest, int is_status_file); +int dist_hash_add_from_file(const char *file_name, pkg_src_t * dist); +int pkg_hash_add_from_file(const char *file_name, pkg_src_t * src, + pkg_dest_t * dest, int is_status_file); int pkg_hash_load_feeds(void); int pkg_hash_load_status_files(void); -void hash_insert_pkg(pkg_t *pkg, int set_status); - -abstract_pkg_t * ensure_abstract_pkg_by_name(const char * pkg_name); -void pkg_hash_fetch_all_installed(pkg_vec_t *installed); -pkg_t * pkg_hash_fetch_by_name_version(const char *pkg_name, - const char * version); -pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t *apkg, - int (*constraint_fcn)(pkg_t *pkg, void *data), void *cdata, int quiet); +void hash_insert_pkg(pkg_t * pkg, int set_status); + +abstract_pkg_t *ensure_abstract_pkg_by_name(const char *pkg_name); +void pkg_hash_fetch_all_installed(pkg_vec_t * installed); +pkg_t *pkg_hash_fetch_by_name_version(const char *pkg_name, + const char *version); +pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg, + int (*constraint_fcn) (pkg_t * + pkg, + void + *data), + void *cdata, int quiet); pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(const char *name); pkg_t *pkg_hash_fetch_installed_by_name(const char *pkg_name); pkg_t *pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, - pkg_dest_t *dest); + pkg_dest_t * dest); void file_hash_remove(const char *file_name); pkg_t *file_hash_get_file_owner(const char *file_name); -void file_hash_set_file_owner(const char *file_name, pkg_t *pkg); +void file_hash_set_file_owner(const char *file_name, pkg_t * pkg); #endif - diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index edf6312..f9da0cc 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -29,15 +29,13 @@ #include "parse_util.h" -static void -parse_status(pkg_t *pkg, const char *sstr) +static void parse_status(pkg_t * pkg, const char *sstr) { char sw_str[64], sf_str[64], ss_str[64]; - if (sscanf(sstr, "Status: %63s %63s %63s", - sw_str, sf_str, ss_str) != 3) { + if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) { opkg_msg(ERROR, "Failed to parse Status line for %s\n", - pkg->name); + pkg->name); return; } @@ -46,22 +44,20 @@ parse_status(pkg_t *pkg, const char *sstr) pkg->state_status = pkg_state_status_from_str(ss_str); } -static void -parse_conffiles(pkg_t *pkg, const char *cstr) +static void parse_conffiles(pkg_t * pkg, const char *cstr) { char file_name[1024], md5sum[85]; if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) { opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n", - pkg->name); + pkg->name); return; } conffile_list_append(&pkg->conffiles, file_name, md5sum); } -int -parse_version(pkg_t *pkg, const char *vstr) +int parse_version(pkg_t * pkg, const char *vstr) { char *colon; @@ -80,11 +76,11 @@ parse_version(pkg_t *pkg, const char *vstr) } vstr = ++colon; } else { - pkg->epoch= 0; + pkg->epoch = 0; } pkg->version = xstrdup(vstr); - pkg->revision = strrchr(pkg->version,'-'); + pkg->revision = strrchr(pkg->version, '-'); if (pkg->revision) *pkg->revision++ = '\0'; @@ -92,21 +88,19 @@ parse_version(pkg_t *pkg, const char *vstr) return 0; } -static int -get_arch_priority(const char *arch) +static int get_arch_priority(const char *arch) { nv_pair_list_elt_t *l; - list_for_each_entry(l , &conf->arch_list.head, node) { - nv_pair_t *nv = (nv_pair_t *)l->data; + list_for_each_entry(l, &conf->arch_list.head, node) { + nv_pair_t *nv = (nv_pair_t *) l->data; if (strcmp(nv->name, arch) == 0) return strtol(nv->value, NULL, 0); } return 0; } -int -pkg_parse_line(void *ptr, const char *line, uint mask) +int pkg_parse_line(void *ptr, const char *line, uint mask) { pkg_t *pkg = (pkg_t *) ptr; @@ -122,13 +116,15 @@ pkg_parse_line(void *ptr, const char *line, uint mask) switch (*line) { case 'A': - if ((mask & PFM_ARCHITECTURE ) && is_field("Architecture", line)) { + if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line)) { pkg->architecture = parse_simple("Architecture", line); - pkg->arch_priority = get_arch_priority(pkg->architecture); - } else if ((mask & PFM_AUTO_INSTALLED) && is_field("Auto-Installed", line)) { + pkg->arch_priority = + get_arch_priority(pkg->architecture); + } else if ((mask & PFM_AUTO_INSTALLED) + && is_field("Auto-Installed", line)) { char *tmp = parse_simple("Auto-Installed", line); if (strcmp(tmp, "yes") == 0) - pkg->auto_installed = 1; + pkg->auto_installed = 1; free(tmp); } break; @@ -138,9 +134,10 @@ pkg_parse_line(void *ptr, const char *line, uint mask) reading_conffiles = 1; reading_description = 0; goto dont_reset_flags; - } - else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line)) - pkg->conflicts_str = parse_list(line, &pkg->conflicts_count, ',', 0); + } else if ((mask & PFM_CONFLICTS) + && is_field("Conflicts", line)) + pkg->conflicts_str = + parse_list(line, &pkg->conflicts_count, ',', 0); break; case 'D': @@ -150,11 +147,12 @@ pkg_parse_line(void *ptr, const char *line, uint mask) reading_description = 1; goto dont_reset_flags; } else if ((mask & PFM_DEPENDS) && is_field("Depends", line)) - pkg->depends_str = parse_list(line, &pkg->depends_count, ',', 0); + pkg->depends_str = + parse_list(line, &pkg->depends_count, ',', 0); break; case 'E': - if((mask & PFM_ESSENTIAL) && is_field("Essential", line)) { + if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) { char *tmp = parse_simple("Essential", line); if (strcmp(tmp, "yes") == 0) pkg->essential = 1; @@ -163,30 +161,33 @@ pkg_parse_line(void *ptr, const char *line, uint mask) break; case 'F': - if((mask & PFM_FILENAME) && is_field("Filename", line)) + if ((mask & PFM_FILENAME) && is_field("Filename", line)) pkg->filename = parse_simple("Filename", line); break; case 'I': - if ((mask & PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) { + if ((mask & PFM_INSTALLED_SIZE) + && is_field("Installed-Size", line)) { char *tmp = parse_simple("Installed-Size", line); pkg->installed_size = strtoul(tmp, NULL, 0); - free (tmp); - } else if ((mask & PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) { + free(tmp); + } else if ((mask & PFM_INSTALLED_TIME) + && is_field("Installed-Time", line)) { char *tmp = parse_simple("Installed-Time", line); pkg->installed_time = strtoul(tmp, NULL, 0); - free (tmp); + free(tmp); } break; case 'M': if ((mask & PFM_MD5SUM) && is_field("MD5sum:", line)) pkg->md5sum = parse_simple("MD5sum", line); - /* The old opkg wrote out status files with the wrong - * case for MD5sum, let's parse it either way */ - else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line)) + /* The old opkg wrote out status files with the wrong + * case for MD5sum, let's parse it either way */ + else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line)) pkg->md5sum = parse_simple("MD5Sum", line); - else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line)) + else if ((mask & PFM_MAINTAINER) + && is_field("Maintainer", line)) pkg->maintainer = parse_simple("Maintainer", line); break; @@ -196,16 +197,21 @@ pkg_parse_line(void *ptr, const char *line, uint mask) else if ((mask & PFM_PRIORITY) && is_field("Priority", line)) pkg->priority = parse_simple("Priority", line); else if ((mask & PFM_PROVIDES) && is_field("Provides", line)) - pkg->provides_str = parse_list(line, &pkg->provides_count, ',', 0); - else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line)) - pkg->pre_depends_str = parse_list(line, &pkg->pre_depends_count, ',', 0); + pkg->provides_str = + parse_list(line, &pkg->provides_count, ',', 0); + else if ((mask & PFM_PRE_DEPENDS) + && is_field("Pre-Depends", line)) + pkg->pre_depends_str = + parse_list(line, &pkg->pre_depends_count, ',', 0); break; case 'R': if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line)) - pkg->recommends_str = parse_list(line, &pkg->recommends_count, ',', 0); + pkg->recommends_str = + parse_list(line, &pkg->recommends_count, ',', 0); else if ((mask & PFM_REPLACES) && is_field("Replaces", line)) - pkg->replaces_str = parse_list(line, &pkg->replaces_count, ',', 0); + pkg->replaces_str = + parse_list(line, &pkg->replaces_count, ',', 0); break; @@ -219,13 +225,14 @@ pkg_parse_line(void *ptr, const char *line, uint mask) else if ((mask & PFM_SIZE) && is_field("Size", line)) { char *tmp = parse_simple("Size", line); pkg->size = strtoul(tmp, NULL, 0); - free (tmp); + free(tmp); } else if ((mask & PFM_SOURCE) && is_field("Source", line)) pkg->source = parse_simple("Source", line); else if ((mask & PFM_STATUS) && is_field("Status", line)) parse_status(pkg, line); else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line)) - pkg->suggests_str = parse_list(line, &pkg->suggests_count, ',', 0); + pkg->suggests_str = + parse_list(line, &pkg->suggests_count, ',', 0); break; case 'T': @@ -242,13 +249,16 @@ pkg_parse_line(void *ptr, const char *line, uint mask) if ((mask & PFM_DESCRIPTION) && reading_description) { if (isatty(1)) { pkg->description = xrealloc(pkg->description, - strlen(pkg->description) - + 1 + strlen(line) + 1); + strlen(pkg-> + description) + + 1 + strlen(line) + + 1); strcat(pkg->description, "\n"); } else { pkg->description = xrealloc(pkg->description, - strlen(pkg->description) - + 1 + strlen(line)); + strlen(pkg-> + description) + + 1 + strlen(line)); } strcat(pkg->description, (line)); goto dont_reset_flags; @@ -260,7 +270,7 @@ pkg_parse_line(void *ptr, const char *line, uint mask) /* FALLTHROUGH */ default: /* For package lists, signifies end of package. */ - if(line_is_blank(line)) { + if (line_is_blank(line)) { ret = 1; break; } @@ -274,15 +284,16 @@ dont_reset_flags: return ret; } -int -pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask) +int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask) { int ret; char *buf; const size_t len = 4096; buf = xmalloc(len); - ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, len); + ret = + parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, + len); free(buf); if (pkg->name == NULL) { diff --git a/libopkg/pkg_parse.h b/libopkg/pkg_parse.h index bab88fb..5f999f8 100644 --- a/libopkg/pkg_parse.h +++ b/libopkg/pkg_parse.h @@ -20,8 +20,8 @@ #include "pkg.h" -int parse_version(pkg_t *pkg, const char *raw); -int pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask); +int parse_version(pkg_t * pkg, const char *raw); +int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask); int pkg_parse_line(void *ptr, const char *line, uint mask); #define EXCESSIVE_LINE_LEN (4096 << 8) diff --git a/libopkg/pkg_src.c b/libopkg/pkg_src.c index 690fef6..fae3ce3 100644 --- a/libopkg/pkg_src.c +++ b/libopkg/pkg_src.c @@ -18,22 +18,23 @@ #include "pkg_src.h" #include "libbb/libbb.h" -int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip) +int pkg_src_init(pkg_src_t * src, const char *name, const char *base_url, + const char *extra_data, int gzip) { - src->gzip = gzip; - src->name = xstrdup(name); - src->value = xstrdup(base_url); - if (extra_data) - src->extra_data = xstrdup(extra_data); - else - src->extra_data = NULL; - return 0; + src->gzip = gzip; + src->name = xstrdup(name); + src->value = xstrdup(base_url); + if (extra_data) + src->extra_data = xstrdup(extra_data); + else + src->extra_data = NULL; + return 0; } -void pkg_src_deinit(pkg_src_t *src) +void pkg_src_deinit(pkg_src_t * src) { - free (src->name); - free (src->value); - if (src->extra_data) - free (src->extra_data); + free(src->name); + free(src->value); + if (src->extra_data) + free(src->extra_data); } diff --git a/libopkg/pkg_src.h b/libopkg/pkg_src.h index b1a95a5..1320f1f 100644 --- a/libopkg/pkg_src.h +++ b/libopkg/pkg_src.h @@ -20,15 +20,15 @@ #include "nv_pair.h" -typedef struct -{ - char *name; - char *value; - char *extra_data; - int gzip; +typedef struct { + char *name; + char *value; + char *extra_data; + int gzip; } pkg_src_t; -int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip); -void pkg_src_deinit(pkg_src_t *src); +int pkg_src_init(pkg_src_t * src, const char *name, const char *base_url, + const char *extra_data, int gzip); +void pkg_src_deinit(pkg_src_t * src); #endif diff --git a/libopkg/pkg_src_list.c b/libopkg/pkg_src_list.c index 7ad1b41..036d0c9 100644 --- a/libopkg/pkg_src_list.c +++ b/libopkg/pkg_src_list.c @@ -19,46 +19,46 @@ #include "void_list.h" #include "libbb/libbb.h" -void pkg_src_list_init(pkg_src_list_t *list) +void pkg_src_list_init(pkg_src_list_t * list) { - void_list_init((void_list_t *) list); + void_list_init((void_list_t *) list); } -void pkg_src_list_deinit(pkg_src_list_t *list) +void pkg_src_list_deinit(pkg_src_list_t * list) { - pkg_src_list_elt_t *iter, *n; - pkg_src_t *pkg_src; + pkg_src_list_elt_t *iter, *n; + pkg_src_t *pkg_src; - list_for_each_entry_safe(iter, n, &list->head, node) { - pkg_src = (pkg_src_t *)iter->data; - pkg_src_deinit(pkg_src); + list_for_each_entry_safe(iter, n, &list->head, node) { + pkg_src = (pkg_src_t *) iter->data; + pkg_src_deinit(pkg_src); - /* malloced in pkg_src_list_append */ - free(pkg_src); - iter->data = NULL; - } - void_list_deinit((void_list_t *) list); + /* malloced in pkg_src_list_append */ + free(pkg_src); + iter->data = NULL; + } + void_list_deinit((void_list_t *) list); } -pkg_src_t *pkg_src_list_append(pkg_src_list_t *list, - const char *name, const char *base_url, const char *extra_data, - int gzip) +pkg_src_t *pkg_src_list_append(pkg_src_list_t * list, + const char *name, const char *base_url, + const char *extra_data, int gzip) { - /* freed in pkg_src_list_deinit */ - pkg_src_t *pkg_src = xcalloc(1, sizeof(pkg_src_t)); - pkg_src_init(pkg_src, name, base_url, extra_data, gzip); + /* freed in pkg_src_list_deinit */ + pkg_src_t *pkg_src = xcalloc(1, sizeof(pkg_src_t)); + pkg_src_init(pkg_src, name, base_url, extra_data, gzip); - void_list_append((void_list_t *) list, pkg_src); + void_list_append((void_list_t *) list, pkg_src); - return pkg_src; + return pkg_src; } -void pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data) +void pkg_src_list_push(pkg_src_list_t * list, pkg_src_t * data) { - void_list_push((void_list_t *) list, data); + void_list_push((void_list_t *) list, data); } -pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list) +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t * list) { - return (pkg_src_list_elt_t *) void_list_pop((void_list_t *) list); + return (pkg_src_list_elt_t *) void_list_pop((void_list_t *) list); } diff --git a/libopkg/pkg_src_list.h b/libopkg/pkg_src_list.h index 529f013..5840c04 100644 --- a/libopkg/pkg_src_list.h +++ b/libopkg/pkg_src_list.h @@ -25,20 +25,21 @@ typedef struct void_list_elt pkg_src_list_elt_t; typedef struct void_list pkg_src_list_t; -static inline int pkg_src_list_empty(pkg_src_list_t *list) +static inline int pkg_src_list_empty(pkg_src_list_t * list) { - return void_list_empty((void_list_t *)list); + return void_list_empty((void_list_t *) list); } -void pkg_src_list_elt_init(pkg_src_list_elt_t *elt, nv_pair_t *data); -void pkg_src_list_elt_deinit(pkg_src_list_elt_t *elt); +void pkg_src_list_elt_init(pkg_src_list_elt_t * elt, nv_pair_t * data); +void pkg_src_list_elt_deinit(pkg_src_list_elt_t * elt); -void pkg_src_list_init(pkg_src_list_t *list); -void pkg_src_list_deinit(pkg_src_list_t *list); +void pkg_src_list_init(pkg_src_list_t * list); +void pkg_src_list_deinit(pkg_src_list_t * list); -pkg_src_t *pkg_src_list_append(pkg_src_list_t *list, const char *name, const char *root_dir, const char *extra_data, int gzip); -void pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data); -pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list); +pkg_src_t *pkg_src_list_append(pkg_src_list_t * list, const char *name, + const char *root_dir, const char *extra_data, + int gzip); +void pkg_src_list_push(pkg_src_list_t * list, pkg_src_t * data); +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t * list); #endif - diff --git a/libopkg/pkg_vec.c b/libopkg/pkg_vec.c index 472962c..162c70f 100644 --- a/libopkg/pkg_vec.c +++ b/libopkg/pkg_vec.c @@ -22,24 +22,24 @@ #include "opkg_message.h" #include "libbb/libbb.h" -pkg_vec_t * pkg_vec_alloc(void) +pkg_vec_t *pkg_vec_alloc(void) { - pkg_vec_t * vec = xcalloc(1, sizeof(pkg_vec_t)); - vec->pkgs = NULL; - vec->len = 0; + pkg_vec_t *vec = xcalloc(1, sizeof(pkg_vec_t)); + vec->pkgs = NULL; + vec->len = 0; - return vec; + return vec; } -void pkg_vec_free(pkg_vec_t *vec) +void pkg_vec_free(pkg_vec_t * vec) { - if (!vec) - return; + if (!vec) + return; - if (vec->pkgs) - free(vec->pkgs); + if (vec->pkgs) + free(vec->pkgs); - free(vec); + free(vec); } /* @@ -48,161 +48,164 @@ void pkg_vec_free(pkg_vec_t *vec) * so identical versions have identical version strings, * implying identical packages; let's marry these */ -void pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status) +void pkg_vec_insert_merge(pkg_vec_t * vec, pkg_t * pkg, int set_status) { - int i; - int found = 0; - - /* look for a duplicate pkg by name, version, and architecture */ - for (i = 0; i < vec->len; i++){ - opkg_msg(DEBUG2, "%s %s arch=%s vs. %s %s arch=%s.\n", - pkg->name, pkg->version, pkg->architecture, - vec->pkgs[i]->name, vec->pkgs[i]->version, - vec->pkgs[i]->architecture); - /* if the name,ver,arch matches, or the name matches and the - * package is marked deinstall/hold */ - if ((!strcmp(pkg->name, vec->pkgs[i]->name)) - && ((pkg->state_want == SW_DEINSTALL - && (pkg->state_flag & SF_HOLD)) - || ((pkg_compare_versions(pkg, vec->pkgs[i]) == 0) - && (!strcmp(pkg->architecture, vec->pkgs[i]->architecture))))) { - found = 1; - opkg_msg(DEBUG2, "Duplicate for pkg=%s version=%s arch=%s.\n", - pkg->name, pkg->version, pkg->architecture); - break; - } - } - - /* we didn't find one, add it */ - if (!found){ - opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n", - pkg->name, pkg->version, pkg->architecture); - pkg_vec_insert(vec, pkg); - return; - } - - /* update the one that we have */ - opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n", - pkg->name, pkg->version, pkg->architecture, set_status); - if (set_status) { - /* This is from the status file, - * so need to merge with existing database */ - pkg_merge(pkg, vec->pkgs[i]); - } - - /* overwrite the old one */ - pkg_deinit(vec->pkgs[i]); - free(vec->pkgs[i]); - vec->pkgs[i] = pkg; + int i; + int found = 0; + + /* look for a duplicate pkg by name, version, and architecture */ + for (i = 0; i < vec->len; i++) { + opkg_msg(DEBUG2, "%s %s arch=%s vs. %s %s arch=%s.\n", + pkg->name, pkg->version, pkg->architecture, + vec->pkgs[i]->name, vec->pkgs[i]->version, + vec->pkgs[i]->architecture); + /* if the name,ver,arch matches, or the name matches and the + * package is marked deinstall/hold */ + if ((!strcmp(pkg->name, vec->pkgs[i]->name)) + && ((pkg->state_want == SW_DEINSTALL + && (pkg->state_flag & SF_HOLD)) + || ((pkg_compare_versions(pkg, vec->pkgs[i]) == 0) + && + (!strcmp + (pkg->architecture, + vec->pkgs[i]->architecture))))) { + found = 1; + opkg_msg(DEBUG2, + "Duplicate for pkg=%s version=%s arch=%s.\n", + pkg->name, pkg->version, pkg->architecture); + break; + } + } + + /* we didn't find one, add it */ + if (!found) { + opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n", + pkg->name, pkg->version, pkg->architecture); + pkg_vec_insert(vec, pkg); + return; + } + + /* update the one that we have */ + opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n", + pkg->name, pkg->version, pkg->architecture, set_status); + if (set_status) { + /* This is from the status file, + * so need to merge with existing database */ + pkg_merge(pkg, vec->pkgs[i]); + } + + /* overwrite the old one */ + pkg_deinit(vec->pkgs[i]); + free(vec->pkgs[i]); + vec->pkgs[i] = pkg; } -void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg) +void pkg_vec_insert(pkg_vec_t * vec, const pkg_t * pkg) { - vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *)); - vec->pkgs[vec->len] = (pkg_t *)pkg; - vec->len++; + vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *)); + vec->pkgs[vec->len] = (pkg_t *) pkg; + vec->len++; } -int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg) +int pkg_vec_contains(pkg_vec_t * vec, pkg_t * apkg) { - int i; - for (i = 0; i < vec->len; i++) - if (vec->pkgs[i] == apkg) - return 1; - return 0; + int i; + for (i = 0; i < vec->len; i++) + if (vec->pkgs[i] == apkg) + return 1; + return 0; } -void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar) +void pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar) { - qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar); + qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar); } -int pkg_vec_clear_marks(pkg_vec_t *vec) +int pkg_vec_clear_marks(pkg_vec_t * vec) { - int npkgs = vec->len; - int i; - for (i = 0; i < npkgs; i++) { - pkg_t *pkg = vec->pkgs[i]; - pkg->state_flag &= ~SF_MARKED; - } - return 0; + int npkgs = vec->len; + int i; + for (i = 0; i < npkgs; i++) { + pkg_t *pkg = vec->pkgs[i]; + pkg->state_flag &= ~SF_MARKED; + } + return 0; } -int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern) +int pkg_vec_mark_if_matches(pkg_vec_t * vec, const char *pattern) { - int matching_count = 0; - pkg_t **pkgs = vec->pkgs; - int npkgs = vec->len; - int i; - for (i = 0; i < npkgs; i++) { - pkg_t *pkg = pkgs[i]; - if (fnmatch(pattern, pkg->name, 0)==0) { - pkg->state_flag |= SF_MARKED; - matching_count++; - } - } - return matching_count; + int matching_count = 0; + pkg_t **pkgs = vec->pkgs; + int npkgs = vec->len; + int i; + for (i = 0; i < npkgs; i++) { + pkg_t *pkg = pkgs[i]; + if (fnmatch(pattern, pkg->name, 0) == 0) { + pkg->state_flag |= SF_MARKED; + matching_count++; + } + } + return matching_count; } - -abstract_pkg_vec_t * abstract_pkg_vec_alloc(void) +abstract_pkg_vec_t *abstract_pkg_vec_alloc(void) { - abstract_pkg_vec_t * vec ; - vec = xcalloc(1, sizeof(abstract_pkg_vec_t)); - vec->pkgs = NULL; - vec->len = 0; + abstract_pkg_vec_t *vec; + vec = xcalloc(1, sizeof(abstract_pkg_vec_t)); + vec->pkgs = NULL; + vec->len = 0; - return vec; + return vec; } -void abstract_pkg_vec_free(abstract_pkg_vec_t *vec) +void abstract_pkg_vec_free(abstract_pkg_vec_t * vec) { - if (!vec) - return; - free(vec->pkgs); - free(vec); + if (!vec) + return; + free(vec->pkgs); + free(vec); } /* * assumption: all names in a vector are unique */ -void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg) +void abstract_pkg_vec_insert(abstract_pkg_vec_t * vec, abstract_pkg_t * pkg) { - vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *)); - vec->pkgs[vec->len] = pkg; - vec->len++; + vec->pkgs = + xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *)); + vec->pkgs[vec->len] = pkg; + vec->len++; } -abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i) +abstract_pkg_t *abstract_pkg_vec_get(abstract_pkg_vec_t * vec, int i) { - if (vec->len > i) - return vec->pkgs[i]; - else - return NULL; + if (vec->len > i) + return vec->pkgs[i]; + else + return NULL; } -int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg) +int abstract_pkg_vec_contains(abstract_pkg_vec_t * vec, abstract_pkg_t * apkg) { - int i; - for (i = 0; i < vec->len; i++) - if (vec->pkgs[i] == apkg) - return 1; - return 0; + int i; + for (i = 0; i < vec->len; i++) + if (vec->pkgs[i] == apkg) + return 1; + return 0; } -void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar) +void abstract_pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar) { - qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar); + qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar); } int pkg_compare_names(const void *p1, const void *p2) { - const pkg_t *pkg1 = *(const pkg_t **)p1; - const pkg_t *pkg2 = *(const pkg_t **)p2; - if (pkg1->name == NULL) - return 1; - if (pkg2->name == NULL) - return -1; - return(strcmp(pkg1->name, pkg2->name)); + const pkg_t *pkg1 = *(const pkg_t **)p1; + const pkg_t *pkg2 = *(const pkg_t **)p2; + if (pkg1->name == NULL) + return 1; + if (pkg2->name == NULL) + return -1; + return (strcmp(pkg1->name, pkg2->name)); } - diff --git a/libopkg/pkg_vec.h b/libopkg/pkg_vec.h index 8ee1673..876455a 100644 --- a/libopkg/pkg_vec.h +++ b/libopkg/pkg_vec.h @@ -25,39 +25,35 @@ typedef struct abstract_pkg_vec abstract_pkg_vec_t; #include "opkg_conf.h" -struct pkg_vec -{ - pkg_t **pkgs; - unsigned int len; +struct pkg_vec { + pkg_t **pkgs; + unsigned int len; }; -struct abstract_pkg_vec -{ - abstract_pkg_t **pkgs; - unsigned int len; +struct abstract_pkg_vec { + abstract_pkg_t **pkgs; + unsigned int len; }; +pkg_vec_t *pkg_vec_alloc(void); +void pkg_vec_free(pkg_vec_t * vec); -pkg_vec_t * pkg_vec_alloc(void); -void pkg_vec_free(pkg_vec_t *vec); +void pkg_vec_insert_merge(pkg_vec_t * vec, pkg_t * pkg, int set_status); +void pkg_vec_insert(pkg_vec_t * vec, const pkg_t * pkg); +int pkg_vec_contains(pkg_vec_t * vec, pkg_t * apkg); -void pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status); -void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg); -int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg); +typedef int (*compare_fcn_t) (const void *, const void *); +void pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar); -typedef int (*compare_fcn_t)(const void *, const void *); -void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar); +int pkg_vec_clear_marks(pkg_vec_t * vec); +int pkg_vec_mark_if_matches(pkg_vec_t * vec, const char *pattern); -int pkg_vec_clear_marks(pkg_vec_t *vec); -int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern); - -abstract_pkg_vec_t * abstract_pkg_vec_alloc(void); -void abstract_pkg_vec_free(abstract_pkg_vec_t *vec); -void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg); -abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i); -int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg); -void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar); +abstract_pkg_vec_t *abstract_pkg_vec_alloc(void); +void abstract_pkg_vec_free(abstract_pkg_vec_t * vec); +void abstract_pkg_vec_insert(abstract_pkg_vec_t * vec, abstract_pkg_t * pkg); +abstract_pkg_t *abstract_pkg_vec_get(abstract_pkg_vec_t * vec, int i); +int abstract_pkg_vec_contains(abstract_pkg_vec_t * vec, abstract_pkg_t * apkg); +void abstract_pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar); int pkg_compare_names(const void *p1, const void *p2); #endif - diff --git a/libopkg/sha256.c b/libopkg/sha256.c index 0ad9444..d2806a0 100644 --- a/libopkg/sha256.c +++ b/libopkg/sha256.c @@ -28,369 +28,345 @@ #include #if USE_UNLOCKED_IO -# include "unlocked-io.h" +#include "unlocked-io.h" #endif #ifdef WORDS_BIGENDIAN -# define SWAP(n) (n) +#define SWAP(n) (n) #else -# define SWAP(n) \ +#define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) #endif #define BLOCKSIZE 4096 #if BLOCKSIZE % 64 != 0 -# error "invalid BLOCKSIZE" +#error "invalid BLOCKSIZE" #endif /* This array contains the bytes used to pad the buffer to the next 64-byte boundary. */ -static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; /* Takes a pointer to a 256 bit block of data (eight 32 bit ints) and intializes it to the start constants of the SHA256 algorithm. This must be called before using hash in the call to sha256_hash */ -void -sha256_init_ctx (struct sha256_ctx *ctx) +void sha256_init_ctx(struct sha256_ctx *ctx) { - ctx->state[0] = 0x6a09e667UL; - ctx->state[1] = 0xbb67ae85UL; - ctx->state[2] = 0x3c6ef372UL; - ctx->state[3] = 0xa54ff53aUL; - ctx->state[4] = 0x510e527fUL; - ctx->state[5] = 0x9b05688cUL; - ctx->state[6] = 0x1f83d9abUL; - ctx->state[7] = 0x5be0cd19UL; - - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; + ctx->state[0] = 0x6a09e667UL; + ctx->state[1] = 0xbb67ae85UL; + ctx->state[2] = 0x3c6ef372UL; + ctx->state[3] = 0xa54ff53aUL; + ctx->state[4] = 0x510e527fUL; + ctx->state[5] = 0x9b05688cUL; + ctx->state[6] = 0x1f83d9abUL; + ctx->state[7] = 0x5be0cd19UL; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; } -void -sha224_init_ctx (struct sha256_ctx *ctx) +void sha224_init_ctx(struct sha256_ctx *ctx) { - ctx->state[0] = 0xc1059ed8UL; - ctx->state[1] = 0x367cd507UL; - ctx->state[2] = 0x3070dd17UL; - ctx->state[3] = 0xf70e5939UL; - ctx->state[4] = 0xffc00b31UL; - ctx->state[5] = 0x68581511UL; - ctx->state[6] = 0x64f98fa7UL; - ctx->state[7] = 0xbefa4fa4UL; - - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; + ctx->state[0] = 0xc1059ed8UL; + ctx->state[1] = 0x367cd507UL; + ctx->state[2] = 0x3070dd17UL; + ctx->state[3] = 0xf70e5939UL; + ctx->state[4] = 0xffc00b31UL; + ctx->state[5] = 0x68581511UL; + ctx->state[6] = 0x64f98fa7UL; + ctx->state[7] = 0xbefa4fa4UL; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; } /* Copy the value from v into the memory location pointed to by *cp, If your architecture allows unaligned access this is equivalent to * (uint32_t *) cp = v */ -static inline void -set_uint32 (char *cp, uint32_t v) +static inline void set_uint32(char *cp, uint32_t v) { - memcpy (cp, &v, sizeof v); + memcpy(cp, &v, sizeof v); } /* Put result from CTX in first 32 bytes following RESBUF. The result must be in little endian byte order. */ -void * -sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf) +void *sha256_read_ctx(const struct sha256_ctx *ctx, void *resbuf) { - int i; - char *r = resbuf; + int i; + char *r = resbuf; - for (i = 0; i < 8; i++) - set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i])); + for (i = 0; i < 8; i++) + set_uint32(r + i * sizeof ctx->state[0], SWAP(ctx->state[i])); - return resbuf; + return resbuf; } -void * -sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf) +void *sha224_read_ctx(const struct sha256_ctx *ctx, void *resbuf) { - int i; - char *r = resbuf; + int i; + char *r = resbuf; - for (i = 0; i < 7; i++) - set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i])); + for (i = 0; i < 7; i++) + set_uint32(r + i * sizeof ctx->state[0], SWAP(ctx->state[i])); - return resbuf; + return resbuf; } /* Process the remaining bytes in the internal buffer and the usual prolog according to the standard and write the result to RESBUF. */ -static void -sha256_conclude_ctx (struct sha256_ctx *ctx) +static void sha256_conclude_ctx(struct sha256_ctx *ctx) { - /* Take yet unprocessed bytes into account. */ - size_t bytes = ctx->buflen; - size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; - - /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; - - /* Put the 64-bit file length in *bits* at the end of the buffer. - Use set_uint32 rather than a simple assignment, to avoid risk of - unaligned access. */ - set_uint32 ((char *) &ctx->buffer[size - 2], - SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29))); - set_uint32 ((char *) &ctx->buffer[size - 1], - SWAP (ctx->total[0] << 3)); - - memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); - - /* Process last bytes. */ - sha256_process_block (ctx->buffer, size * 4, ctx); + /* Take yet unprocessed bytes into account. */ + size_t bytes = ctx->buflen; + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + /* Put the 64-bit file length in *bits* at the end of the buffer. + Use set_uint32 rather than a simple assignment, to avoid risk of + unaligned access. */ + set_uint32((char *)&ctx->buffer[size - 2], + SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29))); + set_uint32((char *)&ctx->buffer[size - 1], SWAP(ctx->total[0] << 3)); + + memcpy(&((char *)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); + + /* Process last bytes. */ + sha256_process_block(ctx->buffer, size * 4, ctx); } -void * -sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf) +void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) { - sha256_conclude_ctx (ctx); - return sha256_read_ctx (ctx, resbuf); + sha256_conclude_ctx(ctx); + return sha256_read_ctx(ctx, resbuf); } -void * -sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf) +void *sha224_finish_ctx(struct sha256_ctx *ctx, void *resbuf) { - sha256_conclude_ctx (ctx); - return sha224_read_ctx (ctx, resbuf); + sha256_conclude_ctx(ctx); + return sha224_read_ctx(ctx, resbuf); } /* Compute SHA256 message digest for bytes read from STREAM. The resulting message digest number will be written into the 32 bytes beginning at RESBLOCK. */ -int -sha256_stream (FILE *stream, void *resblock) +int sha256_stream(FILE * stream, void *resblock) { - struct sha256_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - sha256_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - while (1) - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - - if (sum == BLOCKSIZE) - break; - - if (n == 0) - { - /* Check for the error flag IFF N == 0, so that we don't - exit the loop after a partial read due to e.g., EAGAIN - or EWOULDBLOCK. */ - if (ferror (stream)) - return 1; - goto process_partial_block; - } - - /* We've read at least one byte, so ignore errors. But always - check for EOF, since feof may be true even though N > 0. - Otherwise, we could end up calling fread after EOF. */ - if (feof (stream)) - goto process_partial_block; + struct sha256_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + sha256_init_ctx(&ctx); + + /* Iterate over full file contents. */ + while (1) { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + while (1) { + n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + + if (sum == BLOCKSIZE) + break; + + if (n == 0) { + /* Check for the error flag IFF N == 0, so that we don't + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ + if (ferror(stream)) + return 1; + goto process_partial_block; + } + + /* We've read at least one byte, so ignore errors. But always + check for EOF, since feof may be true even though N > 0. + Otherwise, we could end up calling fread after EOF. */ + if (feof(stream)) + goto process_partial_block; + } + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + sha256_process_block(buffer, BLOCKSIZE, &ctx); } - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - sha256_process_block (buffer, BLOCKSIZE, &ctx); - } - - process_partial_block:; +process_partial_block:; - /* Process any remaining bytes. */ - if (sum > 0) - sha256_process_bytes (buffer, sum, &ctx); + /* Process any remaining bytes. */ + if (sum > 0) + sha256_process_bytes(buffer, sum, &ctx); - /* Construct result in desired memory. */ - sha256_finish_ctx (&ctx, resblock); - return 0; + /* Construct result in desired memory. */ + sha256_finish_ctx(&ctx, resblock); + return 0; } /* FIXME: Avoid code duplication */ -int -sha224_stream (FILE *stream, void *resblock) +int sha224_stream(FILE * stream, void *resblock) { - struct sha256_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - sha224_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - while (1) - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - - if (sum == BLOCKSIZE) - break; - - if (n == 0) - { - /* Check for the error flag IFF N == 0, so that we don't - exit the loop after a partial read due to e.g., EAGAIN - or EWOULDBLOCK. */ - if (ferror (stream)) - return 1; - goto process_partial_block; - } - - /* We've read at least one byte, so ignore errors. But always - check for EOF, since feof may be true even though N > 0. - Otherwise, we could end up calling fread after EOF. */ - if (feof (stream)) - goto process_partial_block; + struct sha256_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + sha224_init_ctx(&ctx); + + /* Iterate over full file contents. */ + while (1) { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + while (1) { + n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + + if (sum == BLOCKSIZE) + break; + + if (n == 0) { + /* Check for the error flag IFF N == 0, so that we don't + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ + if (ferror(stream)) + return 1; + goto process_partial_block; + } + + /* We've read at least one byte, so ignore errors. But always + check for EOF, since feof may be true even though N > 0. + Otherwise, we could end up calling fread after EOF. */ + if (feof(stream)) + goto process_partial_block; + } + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + sha256_process_block(buffer, BLOCKSIZE, &ctx); } - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - sha256_process_block (buffer, BLOCKSIZE, &ctx); - } +process_partial_block:; - process_partial_block:; + /* Process any remaining bytes. */ + if (sum > 0) + sha256_process_bytes(buffer, sum, &ctx); - /* Process any remaining bytes. */ - if (sum > 0) - sha256_process_bytes (buffer, sum, &ctx); - - /* Construct result in desired memory. */ - sha224_finish_ctx (&ctx, resblock); - return 0; + /* Construct result in desired memory. */ + sha224_finish_ctx(&ctx, resblock); + return 0; } /* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -void * -sha256_buffer (const char *buffer, size_t len, void *resblock) +void *sha256_buffer(const char *buffer, size_t len, void *resblock) { - struct sha256_ctx ctx; + struct sha256_ctx ctx; - /* Initialize the computation context. */ - sha256_init_ctx (&ctx); + /* Initialize the computation context. */ + sha256_init_ctx(&ctx); - /* Process whole buffer but last len % 64 bytes. */ - sha256_process_bytes (buffer, len, &ctx); + /* Process whole buffer but last len % 64 bytes. */ + sha256_process_bytes(buffer, len, &ctx); - /* Put result in desired memory area. */ - return sha256_finish_ctx (&ctx, resblock); + /* Put result in desired memory area. */ + return sha256_finish_ctx(&ctx, resblock); } -void * -sha224_buffer (const char *buffer, size_t len, void *resblock) +void *sha224_buffer(const char *buffer, size_t len, void *resblock) { - struct sha256_ctx ctx; + struct sha256_ctx ctx; - /* Initialize the computation context. */ - sha224_init_ctx (&ctx); + /* Initialize the computation context. */ + sha224_init_ctx(&ctx); - /* Process whole buffer but last len % 64 bytes. */ - sha256_process_bytes (buffer, len, &ctx); + /* Process whole buffer but last len % 64 bytes. */ + sha256_process_bytes(buffer, len, &ctx); - /* Put result in desired memory area. */ - return sha224_finish_ctx (&ctx, resblock); + /* Put result in desired memory area. */ + return sha224_finish_ctx(&ctx, resblock); } void -sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) +sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx) { - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (ctx->buflen != 0) - { - size_t left_over = ctx->buflen; - size_t add = 128 - left_over > len ? len : 128 - left_over; - - memcpy (&((char *) ctx->buffer)[left_over], buffer, add); - ctx->buflen += add; - - if (ctx->buflen > 64) - { - sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx); - - ctx->buflen &= 63; - /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, - &((char *) ctx->buffer)[(left_over + add) & ~63], - ctx->buflen); + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > len ? len : 128 - left_over; + + memcpy(&((char *)ctx->buffer)[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 64) { + sha256_process_block(ctx->buffer, ctx->buflen & ~63, + ctx); + + ctx->buflen &= 63; + /* The regions in the following copy operation cannot overlap. */ + memcpy(ctx->buffer, + &((char *)ctx->buffer)[(left_over + add) & ~63], + ctx->buflen); + } + + buffer = (const char *)buffer + add; + len -= add; } - buffer = (const char *) buffer + add; - len -= add; - } - - /* Process available complete blocks. */ - if (len >= 64) - { + /* Process available complete blocks. */ + if (len >= 64) { #if !_STRING_ARCH_unaligned -# define alignof(type) offsetof (struct { char c; type x; }, x) -# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) - if (UNALIGNED_P (buffer)) - while (len > 64) - { - sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); - buffer = (const char *) buffer + 64; - len -= 64; - } - else +#define alignof(type) offsetof (struct { char c; type x; }, x) +#define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) + if (UNALIGNED_P(buffer)) + while (len > 64) { + sha256_process_block(memcpy + (ctx->buffer, buffer, 64), + 64, ctx); + buffer = (const char *)buffer + 64; + len -= 64; + } else #endif - { - sha256_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; + { + sha256_process_block(buffer, len & ~63, ctx); + buffer = (const char *)buffer + (len & ~63); + len &= 63; + } } - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0) - { - size_t left_over = ctx->buflen; - - memcpy (&((char *) ctx->buffer)[left_over], buffer, len); - left_over += len; - if (left_over >= 64) - { - sha256_process_block (ctx->buffer, 64, ctx); - left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[16], left_over); + + /* Move remaining bytes in internal buffer. */ + if (len > 0) { + size_t left_over = ctx->buflen; + + memcpy(&((char *)ctx->buffer)[left_over], buffer, len); + left_over += len; + if (left_over >= 64) { + sha256_process_block(ctx->buffer, 64, ctx); + left_over -= 64; + memcpy(ctx->buffer, &ctx->buffer[16], left_over); + } + ctx->buflen = left_over; } - ctx->buflen = left_over; - } } /* --- Code below is the primary difference between sha1.c and sha256.c --- */ @@ -398,22 +374,22 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) /* SHA256 round constants */ #define K(I) sha256_round_constants[I] static const uint32_t sha256_round_constants[64] = { - 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, - 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, - 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, - 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, - 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, - 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, - 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, - 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, - 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, - 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, - 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, - 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, - 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, - 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, - 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, - 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, + 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, + 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, + 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, + 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, + 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, + 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, + 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, + 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, + 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, + 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, + 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, + 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, + 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, + 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, + 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, + 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, }; /* Round functions. */ @@ -425,27 +401,27 @@ static const uint32_t sha256_round_constants[64] = { Most of this code comes from GnuPG's cipher/sha1.c. */ void -sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) +sha256_process_block(const void *buffer, size_t len, struct sha256_ctx *ctx) { - const uint32_t *words = buffer; - size_t nwords = len / sizeof (uint32_t); - const uint32_t *endp = words + nwords; - uint32_t x[16]; - uint32_t a = ctx->state[0]; - uint32_t b = ctx->state[1]; - uint32_t c = ctx->state[2]; - uint32_t d = ctx->state[3]; - uint32_t e = ctx->state[4]; - uint32_t f = ctx->state[5]; - uint32_t g = ctx->state[6]; - uint32_t h = ctx->state[7]; - - /* First increment the byte count. FIPS PUB 180-2 specifies the possible - length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; + const uint32_t *words = buffer; + size_t nwords = len / sizeof(uint32_t); + const uint32_t *endp = words + nwords; + uint32_t x[16]; + uint32_t a = ctx->state[0]; + uint32_t b = ctx->state[1]; + uint32_t c = ctx->state[2]; + uint32_t d = ctx->state[3]; + uint32_t e = ctx->state[4]; + uint32_t f = ctx->state[5]; + uint32_t g = ctx->state[6]; + uint32_t h = ctx->state[7]; + + /* First increment the byte count. FIPS PUB 180-2 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; #define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define S0(x) (rol(x,25)^rol(x,14)^(x>>3)) @@ -465,90 +441,88 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) D += t1; H = t0 + t1; \ } while(0) - while (words < endp) - { - uint32_t tm; - uint32_t t0, t1; - int t; - /* FIXME: see sha1.c for a better implementation. */ - for (t = 0; t < 16; t++) - { - x[t] = SWAP (*words); - words++; + while (words < endp) { + uint32_t tm; + uint32_t t0, t1; + int t; + /* FIXME: see sha1.c for a better implementation. */ + for (t = 0; t < 16; t++) { + x[t] = SWAP(*words); + words++; + } + + R(a, b, c, d, e, f, g, h, K(0), x[0]); + R(h, a, b, c, d, e, f, g, K(1), x[1]); + R(g, h, a, b, c, d, e, f, K(2), x[2]); + R(f, g, h, a, b, c, d, e, K(3), x[3]); + R(e, f, g, h, a, b, c, d, K(4), x[4]); + R(d, e, f, g, h, a, b, c, K(5), x[5]); + R(c, d, e, f, g, h, a, b, K(6), x[6]); + R(b, c, d, e, f, g, h, a, K(7), x[7]); + R(a, b, c, d, e, f, g, h, K(8), x[8]); + R(h, a, b, c, d, e, f, g, K(9), x[9]); + R(g, h, a, b, c, d, e, f, K(10), x[10]); + R(f, g, h, a, b, c, d, e, K(11), x[11]); + R(e, f, g, h, a, b, c, d, K(12), x[12]); + R(d, e, f, g, h, a, b, c, K(13), x[13]); + R(c, d, e, f, g, h, a, b, K(14), x[14]); + R(b, c, d, e, f, g, h, a, K(15), x[15]); + R(a, b, c, d, e, f, g, h, K(16), M(16)); + R(h, a, b, c, d, e, f, g, K(17), M(17)); + R(g, h, a, b, c, d, e, f, K(18), M(18)); + R(f, g, h, a, b, c, d, e, K(19), M(19)); + R(e, f, g, h, a, b, c, d, K(20), M(20)); + R(d, e, f, g, h, a, b, c, K(21), M(21)); + R(c, d, e, f, g, h, a, b, K(22), M(22)); + R(b, c, d, e, f, g, h, a, K(23), M(23)); + R(a, b, c, d, e, f, g, h, K(24), M(24)); + R(h, a, b, c, d, e, f, g, K(25), M(25)); + R(g, h, a, b, c, d, e, f, K(26), M(26)); + R(f, g, h, a, b, c, d, e, K(27), M(27)); + R(e, f, g, h, a, b, c, d, K(28), M(28)); + R(d, e, f, g, h, a, b, c, K(29), M(29)); + R(c, d, e, f, g, h, a, b, K(30), M(30)); + R(b, c, d, e, f, g, h, a, K(31), M(31)); + R(a, b, c, d, e, f, g, h, K(32), M(32)); + R(h, a, b, c, d, e, f, g, K(33), M(33)); + R(g, h, a, b, c, d, e, f, K(34), M(34)); + R(f, g, h, a, b, c, d, e, K(35), M(35)); + R(e, f, g, h, a, b, c, d, K(36), M(36)); + R(d, e, f, g, h, a, b, c, K(37), M(37)); + R(c, d, e, f, g, h, a, b, K(38), M(38)); + R(b, c, d, e, f, g, h, a, K(39), M(39)); + R(a, b, c, d, e, f, g, h, K(40), M(40)); + R(h, a, b, c, d, e, f, g, K(41), M(41)); + R(g, h, a, b, c, d, e, f, K(42), M(42)); + R(f, g, h, a, b, c, d, e, K(43), M(43)); + R(e, f, g, h, a, b, c, d, K(44), M(44)); + R(d, e, f, g, h, a, b, c, K(45), M(45)); + R(c, d, e, f, g, h, a, b, K(46), M(46)); + R(b, c, d, e, f, g, h, a, K(47), M(47)); + R(a, b, c, d, e, f, g, h, K(48), M(48)); + R(h, a, b, c, d, e, f, g, K(49), M(49)); + R(g, h, a, b, c, d, e, f, K(50), M(50)); + R(f, g, h, a, b, c, d, e, K(51), M(51)); + R(e, f, g, h, a, b, c, d, K(52), M(52)); + R(d, e, f, g, h, a, b, c, K(53), M(53)); + R(c, d, e, f, g, h, a, b, K(54), M(54)); + R(b, c, d, e, f, g, h, a, K(55), M(55)); + R(a, b, c, d, e, f, g, h, K(56), M(56)); + R(h, a, b, c, d, e, f, g, K(57), M(57)); + R(g, h, a, b, c, d, e, f, K(58), M(58)); + R(f, g, h, a, b, c, d, e, K(59), M(59)); + R(e, f, g, h, a, b, c, d, K(60), M(60)); + R(d, e, f, g, h, a, b, c, K(61), M(61)); + R(c, d, e, f, g, h, a, b, K(62), M(62)); + R(b, c, d, e, f, g, h, a, K(63), M(63)); + + a = ctx->state[0] += a; + b = ctx->state[1] += b; + c = ctx->state[2] += c; + d = ctx->state[3] += d; + e = ctx->state[4] += e; + f = ctx->state[5] += f; + g = ctx->state[6] += g; + h = ctx->state[7] += h; } - - R( a, b, c, d, e, f, g, h, K( 0), x[ 0] ); - R( h, a, b, c, d, e, f, g, K( 1), x[ 1] ); - R( g, h, a, b, c, d, e, f, K( 2), x[ 2] ); - R( f, g, h, a, b, c, d, e, K( 3), x[ 3] ); - R( e, f, g, h, a, b, c, d, K( 4), x[ 4] ); - R( d, e, f, g, h, a, b, c, K( 5), x[ 5] ); - R( c, d, e, f, g, h, a, b, K( 6), x[ 6] ); - R( b, c, d, e, f, g, h, a, K( 7), x[ 7] ); - R( a, b, c, d, e, f, g, h, K( 8), x[ 8] ); - R( h, a, b, c, d, e, f, g, K( 9), x[ 9] ); - R( g, h, a, b, c, d, e, f, K(10), x[10] ); - R( f, g, h, a, b, c, d, e, K(11), x[11] ); - R( e, f, g, h, a, b, c, d, K(12), x[12] ); - R( d, e, f, g, h, a, b, c, K(13), x[13] ); - R( c, d, e, f, g, h, a, b, K(14), x[14] ); - R( b, c, d, e, f, g, h, a, K(15), x[15] ); - R( a, b, c, d, e, f, g, h, K(16), M(16) ); - R( h, a, b, c, d, e, f, g, K(17), M(17) ); - R( g, h, a, b, c, d, e, f, K(18), M(18) ); - R( f, g, h, a, b, c, d, e, K(19), M(19) ); - R( e, f, g, h, a, b, c, d, K(20), M(20) ); - R( d, e, f, g, h, a, b, c, K(21), M(21) ); - R( c, d, e, f, g, h, a, b, K(22), M(22) ); - R( b, c, d, e, f, g, h, a, K(23), M(23) ); - R( a, b, c, d, e, f, g, h, K(24), M(24) ); - R( h, a, b, c, d, e, f, g, K(25), M(25) ); - R( g, h, a, b, c, d, e, f, K(26), M(26) ); - R( f, g, h, a, b, c, d, e, K(27), M(27) ); - R( e, f, g, h, a, b, c, d, K(28), M(28) ); - R( d, e, f, g, h, a, b, c, K(29), M(29) ); - R( c, d, e, f, g, h, a, b, K(30), M(30) ); - R( b, c, d, e, f, g, h, a, K(31), M(31) ); - R( a, b, c, d, e, f, g, h, K(32), M(32) ); - R( h, a, b, c, d, e, f, g, K(33), M(33) ); - R( g, h, a, b, c, d, e, f, K(34), M(34) ); - R( f, g, h, a, b, c, d, e, K(35), M(35) ); - R( e, f, g, h, a, b, c, d, K(36), M(36) ); - R( d, e, f, g, h, a, b, c, K(37), M(37) ); - R( c, d, e, f, g, h, a, b, K(38), M(38) ); - R( b, c, d, e, f, g, h, a, K(39), M(39) ); - R( a, b, c, d, e, f, g, h, K(40), M(40) ); - R( h, a, b, c, d, e, f, g, K(41), M(41) ); - R( g, h, a, b, c, d, e, f, K(42), M(42) ); - R( f, g, h, a, b, c, d, e, K(43), M(43) ); - R( e, f, g, h, a, b, c, d, K(44), M(44) ); - R( d, e, f, g, h, a, b, c, K(45), M(45) ); - R( c, d, e, f, g, h, a, b, K(46), M(46) ); - R( b, c, d, e, f, g, h, a, K(47), M(47) ); - R( a, b, c, d, e, f, g, h, K(48), M(48) ); - R( h, a, b, c, d, e, f, g, K(49), M(49) ); - R( g, h, a, b, c, d, e, f, K(50), M(50) ); - R( f, g, h, a, b, c, d, e, K(51), M(51) ); - R( e, f, g, h, a, b, c, d, K(52), M(52) ); - R( d, e, f, g, h, a, b, c, K(53), M(53) ); - R( c, d, e, f, g, h, a, b, K(54), M(54) ); - R( b, c, d, e, f, g, h, a, K(55), M(55) ); - R( a, b, c, d, e, f, g, h, K(56), M(56) ); - R( h, a, b, c, d, e, f, g, K(57), M(57) ); - R( g, h, a, b, c, d, e, f, K(58), M(58) ); - R( f, g, h, a, b, c, d, e, K(59), M(59) ); - R( e, f, g, h, a, b, c, d, K(60), M(60) ); - R( d, e, f, g, h, a, b, c, K(61), M(61) ); - R( c, d, e, f, g, h, a, b, K(62), M(62) ); - R( b, c, d, e, f, g, h, a, K(63), M(63) ); - - a = ctx->state[0] += a; - b = ctx->state[1] += b; - c = ctx->state[2] += c; - d = ctx->state[3] += d; - e = ctx->state[4] += e; - f = ctx->state[5] += f; - g = ctx->state[6] += g; - h = ctx->state[7] += h; - } } diff --git a/libopkg/sha256.h b/libopkg/sha256.h index 6a9aed4..c49d31d 100644 --- a/libopkg/sha256.h +++ b/libopkg/sha256.h @@ -16,76 +16,76 @@ along with this program. If not, see . */ #ifndef SHA256_H -# define SHA256_H 1 +#define SHA256_H 1 -# include -# include +#include +#include -# ifdef __cplusplus +#ifdef __cplusplus extern "C" { -# endif +#endif /* Structure to save state of computation between the single steps. */ -struct sha256_ctx -{ - uint32_t state[8]; + struct sha256_ctx { + uint32_t state[8]; - uint32_t total[2]; - size_t buflen; - uint32_t buffer[32]; -}; + uint32_t total[2]; + size_t buflen; + uint32_t buffer[32]; + }; -enum { SHA224_DIGEST_SIZE = 224 / 8 }; -enum { SHA256_DIGEST_SIZE = 256 / 8 }; + enum { SHA224_DIGEST_SIZE = 224 / 8 }; + enum { SHA256_DIGEST_SIZE = 256 / 8 }; /* Initialize structure containing state of computation. */ -extern void sha256_init_ctx (struct sha256_ctx *ctx); -extern void sha224_init_ctx (struct sha256_ctx *ctx); + extern void sha256_init_ctx(struct sha256_ctx *ctx); + extern void sha224_init_ctx(struct sha256_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 64!!! */ -extern void sha256_process_block (const void *buffer, size_t len, - struct sha256_ctx *ctx); + extern void sha256_process_block(const void *buffer, size_t len, + struct sha256_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void sha256_process_bytes (const void *buffer, size_t len, - struct sha256_ctx *ctx); + extern void sha256_process_bytes(const void *buffer, size_t len, + struct sha256_ctx *ctx); /* Process the remaining bytes in the buffer and put result from CTX in first 32 (28) bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf); -extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf); - + extern void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf); + extern void *sha224_finish_ctx(struct sha256_ctx *ctx, void *resbuf); /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf); -extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf); - + extern void *sha256_read_ctx(const struct sha256_ctx *ctx, + void *resbuf); + extern void *sha224_read_ctx(const struct sha256_ctx *ctx, + void *resbuf); /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The resulting message digest number will be written into the 32 (28) bytes beginning at RESBLOCK. */ -extern int sha256_stream (FILE *stream, void *resblock); -extern int sha224_stream (FILE *stream, void *resblock); + extern int sha256_stream(FILE * stream, void *resblock); + extern int sha224_stream(FILE * stream, void *resblock); /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha256_buffer (const char *buffer, size_t len, void *resblock); -extern void *sha224_buffer (const char *buffer, size_t len, void *resblock); + extern void *sha256_buffer(const char *buffer, size_t len, + void *resblock); + extern void *sha224_buffer(const char *buffer, size_t len, + void *resblock); -# ifdef __cplusplus +#ifdef __cplusplus } -# endif - +#endif #endif diff --git a/libopkg/sprintf_alloc.c b/libopkg/sprintf_alloc.c index aef8e06..82a8471 100644 --- a/libopkg/sprintf_alloc.c +++ b/libopkg/sprintf_alloc.c @@ -18,8 +18,7 @@ #include "sprintf_alloc.h" #include "libbb/libbb.h" -void -sprintf_alloc(char **str, const char *fmt, ...) +void sprintf_alloc(char **str, const char *fmt, ...) { va_list ap; int n; @@ -29,13 +28,12 @@ sprintf_alloc(char **str, const char *fmt, ...) for (;;) { va_start(ap, fmt); - n = vsnprintf (*str, size, fmt, ap); + n = vsnprintf(*str, size, fmt, ap); va_end(ap); if (n < 0) { fprintf(stderr, "%s: encountered an output or encoding" - " error during vsnprintf.\n", - __FUNCTION__); + " error during vsnprintf.\n", __FUNCTION__); exit(EXIT_FAILURE); } @@ -43,7 +41,7 @@ sprintf_alloc(char **str, const char *fmt, ...) break; /* Truncated, try again with more space. */ - size = n+1; + size = n + 1; *str = xrealloc(*str, size); } } diff --git a/libopkg/str_list.c b/libopkg/str_list.c index d3a0179..58a00a5 100644 --- a/libopkg/str_list.c +++ b/libopkg/str_list.c @@ -18,95 +18,101 @@ #include "str_list.h" #include "libbb/libbb.h" -void str_list_elt_init(str_list_elt_t *elt, char *data) +void str_list_elt_init(str_list_elt_t * elt, char *data) { - void_list_elt_init((void_list_elt_t *) elt, data); + void_list_elt_init((void_list_elt_t *) elt, data); } -void str_list_elt_deinit(str_list_elt_t *elt) +void str_list_elt_deinit(str_list_elt_t * elt) { - if (elt->data) - free(elt->data); - void_list_elt_deinit((void_list_elt_t *) elt); + if (elt->data) + free(elt->data); + void_list_elt_deinit((void_list_elt_t *) elt); } str_list_t *str_list_alloc() { - str_list_t *list = xcalloc(1, sizeof(str_list_t)); - str_list_init(list); - return list; + str_list_t *list = xcalloc(1, sizeof(str_list_t)); + str_list_init(list); + return list; } -void str_list_init(str_list_t *list) +void str_list_init(str_list_t * list) { - void_list_init((void_list_t *) list); + void_list_init((void_list_t *) list); } -void str_list_deinit(str_list_t *list) +void str_list_deinit(str_list_t * list) { - str_list_elt_t *elt; - while (!void_list_empty(list)) { - elt = str_list_first(list); - if (!elt) - return; - list_del_init(&elt->node); - free(elt->data); - elt->data=NULL; - free(elt); - } + str_list_elt_t *elt; + while (!void_list_empty(list)) { + elt = str_list_first(list); + if (!elt) + return; + list_del_init(&elt->node); + free(elt->data); + elt->data = NULL; + free(elt); + } } -void str_list_append(str_list_t *list, char *data) +void str_list_append(str_list_t * list, char *data) { - void_list_append((void_list_t *) list, xstrdup(data)); + void_list_append((void_list_t *) list, xstrdup(data)); } -void str_list_push(str_list_t *list, char *data) +void str_list_push(str_list_t * list, char *data) { - void_list_push((void_list_t *) list, xstrdup(data)); + void_list_push((void_list_t *) list, xstrdup(data)); } -str_list_elt_t *str_list_pop(str_list_t *list) +str_list_elt_t *str_list_pop(str_list_t * list) { - return (str_list_elt_t *) void_list_pop((void_list_t *) list); + return (str_list_elt_t *) void_list_pop((void_list_t *) list); } -void str_list_remove(str_list_t *list, str_list_elt_t **iter) +void str_list_remove(str_list_t * list, str_list_elt_t ** iter) { - char *str = void_list_remove((void_list_t *) list, - (void_list_elt_t **) iter); + char *str = void_list_remove((void_list_t *) list, + (void_list_elt_t **) iter); - if (str) - free(str); + if (str) + free(str); } -void str_list_remove_elt(str_list_t *list, const char *target_str) +void str_list_remove_elt(str_list_t * list, const char *target_str) { - char *str = void_list_remove_elt((void_list_t *) list, + char *str = void_list_remove_elt((void_list_t *) list, (void *)target_str, - (void_list_cmp_t)strcmp); - if (str) - free(str); + (void_list_cmp_t) strcmp); + if (str) + free(str); } -str_list_elt_t *str_list_first(str_list_t *list) { - return (str_list_elt_t * )void_list_first((void_list_t *) list); +str_list_elt_t *str_list_first(str_list_t * list) +{ + return (str_list_elt_t *) void_list_first((void_list_t *) list); } -str_list_elt_t *str_list_prev(str_list_t *list, str_list_elt_t *node) { - return (str_list_elt_t * )void_list_prev((void_list_t *) list, (void_list_elt_t *)node); +str_list_elt_t *str_list_prev(str_list_t * list, str_list_elt_t * node) +{ + return (str_list_elt_t *) void_list_prev((void_list_t *) list, + (void_list_elt_t *) node); } -str_list_elt_t *str_list_next(str_list_t *list, str_list_elt_t *node) { - return (str_list_elt_t * )void_list_next((void_list_t *) list, (void_list_elt_t *)node); +str_list_elt_t *str_list_next(str_list_t * list, str_list_elt_t * node) +{ + return (str_list_elt_t *) void_list_next((void_list_t *) list, + (void_list_elt_t *) node); } -str_list_elt_t *str_list_last(str_list_t *list) { - return (str_list_elt_t * )void_list_last((void_list_t *) list); +str_list_elt_t *str_list_last(str_list_t * list) +{ + return (str_list_elt_t *) void_list_last((void_list_t *) list); } - -void str_list_purge(str_list_t *list) { - str_list_deinit(list); - free(list); +void str_list_purge(str_list_t * list) +{ + str_list_deinit(list); + free(list); } diff --git a/libopkg/str_list.h b/libopkg/str_list.h index 3690820..68328a7 100644 --- a/libopkg/str_list.h +++ b/libopkg/str_list.h @@ -24,24 +24,24 @@ typedef struct void_list_elt str_list_elt_t; typedef struct void_list str_list_t; -void str_list_elt_init(str_list_elt_t *elt, char *data); -void str_list_elt_deinit(str_list_elt_t *elt); +void str_list_elt_init(str_list_elt_t * elt, char *data); +void str_list_elt_deinit(str_list_elt_t * elt); str_list_t *str_list_alloc(void); -void str_list_init(str_list_t *list); -void str_list_deinit(str_list_t *list); +void str_list_init(str_list_t * list); +void str_list_deinit(str_list_t * list); -void str_list_append(str_list_t *list, char *data); -void str_list_push(str_list_t *list, char *data); -str_list_elt_t *str_list_pop(str_list_t *list); -void str_list_remove(str_list_t *list, str_list_elt_t **iter); -void str_list_remove_elt(str_list_t *list, const char *target_str); +void str_list_append(str_list_t * list, char *data); +void str_list_push(str_list_t * list, char *data); +str_list_elt_t *str_list_pop(str_list_t * list); +void str_list_remove(str_list_t * list, str_list_elt_t ** iter); +void str_list_remove_elt(str_list_t * list, const char *target_str); -str_list_elt_t *str_list_first(str_list_t *list); -str_list_elt_t *str_list_prev(str_list_t *list, str_list_elt_t *node); -str_list_elt_t *str_list_next(str_list_t *list, str_list_elt_t *node); -str_list_elt_t *str_list_last(str_list_t *list); +str_list_elt_t *str_list_first(str_list_t * list); +str_list_elt_t *str_list_prev(str_list_t * list, str_list_elt_t * node); +str_list_elt_t *str_list_next(str_list_t * list, str_list_elt_t * node); +str_list_elt_t *str_list_last(str_list_t * list); -void str_list_purge(str_list_t *list); +void str_list_purge(str_list_t * list); #endif diff --git a/libopkg/void_list.c b/libopkg/void_list.c index 4c9d897..e8fa808 100644 --- a/libopkg/void_list.c +++ b/libopkg/void_list.c @@ -18,148 +18,153 @@ #include "void_list.h" #include "libbb/libbb.h" -void void_list_elt_init(void_list_elt_t *elt, void *data) +void void_list_elt_init(void_list_elt_t * elt, void *data) { - INIT_LIST_HEAD(&elt->node); - elt->data = data; + INIT_LIST_HEAD(&elt->node); + elt->data = data; } -static void_list_elt_t * void_list_elt_new (void *data) { - void_list_elt_t *elt; - /* freed in void_list_elt_deinit */ - elt = xcalloc(1, sizeof(void_list_elt_t)); - void_list_elt_init(elt, data); - return elt; +static void_list_elt_t *void_list_elt_new(void *data) +{ + void_list_elt_t *elt; + /* freed in void_list_elt_deinit */ + elt = xcalloc(1, sizeof(void_list_elt_t)); + void_list_elt_init(elt, data); + return elt; } -void void_list_elt_deinit(void_list_elt_t *elt) +void void_list_elt_deinit(void_list_elt_t * elt) { - list_del_init(&elt->node); - void_list_elt_init(elt, NULL); - free(elt); + list_del_init(&elt->node); + void_list_elt_init(elt, NULL); + free(elt); } -void void_list_init(void_list_t *list) +void void_list_init(void_list_t * list) { - INIT_LIST_HEAD(&list->head); + INIT_LIST_HEAD(&list->head); } -void void_list_deinit(void_list_t *list) +void void_list_deinit(void_list_t * list) { - void_list_elt_t *elt; + void_list_elt_t *elt; - while (!void_list_empty(list)) { - elt = void_list_pop(list); - void_list_elt_deinit(elt); - } - INIT_LIST_HEAD(&list->head); + while (!void_list_empty(list)) { + elt = void_list_pop(list); + void_list_elt_deinit(elt); + } + INIT_LIST_HEAD(&list->head); } -void void_list_append(void_list_t *list, void *data) +void void_list_append(void_list_t * list, void *data) { - void_list_elt_t *elt = void_list_elt_new(data); - list_add_tail(&elt->node, &list->head); + void_list_elt_t *elt = void_list_elt_new(data); + list_add_tail(&elt->node, &list->head); } -void void_list_push(void_list_t *list, void *data) +void void_list_push(void_list_t * list, void *data) { - void_list_elt_t *elt = void_list_elt_new(data); - list_add(&elt->node, &list->head); + void_list_elt_t *elt = void_list_elt_new(data); + list_add(&elt->node, &list->head); } -void_list_elt_t *void_list_pop(void_list_t *list) +void_list_elt_t *void_list_pop(void_list_t * list) { - struct list_head *node; + struct list_head *node; - if (void_list_empty(list)) - return NULL; - node = list->head.next; - list_del_init(node); - return list_entry(node, void_list_elt_t, node); + if (void_list_empty(list)) + return NULL; + node = list->head.next; + list_del_init(node); + return list_entry(node, void_list_elt_t, node); } -void *void_list_remove(void_list_t *list, void_list_elt_t **iter) +void *void_list_remove(void_list_t * list, void_list_elt_t ** iter) { - void_list_elt_t *pos, *n; - void_list_elt_t *old_elt; - void *old_data = NULL; - - old_elt = *iter; - if (!old_elt) - return old_data; - old_data = old_elt->data; - - list_for_each_entry_safe(pos, n, &list->head, node) { - if (pos == old_elt) - break; - } - if ( pos != old_elt) { - opkg_msg(ERROR, "Internal error: element not found in list.\n"); - return NULL; - } - - *iter = list_entry(pos->node.prev, void_list_elt_t, node); - void_list_elt_deinit(pos); - - return old_data; + void_list_elt_t *pos, *n; + void_list_elt_t *old_elt; + void *old_data = NULL; + + old_elt = *iter; + if (!old_elt) + return old_data; + old_data = old_elt->data; + + list_for_each_entry_safe(pos, n, &list->head, node) { + if (pos == old_elt) + break; + } + if (pos != old_elt) { + opkg_msg(ERROR, "Internal error: element not found in list.\n"); + return NULL; + } + + *iter = list_entry(pos->node.prev, void_list_elt_t, node); + void_list_elt_deinit(pos); + + return old_data; } /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */ -void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp) +void *void_list_remove_elt(void_list_t * list, const void *target_data, + void_list_cmp_t cmp) { - void_list_elt_t *pos, *n; - void *old_data = NULL; - list_for_each_entry_safe(pos, n, &list->head, node) { - if ( pos->data && cmp(pos->data, target_data)==0 ){ - old_data = pos->data; - void_list_elt_deinit(pos); - break; - } - } - return old_data; + void_list_elt_t *pos, *n; + void *old_data = NULL; + list_for_each_entry_safe(pos, n, &list->head, node) { + if (pos->data && cmp(pos->data, target_data) == 0) { + old_data = pos->data; + void_list_elt_deinit(pos); + break; + } + } + return old_data; } -void_list_elt_t *void_list_first(void_list_t *list) { - struct list_head *elt; - if (!list) - return NULL; - elt = list->head.next; - if (elt == &list->head ) { - return NULL; - } - return list_entry(elt, void_list_elt_t, node); +void_list_elt_t *void_list_first(void_list_t * list) +{ + struct list_head *elt; + if (!list) + return NULL; + elt = list->head.next; + if (elt == &list->head) { + return NULL; + } + return list_entry(elt, void_list_elt_t, node); } -void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node) { - struct list_head *elt; - if (!list || !node) - return NULL; - elt = node->node.prev; - if (elt == &list->head ) { - return NULL; - } - return list_entry(elt, void_list_elt_t, node); +void_list_elt_t *void_list_prev(void_list_t * list, void_list_elt_t * node) +{ + struct list_head *elt; + if (!list || !node) + return NULL; + elt = node->node.prev; + if (elt == &list->head) { + return NULL; + } + return list_entry(elt, void_list_elt_t, node); } -void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node) { - struct list_head *elt; - if (!list || !node) - return NULL; - elt = node->node.next; - if (elt == &list->head ) { - return NULL; - } - return list_entry(elt, void_list_elt_t, node); +void_list_elt_t *void_list_next(void_list_t * list, void_list_elt_t * node) +{ + struct list_head *elt; + if (!list || !node) + return NULL; + elt = node->node.next; + if (elt == &list->head) { + return NULL; + } + return list_entry(elt, void_list_elt_t, node); } -void_list_elt_t *void_list_last(void_list_t *list) { - struct list_head *elt; - if (!list) - return NULL; - elt = list->head.prev; - if (elt == &list->head ) { - return NULL; - } - return list_entry(elt, void_list_elt_t, node); +void_list_elt_t *void_list_last(void_list_t * list) +{ + struct list_head *elt; + if (!list) + return NULL; + elt = list->head.prev; + if (elt == &list->head) { + return NULL; + } + return list_entry(elt, void_list_elt_t, node); } - diff --git a/libopkg/void_list.h b/libopkg/void_list.h index b63a68d..3e900bf 100644 --- a/libopkg/void_list.h +++ b/libopkg/void_list.h @@ -21,44 +21,42 @@ #include "list.h" typedef struct void_list_elt void_list_elt_t; -struct void_list_elt -{ - struct list_head node; - void *data; +struct void_list_elt { + struct list_head node; + void *data; }; typedef struct void_list void_list_t; -struct void_list -{ - struct list_head head; +struct void_list { + struct list_head head; }; -static inline int void_list_empty(void_list_t *list) +static inline int void_list_empty(void_list_t * list) { - return list_empty(&list->head); + return list_empty(&list->head); } -void void_list_elt_init(void_list_elt_t *elt, void *data); -void void_list_elt_deinit(void_list_elt_t *elt); +void void_list_elt_init(void_list_elt_t * elt, void *data); +void void_list_elt_deinit(void_list_elt_t * elt); -void void_list_init(void_list_t *list); -void void_list_deinit(void_list_t *list); +void void_list_init(void_list_t * list); +void void_list_deinit(void_list_t * list); -void void_list_append(void_list_t *list, void *data); -void void_list_push(void_list_t *list, void *data); -void_list_elt_t *void_list_pop(void_list_t *list); +void void_list_append(void_list_t * list, void *data); +void void_list_push(void_list_t * list, void *data); +void_list_elt_t *void_list_pop(void_list_t * list); -void *void_list_remove(void_list_t *list, void_list_elt_t **iter); +void *void_list_remove(void_list_t * list, void_list_elt_t ** iter); /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */ -typedef int (*void_list_cmp_t)(const void *, const void *); -void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp); - -void_list_elt_t *void_list_first(void_list_t *list); -void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node); -void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node); -void_list_elt_t *void_list_last(void_list_t *list); +typedef int (*void_list_cmp_t) (const void *, const void *); +void *void_list_remove_elt(void_list_t * list, const void *target_data, + void_list_cmp_t cmp); -void void_list_purge(void_list_t *list); +void_list_elt_t *void_list_first(void_list_t * list); +void_list_elt_t *void_list_prev(void_list_t * list, void_list_elt_t * node); +void_list_elt_t *void_list_next(void_list_t * list, void_list_elt_t * node); +void_list_elt_t *void_list_last(void_list_t * list); +void void_list_purge(void_list_t * list); #endif diff --git a/libopkg/xregex.c b/libopkg/xregex.c index f682d4c..1c67d30 100644 --- a/libopkg/xregex.c +++ b/libopkg/xregex.c @@ -18,29 +18,29 @@ #include "xregex.h" #include "libbb/libbb.h" -static void print_regcomp_err(const regex_t *preg, int err); +static void print_regcomp_err(const regex_t * preg, int err); -int xregcomp(regex_t *preg, const char *regex, int cflags) +int xregcomp(regex_t * preg, const char *regex, int cflags) { - int err; - err = regcomp(preg, regex, cflags); - if (err) { - print_regcomp_err(preg, err); - } + int err; + err = regcomp(preg, regex, cflags); + if (err) { + print_regcomp_err(preg, err); + } - return err; + return err; } -static void print_regcomp_err(const regex_t *preg, int err) +static void print_regcomp_err(const regex_t * preg, int err) { - unsigned int size; - char *error; + unsigned int size; + char *error; - size = regerror(err, preg, 0, 0); - error = xcalloc(1, size); - regerror(err, preg, error, size); + size = regerror(err, preg, 0, 0); + error = xcalloc(1, size); + regerror(err, preg, error, size); - opkg_msg(ERROR, "Internal error compiling regex: %s.", error); + opkg_msg(ERROR, "Internal error compiling regex: %s.", error); - free(error); + free(error); } diff --git a/libopkg/xregex.h b/libopkg/xregex.h index f67572b..7d92d43 100644 --- a/libopkg/xregex.h +++ b/libopkg/xregex.h @@ -21,11 +21,10 @@ #include #include -int xregcomp(regex_t *preg, const char *regex, int cflags); -static inline void xregfree(regex_t *preg) +int xregcomp(regex_t * preg, const char *regex, int cflags); +static inline void xregfree(regex_t * preg) { - regfree(preg); + regfree(preg); } - #endif diff --git a/libopkg/xsystem.c b/libopkg/xsystem.c index ae7ca87..b8ada59 100644 --- a/libopkg/xsystem.c +++ b/libopkg/xsystem.c @@ -30,8 +30,7 @@ Otherwise, the 8-bit return value of the program ala WEXITSTATUS as defined in . */ -int -xsystem(const char *argv[]) +int xsystem(const char *argv[]) { int status; pid_t pid; @@ -44,7 +43,7 @@ xsystem(const char *argv[]) return -1; case 0: /* child */ - execvp(argv[0], (char*const*)argv); + execvp(argv[0], (char *const *)argv); _exit(-1); default: /* parent */ @@ -58,14 +57,14 @@ xsystem(const char *argv[]) if (WIFSIGNALED(status)) { opkg_msg(ERROR, "%s: Child killed by signal %d.\n", - argv[0], WTERMSIG(status)); + argv[0], WTERMSIG(status)); return -1; } if (!WIFEXITED(status)) { /* shouldn't happen */ opkg_msg(ERROR, "%s: Your system is broken: got status %d " - "from waitpid.\n", argv[0], status); + "from waitpid.\n", argv[0], status); return -1; } diff --git a/libopkg/xsystem.h b/libopkg/xsystem.h index 042efad..ff7ea1e 100644 --- a/libopkg/xsystem.h +++ b/libopkg/xsystem.h @@ -29,4 +29,3 @@ int xsystem(const char *argv[]); #endif - diff --git a/src/opkg-cl.c b/src/opkg-cl.c index 8a39578..e0a4b23 100644 --- a/src/opkg-cl.c +++ b/src/opkg-cl.c @@ -62,8 +62,8 @@ static struct option long_options[] = { {"conf-file", 1, 0, 'f'}, {"conf", 1, 0, 'f'}, {"dest", 1, 0, 'd'}, - {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER}, - {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER}, + {"force-maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER}, + {"force_maintainer", 0, 0, ARGS_OPT_FORCE_MAINTAINER}, {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS}, {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS}, {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE}, @@ -76,13 +76,13 @@ static struct option long_options[] = { {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE}, {"recursive", 0, 0, ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES}, {"force-removal-of-dependent-packages", 0, 0, - ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES}, + ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES}, {"force_removal_of_dependent_packages", 0, 0, - ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES}, + ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES}, {"force-removal-of-essential-packages", 0, 0, - ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES}, + ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES}, {"force_removal_of_essential_packages", 0, 0, - ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES}, + ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES}, {"force-postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL}, {"force_postinstall", 0, 0, ARGS_OPT_FORCE_POSTINSTALL}, {"force-remove", 0, 0, ARGS_OPT_FORCE_REMOVE}, @@ -110,8 +110,7 @@ static struct option long_options[] = { {0, 0, 0, 0} }; -static int -args_parse(int argc, char *argv[]) +static int args_parse(int argc, char *argv[]) { int c; int option_index = 0; @@ -120,7 +119,7 @@ args_parse(int argc, char *argv[]) while (1) { c = getopt_long_only(argc, argv, "Ad:f:ino:p:l:t:vV::", - long_options, &option_index); + long_options, &option_index); if (c == -1) break; @@ -206,10 +205,11 @@ args_parse(int argc, char *argv[]) if ((targ = strchr(tuple, ':')) != NULL) { *targ++ = 0; if ((strlen(tuple) > 0) && (strlen(targ) > 0)) { - nv_pair_list_append( - (c == ARGS_OPT_ADD_ARCH) - ? &conf->arch_list : &conf->tmp_dest_list, - tuple, targ); + nv_pair_list_append((c == + ARGS_OPT_ADD_ARCH) + ? &conf->arch_list : + &conf->tmp_dest_list, + tuple, targ); } } free(tuple); @@ -220,7 +220,7 @@ args_parse(int argc, char *argv[]) case ARGS_OPT_NOACTION: conf->noaction = 1; break; - case ARGS_OPT_DOWNLOAD_ONLY: + case ARGS_OPT_DOWNLOAD_ONLY: conf->download_only = 1; break; case ARGS_OPT_FORCE_SIGNATURE: @@ -237,7 +237,7 @@ args_parse(int argc, char *argv[]) } } - if(!conf->conf_file && !conf->offline_root) + if (!conf->conf_file && !conf->offline_root) conf->conf_file = xstrdup("/etc/opkg.conf"); if (parse_err) @@ -246,35 +246,42 @@ args_parse(int argc, char *argv[]) return optind; } -static void -usage() +static void usage() { printf("usage: opkg [options...] sub-command [arguments...]\n"); printf("where sub-command is one of:\n"); printf("\nPackage Manipulation:\n"); - printf("\tupdate Update list of available packages\n"); + printf + ("\tupdate Update list of available packages\n"); printf("\tupgrade Upgrade packages\n"); printf("\tinstall Install package(s)\n"); printf("\tconfigure Configure unpacked package(s)\n"); printf("\tremove Remove package(s)\n"); printf("\tflag Flag package(s)\n"); - printf("\t =hold|noprune|user|ok|installed|unpacked (one per invocation)\n"); + printf + ("\t =hold|noprune|user|ok|installed|unpacked (one per invocation)\n"); printf("\nInformational Commands:\n"); printf("\tlist List available packages\n"); printf("\tlist-installed List installed packages\n"); - printf("\tlist-upgradable List installed and upgradable packages\n"); - printf("\tlist-changed-conffiles List user modified configuration files\n"); + printf + ("\tlist-upgradable List installed and upgradable packages\n"); + printf + ("\tlist-changed-conffiles List user modified configuration files\n"); printf("\tfiles List files belonging to \n"); printf("\tsearch List package providing \n"); - printf("\tfind List packages whose name or description matches \n"); + printf + ("\tfind List packages whose name or description matches \n"); printf("\tinfo [pkg|regexp] Display all info for \n"); printf("\tstatus [pkg|regexp] Display all status for \n"); - printf("\tdownload Download to current directory\n"); + printf + ("\tdownload Download to current directory\n"); printf("\tcompare-versions \n"); - printf("\t compare versions using <= < > >= = << >>\n"); - printf("\tprint-architecture List installable package architectures\n"); + printf + ("\t compare versions using <= < > >= = << >>\n"); + printf + ("\tprint-architecture List installable package architectures\n"); printf("\tdepends [-A] [pkgname|pat]+\n"); printf("\twhatdepends [-A] [pkgname|pat]+\n"); printf("\twhatdependsrec [-A] [pkgname|pat]+\n"); @@ -285,7 +292,8 @@ usage() printf("\twhatreplaces [-A] [pkgname|pat]+\n"); printf("\nOptions:\n"); - printf("\t-A Query all packages not just those installed\n"); + printf + ("\t-A Query all packages not just those installed\n"); printf("\t-V[] Set verbosity level to .\n"); printf("\t--verbosity[=] Verbosity levels:\n"); printf("\t 0 errors only\n"); @@ -293,38 +301,52 @@ usage() printf("\t 2 informative messages\n"); printf("\t 3 debug\n"); printf("\t 4 debug level 2\n"); - printf("\t-f Use as the opkg configuration file\n"); + printf + ("\t-f Use as the opkg configuration file\n"); printf("\t--conf \n"); printf("\t--cache Use a package cache\n"); - printf("\t-d Use as the the root directory for\n"); - printf("\t--dest package installation, removal, upgrading.\n"); - printf(" should be a defined dest name from\n"); - printf(" the configuration file, (but can also be a\n"); + printf + ("\t-d Use as the the root directory for\n"); + printf + ("\t--dest package installation, removal, upgrading.\n"); + printf + (" should be a defined dest name from\n"); + printf + (" the configuration file, (but can also be a\n"); printf(" directory name in a pinch).\n"); printf("\t-o Use as the root directory for\n"); printf("\t--offline-root offline installation of packages.\n"); - printf("\t--add-arch : Register architecture with given priority\n"); - printf("\t--add-dest : Register destination with given path\n"); + printf + ("\t--add-arch : Register architecture with given priority\n"); + printf + ("\t--add-dest : Register destination with given path\n"); printf("\nForce Options:\n"); - printf("\t--force-depends Install/remove despite failed dependencies\n"); + printf + ("\t--force-depends Install/remove despite failed dependencies\n"); printf("\t--force-maintainer Overwrite preexisting config files\n"); printf("\t--force-reinstall Reinstall package(s)\n"); - printf("\t--force-overwrite Overwrite files from other package(s)\n"); + printf + ("\t--force-overwrite Overwrite files from other package(s)\n"); printf("\t--force-downgrade Allow opkg to downgrade packages\n"); printf("\t--force-space Disable free space checks\n"); - printf("\t--force-postinstall Run postinstall scripts even in offline mode\n"); - printf("\t--force-remove Remove package even if prerm script fails\n"); + printf + ("\t--force-postinstall Run postinstall scripts even in offline mode\n"); + printf + ("\t--force-remove Remove package even if prerm script fails\n"); printf("\t--force-checksum Don't fail on checksum mismatches\n"); printf("\t--noaction No action -- test only\n"); printf("\t--download-only No action -- download only\n"); printf("\t--nodeps Do not follow dependencies\n"); - printf("\t--nocase Perform case insensitive pattern matching\n"); - printf("\t--size Print package size when listing available packages\n"); + printf + ("\t--nocase Perform case insensitive pattern matching\n"); + printf + ("\t--size Print package size when listing available packages\n"); printf("\t--force-removal-of-dependent-packages\n"); printf("\t Remove package and all dependencies\n"); printf("\t--autoremove Remove packages that were installed\n"); - printf("\t automatically to satisfy dependencies\n"); + printf + ("\t automatically to satisfy dependencies\n"); printf("\t-t Specify tmp-dir.\n"); printf("\t--tmp-dir Specify tmp-dir.\n"); printf("\t-l Specify lists-dir.\n"); @@ -332,24 +354,25 @@ usage() printf("\n"); - printf(" regexp could be something like 'pkgname*' '*file*' or similar\n"); - printf(" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n"); + printf + (" regexp could be something like 'pkgname*' '*file*' or similar\n"); + printf + (" e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*'\n"); - /* --force-removal-of-essential-packages Let opkg remove essential packages. - Using this option is almost guaranteed to break your system, hence this option - is not even advertised in the usage statement. */ + /* --force-removal-of-essential-packages Let opkg remove essential packages. + Using this option is almost guaranteed to break your system, hence this option + is not even advertised in the usage statement. */ exit(1); } -int -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { int opts, err = -1; char *cmd_name; opkg_cmd_t *cmd; int nocheckfordirorfile = 0; - int noreadfeedsfile = 0; + int noreadfeedsfile = 0; if (opkg_conf_init()) goto err0; @@ -364,30 +387,30 @@ main(int argc, char *argv[]) cmd_name = argv[opts++]; - if (!strcmp(cmd_name,"print-architecture") || - !strcmp(cmd_name,"print_architecture") || - !strcmp(cmd_name,"print-installation-architecture") || - !strcmp(cmd_name,"print_installation_architecture") ) + if (!strcmp(cmd_name, "print-architecture") || + !strcmp(cmd_name, "print_architecture") || + !strcmp(cmd_name, "print-installation-architecture") || + !strcmp(cmd_name, "print_installation_architecture")) nocheckfordirorfile = 1; - if (!strcmp(cmd_name,"flag") || - !strcmp(cmd_name,"configure") || - !strcmp(cmd_name,"remove") || - !strcmp(cmd_name,"files") || - !strcmp(cmd_name,"search") || - !strcmp(cmd_name,"compare_versions") || - !strcmp(cmd_name,"compare-versions") || - !strcmp(cmd_name,"list_installed") || - !strcmp(cmd_name,"list-installed") || - !strcmp(cmd_name,"list_changed_conffiles") || - !strcmp(cmd_name,"list-changed-conffiles") || - !strcmp(cmd_name,"status") ) + if (!strcmp(cmd_name, "flag") || + !strcmp(cmd_name, "configure") || + !strcmp(cmd_name, "remove") || + !strcmp(cmd_name, "files") || + !strcmp(cmd_name, "search") || + !strcmp(cmd_name, "compare_versions") || + !strcmp(cmd_name, "compare-versions") || + !strcmp(cmd_name, "list_installed") || + !strcmp(cmd_name, "list-installed") || + !strcmp(cmd_name, "list_changed_conffiles") || + !strcmp(cmd_name, "list-changed-conffiles") || + !strcmp(cmd_name, "status")) noreadfeedsfile = 1; cmd = opkg_cmd_find(cmd_name); if (cmd == NULL) { fprintf(stderr, "%s: unknown sub-command %s\n", argv[0], - cmd_name); + cmd_name); usage(); } @@ -408,12 +431,12 @@ main(int argc, char *argv[]) if (cmd->requires_args && opts == argc) { fprintf(stderr, - "%s: the ``%s'' command requires at least one argument\n", - argv[0], cmd_name); + "%s: the ``%s'' command requires at least one argument\n", + argv[0], cmd_name); usage(); } - err = opkg_cmd_exec(cmd, argc - opts, (const char **) (argv + opts)); + err = opkg_cmd_exec(cmd, argc - opts, (const char **)(argv + opts)); #ifdef HAVE_CURL opkg_curl_cleanup(); diff --git a/tests/libopkg_test.c b/tests/libopkg_test.c index 31a22b0..3ed3b8b 100644 --- a/tests/libopkg_test.c +++ b/tests/libopkg_test.c @@ -8,242 +8,234 @@ int opkg_state_changed; pkg_t *find_pkg = NULL; - #define TEST_PACKAGE "aspell" -void -progress_callback (const opkg_progress_data_t *progress, void *data) +void progress_callback(const opkg_progress_data_t * progress, void *data) { - printf ("\r%s %3d%%\n", (char*) data, progress->percentage); - fflush (stdout); + printf("\r%s %3d%%\n", (char *)data, progress->percentage); + fflush(stdout); } -static void list_pkg(pkg_t *pkg) +static void list_pkg(pkg_t * pkg) { - char *v = pkg_version_str_alloc(pkg); - printf ("%s - %s\n", pkg->name, v); - free(v); + char *v = pkg_version_str_alloc(pkg); + printf("%s - %s\n", pkg->name, v); + free(v); } -void -package_list_installed_callback (pkg_t *pkg, void *data) +void package_list_installed_callback(pkg_t * pkg, void *data) { - if (pkg->state_status == SS_INSTALLED) - list_pkg(pkg); + if (pkg->state_status == SS_INSTALLED) + list_pkg(pkg); } -void -package_list_callback (pkg_t *pkg, void *data) +void package_list_callback(pkg_t * pkg, void *data) { - static int install_count = 0; - static int total_count = 0; + static int install_count = 0; + static int total_count = 0; - if (pkg->state_status == SS_INSTALLED) - install_count++; + if (pkg->state_status == SS_INSTALLED) + install_count++; - total_count++; + total_count++; - printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count); - fflush (stdout); + printf("\rPackage count: %d Installed, %d Total Available", + install_count, total_count); + fflush(stdout); - if (!find_pkg) - { - /* store the first package to print out later */ - find_pkg = pkg; - } + if (!find_pkg) { + /* store the first package to print out later */ + find_pkg = pkg; + } } -void -package_list_upgradable_callback (pkg_t *pkg, void *data) +void package_list_upgradable_callback(pkg_t * pkg, void *data) { - list_pkg(pkg); + list_pkg(pkg); } -void -print_package (pkg_t *pkg) +void print_package(pkg_t * pkg) { - char *v = pkg_version_str_alloc(pkg); - printf ( - "Name: %s\n" - "Version: %s\n" - "Repository: %s\n" - "Architecture: %s\n" - "Description: %s\n" - "Tags: %s\n" - "Size: %ld\n" - "Status: %d\n", - pkg->name, - v, - pkg->src->name, - pkg->architecture, - pkg->description, - pkg->tags? pkg->tags : "", - pkg->size, - pkg->state_status); - free(v); + char *v = pkg_version_str_alloc(pkg); + printf("Name: %s\n" + "Version: %s\n" + "Repository: %s\n" + "Architecture: %s\n" + "Description: %s\n" + "Tags: %s\n" + "Size: %ld\n" + "Status: %d\n", + pkg->name, + v, + pkg->src->name, + pkg->architecture, + pkg->description, + pkg->tags ? pkg->tags : "", pkg->size, pkg->state_status); + free(v); } - -void -opkg_test (void) +void opkg_test(void) { - int err; - pkg_t *pkg; - - err = opkg_update_package_lists (progress_callback, "Updating..."); - printf ("\nopkg_update_package_lists returned %d\n", err); - - opkg_list_packages (package_list_callback, NULL); - printf ("\n"); - - if (find_pkg) - { - printf ("Finding package \"%s\"\n", find_pkg->name); - pkg = opkg_find_package (find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->src->name); - if (pkg) - { - print_package (pkg); - } - else - printf ("Package \"%s\" not found!\n", find_pkg->name); - } - else - printf ("No package available to test find_package.\n"); - - err = opkg_install_package (TEST_PACKAGE, progress_callback, "Installing..."); - printf ("\nopkg_install_package returned %d\n", err); - - err = opkg_upgrade_package (TEST_PACKAGE, progress_callback, "Upgrading..."); - printf ("\nopkg_upgrade_package returned %d\n", err); - - err = opkg_remove_package (TEST_PACKAGE, progress_callback, "Removing..."); - printf ("\nopkg_remove_package returned %d\n", err); - - printf ("Listing upgradable packages...\n"); - opkg_list_upgradable_packages (package_list_upgradable_callback, NULL); - - err = opkg_upgrade_all (progress_callback, "Upgrading all..."); - printf ("\nopkg_upgrade_all returned %d\n", err); + int err; + pkg_t *pkg; + + err = opkg_update_package_lists(progress_callback, "Updating..."); + printf("\nopkg_update_package_lists returned %d\n", err); + + opkg_list_packages(package_list_callback, NULL); + printf("\n"); + + if (find_pkg) { + printf("Finding package \"%s\"\n", find_pkg->name); + pkg = + opkg_find_package(find_pkg->name, find_pkg->version, + find_pkg->architecture, + find_pkg->src->name); + if (pkg) { + print_package(pkg); + } else + printf("Package \"%s\" not found!\n", find_pkg->name); + } else + printf("No package available to test find_package.\n"); + + err = + opkg_install_package(TEST_PACKAGE, progress_callback, + "Installing..."); + printf("\nopkg_install_package returned %d\n", err); + + err = + opkg_upgrade_package(TEST_PACKAGE, progress_callback, + "Upgrading..."); + printf("\nopkg_upgrade_package returned %d\n", err); + + err = + opkg_remove_package(TEST_PACKAGE, progress_callback, "Removing..."); + printf("\nopkg_remove_package returned %d\n", err); + + printf("Listing upgradable packages...\n"); + opkg_list_upgradable_packages(package_list_upgradable_callback, NULL); + + err = opkg_upgrade_all(progress_callback, "Upgrading all..."); + printf("\nopkg_upgrade_all returned %d\n", err); } -int -main (int argc, char **argv) +int main(int argc, char **argv) { - pkg_t *pkg; - int err; - - if (argc < 2) - { - printf ("Usage: %s command\n" - "\nCommands:\n" - "\tupdate - Update package lists\n" - "\tfind [package] - Print details of the specified package\n" - "\tinstall [package] - Install the specified package\n" - "\tupgrade [package] - Upgrade the specified package\n" - "\tlist upgrades - List the available upgrades\n" - "\tlist all - List all available packages\n" - "\tlist installed - List all the installed packages\n" - "\tremove [package] - Remove the specified package\n" - "\trping - Reposiroties ping, check the accessibility of repositories\n" - "\ttest - Run test script\n" - , basename (argv[0])); - exit (0); - } - - setenv("OFFLINE_ROOT", "/tmp", 0); - - if (opkg_new ()) { - printf("opkg_new() failed. This sucks.\n"); - print_error_list(); - return 1; - } - - switch (argv[1][0]) - { - case 'f': - pkg = opkg_find_package (argv[2], NULL, NULL, NULL); - if (pkg) - { - print_package (pkg); - } - else - printf ("Package \"%s\" not found!\n", find_pkg->name); - break; - case 'i': - err = opkg_install_package (argv[2], progress_callback, "Installing..."); - printf ("\nopkg_install_package returned %d\n", err); - break; - - case 'u': - if (argv[1][2] == 'd') - { - err = opkg_update_package_lists (progress_callback, "Updating..."); - printf ("\nopkg_update_package_lists returned %d\n", err); - break; - } - else - { - if (argc < 3) - { - err = opkg_upgrade_all (progress_callback, "Upgrading all..."); - printf ("\nopkg_upgrade_all returned %d\n", err); - } - else - { - err = opkg_upgrade_package (argv[2], progress_callback, "Upgrading..."); - printf ("\nopkg_upgrade_package returned %d\n", err); - } - } - break; - - case 'l': - if (argc < 3) - { - printf ("Please specify one either all, installed or upgrades\n"); - } - else - { - switch (argv[2][0]) - { - case 'u': - printf ("Listing upgradable packages...\n"); - opkg_list_upgradable_packages (package_list_upgradable_callback, NULL); - break; - case 'a': - printf ("Listing all packages...\n"); - opkg_list_packages (package_list_callback, NULL); - printf ("\n"); - break; - case 'i': - printf ("Listing installed packages...\n"); - opkg_list_packages (package_list_installed_callback, NULL); - break; - default: - printf ("Unknown list option \"%s\"\n", argv[2]); - } - } - break; - - case 'r': - if (argv[1][1] == 'e') - { - err = opkg_remove_package (argv[2], progress_callback, "Removing..."); - printf ("\nopkg_remove_package returned %d\n", err); - break; - }else if (argv[1][1] == 'p') - { - err = opkg_repository_accessibility_check(); - printf("\nopkg_repository_accessibility_check returned (%d)\n", err); - break; - } - - default: - printf ("Unknown command \"%s\"\n", argv[1]); - } - - - opkg_free (); - - return 0; + pkg_t *pkg; + int err; + + if (argc < 2) { + printf("Usage: %s command\n" + "\nCommands:\n" + "\tupdate - Update package lists\n" + "\tfind [package] - Print details of the specified package\n" + "\tinstall [package] - Install the specified package\n" + "\tupgrade [package] - Upgrade the specified package\n" + "\tlist upgrades - List the available upgrades\n" + "\tlist all - List all available packages\n" + "\tlist installed - List all the installed packages\n" + "\tremove [package] - Remove the specified package\n" + "\trping - Reposiroties ping, check the accessibility of repositories\n" + "\ttest - Run test script\n", basename(argv[0])); + exit(0); + } + + setenv("OFFLINE_ROOT", "/tmp", 0); + + if (opkg_new()) { + printf("opkg_new() failed. This sucks.\n"); + print_error_list(); + return 1; + } + + switch (argv[1][0]) { + case 'f': + pkg = opkg_find_package(argv[2], NULL, NULL, NULL); + if (pkg) { + print_package(pkg); + } else + printf("Package \"%s\" not found!\n", find_pkg->name); + break; + case 'i': + err = + opkg_install_package(argv[2], progress_callback, + "Installing..."); + printf("\nopkg_install_package returned %d\n", err); + break; + + case 'u': + if (argv[1][2] == 'd') { + err = + opkg_update_package_lists(progress_callback, + "Updating..."); + printf("\nopkg_update_package_lists returned %d\n", + err); + break; + } else { + if (argc < 3) { + err = + opkg_upgrade_all(progress_callback, + "Upgrading all..."); + printf("\nopkg_upgrade_all returned %d\n", err); + } else { + err = + opkg_upgrade_package(argv[2], + progress_callback, + "Upgrading..."); + printf("\nopkg_upgrade_package returned %d\n", + err); + } + } + break; + + case 'l': + if (argc < 3) { + printf + ("Please specify one either all, installed or upgrades\n"); + } else { + switch (argv[2][0]) { + case 'u': + printf("Listing upgradable packages...\n"); + opkg_list_upgradable_packages + (package_list_upgradable_callback, NULL); + break; + case 'a': + printf("Listing all packages...\n"); + opkg_list_packages(package_list_callback, NULL); + printf("\n"); + break; + case 'i': + printf("Listing installed packages...\n"); + opkg_list_packages + (package_list_installed_callback, NULL); + break; + default: + printf("Unknown list option \"%s\"\n", argv[2]); + } + } + break; + + case 'r': + if (argv[1][1] == 'e') { + err = + opkg_remove_package(argv[2], progress_callback, + "Removing..."); + printf("\nopkg_remove_package returned %d\n", err); + break; + } else if (argv[1][1] == 'p') { + err = opkg_repository_accessibility_check(); + printf + ("\nopkg_repository_accessibility_check returned (%d)\n", + err); + break; + } + + default: + printf("Unknown command \"%s\"\n", argv[1]); + } + + opkg_free(); + + return 0; } diff --git a/tests/opkg_active_list_test.c b/tests/opkg_active_list_test.c index b6af3b3..ad964dc 100644 --- a/tests/opkg_active_list_test.c +++ b/tests/opkg_active_list_test.c @@ -15,29 +15,33 @@ General Public License for more details. */ - #include #include #include #include struct active_test { - char *str; - struct active_list list; + char *str; + struct active_list list; }; -struct active_test *active_test_new(char *str) { - struct active_test *ans = (struct active_test *)calloc(1, sizeof(struct active_test)); - ans->str = str; - active_list_init(&ans->list); - return ans; +struct active_test *active_test_new(char *str) +{ + struct active_test *ans = + (struct active_test *)calloc(1, sizeof(struct active_test)); + ans->str = str; + active_list_init(&ans->list); + return ans; } -void active_test_add(struct active_list *head, struct active_test *node) { - active_list_add(head, &node->list); + +void active_test_add(struct active_list *head, struct active_test *node) +{ + active_list_add(head, &node->list); } -void active_test_add_depend(struct active_test *A, struct active_test *B) { - active_list_add_depend(&A->list, &B->list); +void active_test_add_depend(struct active_test *A, struct active_test *B) +{ + active_list_add_depend(&A->list, &B->list); } /* @@ -51,95 +55,101 @@ Then the sequence will be +: G M H I O J A B K N L C D E F -: F E D C L N K B A J O I H M G */ -void make_list(struct active_list *head) { - struct active_test *A = active_test_new("A"); - struct active_test *B = active_test_new("B"); - struct active_test *C = active_test_new("C"); - struct active_test *D = active_test_new("D"); - struct active_test *E = active_test_new("E"); - struct active_test *F = active_test_new("F"); - struct active_test *G = active_test_new("G"); - struct active_test *H = active_test_new("H"); - struct active_test *I = active_test_new("I"); - struct active_test *J = active_test_new("J"); - struct active_test *K = active_test_new("K"); - struct active_test *L = active_test_new("L"); - struct active_test *M = active_test_new("M"); - struct active_test *N = active_test_new("N"); - struct active_test *O = active_test_new("O"); - - active_test_add(head, A); - active_test_add(head, B); - active_test_add(head, C); - active_test_add(head, D); - active_test_add(head, E); - active_test_add(head, F); - active_test_add(head, G); - active_test_add(head, H); - active_test_add(head, I); - active_test_add(head, J); - active_test_add(head, K); - active_test_add(head, L); - active_test_add(head, M); - active_test_add(head, N); - active_test_add(head, O); - active_test_add_depend(H, M); - active_test_add_depend(A, G); - active_test_add_depend(A, H); - active_test_add_depend(A, I); - active_test_add_depend(A, J); - active_test_add_depend(J, O); - active_test_add_depend(C, K); - active_test_add_depend(C, L); - active_test_add_depend(L, N); +void make_list(struct active_list *head) +{ + struct active_test *A = active_test_new("A"); + struct active_test *B = active_test_new("B"); + struct active_test *C = active_test_new("C"); + struct active_test *D = active_test_new("D"); + struct active_test *E = active_test_new("E"); + struct active_test *F = active_test_new("F"); + struct active_test *G = active_test_new("G"); + struct active_test *H = active_test_new("H"); + struct active_test *I = active_test_new("I"); + struct active_test *J = active_test_new("J"); + struct active_test *K = active_test_new("K"); + struct active_test *L = active_test_new("L"); + struct active_test *M = active_test_new("M"); + struct active_test *N = active_test_new("N"); + struct active_test *O = active_test_new("O"); + + active_test_add(head, A); + active_test_add(head, B); + active_test_add(head, C); + active_test_add(head, D); + active_test_add(head, E); + active_test_add(head, F); + active_test_add(head, G); + active_test_add(head, H); + active_test_add(head, I); + active_test_add(head, J); + active_test_add(head, K); + active_test_add(head, L); + active_test_add(head, M); + active_test_add(head, N); + active_test_add(head, O); + active_test_add_depend(H, M); + active_test_add_depend(A, G); + active_test_add_depend(A, H); + active_test_add_depend(A, I); + active_test_add_depend(A, J); + active_test_add_depend(J, O); + active_test_add_depend(C, K); + active_test_add_depend(C, L); + active_test_add_depend(L, N); } -int active_test_compare(const void *a, const void *b) { - struct active_list *first = (struct active_list *)a; - struct active_list *second = (struct active_list *)b; - return strcmp(list_entry(first, struct active_test, list), - list_entry(second, struct active_test, list)); +int active_test_compare(const void *a, const void *b) +{ + struct active_list *first = (struct active_list *)a; + struct active_list *second = (struct active_list *)b; + return strcmp(list_entry(first, struct active_test, list), + list_entry(second, struct active_test, list)); } -void show_list(struct active_list *head) { - struct active_list *ptr; - struct active_test *test; - for(ptr = active_list_next(head, NULL); ptr ;ptr = active_list_next(head, ptr)) { - test = list_entry(ptr, struct active_test, list); - printf ("%s ",test->str); - } - printf("\n"); +void show_list(struct active_list *head) +{ + struct active_list *ptr; + struct active_test *test; + for (ptr = active_list_next(head, NULL); ptr; + ptr = active_list_next(head, ptr)) { + test = list_entry(ptr, struct active_test, list); + printf("%s ", test->str); + } + printf("\n"); } -int main (void) { - struct active_list head; - struct active_list *ptr; - struct active_test *test; - active_list_init(&head); - make_list(&head); +int main(void) +{ + struct active_list head; + struct active_list *ptr; + struct active_test *test; + active_list_init(&head); + make_list(&head); - printf("pos order: "); - show_list(&head); + printf("pos order: "); + show_list(&head); /* for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) { test = list_entry(ptr, struct active_test, list); printf ("%s ",test->str); }*/ - printf("neg order: "); - for(ptr = active_list_prev(&head, &head); ptr ;ptr = active_list_prev(&head, ptr)) { - test = list_entry(ptr, struct active_test, list); - printf ("%s ",test->str); - } - printf("\npos order after sort: "); - active_list_sort(&head, &active_test_compare); - show_list(&head); - - printf("after clear: "); - active_list_clear(&head); - for(ptr = active_list_next(&head, NULL); ptr ;ptr = active_list_next(&head, ptr)) { - test = list_entry(ptr, struct active_test, list); - printf ("%s ",test->str); - } - printf("\n"); - + printf("neg order: "); + for (ptr = active_list_prev(&head, &head); ptr; + ptr = active_list_prev(&head, ptr)) { + test = list_entry(ptr, struct active_test, list); + printf("%s ", test->str); + } + printf("\npos order after sort: "); + active_list_sort(&head, &active_test_compare); + show_list(&head); + + printf("after clear: "); + active_list_clear(&head); + for (ptr = active_list_next(&head, NULL); ptr; + ptr = active_list_next(&head, ptr)) { + test = list_entry(ptr, struct active_test, list); + printf("%s ", test->str); + } + printf("\n"); } diff --git a/tests/opkg_extract_test.c b/tests/opkg_extract_test.c index 9754691..01d2af9 100644 --- a/tests/opkg_extract_test.c +++ b/tests/opkg_extract_test.c @@ -8,39 +8,41 @@ * gcc -o opkg_extract_test opkg_extract_test.c -I./busybox-0.60.2/libbb -L./busybox-0.60.2 -lbb * */ -const char * applet_name; +const char *applet_name; -int main(int argc, char * argv[]) +int main(int argc, char *argv[]) { - /* - * see libbb.h and let your imagination run wild - * or, set the last item below to extract_one_to_buffer, and you get the control file in - * "returned" - * or, set the last one to extract_all_to_fs, and, well, guess what happens - */ - - /* enum extract_functions_e dowhat = extract_control_tar_gz | extract_unconditional | extract_one_to_buffer; */ - enum extract_functions_e dowhat = extract_control_tar_gz | extract_all_to_fs | extract_preserve_date; - char * returned; - char * filename; - - if(argc < 2){ - fprintf(stderr, "syntax: %s []\n", argv[0]); - exit(0); - } - - if (argc < 3){ - filename=NULL; - } else { - filename = argv[2]; - } - - returned = deb_extract(argv[1], stdout, dowhat, NULL, filename); - - if(returned) - fprintf(stderr, "returned %s\n", returned); - else - fprintf(stderr, "extract returned nuthin'\n"); - - return 0; + /* + * see libbb.h and let your imagination run wild + * or, set the last item below to extract_one_to_buffer, and you get the control file in + * "returned" + * or, set the last one to extract_all_to_fs, and, well, guess what happens + */ + + /* enum extract_functions_e dowhat = extract_control_tar_gz | extract_unconditional | extract_one_to_buffer; */ + enum extract_functions_e dowhat = + extract_control_tar_gz | extract_all_to_fs | extract_preserve_date; + char *returned; + char *filename; + + if (argc < 2) { + fprintf(stderr, "syntax: %s []\n", + argv[0]); + exit(0); + } + + if (argc < 3) { + filename = NULL; + } else { + filename = argv[2]; + } + + returned = deb_extract(argv[1], stdout, dowhat, NULL, filename); + + if (returned) + fprintf(stderr, "returned %s\n", returned); + else + fprintf(stderr, "extract returned nuthin'\n"); + + return 0; } diff --git a/tests/opkg_hash_test.c b/tests/opkg_hash_test.c index de1d82c..6c67e59 100644 --- a/tests/opkg_hash_test.c +++ b/tests/opkg_hash_test.c @@ -23,57 +23,68 @@ int main(int argc, char *argv[]) { - opkg_conf_t conf; - hash_table_t *hash = &conf.pkg_hash; - pkg_vec_t * pkg_vec; - - if (argc < 3) { - fprintf(stderr, "Usage: %s [pkg_name...]\n", argv[0]); - exit(1); - } - pkg_hash_init("test", hash, 1024); - - pkg_hash_add_from_file(&conf, argv[1], NULL, NULL, 0); - pkg_hash_add_from_file(&conf, argv[2], NULL, NULL, 0); - - if (argc < 4) { - pkg_print_info( pkg_hash_fetch_by_name_version(hash, "libc6", "2.2.3-2"), stdout); - /* for(i = 0; i < pkg_vec->len; i++) - pkg_print(pkg_vec->pkgs[i], stdout); - */ - } else { - int i, j, k; - char **unresolved; - - pkg_vec_t * dep_vec; - for (i = 3; i < argc; i++) { - pkg_vec = pkg_vec_fetch_by_name(hash, argv[i]); - if (pkg_vec == NULL) { - fprintf(stderr, "*** WARNING: Unknown package: %s\n\n", argv[i]); - continue; - } - - for(j = 0; j < pkg_vec->len; j++){ - pkg_print_info(pkg_vec->pkgs[j], stdout); - dep_vec = pkg_vec_alloc(); - pkg_hash_fetch_unsatisfied_dependencies(&conf, - pkg_vec->pkgs[j], - dep_vec, - &unresolved); - if(dep_vec){ - fprintf(stderr, "and the unsatisfied dependencies are:\n"); - for(k = 0; k < dep_vec->len; k++){ - fprintf(stderr, "%s version %s\n", dep_vec->pkgs[k]->name, dep_vec->pkgs[k]->version); - } + opkg_conf_t conf; + hash_table_t *hash = &conf.pkg_hash; + pkg_vec_t *pkg_vec; + + if (argc < 3) { + fprintf(stderr, + "Usage: %s [pkg_name...]\n", + argv[0]); + exit(1); + } + pkg_hash_init("test", hash, 1024); + + pkg_hash_add_from_file(&conf, argv[1], NULL, NULL, 0); + pkg_hash_add_from_file(&conf, argv[2], NULL, NULL, 0); + + if (argc < 4) { + pkg_print_info(pkg_hash_fetch_by_name_version + (hash, "libc6", "2.2.3-2"), stdout); + /* for(i = 0; i < pkg_vec->len; i++) + pkg_print(pkg_vec->pkgs[i], stdout); + */ + } else { + int i, j, k; + char **unresolved; + + pkg_vec_t *dep_vec; + for (i = 3; i < argc; i++) { + pkg_vec = pkg_vec_fetch_by_name(hash, argv[i]); + if (pkg_vec == NULL) { + fprintf(stderr, + "*** WARNING: Unknown package: %s\n\n", + argv[i]); + continue; + } + + for (j = 0; j < pkg_vec->len; j++) { + pkg_print_info(pkg_vec->pkgs[j], stdout); + dep_vec = pkg_vec_alloc(); + pkg_hash_fetch_unsatisfied_dependencies(&conf, + pkg_vec-> + pkgs[j], + dep_vec, + &unresolved); + if (dep_vec) { + fprintf(stderr, + "and the unsatisfied dependencies are:\n"); + for (k = 0; k < dep_vec->len; k++) { + fprintf(stderr, + "%s version %s\n", + dep_vec->pkgs[k]->name, + dep_vec->pkgs[k]-> + version); + } + } + + fputs("", stdout); + + } } + } - fputs("", stdout); + pkg_hash_deinit(hash); - } - } - } - - pkg_hash_deinit(hash); - - return 0; + return 0; } -- 2.25.1