~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/azio.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 18:24:57 UTC
  • mfrom: (1999.6.3 trunk)
  • Revision ID: brian@tangent.org-20101218182457-yi1wd0so2hml1k1w
Merge in Lee's copyright header fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
/* @(#) $Id$ */
13
13
 
 
14
#include "config.h"
 
15
 
14
16
#include "azio.h"
15
17
 
16
 
#include <stdio.h>
17
 
#include <string.h>
18
 
#include <assert.h>
 
18
#include <fcntl.h>
19
19
#include <unistd.h>
20
20
 
 
21
#include <cstdio>
 
22
#include <cstring>
 
23
#include <cstdlib>
 
24
#include <cassert>
 
25
 
 
26
using namespace drizzled;
 
27
 
21
28
static int const az_magic[3] = {0xfe, 0x03, 0x01}; /* az magic header */
22
29
 
23
 
/* gzip flag uchar */
24
 
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
25
 
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
26
 
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
27
 
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
28
 
#define COMMENT      0x10 /* bit 4 set: file comment present */
29
 
#define RESERVED     0xE0 /* bits 5..7: reserved */
30
 
 
31
30
static unsigned int azwrite(azio_stream *s, void *buf, unsigned int len);
32
31
static int azrewind (azio_stream *s);
33
32
static unsigned int azio_enable_aio(azio_stream *s);
34
33
static int do_flush(azio_stream *file, int flush);
35
34
static int    get_byte(azio_stream *s);
36
35
static void   check_header(azio_stream *s);
37
 
static void write_header(azio_stream *s);
 
36
static int write_header(azio_stream *s);
38
37
static int    destroy(azio_stream *s);
39
38
static void putLong(azio_stream *s, uLong x);
40
39
static uLong  getLong(azio_stream *s);
44
43
static void do_aio_cleanup(azio_stream *s);
45
44
#endif
46
45
 
47
 
static pthread_handler_t run_task(void *p)
 
46
extern "C" pthread_handler_t run_task(void *p);
 
47
 
 
48
extern "C" pthread_handler_t run_task(void *p)
48
49
{
49
50
  int fd;
50
51
  char *buffer;
51
52
  size_t offset;
52
 
  azio_stream *s= (azio_stream *)p;  
53
 
 
54
 
  my_thread_init();
 
53
  azio_stream *s= (azio_stream *)p;
55
54
 
56
55
  while (1)
57
56
  {
62
61
    }
63
62
    offset= s->container.offset;
64
63
    fd= s->container.fd;
65
 
    buffer= s->container.buffer;
 
64
    buffer= (char *)s->container.buffer;
66
65
    pthread_mutex_unlock(&s->container.thresh_mutex);
67
66
 
68
67
    if (s->container.ready == AZ_THREAD_DEAD)
76
75
    pthread_mutex_unlock(&s->container.thresh_mutex);
77
76
  }
78
77
 
79
 
  my_thread_end();
80
 
 
81
78
  return 0;
82
79
}
83
80
 
84
81
static void azio_kill(azio_stream *s)
85
82
{
86
83
  pthread_mutex_lock(&s->container.thresh_mutex);
87
 
  s->container.ready= AZ_THREAD_DEAD; 
 
84
  s->container.ready= AZ_THREAD_DEAD;
88
85
  pthread_mutex_unlock(&s->container.thresh_mutex);
89
86
 
90
87
  pthread_cond_signal(&s->container.threshhold);
91
 
  pthread_join(s->container.mainthread, (void *)NULL);
 
88
  pthread_join(s->container.mainthread, NULL);
92
89
}
93
90
 
94
91
static size_t azio_return(azio_stream *s)
126
123
  pthread_attr_init(&attr);
127
124
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
128
125
 
129
 
  s->container.ready= AZ_THREAD_FINISHED; 
 
126
  s->container.ready= AZ_THREAD_FINISHED;
130
127
 
131
128
  /* If we don't create a thread, signal the caller */
132
129
  if (pthread_create(&s->container.mainthread, &attr, run_task,
141
138
static int azio_read(azio_stream *s)
142
139
{
143
140
  pthread_mutex_lock(&s->container.thresh_mutex);
144
 
  s->container.ready= AZ_THREAD_ACTIVE; 
 
141
  s->container.ready= AZ_THREAD_ACTIVE;
145
142
  pthread_mutex_unlock(&s->container.thresh_mutex);
146
143
  pthread_cond_broadcast(&s->container.threshhold);
147
144
 
162
159
  int err;
163
160
  int level = Z_DEFAULT_COMPRESSION ; /* compression level */
164
161
  int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
165
 
  File fd= -1;
 
162
  int fd= -1;
166
163
 
167
164
  memset(s, 0, sizeof(azio_stream));
168
165
 
187
184
  s->method= method;
188
185
 
189
186
  /*
190
 
    We do our own version of append by nature. 
 
187
    We do our own version of append by nature.
191
188
    We must always have write access to take card of the header.
192
189
  */
193
190
  assert(Flags | O_APPEND);
194
191
  assert(Flags | O_WRONLY);
195
192
 
196
 
  if (Flags & O_RDWR) 
 
193
  if (Flags & O_RDWR)
197
194
    s->mode = 'w';
198
195
 
199
 
  if (s->mode == 'w') 
 
196
  if (s->mode == 'w')
200
197
  {
201
198
    err = deflateInit2(&(s->stream), level,
202
199
                       Z_DEFLATED, -MAX_WBITS, 8, strategy);
228
225
  s->stream.avail_out = AZ_BUFSIZE_WRITE;
229
226
 
230
227
  errno = 0;
231
 
  s->file = fd < 0 ? my_open(path, Flags, MYF(0)) : fd;
 
228
  s->file = fd < 0 ? internal::my_open(path, Flags, MYF(0)) : fd;
232
229
#ifdef AZIO_AIO
233
230
  s->container.fd= s->file;
234
231
#endif
235
232
 
236
 
  if (s->file < 0 ) 
 
233
  if (s->file < 0 )
237
234
  {
238
235
    destroy(s);
239
236
    return Z_NULL;
240
237
  }
241
238
 
242
 
  if (Flags & O_CREAT || Flags & O_TRUNC) 
 
239
  if (Flags & O_CREAT || Flags & O_TRUNC)
243
240
  {
244
241
    s->rows= 0;
245
242
    s->forced_flushes= 0;
253
250
    s->frm_length= 0;
254
251
    s->dirty= 1; /* We create the file dirty */
255
252
    s->start = AZHEADER_SIZE + AZMETA_BUFFER_SIZE;
256
 
    write_header(s);
257
 
    s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
 
253
    if(write_header(s))
 
254
      return Z_NULL;
 
255
    s->pos= (size_t)lseek(s->file, 0, SEEK_END);
258
256
  }
259
 
  else if (s->mode == 'w') 
 
257
  else if (s->mode == 'w')
260
258
  {
261
 
    uchar buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
262
 
    pread(s->file, buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0);
 
259
    unsigned char buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
 
260
    const ssize_t read_size= AZHEADER_SIZE + AZMETA_BUFFER_SIZE;
 
261
    if(pread(s->file, buffer, read_size, 0) < read_size)
 
262
      return Z_NULL;
263
263
    read_header(s, buffer);
264
 
    s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
 
264
    s->pos= (size_t)lseek(s->file, 0, SEEK_END);
265
265
  }
266
266
  else
267
267
  {
282
282
}
283
283
 
284
284
 
285
 
void write_header(azio_stream *s)
 
285
int write_header(azio_stream *s)
286
286
{
287
287
  char buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
288
288
  char *ptr= buffer;
306
306
  int4store(ptr + AZ_COMMENT_LENGTH_POS, s->comment_length); /* COMMENT Block */
307
307
  int4store(ptr + AZ_META_POS, 0); /* Meta Block */
308
308
  int4store(ptr + AZ_META_LENGTH_POS, 0); /* Meta Block */
309
 
  int8store(ptr + AZ_START_POS, (unsigned long long)s->start); /* Start of Data Block Index Block */
310
 
  int8store(ptr + AZ_ROW_POS, (unsigned long long)s->rows); /* Start of Data Block Index Block */
311
 
  int8store(ptr + AZ_FLUSH_POS, (unsigned long long)s->forced_flushes); /* Start of Data Block Index Block */
312
 
  int8store(ptr + AZ_CHECK_POS, (unsigned long long)s->check_point); /* Start of Data Block Index Block */
313
 
  int8store(ptr + AZ_AUTOINCREMENT_POS, (unsigned long long)s->auto_increment); /* Start of Data Block Index Block */
 
309
  int8store(ptr + AZ_START_POS, (uint64_t)s->start); /* Start of Data Block Index Block */
 
310
  int8store(ptr + AZ_ROW_POS, (uint64_t)s->rows); /* Start of Data Block Index Block */
 
311
  int8store(ptr + AZ_FLUSH_POS, (uint64_t)s->forced_flushes); /* Start of Data Block Index Block */
 
312
  int8store(ptr + AZ_CHECK_POS, (uint64_t)s->check_point); /* Start of Data Block Index Block */
 
313
  int8store(ptr + AZ_AUTOINCREMENT_POS, (uint64_t)s->auto_increment); /* Start of Data Block Index Block */
314
314
  int4store(ptr+ AZ_LONGEST_POS , s->longest_row); /* Longest row */
315
315
  int4store(ptr+ AZ_SHORTEST_POS, s->shortest_row); /* Shorest row */
316
 
  int4store(ptr+ AZ_FRM_POS, 
 
316
  int4store(ptr+ AZ_FRM_POS,
317
317
            AZHEADER_SIZE + AZMETA_BUFFER_SIZE); /* FRM position */
318
318
  *(ptr + AZ_DIRTY_POS)= (unsigned char)s->dirty; /* Start of Data Block Index Block */
319
319
 
320
320
  /* Always begin at the begining, and end there as well */
321
 
  pwrite(s->file, (uchar*) buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0);
 
321
  const ssize_t write_size= AZHEADER_SIZE + AZMETA_BUFFER_SIZE;
 
322
  if(pwrite(s->file, (unsigned char*) buffer, write_size, 0)!=write_size)
 
323
    return -1;
 
324
 
 
325
  return 0;
322
326
}
323
327
 
324
328
/* ===========================================================================
326
330
  for end of file.
327
331
  IN assertion: the stream s has been sucessfully opened for reading.
328
332
*/
329
 
int get_byte(s)
330
 
  azio_stream *s;
 
333
int get_byte(azio_stream *s)
331
334
{
332
335
  if (s->z_eof) return EOF;
333
 
  if (s->stream.avail_in == 0) 
 
336
  if (s->stream.avail_in == 0)
334
337
  {
335
338
    errno = 0;
336
 
    if (s->stream.avail_in == 0) 
 
339
    if (s->stream.avail_in == 0)
337
340
    {
338
341
      s->z_eof = 1;
339
342
      return EOF;
367
370
  if (len < 2) {
368
371
    if (len) s->inbuf[0] = s->stream.next_in[0];
369
372
    errno = 0;
370
 
    len = (uInt)pread(s->file, (uchar *)s->inbuf + len, AZ_BUFSIZE_READ >> len, s->pos);
 
373
    len = (uInt)pread(s->file, (unsigned char *)s->inbuf + len, AZ_BUFSIZE_READ >> len, s->pos);
371
374
    s->pos+= len;
372
375
    if (len == (uInt)-1) s->z_err = Z_ERRNO;
373
376
    s->stream.avail_in += len;
402
405
    s->minor_version= (unsigned int)buffer[AZ_MINOR_VERSION_POS];
403
406
    s->block_size= 1024 * buffer[AZ_BLOCK_POS];
404
407
    s->start= (size_t)uint8korr(buffer + AZ_START_POS);
405
 
    s->rows= (unsigned long long)uint8korr(buffer + AZ_ROW_POS);
406
 
    s->check_point= (unsigned long long)uint8korr(buffer + AZ_CHECK_POS);
407
 
    s->forced_flushes= (unsigned long long)uint8korr(buffer + AZ_FLUSH_POS);
408
 
    s->auto_increment= (unsigned long long)uint8korr(buffer + AZ_AUTOINCREMENT_POS);
 
408
    s->rows= (uint64_t)uint8korr(buffer + AZ_ROW_POS);
 
409
    s->check_point= (uint64_t)uint8korr(buffer + AZ_CHECK_POS);
 
410
    s->forced_flushes= (uint64_t)uint8korr(buffer + AZ_FLUSH_POS);
 
411
    s->auto_increment= (uint64_t)uint8korr(buffer + AZ_AUTOINCREMENT_POS);
409
412
    s->longest_row= (unsigned int)uint4korr(buffer + AZ_LONGEST_POS);
410
413
    s->shortest_row= (unsigned int)uint4korr(buffer + AZ_SHORTEST_POS);
411
414
    s->frm_start_pos= (unsigned int)uint4korr(buffer + AZ_FRM_POS);
425
428
 * Cleanup then free the given azio_stream. Return a zlib error code.
426
429
 Try freeing in the reverse order of allocations.
427
430
 */
428
 
int destroy (s)
429
 
  azio_stream *s;
 
431
int destroy (azio_stream *s)
430
432
{
431
433
  int err = Z_OK;
432
434
 
433
 
  if (s->stream.state != NULL) 
 
435
  if (s->stream.state != NULL)
434
436
  {
435
 
    if (s->mode == 'w') 
 
437
    if (s->mode == 'w')
436
438
    {
437
439
      err = deflateEnd(&(s->stream));
438
 
      my_sync(s->file, MYF(0));
 
440
      internal::my_sync(s->file, MYF(0));
439
441
    }
440
 
    else if (s->mode == 'r') 
 
442
    else if (s->mode == 'r')
441
443
      err = inflateEnd(&(s->stream));
442
444
  }
443
445
 
444
446
  do_aio_cleanup(s);
445
447
 
446
 
  if (s->file > 0 && my_close(s->file, MYF(0))) 
 
448
  if (s->file > 0 && internal::my_close(s->file, MYF(0)))
447
449
      err = Z_ERRNO;
448
450
 
449
451
  s->file= -1;
457
459
  Reads the given number of uncompressed bytes from the compressed file.
458
460
  azread returns the number of bytes actually read (0 for end of file).
459
461
*/
460
 
unsigned int azread_internal( azio_stream *s, voidp buf, unsigned int len, int *error)
 
462
/*
 
463
   This function is legacy, do not use.
 
464
 
 
465
     Reads the given number of uncompressed bytes from the compressed file.
 
466
   If the input file was not in gzip format, gzread copies the given number
 
467
   of bytes into the buffer.
 
468
     gzread returns the number of uncompressed bytes actually read (0 for
 
469
   end of file, -1 for error).
 
470
*/
 
471
static unsigned int azread_internal( azio_stream *s, voidp buf, unsigned int len, int *error)
461
472
{
462
473
  Bytef *start = (Bytef*)buf; /* starting point for crc computation */
463
474
  Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
464
475
  *error= 0;
465
476
 
466
477
  if (s->mode != 'r')
467
 
  { 
 
478
  {
468
479
    *error= Z_STREAM_ERROR;
469
480
    return 0;
470
481
  }
471
482
 
472
483
  if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
473
 
  { 
 
484
  {
474
485
    *error= s->z_err;
475
486
    return 0;
476
487
  }
477
488
 
478
489
  if (s->z_err == Z_STREAM_END)  /* EOF */
479
 
  { 
 
490
  {
480
491
    return 0;
481
492
  }
482
493
 
493
504
    start++;
494
505
    if (s->last) {
495
506
      s->z_err = Z_STREAM_END;
496
 
      { 
 
507
      {
497
508
        return 1;
498
509
      }
499
510
    }
505
516
 
506
517
      errno = 0;
507
518
      get_block(s);
508
 
      if (s->stream.avail_in == 0) 
 
519
      if (s->stream.avail_in == 0)
509
520
      {
510
521
        s->z_eof = 1;
511
522
      }
531
542
         * Check for such files:
532
543
       */
533
544
        check_header(s);
534
 
        if (s->z_err == Z_OK) 
 
545
        if (s->z_err == Z_OK)
535
546
        {
536
547
          inflateReset(&(s->stream));
537
548
          s->crc = crc32(0L, Z_NULL, 0);
554
565
}
555
566
 
556
567
/* ===========================================================================
557
 
  Experimental Interface. We abstract out a concecpt of rows 
 
568
  Experimental Interface. We abstract out a concecpt of rows
558
569
*/
559
570
size_t azwrite_row(azio_stream *s, void *buf, unsigned int len)
560
571
{
588
599
  size_t read;
589
600
 
590
601
  read= azread_internal(s, buffer, sizeof(unsigned int), error);
591
 
  
 
602
 
592
603
  /* On error the return value will be zero as well */
593
604
  if (read == 0)
594
605
    return read;
597
608
  new_ptr= (char *)realloc(s->row_ptr, (sizeof(char) * row_length));
598
609
 
599
610
  if (!new_ptr)
600
 
    return -1;
 
611
    return SIZE_MAX;
601
612
 
602
613
  s->row_ptr= new_ptr;
603
614
 
617
628
  s->stream.next_in = (Bytef*)buf;
618
629
  s->stream.avail_in = len;
619
630
 
620
 
  while (s->stream.avail_in != 0) 
 
631
  while (s->stream.avail_in != 0)
621
632
  {
622
 
    if (s->stream.avail_out == 0) 
 
633
    if (s->stream.avail_out == 0)
623
634
    {
624
635
 
625
636
      s->stream.next_out = s->outbuf;
626
 
      if (pwrite(s->file, (uchar *)s->outbuf, AZ_BUFSIZE_WRITE, s->pos) != AZ_BUFSIZE_WRITE) 
 
637
      if (pwrite(s->file, (unsigned char *)s->outbuf, AZ_BUFSIZE_WRITE, s->pos) != AZ_BUFSIZE_WRITE)
627
638
      {
628
639
        s->z_err = Z_ERRNO;
629
640
        break;
658
669
 
659
670
  s->stream.avail_in = 0; /* should be zero already anyway */
660
671
 
661
 
  for (;;) 
 
672
  for (;;)
662
673
  {
663
674
    len = AZ_BUFSIZE_WRITE - s->stream.avail_out;
664
675
 
665
 
    if (len != 0) 
 
676
    if (len != 0)
666
677
    {
667
 
      if ((uInt)pwrite(s->file, (uchar *)s->outbuf, len, s->pos) != len) 
 
678
      if ((uInt)pwrite(s->file, (unsigned char *)s->outbuf, len, s->pos) != len)
668
679
      {
669
680
        s->z_err = Z_ERRNO;
670
681
        assert(0);
696
707
  else
697
708
    s->dirty= AZ_STATE_SAVED; /* Mark it clean, we should be good now */
698
709
 
699
 
  afterwrite_pos= (size_t)my_tell(s->file, MYF(0));
700
 
  write_header(s);
 
710
  afterwrite_pos= (size_t)lseek(s->file, 0, SEEK_CUR);
 
711
  if(write_header(s))
 
712
    return Z_ERRNO;
701
713
 
702
714
  return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
703
715
}
704
716
 
705
717
static unsigned int azio_enable_aio(azio_stream *s)
706
718
{
707
 
  VOID(pthread_cond_init(&s->container.threshhold, NULL));
708
 
  VOID(pthread_mutex_init(&s->container.thresh_mutex, NULL));
 
719
  pthread_cond_init(&s->container.threshhold, NULL);
 
720
  pthread_mutex_init(&s->container.thresh_mutex, NULL);
709
721
  azio_start(s);
710
722
 
711
723
  return 0;
715
727
{
716
728
  azio_kill(s);
717
729
 
718
 
  VOID(pthread_mutex_destroy(&s->container.thresh_mutex));
719
 
  VOID(pthread_cond_destroy(&s->container.threshhold));
 
730
  pthread_mutex_destroy(&s->container.thresh_mutex);
 
731
  pthread_cond_destroy(&s->container.threshhold);
720
732
 
721
733
  s->method= AZ_METHOD_BLOCK;
722
734
}
723
735
 
724
 
int ZEXPORT azflush (s, flush)
725
 
  azio_stream *s;
726
 
  int flush;
 
736
int ZEXPORT azflush (azio_stream *s,int flush)
727
737
{
728
738
  int err;
729
739
 
730
 
  if (s->mode == 'r') 
 
740
  if (s->mode == 'r')
731
741
  {
732
742
    unsigned char buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
733
 
    pread(s->file, (uchar*) buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0);
 
743
    const ssize_t read_size= AZHEADER_SIZE + AZMETA_BUFFER_SIZE;
 
744
    if(pread(s->file, (unsigned char*) buffer, read_size, 0)!=read_size)
 
745
      return Z_ERRNO;
734
746
    read_header(s, buffer); /* skip the .az header */
735
747
    azrewind(s);
736
748
 
742
754
    err= do_flush(s, flush);
743
755
 
744
756
    if (err) return err;
745
 
    my_sync(s->file, MYF(0));
 
757
    internal::my_sync(s->file, MYF(0));
746
758
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
747
759
  }
748
760
}
777
789
/* ===========================================================================
778
790
  Rewinds input file.
779
791
*/
780
 
int azrewind (s)
781
 
  azio_stream *s;
 
792
int azrewind (azio_stream *s)
782
793
{
783
794
  if (s == NULL || s->mode != 'r') return -1;
784
795
 
807
818
  SEEK_END is not implemented, returns error.
808
819
  In this version of the library, azseek can be extremely slow.
809
820
*/
810
 
size_t azseek (s, offset, whence)
811
 
  azio_stream *s;
812
 
  size_t offset;
813
 
  int whence;
 
821
size_t azseek (azio_stream *s, size_t offset, int whence)
814
822
{
815
823
 
816
824
  if (s == NULL || whence == SEEK_END ||
817
825
      s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
818
 
    return -1L;
 
826
    return SIZE_MAX;
819
827
  }
820
828
 
821
 
  if (s->mode == 'w') 
 
829
  if (s->mode == 'w')
822
830
  {
823
 
    if (whence == SEEK_SET) 
 
831
    if (whence == SEEK_SET)
824
832
      offset -= s->in;
825
833
 
826
834
    /* At this point, offset is the number of zero bytes to write. */
827
835
    /* There was a zmemzero here if inbuf was null -Brian */
828
 
    while (offset > 0)  
 
836
    while (offset > 0)
829
837
    {
830
838
      uInt size = AZ_BUFSIZE_READ;
831
839
      if (offset < AZ_BUFSIZE_READ) size = (uInt)offset;
832
840
 
833
841
      size = azwrite(s, s->inbuf, size);
834
 
      if (size == 0) return -1L;
 
842
      if (size == 0)
 
843
        return SIZE_MAX;
835
844
 
836
845
      offset -= size;
837
846
    }
848
857
  if (offset >= s->out) {
849
858
    offset -= s->out;
850
859
  } else if (azrewind(s)) {
851
 
    return -1L;
 
860
    return SIZE_MAX;
852
861
  }
853
862
  /* offset is now the number of bytes to skip. */
854
863
 
864
873
    if (offset < AZ_BUFSIZE_WRITE) size = (int)offset;
865
874
 
866
875
    size = azread_internal(s, s->outbuf, size, &error);
867
 
    if (error < 0) return -1L;
 
876
    if (error < 0) return SIZE_MAX;
868
877
    offset -= size;
869
878
  }
870
879
  return s->out;
875
884
  given compressed file. This position represents a number of bytes in the
876
885
  uncompressed data stream.
877
886
*/
878
 
size_t ZEXPORT aztell (file)
879
 
  azio_stream *file;
 
887
size_t ZEXPORT aztell (azio_stream *file)
880
888
{
881
889
  return azseek(file, 0L, SEEK_CUR);
882
890
}
888
896
void putLong (azio_stream *s, uLong x)
889
897
{
890
898
  int n;
891
 
  uchar buffer[1];
 
899
  unsigned char buffer[1];
892
900
 
893
 
  for (n = 0; n < 4; n++) 
 
901
  for (n = 0; n < 4; n++)
894
902
  {
895
903
    buffer[0]= (int)(x & 0xff);
896
 
    pwrite(s->file, buffer, 1, s->pos);
 
904
    size_t ret= pwrite(s->file, buffer, 1, s->pos);
 
905
    assert(ret == 1);
897
906
    s->pos++;
898
907
    x >>= 8;
899
908
  }
925
934
  int returnable;
926
935
 
927
936
  if (s == NULL) return Z_STREAM_ERROR;
928
 
  
 
937
 
929
938
  if (s->file < 1) return Z_OK;
930
939
 
931
 
  if (s->mode == 'w') 
 
940
  if (s->mode == 'w')
932
941
  {
933
942
    if (do_flush(s, Z_FINISH) != Z_OK)
934
943
      return destroy(s);
959
968
}
960
969
 
961
970
/*
962
 
  Though this was added to support MySQL's FRM file, anything can be 
 
971
  Though this was added to support MySQL's FRM file, anything can be
963
972
  stored in this location.
964
973
*/
965
 
int azwrite_frm(azio_stream *s, char *blob, unsigned int length)
 
974
int azwrite_frm(azio_stream *s, const char *blob, unsigned int length)
966
975
{
967
 
  if (s->mode == 'r') 
 
976
  if (s->mode == 'r')
968
977
    return 1;
969
978
 
970
 
  if (s->rows > 0) 
 
979
  if (s->rows > 0)
971
980
    return 1;
972
981
 
973
982
  s->frm_start_pos= (uint) s->start;
974
983
  s->frm_length= length;
975
984
  s->start+= length;
976
985
 
977
 
  pwrite(s->file, (uchar*) blob, s->frm_length, s->frm_start_pos);
 
986
  if (pwrite(s->file, (unsigned char*) blob, s->frm_length, s->frm_start_pos) != (ssize_t)s->frm_length)
 
987
    return 1;
978
988
 
979
989
  write_header(s);
980
 
  s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
 
990
  s->pos= (size_t)lseek(s->file, 0, SEEK_END);
981
991
 
982
992
  return 0;
983
993
}
984
994
 
985
995
int azread_frm(azio_stream *s, char *blob)
986
996
{
987
 
  pread(s->file, (uchar*) blob, s->frm_length, s->frm_start_pos);
 
997
  ssize_t r= pread(s->file, (unsigned char*) blob,
 
998
                   s->frm_length, s->frm_start_pos);
 
999
  if (r != (ssize_t)s->frm_length)
 
1000
    return r;
988
1001
 
989
1002
  return 0;
990
1003
}
993
1006
/*
994
1007
  Simple comment field
995
1008
*/
996
 
int azwrite_comment(azio_stream *s, char *blob, unsigned int length)
 
1009
int azwrite_comment(azio_stream *s, const char *blob, unsigned int length)
997
1010
{
998
 
  if (s->mode == 'r') 
999
 
    return 1;
 
1011
  if (s->mode == 'r')
 
1012
    return -1;
1000
1013
 
1001
 
  if (s->rows > 0) 
1002
 
    return 1;
 
1014
  if (s->rows > 0)
 
1015
    return -1;
1003
1016
 
1004
1017
  s->comment_start_pos= (uint) s->start;
1005
1018
  s->comment_length= length;
1006
1019
  s->start+= length;
1007
1020
 
1008
 
  pwrite(s->file, (uchar*) blob, s->comment_length, s->comment_start_pos);
 
1021
  ssize_t r= pwrite(s->file, (unsigned char*) blob,
 
1022
                    s->comment_length, s->comment_start_pos);
 
1023
  if (r != (ssize_t)s->comment_length)
 
1024
    return -1;
1009
1025
 
1010
1026
  write_header(s);
1011
 
  s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
 
1027
  s->pos= (size_t)lseek(s->file, 0, SEEK_END);
1012
1028
 
1013
1029
  return 0;
1014
1030
}
1015
1031
 
1016
1032
int azread_comment(azio_stream *s, char *blob)
1017
1033
{
1018
 
  pread(s->file, (uchar*) blob, s->comment_length, s->comment_start_pos);
 
1034
  ssize_t r= pread(s->file, (unsigned char*) blob,
 
1035
                   s->comment_length, s->comment_start_pos);
 
1036
  if (r != (ssize_t)s->comment_length)
 
1037
    return r;
1019
1038
 
1020
1039
  return 0;
1021
1040
}
1031
1050
}
1032
1051
#endif
1033
1052
 
1034
 
/* 
 
1053
/*
1035
1054
  Normally all IO goes through azio_read(), but in case of error or non-support
1036
1055
  we make use of pread().
1037
1056
*/
1038
1057
static void get_block(azio_stream *s)
1039
1058
{
1040
1059
#ifdef AZIO_AIO
1041
 
  if (s->method == AZ_METHOD_AIO && s->mode == 'r' 
 
1060
  if (s->method == AZ_METHOD_AIO && s->mode == 'r'
1042
1061
      && s->pos < s->check_point
1043
1062
      && s->aio_inited)
1044
1063
  {
1053
1072
    }
1054
1073
    s->pos+= s->stream.avail_in;
1055
1074
    s->inbuf= (Byte *)s->container.buffer;
1056
 
    /* We only aio_read when we know there is more data to be read */
 
1075
    /* We only azio_read when we know there is more data to be read */
1057
1076
    if (s->pos >= s->check_point)
1058
1077
    {
1059
1078
      s->aio_inited= 0;
1069
1088
#ifdef AZIO_AIO
1070
1089
use_pread:
1071
1090
#endif
1072
 
    s->stream.avail_in = (uInt)pread(s->file, (uchar *)s->inbuf,
 
1091
    s->stream.avail_in = (uInt)pread(s->file, (unsigned char *)s->inbuf,
1073
1092
                                     AZ_BUFSIZE_READ, s->pos);
1074
1093
    s->pos+= s->stream.avail_in;
1075
1094
  }