~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/azio.c

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

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