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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/* This file is part of "reprepro"
* Copyright (C) 2003,2004,2005,2007 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "strlist.h"
#include "names.h"
#include "dirs.h"
#include "database_p.h"
#include "pool.h"
#include "reference.h"
retvalue references_isused( const char *what) {
return table_gettemprecord(rdb_references, what, NULL, NULL);
}
retvalue references_check(const char *referee, const struct strlist *filekeys) {
int i;
retvalue result, r;
result = RET_NOTHING;
for (i = 0 ; i < filekeys->count ; i++) {
r = table_checkrecord(rdb_references,
filekeys->values[i], referee);
if (r == RET_NOTHING) {
fprintf(stderr, "Missing reference to '%s' by '%s'\n",
filekeys->values[i], referee);
r = RET_ERROR;
}
RET_UPDATE(result, r);
}
return result;
}
/* add an reference to a file for an identifier. multiple calls */
retvalue references_increment(const char *needed, const char *neededby) {
retvalue r;
r = table_addrecord(rdb_references, needed,
neededby, strlen(neededby), false);
if (RET_IS_OK(r) && verbose > 8)
printf("Adding reference to '%s' by '%s'\n", needed, neededby);
return r;
}
/* remove reference for a file from a given reference */
retvalue references_decrement(const char *needed, const char *neededby) {
retvalue r;
r = table_removerecord(rdb_references, needed, neededby);
if (r == RET_NOTHING)
return r;
if (RET_WAS_ERROR(r)) {
fprintf(stderr,
"Error while trying to removing reference to '%s' by '%s'\n",
needed, neededby);
return r;
}
if (verbose > 8)
fprintf(stderr, "Removed reference to '%s' by '%s'\n",
needed, neededby);
if (RET_IS_OK(r)) {
retvalue r2;
r2 = pool_dereferenced(needed);
RET_UPDATE(r, r2);
}
return r;
}
/* Add an reference by <identifier> for the given <files>,
* excluding <exclude>, if it is nonNULL. */
retvalue references_insert(const char *identifier,
const struct strlist *files, const struct strlist *exclude) {
retvalue result, r;
int i;
result = RET_NOTHING;
for (i = 0 ; i < files->count ; i++) {
const char *filename = files->values[i];
if (exclude == NULL || !strlist_in(exclude, filename)) {
r = references_increment(filename, identifier);
RET_UPDATE(result, r);
}
}
return result;
}
/* add possible already existing references */
retvalue references_add(const char *identifier, const struct strlist *files) {
int i;
retvalue r;
for (i = 0 ; i < files->count ; i++) {
const char *filekey = files->values[i];
r = table_addrecord(rdb_references, filekey,
identifier, strlen(identifier), true);
if (RET_WAS_ERROR(r))
return r;
}
return RET_OK;
}
/* Remove reference by <identifier> for the given <oldfiles>,
* excluding <exclude>, if it is nonNULL. */
retvalue references_delete(const char *identifier, const struct strlist *files, const struct strlist *exclude) {
retvalue result, r;
int i;
assert (files != NULL);
result = RET_NOTHING;
for (i = 0 ; i < files->count ; i++) {
const char *filekey = files->values[i];
if (exclude == NULL || !strlist_in(exclude, filekey)) {
r = references_decrement(filekey, identifier);
RET_UPDATE(result, r);
}
}
return result;
}
/* remove all references from a given identifier */
retvalue references_remove(const char *neededby) {
struct cursor *cursor;
retvalue result, r;
const char *found_to, *found_by;
size_t datalen, l;
r = table_newglobalcursor(rdb_references, &cursor);
if (!RET_IS_OK(r))
return r;
l = strlen(neededby);
result = RET_NOTHING;
while (cursor_nexttempdata(rdb_references, cursor,
&found_to, &found_by, &datalen)) {
if (datalen >= l && strncmp(found_by, neededby, l) == 0 &&
(found_by[l] == '\0' || found_by[l] == ' ')) {
if (verbose > 8)
fprintf(stderr,
"Removing reference to '%s' by '%s'\n",
found_to, neededby);
r = cursor_delete(rdb_references, cursor,
found_to, NULL);
RET_UPDATE(result, r);
if (RET_IS_OK(r)) {
r = pool_dereferenced(found_to);
RET_ENDUPDATE(result, r);
}
}
}
r = cursor_close(rdb_references, cursor);
RET_ENDUPDATE(result, r);
return result;
}
/* dump all references to stdout */
retvalue references_dump(void) {
struct cursor *cursor;
retvalue result, r;
const char *found_to, *found_by;
r = table_newglobalcursor(rdb_references, &cursor);
if (!RET_IS_OK(r))
return r;
result = RET_OK;
while (cursor_nexttemp(rdb_references, cursor,
&found_to, &found_by)) {
if (fputs(found_by, stdout) == EOF ||
putchar(' ') == EOF ||
puts(found_to) == EOF) {
result = RET_ERROR;
break;
}
result = RET_OK;
if (interrupted()) {
result = RET_ERROR_INTERRUPTED;
break;
}
}
r = cursor_close(rdb_references, cursor);
RET_ENDUPDATE(result, r);
return result;
}
|