summaryrefslogtreecommitdiffstats
path: root/src/list.c
blob: 5f1e5e2c6492ad18cde72e9a815eb511a866766c (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* $Id$ */

/***
  This file is part of syrep.

  syrep is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  syrep is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.
  
  You should have received a copy of the GNU General Public License
  along with syrep; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***/

#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include "list.h"
#include "context.h"
#include "md5util.h"
#include "dbstruct.h"
#include "util.h"
#include "dbutil.h"
#include "syrep.h"

static int handle_file(struct syrep_db_context *c, const struct syrep_name *name, const struct syrep_md *md, const struct syrep_meta *meta) {
    struct syrep_meta local_meta;
    assert(c && name && md);

    if (!meta) {
        int f;
        if ((f = get_meta_by_name_md(c, name, md, &local_meta)) < 0)
            return -1;

        if (f)
            meta = &local_meta;
    }
        
    if (!args.show_deleted_flag && meta->last_seen != c->version)
        return 0;

    if (meta) {
        if (!args.show_by_md_flag) {
            char d[SYREP_DIGESTLENGTH*2+1]; 
            fhex(md->digest, SYREP_DIGESTLENGTH, d);
            d[SYREP_DIGESTLENGTH*2] = 0;
            
            fprintf(stderr, "%s %s%s", d, name->path, meta->last_seen == c->version ? "\t\t" : "\t(deleted)");
        } else
            fprintf(stderr, "\t%s%s", name->path, meta->last_seen == c->version ? "\t\t" : "\t(deleted)");
        
        if (args.show_times_flag)
            fprintf(stderr, "\t(first-seen: %u; last-seen: %u)\n", meta->first_seen, meta->last_seen);
        else
            fputc('\n', stderr);
    } else {
        char d[SYREP_DIGESTLENGTH*2+1]; 
        fhex(md->digest, SYREP_DIGESTLENGTH, d);
        d[SYREP_DIGESTLENGTH*2] = 0;
        
        fprintf(stderr, "\t%s", name->path);
    }
            
    return 0;
}

int list(struct syrep_db_context *c) {
    int r = -1, ret;
    DBC *cursor = NULL;
    DBT key, data;


#if 1

    {
        if ((ret = c->db_name_lastmd->cursor(c->db_name_lastmd, NULL, &cursor, 0)) != 0) {
            c->db_name_lastmd->err(c->db_name_lastmd, ret, "db_name_lastmd");
            goto finish;
        }
        
        memset(&key, 0, sizeof(key));
        memset(&data, 0, sizeof(data));
        
        while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
            struct syrep_name *name = (struct syrep_name*) key.data;
            struct syrep_md *md = (struct syrep_md*) data.data;

            if (handle_file(c, name, md, NULL) < 0)
                fprintf(stderr, "handle_file() failed\n");
            
        }
        
        if (ret != DB_NOTFOUND) {
            c->db_name_lastmd->err(c->db_name_lastmd, ret, "name_lastmd::c_get");
            goto finish;
        }
    
        r = 0;
    }

    goto finish;

#endif
    
    if (args.show_by_md_flag) {
        struct syrep_md previous_md;
        memset(&previous_md, 0, sizeof(previous_md));
        
        if ((ret = c->db_md_name->cursor(c->db_md_name, NULL, &cursor, 0)) != 0) {
            c->db_md_name->err(c->db_md_name, ret, "md_name");
            goto finish;
        }
        
        memset(&key, 0, sizeof(key));
        memset(&data, 0, sizeof(data));

        while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
            struct syrep_md *md = (struct syrep_md*) key.data;
            struct syrep_name *name = (struct syrep_name*) data.data;
            struct syrep_meta meta;

            if (memcmp(&previous_md, md, sizeof(previous_md))) {
                char d[SYREP_DIGESTLENGTH*2+1]; 
                fhex(md->digest, SYREP_DIGESTLENGTH, d);
                d[SYREP_DIGESTLENGTH*2] = 0;
                fprintf(stderr, "%s:\n", d);
                memcpy(&previous_md, md, sizeof(previous_md));
            }
                
            if ((ret = get_meta_by_name_md(c, name, md, &meta)) < 0)
                goto finish;

            if (handle_file(c, name, md, &meta) < 0)
                fprintf(stderr, "handle_file() failed\n");
        }
        
        if (ret != DB_NOTFOUND) {
            c->db_md_name->err(c->db_md_name, ret, "md_name::c_get");
            goto finish;
        }
    
        r = 0;
    } else {

        if ((ret = c->db_id_meta->cursor(c->db_id_meta, NULL, &cursor, 0)) != 0) {
            c->db_id_meta->err(c->db_id_meta, ret, "id_meta");
            goto finish;
        }
        
        memset(&key, 0, sizeof(key));
        memset(&data, 0, sizeof(data));

        while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
            struct syrep_id *id = (struct syrep_id*) key.data;
            
            if (handle_file(c, &id->name, &id->md, (struct syrep_meta*) data.data) < 0)
                fprintf(stderr, "handle_file() failed\n");
            
        }
        
        if (ret != DB_NOTFOUND) {
            c->db_id_meta->err(c->db_id_meta, ret, "id_meta::c_get");
            goto finish;
        }
    
        r = 0;
    }
    
finish:

    if (cursor)
        cursor->c_close(cursor);

    return r;
}