~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/azio.cc

  • Committer: Brian Aker
  • Date: 2010-02-07 01:33:54 UTC
  • Revision ID: brian@gaz-20100207013354-d2pg1n68u5c09pgo
Remove giant include header to its own file.

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