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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifndef O_SYNC
#define O_SYNC O_FSYNC
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <sys/types.h>
#endif
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#include "suck_config.h"
#include "suck.h"
#include "suckutils.h"
#include "both.h"
#include "phrases.h"
#include "db.h"
/* ------------------------------------------------------------------------------ */
/* this file handles all the db routines, creating, deleting, adding records to, */
/* flagging as downloaded, etc. It replaces the old suck.restart and suck.sorted */
/* ------------------------------------------------------------------------------ */
int db_delete(PMaster master) {
int retval = RETVAL_OK;
const char *ptr;
db_close(master); /* just in case */
ptr = full_path(FP_GET, FP_TMPDIR, N_DBFILE);
if(master->debug == TRUE) {
do_debug("Unlinking %s\n", ptr);
}
if(unlink(ptr) != 0 && errno != ENOENT) {
MyPerror(ptr);
retval = RETVAL_ERROR;
}
return retval;
}
/*------------------------------------------------------------------------------*/
int db_write(PMaster master) {
/* write out the entire database */
int retval = RETVAL_OK;
const char *fname;
int fd;
long itemnr, grpnr;
PList itemon;
PGroups grps;
fname = full_path(FP_GET, FP_TMPDIR, N_DBFILE);
if(master->debug == TRUE) {
do_debug("Writing entire db - %s\n", fname);
}
if((fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC
#ifdef EMX /* OS-2 */
| O_BINARY
#endif
, S_IRUSR | S_IWUSR)) == -1) {
retval = RETVAL_ERROR;
MyPerror(fname);
}
else {
/* start at the head of the list, add our dbnr */
itemon = master->head;
/* first write out number of items, so can read em in later*/
itemnr = master->nritems;
write(fd, &itemnr, sizeof(itemnr));
/* now write the list */
itemon = master->head;
itemnr = 0L;
while ( itemon != NULL && retval == RETVAL_OK) {
itemon->dbnr = itemnr;
if(write(fd, itemon, sizeof(List)) != sizeof(List)) {
error_log(ERRLOG_REPORT, suck_phrases[23], NULL);
retval = RETVAL_ERROR;
}
itemon = itemon->next;
itemnr++;
}
/* now write out the groups */
/* first write out the number of items, so can read em in later */
grps = master->groups;
grpnr = 0;
while(grps != NULL) {
grps = grps->next;
grpnr++;
}
write(fd, &grpnr, sizeof(grpnr));
grps = master->groups;
grpnr = 0L;
while (grps != NULL) {
if(write(fd, grps, sizeof(Groups)) != sizeof(Groups)) {
error_log(ERRLOG_REPORT, suck_phrases[23], NULL);
retval = RETVAL_ERROR;
}
grps = grps->next;
grpnr++;
}
close(fd);
}
return retval;
}
/*--------------------------------------------------------------------------------*/
int db_read(PMaster master) {
/* read in the entire database, and add it to the end of the current list */
int retval = RETVAL_OK;
const char *fname;
int fd;
PList itemon, ptr;
PGroups grpon, gptr;
long itemnr;
fname = full_path(FP_GET, FP_TMPDIR, N_DBFILE);
if(master->debug == TRUE) {
do_debug("Reading entire db - %s\n", fname);
}
if((fd = open(fname, O_RDONLY
#ifdef EMX /* OS-2 */
| O_BINARY
#endif
)) != -1) { /* read em in */
print_phrases(master->msgs, suck_phrases[2], NULL);
read(fd, &itemnr, sizeof(itemon)); /* get the number of items */
master->nritems = itemnr;
itemon = NULL;
/* have to malloc em first */
do {
if((ptr = malloc(sizeof(List))) == NULL) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[22], NULL);
}
else {
if(read(fd, ptr, sizeof(List)) == sizeof(List)) {
itemnr--;
/* add to list */
if( master->head == NULL) {
master->head = ptr;
}
else {
itemon->next = ptr;
}
itemon = ptr; /* so we can track where we're at */
if(master->debug == TRUE) {
do_debug("restart-read %s-%d-%ld-%d-%d-%d\n", ptr->msgnr,
ptr->groupnr, ptr->nr, ptr->mandatory, ptr->downloaded,
ptr->delete, ptr->sentcmd);
}
}
else {
retval = RETVAL_ERROR; /* didn't get in what we expected */
MyPerror(fname);
}
}
}
while(retval == RETVAL_OK && itemnr > 0);
/* now get the groups */
/* get the count first */
read(fd, &itemnr, sizeof(itemnr));
grpon = NULL;
do {
if((gptr = malloc(sizeof(Groups))) == NULL) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[22], NULL);
}
else
{
if(read(fd, gptr, sizeof(Groups)) == sizeof(Groups)) {
itemnr--;
if( master->groups == NULL) {
master->groups = gptr;
}
else {
grpon->next = gptr;
}
grpon = gptr;
}
else {
retval = RETVAL_ERROR;
MyPerror(fname);
}
}
}
while(retval == RETVAL_OK && itemnr > 0);
close(fd);
if(retval != RETVAL_OK) {
/* free up what we brought in */
itemon = master->head;
master->nritems = 0;
while (itemon != NULL) {
ptr = itemon->next;
free(itemon);
itemon = ptr;
}
grpon = master->groups;
while(grpon != NULL) {
gptr = grpon->next;
free(grpon);
grpon = gptr;
}
master->head = NULL;
master->groups = NULL;
}
}
return retval;
}
/*-----------------------------------------------------------------------------*/
int db_open(PMaster master) {
int retval = RETVAL_OK;
const char *fname;
if(master->db != -1) {
/* just to be on the safe side */
close(master->db);
}
fname = full_path(FP_GET, FP_TMPDIR, N_DBFILE);
/* we have the sync on it, so that we force the write to disk */
if((master->db = open (fname, O_WRONLY | O_SYNC
#ifdef EMX
| O_BINARY
#endif
)) == -1) {
MyPerror(fname);
retval = RETVAL_ERROR;
}
return retval;
}
/*-----------------------------------------------------------------------------*/
void db_close(PMaster master) {
if(master->db != -1) {
close(master->db);
}
}
/*------------------------------------------------------------------------------*/
int db_mark_dled(PMaster master, PList itemon) {
int retval = RETVAL_OK;
off_t offset;
/* first figure out where we need to be writing to */
offset = sizeof(long) + (itemon->dbnr * sizeof(List));
/* the extra long to skip the number we write out at beginning of file */
if(lseek(master->db, offset, SEEK_SET) == (off_t)-1) {
MyPerror(full_path(FP_GET, FP_TMPDIR, N_DBFILE));
retval = RETVAL_ERROR;
}
else {
itemon->downloaded = TRUE;
if(write(master->db, itemon, sizeof(List)) != sizeof(List)) {
MyPerror(full_path(FP_GET, FP_TMPDIR, N_DBFILE));
error_log(ERRLOG_REPORT, suck_phrases[23], NULL);
retval = RETVAL_ERROR;
}
}
return retval;
}
|