|
|
1.1 root 1: /* fixsmb.c */
2:
3: /* Synchronet message base (SMB) index re-generator */
4:
5: /* $Id: fixsmb.c,v 1.30 2005/10/02 23:32:25 rswindell Exp $ */
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: * *
11: * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html *
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:
38: #include <stdio.h>
39: #include <stdlib.h> /* atoi, qsort */
40: #include <string.h> /* strnicmp */
41: #include <ctype.h> /* toupper */
42:
43: #include "smblib.h"
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";
51:
52: int compare_index(const idxrec_t* idx1, const idxrec_t* idx2)
53: {
54: return(idx1->number - idx2->number);
55: }
56:
57: void sort_index(smb_t* smb)
58: {
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);
77:
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");
91: }
92:
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: }
99:
100: int fixsmb(char* sub)
101: {
102: char* p;
103: char* text;
104: char c;
105: int i,w;
106: ulong l,length,size,n;
107: smbmsg_t msg;
108:
109: memset(&smb,0,sizeof(smb));
110:
111: SAFECOPY(smb.file,sub);
112:
113: if((p=getfext(smb.file))!=NULL && stricmp(p,".shd")==0)
114: *p=0; /* Chop off .shd extension, if supplied on command-line */
115:
116: printf("Opening %s\n",smb.file);
117:
118: if((i=smb_open(&smb))!=0) {
119: printf("smb_open returned %d: %s\n",i,smb.last_error);
120: exit(1);
121: }
122:
123: if((i=smb_lock(&smb))!=0) {
124: printf("smb_lock returned %d: %s\n",i,smb.last_error);
125: exit(1);
126: }
127:
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: }
133:
134: if((i=smb_getstatus(&smb))!=0) {
135: smb_unlocksmbhdr(&smb);
136: smb_close(&smb);
137: printf("smb_getstatus returned %d: %s\n",i,smb.last_error);
138: exit(1);
139: }
140:
141: if(!(smb.status.attr&SMB_HYPERALLOC)) {
142:
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: }
148:
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 */
163:
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);
188: smb_unlockmsghdr(&smb,&msg);
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: if((i=smb_putmsg(&smb,&msg))!=0) {
221: printf("\nsmb_putmsg returned %d: %s\n",i,smb.last_error);
222: continue;
223: }
224: n++;
225: }
226:
227: if(!(smb.status.attr&SMB_HYPERALLOC)) {
228: /**************************/
229: /* Allocate header blocks */
230: /**************************/
231: fseek(smb.sha_fp,(l-smb.status.header_offset)/SHD_BLOCK_LEN,SEEK_SET);
232: if(msg.hdr.attr&MSG_DELETE) c=0; /* mark as free */
233: else c=1; /* or allocated */
234:
235: for(i=0;i<(int)(size/SHD_BLOCK_LEN);i++)
236: fputc(c,smb.sha_fp);
237:
238: /************************/
239: /* Allocate data blocks */
240: /************************/
241:
242: if(!(msg.hdr.attr&MSG_DELETE))
243: smb_incmsg_dfields(&smb,&msg,1);
244: }
245:
246: smb_freemsgmem(&msg);
247: }
248: printf("\r%79s\r100%%\n","");
249: smb.status.total_msgs=n;
250: if(renumber)
251: smb.status.last_msg=n;
252: else
253: sort_index(&smb);
254: printf("Saving message base status (%lu total messages).\n",n);
255: if((i=smb_putstatus(&smb))!=0)
256: printf("\nsmb_putstatus returned %d: %s\n",i,smb.last_error);
257: smb_unlocksmbhdr(&smb);
258: printf("Closing message base.\n");
259: smb_close(&smb);
260: unlock_msgbase();
261: printf("Done.\n");
262: return(0);
263: }
264:
265: int main(int argc, char **argv)
266: {
267: char revision[16];
268: int i;
269: str_list_t list;
270:
271: sscanf("$Revision: 1.30 $", "%*s %s", revision);
272:
273: printf("\nFIXSMB v2.10-%s (rev %s) SMBLIB %s - Rebuild Synchronet Message Base\n\n"
274: ,PLATFORM_DESC,revision,smb_lib_ver());
275:
276: list=strListInit();
277:
278: for(i=1;i<argc;i++) {
279: if(argv[i][0]=='-') {
280: if(!stricmp(argv[i],"-renumber"))
281: renumber=TRUE;
282: } else
283: strListPush(&list,argv[i]);
284: }
285:
286: if(!strListCount(list)) {
287: printf(usage);
288: exit(1);
289: }
290:
291: atexit(unlock_msgbase);
292:
293: for(i=0;list[i]!=NULL;i++)
294: fixsmb(list[i]);
295:
296: return(0);
297: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.