Commit ec5b63c5 authored by Timo Teräs's avatar Timo Teräs

pkg: clean up writing of dependencies

parent 968dfbb8
...@@ -63,6 +63,7 @@ struct apk_bstream *apk_bstream_from_url(const char *url); ...@@ -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_fd(int fd);
struct apk_ostream *apk_ostream_to_file(const char *file, mode_t mode); 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); 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_istream(struct apk_istream *istream, size_t size);
apk_blob_t apk_blob_from_file(const char *file); apk_blob_t apk_blob_from_file(const char *file);
......
...@@ -75,8 +75,6 @@ void apk_deps_del(struct apk_dependency_array **deps, ...@@ -75,8 +75,6 @@ void apk_deps_del(struct apk_dependency_array **deps,
void apk_deps_parse(struct apk_database *db, void apk_deps_parse(struct apk_database *db,
struct apk_dependency_array **depends, struct apk_dependency_array **depends,
apk_blob_t blob); 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_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os);
int apk_script_type(const char *name); int apk_script_type(const char *name);
...@@ -94,6 +92,6 @@ int apk_pkg_run_script(struct apk_package *pkg, int root_fd, ...@@ -94,6 +92,6 @@ int apk_pkg_run_script(struct apk_package *pkg, int root_fd,
unsigned int type); unsigned int type);
struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_t entry); 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 #endif
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#define APK_VERSION_LESS 2 #define APK_VERSION_LESS 2
#define APK_VERSION_GREATER 4 #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_validate(apk_blob_t ver);
int apk_version_compare(apk_blob_t a, apk_blob_t b); int apk_version_compare(apk_blob_t a, apk_blob_t b);
......
...@@ -489,13 +489,12 @@ static int apk_db_write_fdb(struct apk_database *db, struct apk_ostream *os) ...@@ -489,13 +489,12 @@ static int apk_db_write_fdb(struct apk_database *db, struct apk_ostream *os)
struct apk_db_file *file; struct apk_db_file *file;
struct hlist_node *c1, *c2; struct hlist_node *c1, *c2;
char buf[1024]; char buf[1024];
apk_blob_t blob; int n = 0, r;
int n = 0;
list_for_each_entry(pkg, &db->installed.packages, installed_pkgs_list) { list_for_each_entry(pkg, &db->installed.packages, installed_pkgs_list) {
blob = apk_pkg_format_index_entry(pkg, sizeof(buf), buf); r = apk_pkg_write_index_entry(pkg, os);
if (blob.ptr) if (r < 0)
os->write(os, blob.ptr, blob.len - 1); return r;
hlist_for_each_entry(diri, c1, &pkg->owned_dirs, pkg_dirs_list) { hlist_for_each_entry(diri, c1, &pkg->owned_dirs, pkg_dirs_list) {
n += snprintf(&buf[n], sizeof(buf)-n, n += snprintf(&buf[n], sizeof(buf)-n,
...@@ -872,17 +871,16 @@ static int write_index_entry(apk_hash_item item, void *ctx) ...@@ -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 index_write_ctx *iwctx = (struct index_write_ctx *) ctx;
struct apk_package *pkg = (struct apk_package *) item; struct apk_package *pkg = (struct apk_package *) item;
char buf[1024]; int r;
apk_blob_t blob;
if (pkg->repos != 0) if (pkg->repos != 0)
return 0; return 0;
blob = apk_pkg_format_index_entry(pkg, sizeof(buf), buf); r = apk_pkg_write_index_entry(pkg, iwctx->os);
if (APK_BLOB_IS_NULL(blob)) if (r < 0)
return 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; return -1;
iwctx->count++; iwctx->count++;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include "apk_defines.h" #include "apk_defines.h"
#include "apk_applet.h" #include "apk_applet.h"
#include "apk_package.h" #include "apk_package.h"
...@@ -85,9 +86,13 @@ static int info_who_owns(struct info_ctx *ctx, struct apk_database *db, ...@@ -85,9 +86,13 @@ static int info_who_owns(struct info_ctx *ctx, struct apk_database *db,
} }
} }
if (apk_verbosity < 1 && deps != NULL) { if (apk_verbosity < 1 && deps != NULL) {
char buf[512]; struct apk_ostream *os;
apk_deps_format(buf, sizeof(buf), deps);
printf("%s\n", buf); os = apk_ostream_to_fd(STDOUT_FILENO);
apk_deps_write(deps, os);
os->write(os, "\n", 1);
os->close(os);
free(deps); free(deps);
} }
......
...@@ -492,3 +492,13 @@ struct apk_ostream *apk_ostream_to_file(const char *file, mode_t mode) ...@@ -492,3 +492,13 @@ struct apk_ostream *apk_ostream_to_file(const char *file, mode_t mode)
return apk_ostream_to_fd(fd); 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;
}
...@@ -188,61 +188,9 @@ void apk_deps_parse(struct apk_database *db, ...@@ -188,61 +188,9 @@ void apk_deps_parse(struct apk_database *db,
apk_blob_for_each_segment(blob, " ", parse_depend, &ctx); 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 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) if (deps == NULL)
return 0; return 0;
...@@ -254,10 +202,29 @@ int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os) ...@@ -254,10 +202,29 @@ int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os)
n += 1; n += 1;
} }
len = strlen(deps->item[i].name->name); if (deps->item[i].result_mask == APK_DEPMASK_CONFLICT) {
if (os->write(os, deps->item[i].name->name, len) != len) if (os->write(os, "!", 1) != 1)
return -1; return -1;
n += len; 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; return n;
...@@ -654,34 +621,45 @@ struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_ ...@@ -654,34 +621,45 @@ struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_
return ctx.pkg; return ctx.pkg;
} }
apk_blob_t apk_pkg_format_index_entry(struct apk_package *info, int size, int apk_pkg_write_index_entry(struct apk_package *info,
char *buf) struct apk_ostream *os)
{ {
int n = 0; char buf[512];
int n, r, t = 0;
n += snprintf(&buf[n], size-n,
"P:%s\n" n = snprintf(buf, sizeof(buf),
"V:%s\n" "P:%s\n"
"S:%zu\n" "V:%s\n"
"I:%zu\n" "S:%zu\n"
"T:%s\n" "I:%zu\n"
"U:%s\n" "T:%s\n"
"L:%s\n", "U:%s\n"
info->name->name, info->version, "L:%s\n",
info->size, info->installed_size, info->name->name, info->version,
info->description, info->url, info->license); 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) { if (info->depends != NULL) {
n += snprintf(&buf[n], size-n, "D:"); if (os->write(os, "D:", 2) != 2)
n += apk_deps_format(&buf[n], size-n, info->depends); return -1;
if (n < size) r = apk_deps_write(info->depends, os);
buf[n++] = '\n'; 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;
} }
...@@ -128,6 +128,24 @@ static int get_token(int *type, apk_blob_t *blob) ...@@ -128,6 +128,24 @@ static int get_token(int *type, apk_blob_t *blob)
return v; 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 apk_version_validate(apk_blob_t ver)
{ {
int t = TOKEN_DIGIT; int t = TOKEN_DIGIT;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment