forked from tbvdm/sigbak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbk-attachment-tree.c
97 lines (80 loc) · 2.59 KB
/
sbk-attachment-tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdlib.h>
#include "sbk-internal.h"
static int sbk_cmp_attachment_entries(struct sbk_attachment_entry *,
struct sbk_attachment_entry *);
RB_GENERATE_STATIC(sbk_attachment_tree, sbk_attachment_entry, entries,
sbk_cmp_attachment_entries)
void
sbk_free_attachment_tree(struct sbk_ctx *ctx)
{
struct sbk_attachment_entry *entry;
while ((entry = RB_ROOT(&ctx->attachments)) != NULL) {
RB_REMOVE(sbk_attachment_tree, &ctx->attachments, entry);
sbk_free_file(entry->file);
free(entry);
}
}
static int
sbk_cmp_attachment_entries(struct sbk_attachment_entry *a,
struct sbk_attachment_entry *b)
{
if (a->id.row_id < b->id.row_id)
return -1;
if (a->id.row_id > b->id.row_id)
return 1;
return (a->id.unique_id < b->id.unique_id) ? -1 :
(a->id.unique_id > b->id.unique_id);
}
int
sbk_insert_attachment_entry(struct sbk_ctx *ctx, Signal__BackupFrame *frm,
struct sbk_file *file)
{
struct sbk_attachment_entry *entry;
if (!frm->attachment->has_rowid)
goto invalid;
if (!frm->attachment->has_attachmentid) {
if (ctx->db_version <
SBK_DB_VERSION_REMOVE_ATTACHMENT_UNIQUE_ID)
goto invalid;
frm->attachment->attachmentid = 0;
}
if ((entry = malloc(sizeof *entry)) == NULL) {
warn(NULL);
sbk_free_file(file);
return -1;
}
entry->id.row_id = frm->attachment->rowid;
entry->id.unique_id = frm->attachment->attachmentid;
entry->file = file;
RB_INSERT(sbk_attachment_tree, &ctx->attachments, entry);
return 0;
invalid:
warnx("Invalid attachment frame");
sbk_free_file(file);
return -1;
}
struct sbk_file *
sbk_get_attachment_file(struct sbk_ctx *ctx,
const struct sbk_attachment_id *id)
{
struct sbk_attachment_entry find, *result;
find.id = *id;
result = RB_FIND(sbk_attachment_tree, &ctx->attachments, &find);
return (result != NULL) ? result->file : NULL;
}