summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTimo Teras <timo.teras@iki.fi>2009-04-16 17:05:27 +0300
committerTimo Teras <timo.teras@iki.fi>2009-04-16 17:05:27 +0300
commitec5b63c5e4d719e07c48b0de0dc0b9f3b25d2cf8 (patch)
tree12885a6778a0a746fc0ff62b0be4b75f368cebc9 /src
parent968dfbb8b376de2dc6f2441dc3966d1416ad0046 (diff)
downloadapk-tools-ec5b63c5e4d719e07c48b0de0dc0b9f3b25d2cf8.tar.gz
apk-tools-ec5b63c5e4d719e07c48b0de0dc0b9f3b25d2cf8.tar.bz2
apk-tools-ec5b63c5e4d719e07c48b0de0dc0b9f3b25d2cf8.tar.xz
apk-tools-ec5b63c5e4d719e07c48b0de0dc0b9f3b25d2cf8.zip
pkg: clean up writing of dependencies
Diffstat (limited to 'src')
-rw-r--r--src/apk_io.h1
-rw-r--r--src/apk_package.h4
-rw-r--r--src/apk_version.h1
-rw-r--r--src/database.c20
-rw-r--r--src/info.c11
-rw-r--r--src/io.c10
-rw-r--r--src/package.c142
-rw-r--r--src/version.c18
8 files changed, 108 insertions, 99 deletions
diff --git a/src/apk_io.h b/src/apk_io.h
index a903277..a0d9049 100644
--- a/src/apk_io.h
+++ b/src/apk_io.h
@@ -63,6 +63,7 @@ struct apk_bstream *apk_bstream_from_url(const char *url);
struct apk_ostream *apk_ostream_to_fd(int fd);
struct apk_ostream *apk_ostream_to_file(const char *file, mode_t mode);
struct apk_ostream *apk_ostream_to_file_gz(const char *file, mode_t mode);
+size_t apk_ostream_write_string(struct apk_ostream *ostream, const char *string);
apk_blob_t apk_blob_from_istream(struct apk_istream *istream, size_t size);
apk_blob_t apk_blob_from_file(const char *file);
diff --git a/src/apk_package.h b/src/apk_package.h
index a848315..1e815d3 100644
--- a/src/apk_package.h
+++ b/src/apk_package.h
@@ -75,8 +75,6 @@ void apk_deps_del(struct apk_dependency_array **deps,
void apk_deps_parse(struct apk_database *db,
struct apk_dependency_array **depends,
apk_blob_t blob);
-int apk_deps_format(char *buf, int size,
- struct apk_dependency_array *depends);
int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os);
int apk_script_type(const char *name);
@@ -94,6 +92,6 @@ int apk_pkg_run_script(struct apk_package *pkg, int root_fd,
unsigned int type);
struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_t entry);
-apk_blob_t apk_pkg_format_index_entry(struct apk_package *pkg, int size, char *buf);
+int apk_pkg_write_index_entry(struct apk_package *pkg, struct apk_ostream *os);
#endif
diff --git a/src/apk_version.h b/src/apk_version.h
index 7650864..e7036b3 100644
--- a/src/apk_version.h
+++ b/src/apk_version.h
@@ -18,6 +18,7 @@
#define APK_VERSION_LESS 2
#define APK_VERSION_GREATER 4
+const char *apk_version_op_string(int result_mask);
int apk_version_validate(apk_blob_t ver);
int apk_version_compare(apk_blob_t a, apk_blob_t b);
diff --git a/src/database.c b/src/database.c
index 649555a..195f653 100644
--- a/src/database.c
+++ b/src/database.c
@@ -489,13 +489,12 @@ static int apk_db_write_fdb(struct apk_database *db, struct apk_ostream *os)
struct apk_db_file *file;
struct hlist_node *c1, *c2;
char buf[1024];
- apk_blob_t blob;
- int n = 0;
+ int n = 0, r;
list_for_each_entry(pkg, &db->installed.packages, installed_pkgs_list) {
- blob = apk_pkg_format_index_entry(pkg, sizeof(buf), buf);
- if (blob.ptr)
- os->write(os, blob.ptr, blob.len - 1);
+ r = apk_pkg_write_index_entry(pkg, os);
+ if (r < 0)
+ return r;
hlist_for_each_entry(diri, c1, &pkg->owned_dirs, pkg_dirs_list) {
n += snprintf(&buf[n], sizeof(buf)-n,
@@ -872,17 +871,16 @@ static int write_index_entry(apk_hash_item item, void *ctx)
{
struct index_write_ctx *iwctx = (struct index_write_ctx *) ctx;
struct apk_package *pkg = (struct apk_package *) item;
- char buf[1024];
- apk_blob_t blob;
+ int r;
if (pkg->repos != 0)
return 0;
- blob = apk_pkg_format_index_entry(pkg, sizeof(buf), buf);
- if (APK_BLOB_IS_NULL(blob))
- return 0;
+ r = apk_pkg_write_index_entry(pkg, iwctx->os);
+ if (r < 0)
+ return r;
- if (iwctx->os->write(iwctx->os, blob.ptr, blob.len) != blob.len)
+ if (iwctx->os->write(iwctx->os, "\n", 1) != 1)
return -1;
iwctx->count++;
diff --git a/src/info.c b/src/info.c
index 4bc3ddb..7a4c519 100644
--- a/src/info.c
+++ b/src/info.c
@@ -10,6 +10,7 @@
*/
#include <stdio.h>
+#include <unistd.h>
#include "apk_defines.h"
#include "apk_applet.h"
#include "apk_package.h"
@@ -85,9 +86,13 @@ static int info_who_owns(struct info_ctx *ctx, struct apk_database *db,
}
}
if (apk_verbosity < 1 && deps != NULL) {
- char buf[512];
- apk_deps_format(buf, sizeof(buf), deps);
- printf("%s\n", buf);
+ struct apk_ostream *os;
+
+ os = apk_ostream_to_fd(STDOUT_FILENO);
+ apk_deps_write(deps, os);
+ os->write(os, "\n", 1);
+ os->close(os);
+
free(deps);
}
diff --git a/src/io.c b/src/io.c
index 81ed289..e0a9c9b 100644
--- a/src/io.c
+++ b/src/io.c
@@ -492,3 +492,13 @@ struct apk_ostream *apk_ostream_to_file(const char *file, mode_t mode)
return apk_ostream_to_fd(fd);
}
+size_t apk_ostream_write_string(struct apk_ostream *os, const char *string)
+{
+ size_t len;
+
+ len = strlen(string);
+ if (os->write(os, string, len) != len)
+ return -1;
+
+ return len;
+}
diff --git a/src/package.c b/src/package.c
index f74ca4d..029167d 100644
--- a/src/package.c
+++ b/src/package.c
@@ -188,61 +188,9 @@ void apk_deps_parse(struct apk_database *db,
apk_blob_for_each_segment(blob, " ", parse_depend, &ctx);
}
-static const char *mask2str(int mask)
-{
- switch (mask) {
- case APK_VERSION_LESS:
- return "<";
- case APK_VERSION_LESS|APK_VERSION_EQUAL:
- return "<=";
- case APK_VERSION_EQUAL:
- return "=";
- case APK_VERSION_GREATER|APK_VERSION_EQUAL:
- return ">=";
- case APK_VERSION_GREATER:
- return ">";
- default:
- return "?";
- }
-}
-
-int apk_deps_format(char *buf, int size,
- struct apk_dependency_array *depends)
-{
- int i, n = 0;
-
- if (depends == NULL)
- return 0;
-
- for (i = 0; i < depends->num; i++) {
- if (i && n < size)
- buf[n++] = ' ';
- switch (depends->item[i].result_mask) {
- case APK_DEPMASK_CONFLICT:
- n += snprintf(&buf[n], size-n,
- "!%s",
- depends->item[i].name->name);
- break;
- case APK_DEPMASK_REQUIRE:
- n += snprintf(&buf[n], size-n,
- "%s",
- depends->item[i].name->name);
- break;
- default:
- n += snprintf(&buf[n], size-n,
- "%s%s%s",
- depends->item[i].name->name,
- mask2str(depends->item[i].result_mask),
- depends->item[i].version);
- break;
- }
- }
- return n;
-}
-
int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os)
{
- int i, len, n = 0;
+ int i, r, n = 0;
if (deps == NULL)
return 0;
@@ -254,10 +202,29 @@ int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os)
n += 1;
}
- len = strlen(deps->item[i].name->name);
- if (os->write(os, deps->item[i].name->name, len) != len)
- return -1;
- n += len;
+ if (deps->item[i].result_mask == APK_DEPMASK_CONFLICT) {
+ if (os->write(os, "!", 1) != 1)
+ return -1;
+ n += 1;
+ }
+
+ r = apk_ostream_write_string(os, deps->item[i].name->name);
+ if (r < 0)
+ return r;
+ n += r;
+
+ if (deps->item[i].result_mask != APK_DEPMASK_CONFLICT &&
+ deps->item[i].result_mask != APK_DEPMASK_REQUIRE) {
+ r = apk_ostream_write_string(os, apk_version_op_string(deps->item[i].result_mask));
+ if (r < 0)
+ return r;
+ n += r;
+
+ r = apk_ostream_write_string(os, deps->item[i].version);
+ if (r < 0)
+ return r;
+ n += r;
+ }
}
return n;
@@ -654,34 +621,45 @@ struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_
return ctx.pkg;
}
-apk_blob_t apk_pkg_format_index_entry(struct apk_package *info, int size,
- char *buf)
+int apk_pkg_write_index_entry(struct apk_package *info,
+ struct apk_ostream *os)
{
- int n = 0;
-
- n += snprintf(&buf[n], size-n,
- "P:%s\n"
- "V:%s\n"
- "S:%zu\n"
- "I:%zu\n"
- "T:%s\n"
- "U:%s\n"
- "L:%s\n",
- info->name->name, info->version,
- info->size, info->installed_size,
- info->description, info->url, info->license);
+ char buf[512];
+ int n, r, t = 0;
+
+ n = snprintf(buf, sizeof(buf),
+ "P:%s\n"
+ "V:%s\n"
+ "S:%zu\n"
+ "I:%zu\n"
+ "T:%s\n"
+ "U:%s\n"
+ "L:%s\n",
+ info->name->name, info->version,
+ info->size, info->installed_size,
+ info->description, info->url, info->license);
+ if (os->write(os, buf, n) != n)
+ return -1;
+ t += n;
if (info->depends != NULL) {
- n += snprintf(&buf[n], size-n, "D:");
- n += apk_deps_format(&buf[n], size-n, info->depends);
- if (n < size)
- buf[n++] = '\n';
+ if (os->write(os, "D:", 2) != 2)
+ return -1;
+ r = apk_deps_write(info->depends, os);
+ if (r < 0)
+ return r;
+ if (os->write(os, "\n", 1) != 1)
+ return -1;
+ t += r + 3;
}
- n += snprintf(&buf[n], size-n, "C:");
- n += apk_hexdump_format(size-n, &buf[n],
- APK_BLOB_BUF(info->csum));
- n += snprintf(&buf[n], size-n,
- "\n\n");
- return APK_BLOB_PTR_LEN(buf, n);
+ n = snprintf(buf, sizeof(buf), "C:");
+ n += apk_hexdump_format(sizeof(buf)-n, &buf[n], APK_BLOB_BUF(info->csum));
+ n += snprintf(&buf[n], sizeof(buf)-n, "\n");
+
+ if (os->write(os, buf, n) != n)
+ return -1;
+ t += n;
+
+ return n;
}
diff --git a/src/version.c b/src/version.c
index 45a9ede..57437aa 100644
--- a/src/version.c
+++ b/src/version.c
@@ -128,6 +128,24 @@ static int get_token(int *type, apk_blob_t *blob)
return v;
}
+const char *apk_version_op_string(int mask)
+{
+ switch (mask) {
+ case APK_VERSION_LESS:
+ return "<";
+ case APK_VERSION_LESS|APK_VERSION_EQUAL:
+ return "<=";
+ case APK_VERSION_EQUAL:
+ return "=";
+ case APK_VERSION_GREATER|APK_VERSION_EQUAL:
+ return ">=";
+ case APK_VERSION_GREATER:
+ return ">";
+ default:
+ return "?";
+ }
+}
+
int apk_version_validate(apk_blob_t ver)
{
int t = TOKEN_DIGIT;