~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/archive/azio.c

Renamed more stuff to drizzle.

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>
19
 
#include <unistd.h>
20
 
 
21
 
#include <cstdio>
22
 
#include <cstring>
23
 
#include <cstdlib>
24
 
#include <cassert>
25
 
 
26
 
using namespace drizzled;
 
16
#include <stdio.h>
 
17
#include <string.h>
 
18
#include <assert.h>
 
19
 
 
20
/* TODO: For some reason these aren't showing up cleanly in the 
 
21
 *  * includes... why? 
 
22
 *   */
 
23
#if !defined(pread)
 
24
extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
 
25
                      off_t __offset);
 
26
#endif
 
27
#if !defined(pwrite)
 
28
extern ssize_t pwrite (int __fd, __const void *__buf, size_t __n,
 
29
                       off_t __offset);
 
30
#endif
27
31
 
28
32
static int const az_magic[3] = {0xfe, 0x03, 0x01}; /* az magic header */
29
33
 
 
34
/* gzip flag uchar */
 
35
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
 
36
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
 
37
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
 
38
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
 
39
#define COMMENT      0x10 /* bit 4 set: file comment present */
 
40
#define RESERVED     0xE0 /* bits 5..7: reserved */
 
41
 
30
42
static unsigned int azwrite(azio_stream *s, void *buf, unsigned int len);
31
43
static int azrewind (azio_stream *s);
32
44
static unsigned int azio_enable_aio(azio_stream *s);
33
45
static int do_flush(azio_stream *file, int flush);
34
46
static int    get_byte(azio_stream *s);
35
47
static void   check_header(azio_stream *s);
36
 
static int write_header(azio_stream *s);
 
48
static void write_header(azio_stream *s);
37
49
static int    destroy(azio_stream *s);
38
50
static void putLong(azio_stream *s, uLong x);
39
51
static uLong  getLong(azio_stream *s);
43
55
static void do_aio_cleanup(azio_stream *s);
44
56
#endif
45
57
 
46
 
extern "C" pthread_handler_t run_task(void *p);
47
 
 
48
 
extern "C" pthread_handler_t run_task(void *p)
 
58
static pthread_handler_t run_task(void *p)
49
59
{
50
60
  int fd;
51
61
  char *buffer;
52
62
  size_t offset;
53
 
  azio_stream *s= (azio_stream *)p;
 
63
  azio_stream *s= (azio_stream *)p;  
54
64
 
55
 
  internal::my_thread_init();
 
65
  my_thread_init();
56
66
 
57
67
  while (1)
58
68
  {
63
73
    }
64
74
    offset= s->container.offset;
65
75
    fd= s->container.fd;
66
 
    buffer= (char *)s->container.buffer;
 
76
    buffer= s->container.buffer;
67
77
    pthread_mutex_unlock(&s->container.thresh_mutex);
68
78
 
69
79
    if (s->container.ready == AZ_THREAD_DEAD)
77
87
    pthread_mutex_unlock(&s->container.thresh_mutex);
78
88
  }
79
89
 
80
 
  internal::my_thread_end();
 
90
  my_thread_end();
81
91
 
82
92
  return 0;
83
93
}
85
95
static void azio_kill(azio_stream *s)
86
96
{
87
97
  pthread_mutex_lock(&s->container.thresh_mutex);
88
 
  s->container.ready= AZ_THREAD_DEAD;
 
98
  s->container.ready= AZ_THREAD_DEAD; 
89
99
  pthread_mutex_unlock(&s->container.thresh_mutex);
90
100
 
91
101
  pthread_cond_signal(&s->container.threshhold);
92
 
  pthread_join(s->container.mainthread, NULL);
 
102
  pthread_join(s->container.mainthread, (void *)NULL);
93
103
}
94
104
 
95
105
static size_t azio_return(azio_stream *s)
127
137
  pthread_attr_init(&attr);
128
138
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
129
139
 
130
 
  s->container.ready= AZ_THREAD_FINISHED;
 
140
  s->container.ready= AZ_THREAD_FINISHED; 
131
141
 
132
142
  /* If we don't create a thread, signal the caller */
133
143
  if (pthread_create(&s->container.mainthread, &attr, run_task,
142
152
static int azio_read(azio_stream *s)
143
153
{
144
154
  pthread_mutex_lock(&s->container.thresh_mutex);
145
 
  s->container.ready= AZ_THREAD_ACTIVE;
 
155
  s->container.ready= AZ_THREAD_ACTIVE; 
146
156
  pthread_mutex_unlock(&s->container.thresh_mutex);
147
157
  pthread_cond_broadcast(&s->container.threshhold);
148
158
 
163
173
  int err;
164
174
  int level = Z_DEFAULT_COMPRESSION ; /* compression level */
165
175
  int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
166
 
  int fd= -1;
 
176
  File fd= -1;
167
177
 
168
178
  memset(s, 0, sizeof(azio_stream));
169
179
 
188
198
  s->method= method;
189
199
 
190
200
  /*
191
 
    We do our own version of append by nature.
 
201
    We do our own version of append by nature. 
192
202
    We must always have write access to take card of the header.
193
203
  */
194
 
  assert(Flags | O_APPEND);
195
 
  assert(Flags | O_WRONLY);
 
204
  DBUG_ASSERT(Flags | O_APPEND);
 
205
  DBUG_ASSERT(Flags | O_WRONLY);
196
206
 
197
 
  if (Flags & O_RDWR)
 
207
  if (Flags & O_RDWR) 
198
208
    s->mode = 'w';
199
209
 
200
 
  if (s->mode == 'w')
 
210
  if (s->mode == 'w') 
201
211
  {
202
212
    err = deflateInit2(&(s->stream), level,
203
213
                       Z_DEFLATED, -MAX_WBITS, 8, strategy);
229
239
  s->stream.avail_out = AZ_BUFSIZE_WRITE;
230
240
 
231
241
  errno = 0;
232
 
  s->file = fd < 0 ? internal::my_open(path, Flags, MYF(0)) : fd;
 
242
  s->file = fd < 0 ? my_open(path, Flags, MYF(0)) : fd;
233
243
#ifdef AZIO_AIO
234
244
  s->container.fd= s->file;
235
245
#endif
236
246
 
237
 
  if (s->file < 0 )
 
247
  if (s->file < 0 ) 
238
248
  {
239
249
    destroy(s);
240
250
    return Z_NULL;
241
251
  }
242
252
 
243
 
  if (Flags & O_CREAT || Flags & O_TRUNC)
 
253
  if (Flags & O_CREAT || Flags & O_TRUNC) 
244
254
  {
245
255
    s->rows= 0;
246
256
    s->forced_flushes= 0;
254
264
    s->frm_length= 0;
255
265
    s->dirty= 1; /* We create the file dirty */
256
266
    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);
 
267
    write_header(s);
 
268
    s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
260
269
  }
261
 
  else if (s->mode == 'w')
 
270
  else if (s->mode == 'w') 
262
271
  {
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;
 
272
    uchar buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
 
273
    pread(s->file, buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0);
267
274
    read_header(s, buffer);
268
 
    s->pos= (size_t)lseek(s->file, 0, SEEK_END);
 
275
    s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
269
276
  }
270
277
  else
271
278
  {
286
293
}
287
294
 
288
295
 
289
 
int write_header(azio_stream *s)
 
296
void write_header(azio_stream *s)
290
297
{
291
298
  char buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
292
299
  char *ptr= buffer;
310
317
  int4store(ptr + AZ_COMMENT_LENGTH_POS, s->comment_length); /* COMMENT Block */
311
318
  int4store(ptr + AZ_META_POS, 0); /* Meta Block */
312
319
  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 */
 
320
  int8store(ptr + AZ_START_POS, (unsigned long long)s->start); /* Start of Data Block Index Block */
 
321
  int8store(ptr + AZ_ROW_POS, (unsigned long long)s->rows); /* Start of Data Block Index Block */
 
322
  int8store(ptr + AZ_FLUSH_POS, (unsigned long long)s->forced_flushes); /* Start of Data Block Index Block */
 
323
  int8store(ptr + AZ_CHECK_POS, (unsigned long long)s->check_point); /* Start of Data Block Index Block */
 
324
  int8store(ptr + AZ_AUTOINCREMENT_POS, (unsigned long long)s->auto_increment); /* Start of Data Block Index Block */
318
325
  int4store(ptr+ AZ_LONGEST_POS , s->longest_row); /* Longest row */
319
326
  int4store(ptr+ AZ_SHORTEST_POS, s->shortest_row); /* Shorest row */
320
 
  int4store(ptr+ AZ_FRM_POS,
 
327
  int4store(ptr+ AZ_FRM_POS, 
321
328
            AZHEADER_SIZE + AZMETA_BUFFER_SIZE); /* FRM position */
322
329
  *(ptr + AZ_DIRTY_POS)= (unsigned char)s->dirty; /* Start of Data Block Index Block */
323
330
 
324
331
  /* 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;
 
332
  pwrite(s->file, (uchar*) buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0);
330
333
}
331
334
 
332
335
/* ===========================================================================
334
337
  for end of file.
335
338
  IN assertion: the stream s has been sucessfully opened for reading.
336
339
*/
337
 
int get_byte(azio_stream *s)
 
340
int get_byte(s)
 
341
  azio_stream *s;
338
342
{
339
343
  if (s->z_eof) return EOF;
340
 
  if (s->stream.avail_in == 0)
 
344
  if (s->stream.avail_in == 0) 
341
345
  {
342
346
    errno = 0;
343
 
    if (s->stream.avail_in == 0)
 
347
    if (s->stream.avail_in == 0) 
344
348
    {
345
349
      s->z_eof = 1;
346
350
      return EOF;
374
378
  if (len < 2) {
375
379
    if (len) s->inbuf[0] = s->stream.next_in[0];
376
380
    errno = 0;
377
 
    len = (uInt)pread(s->file, (unsigned char *)s->inbuf + len, AZ_BUFSIZE_READ >> len, s->pos);
 
381
    len = (uInt)pread(s->file, (uchar *)s->inbuf + len, AZ_BUFSIZE_READ >> len, s->pos);
378
382
    s->pos+= len;
379
383
    if (len == (uInt)-1) s->z_err = Z_ERRNO;
380
384
    s->stream.avail_in += len;
409
413
    s->minor_version= (unsigned int)buffer[AZ_MINOR_VERSION_POS];
410
414
    s->block_size= 1024 * buffer[AZ_BLOCK_POS];
411
415
    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);
 
416
    s->rows= (unsigned long long)uint8korr(buffer + AZ_ROW_POS);
 
417
    s->check_point= (unsigned long long)uint8korr(buffer + AZ_CHECK_POS);
 
418
    s->forced_flushes= (unsigned long long)uint8korr(buffer + AZ_FLUSH_POS);
 
419
    s->auto_increment= (unsigned long long)uint8korr(buffer + AZ_AUTOINCREMENT_POS);
416
420
    s->longest_row= (unsigned int)uint4korr(buffer + AZ_LONGEST_POS);
417
421
    s->shortest_row= (unsigned int)uint4korr(buffer + AZ_SHORTEST_POS);
418
422
    s->frm_start_pos= (unsigned int)uint4korr(buffer + AZ_FRM_POS);
432
436
 * Cleanup then free the given azio_stream. Return a zlib error code.
433
437
 Try freeing in the reverse order of allocations.
434
438
 */
435
 
int destroy (azio_stream *s)
 
439
int destroy (s)
 
440
  azio_stream *s;
436
441
{
437
442
  int err = Z_OK;
438
443
 
439
 
  if (s->stream.state != NULL)
 
444
  if (s->stream.state != NULL) 
440
445
  {
441
 
    if (s->mode == 'w')
 
446
    if (s->mode == 'w') 
442
447
    {
443
448
      err = deflateEnd(&(s->stream));
444
 
      internal::my_sync(s->file, MYF(0));
 
449
      my_sync(s->file, MYF(0));
445
450
    }
446
 
    else if (s->mode == 'r')
 
451
    else if (s->mode == 'r') 
447
452
      err = inflateEnd(&(s->stream));
448
453
  }
449
454
 
450
455
  do_aio_cleanup(s);
451
456
 
452
 
  if (s->file > 0 && internal::my_close(s->file, MYF(0)))
 
457
  if (s->file > 0 && my_close(s->file, MYF(0))) 
453
458
      err = Z_ERRNO;
454
459
 
455
460
  s->file= -1;
463
468
  Reads the given number of uncompressed bytes from the compressed file.
464
469
  azread returns the number of bytes actually read (0 for end of file).
465
470
*/
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)
 
471
unsigned int azread_internal( azio_stream *s, voidp buf, unsigned int len, int *error)
476
472
{
477
473
  Bytef *start = (Bytef*)buf; /* starting point for crc computation */
478
474
  Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
479
475
  *error= 0;
480
476
 
481
477
  if (s->mode != 'r')
482
 
  {
 
478
  { 
483
479
    *error= Z_STREAM_ERROR;
484
480
    return 0;
485
481
  }
486
482
 
487
483
  if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
488
 
  {
 
484
  { 
489
485
    *error= s->z_err;
490
486
    return 0;
491
487
  }
492
488
 
493
489
  if (s->z_err == Z_STREAM_END)  /* EOF */
494
 
  {
 
490
  { 
495
491
    return 0;
496
492
  }
497
493
 
508
504
    start++;
509
505
    if (s->last) {
510
506
      s->z_err = Z_STREAM_END;
511
 
      {
 
507
      { 
512
508
        return 1;
513
509
      }
514
510
    }
520
516
 
521
517
      errno = 0;
522
518
      get_block(s);
523
 
      if (s->stream.avail_in == 0)
 
519
      if (s->stream.avail_in == 0) 
524
520
      {
525
521
        s->z_eof = 1;
526
522
      }
546
542
         * Check for such files:
547
543
       */
548
544
        check_header(s);
549
 
        if (s->z_err == Z_OK)
 
545
        if (s->z_err == Z_OK) 
550
546
        {
551
547
          inflateReset(&(s->stream));
552
548
          s->crc = crc32(0L, Z_NULL, 0);
569
565
}
570
566
 
571
567
/* ===========================================================================
572
 
  Experimental Interface. We abstract out a concecpt of rows
 
568
  Experimental Interface. We abstract out a concecpt of rows 
573
569
*/
574
570
size_t azwrite_row(azio_stream *s, void *buf, unsigned int len)
575
571
{
603
599
  size_t read;
604
600
 
605
601
  read= azread_internal(s, buffer, sizeof(unsigned int), error);
606
 
 
 
602
  
607
603
  /* On error the return value will be zero as well */
608
604
  if (read == 0)
609
605
    return read;
612
608
  new_ptr= (char *)realloc(s->row_ptr, (sizeof(char) * row_length));
613
609
 
614
610
  if (!new_ptr)
615
 
    return SIZE_MAX;
 
611
    return -1;
616
612
 
617
613
  s->row_ptr= new_ptr;
618
614
 
632
628
  s->stream.next_in = (Bytef*)buf;
633
629
  s->stream.avail_in = len;
634
630
 
635
 
  while (s->stream.avail_in != 0)
 
631
  while (s->stream.avail_in != 0) 
636
632
  {
637
 
    if (s->stream.avail_out == 0)
 
633
    if (s->stream.avail_out == 0) 
638
634
    {
639
635
 
640
636
      s->stream.next_out = s->outbuf;
641
 
      if (pwrite(s->file, (unsigned char *)s->outbuf, AZ_BUFSIZE_WRITE, s->pos) != AZ_BUFSIZE_WRITE)
 
637
      if (pwrite(s->file, (uchar *)s->outbuf, AZ_BUFSIZE_WRITE, s->pos) != AZ_BUFSIZE_WRITE) 
642
638
      {
643
639
        s->z_err = Z_ERRNO;
644
640
        break;
673
669
 
674
670
  s->stream.avail_in = 0; /* should be zero already anyway */
675
671
 
676
 
  for (;;)
 
672
  for (;;) 
677
673
  {
678
674
    len = AZ_BUFSIZE_WRITE - s->stream.avail_out;
679
675
 
680
 
    if (len != 0)
 
676
    if (len != 0) 
681
677
    {
682
 
      if ((uInt)pwrite(s->file, (unsigned char *)s->outbuf, len, s->pos) != len)
 
678
      if ((uInt)pwrite(s->file, (uchar *)s->outbuf, len, s->pos) != len) 
683
679
      {
684
680
        s->z_err = Z_ERRNO;
685
681
        assert(0);
711
707
  else
712
708
    s->dirty= AZ_STATE_SAVED; /* Mark it clean, we should be good now */
713
709
 
714
 
  afterwrite_pos= (size_t)lseek(s->file, 0, SEEK_CUR);
715
 
  if(write_header(s))
716
 
    return Z_ERRNO;
 
710
  afterwrite_pos= (size_t)my_tell(s->file, MYF(0));
 
711
  write_header(s);
717
712
 
718
713
  return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
719
714
}
720
715
 
721
716
static unsigned int azio_enable_aio(azio_stream *s)
722
717
{
723
 
  pthread_cond_init(&s->container.threshhold, NULL);
724
 
  pthread_mutex_init(&s->container.thresh_mutex, NULL);
 
718
  VOID(pthread_cond_init(&s->container.threshhold, NULL));
 
719
  VOID(pthread_mutex_init(&s->container.thresh_mutex, NULL));
725
720
  azio_start(s);
726
721
 
727
722
  return 0;
731
726
{
732
727
  azio_kill(s);
733
728
 
734
 
  pthread_mutex_destroy(&s->container.thresh_mutex);
735
 
  pthread_cond_destroy(&s->container.threshhold);
 
729
  VOID(pthread_mutex_destroy(&s->container.thresh_mutex));
 
730
  VOID(pthread_cond_destroy(&s->container.threshhold));
736
731
 
737
732
  s->method= AZ_METHOD_BLOCK;
738
733
}
739
734
 
740
 
int ZEXPORT azflush (azio_stream *s,int flush)
 
735
int ZEXPORT azflush (s, flush)
 
736
  azio_stream *s;
 
737
  int flush;
741
738
{
742
739
  int err;
743
740
 
744
 
  if (s->mode == 'r')
 
741
  if (s->mode == 'r') 
745
742
  {
746
743
    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;
 
744
    pread(s->file, (uchar*) buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0);
750
745
    read_header(s, buffer); /* skip the .az header */
751
746
    azrewind(s);
752
747
 
758
753
    err= do_flush(s, flush);
759
754
 
760
755
    if (err) return err;
761
 
    internal::my_sync(s->file, MYF(0));
 
756
    my_sync(s->file, MYF(0));
762
757
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
763
758
  }
764
759
}
793
788
/* ===========================================================================
794
789
  Rewinds input file.
795
790
*/
796
 
int azrewind (azio_stream *s)
 
791
int azrewind (s)
 
792
  azio_stream *s;
797
793
{
798
794
  if (s == NULL || s->mode != 'r') return -1;
799
795
 
822
818
  SEEK_END is not implemented, returns error.
823
819
  In this version of the library, azseek can be extremely slow.
824
820
*/
825
 
size_t azseek (azio_stream *s, size_t offset, int whence)
 
821
size_t azseek (s, offset, whence)
 
822
  azio_stream *s;
 
823
  size_t offset;
 
824
  int whence;
826
825
{
827
826
 
828
827
  if (s == NULL || whence == SEEK_END ||
829
828
      s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
830
 
    return SIZE_MAX;
 
829
    return -1L;
831
830
  }
832
831
 
833
 
  if (s->mode == 'w')
 
832
  if (s->mode == 'w') 
834
833
  {
835
 
    if (whence == SEEK_SET)
 
834
    if (whence == SEEK_SET) 
836
835
      offset -= s->in;
837
836
 
838
837
    /* At this point, offset is the number of zero bytes to write. */
839
838
    /* There was a zmemzero here if inbuf was null -Brian */
840
 
    while (offset > 0)
 
839
    while (offset > 0)  
841
840
    {
842
841
      uInt size = AZ_BUFSIZE_READ;
843
842
      if (offset < AZ_BUFSIZE_READ) size = (uInt)offset;
844
843
 
845
844
      size = azwrite(s, s->inbuf, size);
846
 
      if (size == 0)
847
 
        return SIZE_MAX;
 
845
      if (size == 0) return -1L;
848
846
 
849
847
      offset -= size;
850
848
    }
861
859
  if (offset >= s->out) {
862
860
    offset -= s->out;
863
861
  } else if (azrewind(s)) {
864
 
    return SIZE_MAX;
 
862
    return -1L;
865
863
  }
866
864
  /* offset is now the number of bytes to skip. */
867
865
 
877
875
    if (offset < AZ_BUFSIZE_WRITE) size = (int)offset;
878
876
 
879
877
    size = azread_internal(s, s->outbuf, size, &error);
880
 
    if (error < 0) return SIZE_MAX;
 
878
    if (error < 0) return -1L;
881
879
    offset -= size;
882
880
  }
883
881
  return s->out;
888
886
  given compressed file. This position represents a number of bytes in the
889
887
  uncompressed data stream.
890
888
*/
891
 
size_t ZEXPORT aztell (azio_stream *file)
 
889
size_t ZEXPORT aztell (file)
 
890
  azio_stream *file;
892
891
{
893
892
  return azseek(file, 0L, SEEK_CUR);
894
893
}
900
899
void putLong (azio_stream *s, uLong x)
901
900
{
902
901
  int n;
903
 
  unsigned char buffer[1];
 
902
  uchar buffer[1];
904
903
 
905
 
  for (n = 0; n < 4; n++)
 
904
  for (n = 0; n < 4; n++) 
906
905
  {
907
906
    buffer[0]= (int)(x & 0xff);
908
 
    size_t ret= pwrite(s->file, buffer, 1, s->pos);
909
 
    assert(ret == 1);
 
907
    pwrite(s->file, buffer, 1, s->pos);
910
908
    s->pos++;
911
909
    x >>= 8;
912
910
  }
938
936
  int returnable;
939
937
 
940
938
  if (s == NULL) return Z_STREAM_ERROR;
941
 
 
 
939
  
942
940
  if (s->file < 1) return Z_OK;
943
941
 
944
 
  if (s->mode == 'w')
 
942
  if (s->mode == 'w') 
945
943
  {
946
944
    if (do_flush(s, Z_FINISH) != Z_OK)
947
945
      return destroy(s);
972
970
}
973
971
 
974
972
/*
975
 
  Though this was added to support MySQL's FRM file, anything can be
 
973
  Though this was added to support MySQL's FRM file, anything can be 
976
974
  stored in this location.
977
975
*/
978
 
int azwrite_frm(azio_stream *s, const char *blob, unsigned int length)
 
976
int azwrite_frm(azio_stream *s, char *blob, unsigned int length)
979
977
{
980
 
  if (s->mode == 'r')
 
978
  if (s->mode == 'r') 
981
979
    return 1;
982
980
 
983
 
  if (s->rows > 0)
 
981
  if (s->rows > 0) 
984
982
    return 1;
985
983
 
986
984
  s->frm_start_pos= (uint) s->start;
987
985
  s->frm_length= length;
988
986
  s->start+= length;
989
987
 
990
 
  if (pwrite(s->file, (unsigned char*) blob, s->frm_length, s->frm_start_pos) != (ssize_t)s->frm_length)
991
 
    return 1;
 
988
  pwrite(s->file, (uchar*) blob, s->frm_length, s->frm_start_pos);
992
989
 
993
990
  write_header(s);
994
 
  s->pos= (size_t)lseek(s->file, 0, SEEK_END);
 
991
  s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
995
992
 
996
993
  return 0;
997
994
}
998
995
 
999
996
int azread_frm(azio_stream *s, char *blob)
1000
997
{
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;
 
998
  pread(s->file, (uchar*) blob, s->frm_length, s->frm_start_pos);
1005
999
 
1006
1000
  return 0;
1007
1001
}
1010
1004
/*
1011
1005
  Simple comment field
1012
1006
*/
1013
 
int azwrite_comment(azio_stream *s, const char *blob, unsigned int length)
 
1007
int azwrite_comment(azio_stream *s, char *blob, unsigned int length)
1014
1008
{
1015
 
  if (s->mode == 'r')
1016
 
    return -1;
 
1009
  if (s->mode == 'r') 
 
1010
    return 1;
1017
1011
 
1018
 
  if (s->rows > 0)
1019
 
    return -1;
 
1012
  if (s->rows > 0) 
 
1013
    return 1;
1020
1014
 
1021
1015
  s->comment_start_pos= (uint) s->start;
1022
1016
  s->comment_length= length;
1023
1017
  s->start+= length;
1024
1018
 
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;
 
1019
  pwrite(s->file, (uchar*) blob, s->comment_length, s->comment_start_pos);
1029
1020
 
1030
1021
  write_header(s);
1031
 
  s->pos= (size_t)lseek(s->file, 0, SEEK_END);
 
1022
  s->pos= (size_t)my_seek(s->file, 0, MY_SEEK_END, MYF(0));
1032
1023
 
1033
1024
  return 0;
1034
1025
}
1035
1026
 
1036
1027
int azread_comment(azio_stream *s, char *blob)
1037
1028
{
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;
 
1029
  pread(s->file, (uchar*) blob, s->comment_length, s->comment_start_pos);
1042
1030
 
1043
1031
  return 0;
1044
1032
}
1054
1042
}
1055
1043
#endif
1056
1044
 
1057
 
/*
 
1045
/* 
1058
1046
  Normally all IO goes through azio_read(), but in case of error or non-support
1059
1047
  we make use of pread().
1060
1048
*/
1061
1049
static void get_block(azio_stream *s)
1062
1050
{
1063
1051
#ifdef AZIO_AIO
1064
 
  if (s->method == AZ_METHOD_AIO && s->mode == 'r'
 
1052
  if (s->method == AZ_METHOD_AIO && s->mode == 'r' 
1065
1053
      && s->pos < s->check_point
1066
1054
      && s->aio_inited)
1067
1055
  {
1076
1064
    }
1077
1065
    s->pos+= s->stream.avail_in;
1078
1066
    s->inbuf= (Byte *)s->container.buffer;
1079
 
    /* We only azio_read when we know there is more data to be read */
 
1067
    /* We only aio_read when we know there is more data to be read */
1080
1068
    if (s->pos >= s->check_point)
1081
1069
    {
1082
1070
      s->aio_inited= 0;
1092
1080
#ifdef AZIO_AIO
1093
1081
use_pread:
1094
1082
#endif
1095
 
    s->stream.avail_in = (uInt)pread(s->file, (unsigned char *)s->inbuf,
 
1083
    s->stream.avail_in = (uInt)pread(s->file, (uchar *)s->inbuf,
1096
1084
                                     AZ_BUFSIZE_READ, s->pos);
1097
1085
    s->pos+= s->stream.avail_in;
1098
1086
  }