|
|
1.1 root 1: /* fixsmb.c */
2:
3: /* Synchronet message base (SMB) index re-generator */
4:
1.1.1.2 ! root 5: /* $Id: fixsmb.c,v 1.28 2004/12/29 10:15:32 rswindell Exp $ */
1.1 root 6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
1.1.1.2 ! root 11: * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html *
1.1 root 12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
1.1.1.2 ! root 38: #include <stdio.h>
! 39: #include <stdlib.h> /* atoi, qsort */
1.1 root 40: #include <string.h> /* strnicmp */
41: #include <ctype.h> /* toupper */
42:
43: #include "smblib.h"
1.1.1.2 ! root 44: #include "genwrap.h" /* PLATFORM_DESC */
! 45: #include "str_list.h" /* strList API */
! 46: #include "crc16.h"
! 47:
! 48: smb_t smb;
! 49: BOOL renumber=FALSE;
! 50: char* usage="usage: fixsmb [-renumber] <smb_file> [[smb_file] [...]]\n";
1.1 root 51:
1.1.1.2 ! root 52: int compare_index(const idxrec_t* idx1, const idxrec_t* idx2)
1.1 root 53: {
1.1.1.2 ! root 54: return(idx1->number - idx2->number);
1.1 root 55: }
56:
1.1.1.2 ! root 57: void sort_index(smb_t* smb)
1.1 root 58: {
1.1.1.2 ! root 59: ulong l;
! 60: idxrec_t* idx;
! 61:
! 62: printf("Sorting index... ");
! 63: if((idx=malloc(sizeof(idxrec_t)*smb->status.total_msgs))==NULL) {
! 64: perror("malloc");
! 65: return;
! 66: }
! 67:
! 68: rewind(smb->sid_fp);
! 69: for(l=0;l<smb->status.total_msgs;l++)
! 70: if(fread(&idx[l],sizeof(idxrec_t),1,smb->sid_fp)<1) {
! 71: perror("reading index");
! 72: break;
! 73: }
! 74:
! 75: qsort(idx,l,sizeof(idxrec_t)
! 76: ,(int(*)(const void*, const void*))compare_index);
1.1 root 77:
1.1.1.2 ! root 78: rewind(smb->sid_fp);
! 79: chsize(fileno(smb->sid_fp),0L); /* Truncate the index */
! 80:
! 81: printf("\nRe-writing index... \n");
! 82: smb->status.total_msgs=l;
! 83: for(l=0;l<smb->status.total_msgs;l++)
! 84: if(fwrite(&idx[l],sizeof(idxrec_t),1,smb->sid_fp)<1) {
! 85: perror("writing index");
! 86: break;
! 87: }
! 88:
! 89: free(idx);
! 90: printf("\n");
1.1 root 91: }
92:
1.1.1.2 ! root 93: void unlock_msgbase(void)
! 94: {
! 95: int i;
! 96: if(smb_islocked(&smb) && (i=smb_unlock(&smb))!=0)
! 97: printf("smb_unlock returned %d: %s\n",i,smb.last_error);
! 98: }
1.1 root 99:
1.1.1.2 ! root 100: int fixsmb(char* sub)
1.1 root 101: {
1.1.1.2 ! root 102: char* p;
! 103: char* text;
! 104: char c;
! 105: int i,w;
1.1 root 106: ulong l,length,size,n;
107: smbmsg_t msg;
108:
1.1.1.2 ! root 109: memset(&smb,0,sizeof(smb));
1.1 root 110:
1.1.1.2 ! root 111: SAFECOPY(smb.file,sub);
1.1 root 112:
1.1.1.2 ! root 113: if((p=getfext(smb.file))!=NULL && stricmp(p,".shd")==0)
! 114: *p=0; /* Chop off .shd extension, if supplied on command-line */
1.1 root 115:
1.1.1.2 ! root 116: printf("Opening %s\n",smb.file);
1.1 root 117:
1.1.1.2 ! root 118: if((i=smb_open(&smb))!=0) {
! 119: printf("smb_open returned %d: %s\n",i,smb.last_error);
! 120: exit(1);
! 121: }
1.1 root 122:
1.1.1.2 ! root 123: if((i=smb_lock(&smb))!=0) {
! 124: printf("smb_lock returned %d: %s\n",i,smb.last_error);
! 125: exit(1);
! 126: }
1.1 root 127:
1.1.1.2 ! root 128: if((i=smb_locksmbhdr(&smb))!=0) {
! 129: smb_close(&smb);
! 130: printf("smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
! 131: exit(1);
! 132: }
1.1 root 133:
1.1.1.2 ! root 134: if((i=smb_getstatus(&smb))!=0) {
1.1 root 135: smb_unlocksmbhdr(&smb);
136: smb_close(&smb);
1.1.1.2 ! root 137: printf("smb_getstatus returned %d: %s\n",i,smb.last_error);
! 138: exit(1);
! 139: }
1.1 root 140:
1.1.1.2 ! root 141: if(!(smb.status.attr&SMB_HYPERALLOC)) {
1.1 root 142:
1.1.1.2 ! root 143: if((i=smb_open_ha(&smb))!=0) {
! 144: smb_close(&smb);
! 145: printf("smb_open_ha returned %d: %s\n",i,smb.last_error);
! 146: exit(1);
! 147: }
1.1 root 148:
1.1.1.2 ! root 149: if((i=smb_open_da(&smb))!=0) {
! 150: smb_close(&smb);
! 151: printf("smb_open_da returned %d: %s\n",i,smb.last_error);
! 152: exit(1);
! 153: }
! 154:
! 155: rewind(smb.sha_fp);
! 156: chsize(fileno(smb.sha_fp),0L); /* Truncate the header allocation file */
! 157: rewind(smb.sda_fp);
! 158: chsize(fileno(smb.sda_fp),0L); /* Truncate the data allocation file */
! 159: }
! 160:
! 161: rewind(smb.sid_fp);
! 162: chsize(fileno(smb.sid_fp),0L); /* Truncate the index */
1.1 root 163:
1.1.1.2 ! root 164:
! 165: if(!(smb.status.attr&SMB_HYPERALLOC)) {
! 166: length=filelength(fileno(smb.sdt_fp));
! 167: w=0;
! 168: for(l=0;l<length;l+=SDT_BLOCK_LEN) /* Init .SDA file to NULL */
! 169: fwrite(&w,2,1,smb.sda_fp);
! 170:
! 171: length=filelength(fileno(smb.shd_fp));
! 172: c=0;
! 173: for(l=0;l<length;l+=SHD_BLOCK_LEN) /* Init .SHD file to NULL */
! 174: fwrite(&c,1,1,smb.sha_fp);
! 175: } else
! 176: length=filelength(fileno(smb.shd_fp));
! 177:
! 178: n=0; /* messsage offset */
! 179: for(l=smb.status.header_offset;l<length;l+=size) {
! 180: size=SHD_BLOCK_LEN;
! 181: printf("\r%2lu%% ",(long)(100.0/((float)length/l)));
! 182: msg.idx.offset=l;
! 183: if((i=smb_lockmsghdr(&smb,&msg))!=0) {
! 184: printf("\n(%06lX) smb_lockmsghdr returned %d:\n%s\n",l,i,smb.last_error);
! 185: continue;
! 186: }
! 187: i=smb_getmsghdr(&smb,&msg);
1.1 root 188: smb_unlockmsghdr(&smb,&msg);
1.1.1.2 ! root 189: if(i!=0) {
! 190: printf("\n(%06lX) smb_getmsghdr returned %d:\n%s\n",l,i,smb.last_error);
! 191: continue;
! 192: }
! 193: size=smb_hdrblocks(smb_getmsghdrlen(&msg))*SHD_BLOCK_LEN;
! 194: printf("#%-5lu (%06lX) %-25.25s ",msg.hdr.number,l,msg.from);
! 195:
! 196: /* Create hash record */
! 197: if(msg.hdr.attr&MSG_DELETE)
! 198: text=NULL;
! 199: else
! 200: text=smb_getmsgtxt(&smb,&msg,GETMSGTXT_BODY_ONLY);
! 201: i=smb_hashmsg(&smb,&msg,text,TRUE /* update */);
! 202: if(i!=SMB_SUCCESS)
! 203: printf("!ERROR %d hashing message\n", i);
! 204: if(text!=NULL)
! 205: free(text);
! 206:
! 207: /* Index the header */
! 208: if(msg.hdr.attr&MSG_DELETE)
! 209: printf("Not indexing deleted message\n");
! 210: else if(msg.hdr.number==0)
! 211: printf("Not indexing invalid message number (0)!\n");
! 212: else {
! 213: msg.offset=n;
! 214: if(renumber)
! 215: msg.hdr.number=n+1;
! 216: if(msg.hdr.netattr&MSG_INTRANSIT) {
! 217: printf("Removing 'in transit' attribute\n");
! 218: msg.hdr.netattr&=~MSG_INTRANSIT;
! 219: }
! 220: msg.idx.number=msg.hdr.number;
! 221: msg.idx.attr=msg.hdr.attr;
! 222: msg.idx.time=msg.hdr.when_imported.time;
! 223: smb_init_idx(&smb,&msg);
! 224: if((i=smb_putmsg(&smb,&msg))!=0) {
! 225: printf("\nsmb_putmsg returned %d: %s\n",i,smb.last_error);
! 226: continue;
! 227: }
! 228: n++;
! 229: }
! 230:
! 231: if(!(smb.status.attr&SMB_HYPERALLOC)) {
! 232: /**************************/
! 233: /* Allocate header blocks */
! 234: /**************************/
! 235: fseek(smb.sha_fp,(l-smb.status.header_offset)/SHD_BLOCK_LEN,SEEK_SET);
! 236: if(msg.hdr.attr&MSG_DELETE) c=0; /* mark as free */
! 237: else c=1; /* or allocated */
! 238:
! 239: for(i=0;i<(int)(size/SHD_BLOCK_LEN);i++)
! 240: fputc(c,smb.sha_fp);
! 241:
! 242: /************************/
! 243: /* Allocate data blocks */
! 244: /************************/
! 245:
! 246: if(!(msg.hdr.attr&MSG_DELETE))
! 247: smb_incmsg_dfields(&smb,&msg,1);
! 248: }
! 249:
! 250: smb_freemsgmem(&msg);
! 251: }
! 252: printf("\r%79s\r100%%\n","");
! 253: smb.status.total_msgs=n;
! 254: if(renumber)
! 255: smb.status.last_msg=n;
1.1 root 256: else
1.1.1.2 ! root 257: sort_index(&smb);
! 258: printf("Saving message base status (%lu total messages).\n",n);
! 259: if((i=smb_putstatus(&smb))!=0)
! 260: printf("\nsmb_putstatus returned %d: %s\n",i,smb.last_error);
! 261: smb_unlocksmbhdr(&smb);
! 262: printf("Closing message base.\n");
! 263: smb_close(&smb);
! 264: unlock_msgbase();
! 265: printf("Done.\n");
! 266: return(0);
! 267: }
1.1 root 268:
1.1.1.2 ! root 269: int main(int argc, char **argv)
! 270: {
! 271: char revision[16];
! 272: int i;
! 273: str_list_t list;
! 274:
! 275: sscanf("$Revision: 1.28 $", "%*s %s", revision);
! 276:
! 277: printf("\nFIXSMB v2.10-%s (rev %s) SMBLIB %s - Rebuild Synchronet Message Base\n\n"
! 278: ,PLATFORM_DESC,revision,smb_lib_ver());
! 279:
! 280: list=strListInit();
! 281:
! 282: for(i=1;i<argc;i++) {
! 283: if(argv[i][0]=='-') {
! 284: if(!stricmp(argv[i],"-renumber"))
! 285: renumber=TRUE;
! 286: } else
! 287: strListPush(&list,argv[i]);
! 288: }
! 289:
! 290: if(!strListCount(list)) {
! 291: printf(usage);
! 292: exit(1);
! 293: }
! 294:
! 295: atexit(unlock_msgbase);
! 296:
! 297: for(i=0;list[i]!=NULL;i++)
! 298: fixsmb(list[i]);
! 299:
! 300: return(0);
1.1 root 301: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.