pkg: clean up writing of dependencies

cute-signatures
Timo Teras 2009-04-16 17:05:27 +03:00
parent 968dfbb8b3
commit ec5b63c5e4
8 changed files with 107 additions and 98 deletions

View File

@ -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);

View File

@ -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,
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

View File

@ -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);

View File

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

View File

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

View File

@ -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;
}

View File

@ -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)
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_
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, n = snprintf(buf, sizeof(buf),
"P:%s\n" "P:%s\n"
"V:%s\n" "V:%s\n"
"S:%zu\n" "S:%zu\n"
"I:%zu\n" "I:%zu\n"
"T:%s\n" "T:%s\n"
"U:%s\n" "U:%s\n"
"L:%s\n", "L:%s\n",
info->name->name, info->version, info->name->name, info->version,
info->size, info->installed_size, info->size, info->installed_size,
info->description, info->url, info->license); 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;
} }

View File

@ -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;