File: sq_help.c

package info (click to toggle)
smapi 2.4.0%2Brc2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 1,016 kB
  • ctags: 1,831
  • sloc: ansic: 11,304; pascal: 394; makefile: 137; asm: 125
file content (524 lines) | stat: -rw-r--r-- 12,670 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/***************************************************************************
 *                                                                         *
 *  Squish Developers Kit Source, Version 2.00                             *
 *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
 *                                                                         *
 *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
 *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
 *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
 *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
 *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
 *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
 *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
 *  ABLE TO REACH WITH THE AUTHOR.                                         *
 *                                                                         *
 *  You can contact the author at one of the address listed below:         *
 *                                                                         *
 *  Scott Dudley       FidoNet     1:249/106                               *
 *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
 *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
 *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
 *                                                                         *
 ***************************************************************************/
/*
#pragma off(unreferenced)
static char rcs_id[]="$Id: sq_help.c,v 1.9 2003/01/15 05:40:38 stas_degteff Exp $";
#pragma on(unreferenced)
*/
#define MSGAPI_HANDLERS
#define MSGAPI_NO_OLD_TYPES

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>


#include "compiler.h"

#ifdef HAS_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAS_IO_H
#  include <io.h>
#endif
#ifdef HAS_SHARE_H
#include <share.h>
#endif
#ifdef HAS_MALLOC_H
#include <malloc.h>
#endif

#include "prog.h"
#include "old_msg.h"
#include "msgapi.h"
#include "api_sq.h"
#include "api_sqp.h"
#include "apidebug.h"
#include "unused.h"

/* Read the base header from the beginning of the .sqd file */

unsigned _SquishReadBaseHeader(HAREA ha, SQBASE *psqb)
{
  if (lseek(Sqd->sfd, 0L, SEEK_SET) != 0 ||
      read_sqbase(Sqd->sfd, psqb) != 1)
  {
    if (errno==EACCES || errno==-1)
      msgapierr=MERR_SHARE;
    else
      msgapierr=MERR_BADF;

    return FALSE;
  }

  return TRUE;
}




/* Write the base header back to the beginning of the .sqd file */

unsigned _SquishWriteBaseHeader(HAREA ha, SQBASE *psqb)
{
  if (lseek(Sqd->sfd, 0L, SEEK_SET) != 0 ||
      write_sqbase(Sqd->sfd, psqb) != 1)
  {
    msgapierr=MERR_NODS;
    return FALSE;
  }

  return TRUE;
}



/* Release the specified frame to the free chain.  The frame is             *
 * located at offset 'fo', and the header at that offset has                *
 * already been loaded into *psqh.  This function does **not**              *
 * change the links of other messages that may point to this                *
 * message; it simply threads the current message into the free chain.      *
 *                                                                          *
 * This function assumes that we have exclusive access to the Squish base.  */

unsigned _SquishInsertFreeChain(HAREA ha, FOFS fo, SQHDR *psqh)
{
  SQHDR sqh=*psqh;

  assert(Sqd->fHaveExclusive);

  sqh.id=SQHDRID;
  sqh.frame_type=FRAME_FREE;
  sqh.msg_length=sqh.clen=0L;

  /* If we have no existing free frames, then this is easy */

  if (Sqd->foLastFree==NULL_FRAME)
  {
    sqh.prev_frame=NULL_FRAME;
    sqh.next_frame=NULL_FRAME;

    /* Try to write this frame back to the file */

    if (! _SquishWriteHdr(ha, fo, &sqh))
      return FALSE;

    Sqd->foFree=Sqd->foLastFree=fo;
    return TRUE;
  }


  /* There is an existing frame, so we must append to the end of the        *
   * chain.                                                                 */

  sqh.prev_frame=Sqd->foLastFree;
  sqh.next_frame=NULL_FRAME;


  /* Update the last chain in the free list, pointing it to us */

  if (!_SquishSetFrameNext(ha, sqh.prev_frame, fo))
    return FALSE;


  /* Try to write the current frame to disk */

  if (_SquishWriteHdr(ha, fo, &sqh))
  {
    Sqd->foLastFree=fo;
    return TRUE;
  }
  else
  {
    /* The write failed, so just hope that we can undo what we did        *
     * earlier.                                                           */

    (void)_SquishSetFrameNext(ha, sqh.prev_frame, NULL_FRAME);
    return FALSE;
  }
}



/* Change the 'next' link of the foModify frame to the value foValue */

unsigned _SquishSetFrameNext(HAREA ha, FOFS foModify, FOFS foValue)
{
  SQHDR sqh;

  if (!_SquishReadHdr(ha, foModify, &sqh))
    return FALSE;

  sqh.next_frame=foValue;

  return _SquishWriteHdr(ha, foModify, &sqh);
}



/* Change the 'prior' link of the foModify frame to the value foValue */

unsigned _SquishSetFramePrev(HAREA ha, FOFS foModify, FOFS foValue)
{
  SQHDR sqh;

  if (!_SquishReadHdr(ha, foModify, &sqh))
    return FALSE;

  sqh.prev_frame=foValue;

  return _SquishWriteHdr(ha, foModify, &sqh);
}



/* This function ensures that the message handle is readable */

unsigned _SquishReadMode(HMSG hmsg)
{
  if (hmsg->wMode != MOPEN_READ && hmsg->wMode != MOPEN_RW)
  {
    msgapierr=MERR_EACCES;
    return FALSE;
  }

  return TRUE;
}



/* This function ensures that the message handle is writable */

unsigned _SquishWriteMode(HMSG hmsg)
{
  if (hmsg->wMode != MOPEN_CREATE && hmsg->wMode != MOPEN_WRITE &&
      hmsg->wMode != MOPEN_RW)
  {
    msgapierr=MERR_EACCES;
    return FALSE;
  }

  return TRUE;
}



/* Translate a message number into a frame offset for area 'ha' */

FOFS _SquishGetFrameOfs(HAREA ha, dword dwMsg)
{
  SQIDX sqi;


  msgapierr=MERR_NOENT;

  /* Check for simple stuff that we can handle by following our own         *
   * linked list.                                                           */

  if (dwMsg==ha->cur_msg)
    return Sqd->foCur;
  else if (dwMsg==ha->cur_msg-1)
    return Sqd->foPrev;
  else if (dwMsg==ha->cur_msg+1)
    return Sqd->foNext;

  /* We couldn't just follow the linked list, so we will have to consult    *
   * the Squish index file to find it.                                      */

  if (! SidxGet(Sqd->hix, dwMsg, &sqi))
    return NULL_FRAME;

  return sqi.ofs;
}


/* Read the Squish header 'psqh' from the specified frame offset */

unsigned _SquishReadHdr(HAREA ha, FOFS fo, SQHDR *psqh)
{
  /* Ensure that we are reading a valid frame header */

  if (fo < SQBASE_SIZE)
  {
    msgapierr=MERR_BADA;
    return FALSE;
  }

  /* Seek and read the header */

  if (fo >= Sqd->foEnd ||
      lseek(Sqd->sfd, fo, SEEK_SET) != fo ||
      read_sqhdr(Sqd->sfd, psqh) != 1 ||
      psqh->id != SQHDRID)
  {
    msgapierr=MERR_BADF;
    return FALSE;
  }

  return TRUE;
}



/* Write the Squish header 'psqh' to the specified frame offset */

unsigned _SquishWriteHdr(HAREA ha, FOFS fo, SQHDR *psqh)
{
  /* Make sure that we don't write over the file header */

  if (fo < SQBASE_SIZE)
  {
    msgapierr=MERR_BADA;
    return FALSE;
  }

  if (lseek(Sqd->sfd, fo, SEEK_SET) != fo ||
      write_sqhdr(Sqd->sfd, psqh) != 1)
  {
    msgapierr=MERR_NODS;
    return FALSE;
  }

  return TRUE;
}


/* This function fixes the in-memory pointers after message dwMsg was       *
 * removed from the index file.                                             *
 *                                                                          *
 * This function assumes that we have exclusive access to the Squish base.  */

unsigned _SquishFixMemoryPointers(HAREA ha, dword dwMsg, SQHDR *psqh)
{
  assert(Sqd->fHaveExclusive);

  /* Adjust the first/last message pointers */

  if (dwMsg==1)
    Sqd->foFirst=psqh->next_frame;

  if (dwMsg==ha->num_msg)
    Sqd->foLast=psqh->prev_frame;


  /* Now fix up the in-memory version of the prior/next links */

  if (dwMsg==ha->cur_msg+1)
    Sqd->foNext=psqh->next_frame;

  if (dwMsg==ha->cur_msg-1)
    Sqd->foPrev=psqh->prev_frame;


  /* If we killed the message that we are on, it's a special case */

  if (dwMsg==ha->cur_msg)
  {
    SQHDR sqh;

    /* Go to the header of the prior msg */

    if (!_SquishReadHdr(ha, psqh->prev_frame, &sqh))
    {
      /* That does not exist, so go to msg 0 */

      Sqd->foCur=Sqd->foPrev=NULL_FRAME;
      Sqd->foNext=Sqd->foFirst;
      ha->cur_msg=0;
    }
    else
    {
      /* Otherwise, adjust pointers appropriately */

      Sqd->foCur=psqh->prev_frame;
      Sqd->foPrev=sqh.prev_frame;
      Sqd->foNext=sqh.next_frame;
      ha->cur_msg--;
    }
  }
  else
  {
    /* We didn't kill the current msg, so just decrement cur_msg if         *
     * we were higher than the deleted message.                             */

    if (ha->cur_msg >= dwMsg)
      ha->cur_msg--;
  }


  /* Adjust the message numbers appropriately */

  ha->num_msg--;
  ha->high_msg--;

  if (ha->high_water >= dwMsg)
    ha->high_water--;

  return TRUE;
}





/* Write the index back to disk and free the associated memory */

unsigned _SquishFreeIndex(HAREA ha, dword dwMsg, SQIDX *psqi,
                          dword dwIdxSize, unsigned fWrite)
{
  unsigned rc=TRUE;
  long ofs;

#ifdef __WATCOMC__
dwIdxSize=dwIdxSize; /* To prevent warning */
#endif

  if (fWrite)
  {
    /* Seek to the offset of the message that we want to delete */

    ofs=((long)dwMsg-1L) * (long)SQIDX_SIZE;

    /* Write it back out to disk at the same position */

    rc=(lseek(Sqd->ifd, ofs, SEEK_SET)==ofs &&
        write_sqidx(Sqd->ifd, psqi, ((long)dwMsg-1L)) == 1);
  }

  pfree(psqi);

  return rc;
}



#if 0
/* Read from the index file, starting at the dwMsg'th record */

SQIDX * _SquishAllocIndex(HAREA ha, dword dwMsg, dword *pdwIdxSize)
{
  SQIDX *psqi;
  dword dwIdxSize;
  long ofs;

  /* We only need enough memory to read in the index file from the point    *
   * that we are deleting a message.                                        */

  dwIdxSize = ((long)ha->num_msg - (long)dwMsg + 1L) * (long)sizeof(SQIDX);


  /* Handle problems that we may have when working on a 16-bit platform */

  if (dwIdxSize > 65000L)
  {
  }

  /* Allocate memory for handling the index */

  if ((psqi=palloc((size_t)dwIdxSize))==NULL)
  {
    msgapierr=MERR_NOMEM;
    return NULL;
  }


  /* Seek to the offset of the message that we want to delete */

  ofs=((long)dwMsg-1L) * (long)SQIDX_SIZE;


  /* Now read it from disk */

  if (lseek(Sqd->ifd, ofs, SEEK_SET) != ofs ||
      read_sqidx(Sqd->ifd, psqi, ((long)ha->num_msg - (long)dwMsg + 1L)) != 1)
  {
    msgapierr=MERR_BADF;
    pfree(psqi);
    return NULL;
  }

  *pdwIdxSize=dwIdxSize;
  return psqi;
}

/* This function removes the specified message number from the Squish index *
 * file, moving the rest of the index file back over the message number to  *
 * fill in the gaps.                                                        *
 *                                                                          *
 * This function also adjusts the message number pointers everywhere to     *
 * accomodate for the deletion of this message.                             *
 *                                                                          *
 * This function assumes that we have exclusive access to the Squish base.  */

unsigned _SquishRemoveIndex(HAREA ha, dword dwMsg, SQIDX *psqiOut, SQHDR *psqh)
{
  dword dwIdxSize;
  SQIDX *psqiLast;
  SQIDX *psqi;
  unsigned rc;

  assert(Sqd->fHaveExclusive);

  /* Read the index from disk */

  if ((psqi=_SquishAllocIndex(ha, dwMsg, &dwIdxSize))==NULL)
    return FALSE;


  /* If the caller wants a copy of the record that we are deleting... */

  if (psqiOut)
    memmove(psqiOut, psqi, sizeof(SQIDX));


  /* Shift everything over by one to accomodate for the deleted msg */

  memmove(psqi, psqi+1, (size_t)dwIdxSize-sizeof(SQIDX));


  /* Blank out the last index pointer in the file so that it is invalid */

  psqiLast=psqi + (ha->num_msg - (long)dwMsg);

  psqiLast->ofs=NULL_FRAME;
  psqiLast->umsgid=(UMSGID)-1L;
  psqiLast->hash=(dword)-1L;


  /* Write it back to disk and free memory */

  rc=_SquishFreeIndex(ha, dwMsg, psqi, dwIdxSize, TRUE);


  /* If the delete succeeded, adjust the memory pointers */

  if (rc)
    rc=_SquishFixMemoryPointers(ha, dwMsg, psqh);

  return rc;
}
#endif