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);
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);
......
......@@ -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
......@@ -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);
......
......@@ -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++;
......
......@@ -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);
}
......
......@@ -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;
}
......@@ -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;
}
......@@ -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;
......
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