summaryrefslogtreecommitdiffstats
path: root/src/package.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/package.c')
-rw-r--r--src/package.c60
1 files changed, 45 insertions, 15 deletions
diff --git a/src/package.c b/src/package.c
index 5a741eb..8f8bd23 100644
--- a/src/package.c
+++ b/src/package.c
@@ -16,6 +16,7 @@ struct package_item;
struct package_item {
char *name;
char *path;
+ int remove;
struct package_item *next;
};
@@ -61,19 +62,27 @@ static char *tmp(char *fn, int l) {
return fn;
}
-static struct package_item *item_new(const char *name, const char *path) {
+static struct package_item *item_new(const char *name, const char *path, int r) {
struct package_item *i = NULL;
- if (!(i = malloc(sizeof(struct package_item))))
- return NULL;
+ if (!(i = malloc(sizeof(struct package_item)))) {
+ fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
+ goto fail;
+ }
memset(i, 0, sizeof(struct package_item));
- if (!(i->name = strdup(name)))
+ if (!(i->name = strdup(name))) {
+ fprintf(stderr, "strdup() failed: %s\n", strerror(errno));
goto fail;
+ }
- if (!(i->path = strdup(path)))
+ if (!(i->path = strdup(path))) {
+ fprintf(stderr, "strdup() failed: %s\n", strerror(errno));
goto fail;
+ }
+
+ i->remove = r;
return i;
@@ -148,7 +157,7 @@ struct package* package_open(const char *fn) {
}
fclose(d);
- if (!(pi = item_new(name, path))) {
+ if (!(pi = item_new(name, path, 1))) {
unlink(path);
fprintf(stderr, "Failed to allocate memory.\n");
goto finish;
@@ -183,9 +192,11 @@ int package_save(struct package *p, const char *fn) {
int r = 0;
struct package_item *i;
- assert(p && fn);
+ assert(p);
- if (!(f = fopen(fn, "w+")))
+ if (!fn)
+ f = stdout;
+ else if (!(f = fopen(fn, "w+")))
return -1;
for (i = p->items; i; i = i->next) {
@@ -234,8 +245,9 @@ int package_save(struct package *p, const char *fn) {
if (s)
fclose(s);
-
- fclose(f);
+
+ if (fn)
+ fclose(f);
if (r != 0)
unlink(fn);
@@ -243,19 +255,21 @@ int package_save(struct package *p, const char *fn) {
return r;
}
-const char *package_get_item(struct package* p, const char *name) {
+const char *package_get_item(struct package* p, const char *name, int c) {
struct package_item *i;
char path[PATH_MAX];
assert(p && name);
- for (i = p->items; i; i = i->next) {
+ for (i = p->items; i; i = i->next)
if (strncmp(name, i->name, PACKAGE_ITEM_NAME_LEN) == 0)
return i->path;
- }
+ if (!c)
+ return NULL;
+
snprintf(path, sizeof(path), "%s/%i", p->base, p->count++);
- if (!(i = item_new(name, path))) {
+ if (!(i = item_new(name, path, 1))) {
unlink(path);
return NULL;
}
@@ -271,6 +285,22 @@ const char *package_get_item(struct package* p, const char *name) {
return i->path;
}
+int package_add_file(struct package *p, const char *name, const char *fn) {
+ struct package_item *i;
+
+ if (!(i = item_new(name, fn, 0)))
+ return -1;
+
+ if (p->last) {
+ p->last->next = i;
+ p->last = i;
+ } else
+ p->items = p->last = i;
+
+ return 0;
+}
+
+
void package_remove(struct package *p) {
struct package_item *i, *n;
assert(p);
@@ -278,7 +308,7 @@ void package_remove(struct package *p) {
for (i = p->items; i; i = n) {
n = i->next;
- if (i->path)
+ if (i->remove && i->path)
if (unlink(i->path))
if (errno != ENOENT)
fprintf(stderr, "Failed to remove <%s>: %s\n", i->path, strerror(errno));