~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_dynrec.c

mergeĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "myisamdef.h"
27
27
 
28
 
#ifdef HAVE_SYS_TYPES
29
 
#include <sys/types.h>
30
 
#endif
31
 
#ifdef HAVE_SYS_MMAN_H
32
 
#include <sys/mman.h>
33
 
#endif
34
 
#include <drizzled/util/test.h>
35
 
 
36
28
/* Enough for comparing if number is zero */
37
29
static char zero_string[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
38
30
 
39
 
static int write_dynamic_record(MI_INFO *info,const unsigned char *record,
 
31
static int write_dynamic_record(MI_INFO *info,const uchar *record,
40
32
                                ulong reclength);
41
33
static int _mi_find_writepos(MI_INFO *info,ulong reclength,my_off_t *filepos,
42
34
                             ulong *length);
43
 
static int update_dynamic_record(MI_INFO *info,my_off_t filepos,unsigned char *record,
 
35
static int update_dynamic_record(MI_INFO *info,my_off_t filepos,uchar *record,
44
36
                                 ulong reclength);
45
37
static int delete_dynamic_record(MI_INFO *info,my_off_t filepos,
46
 
                                 uint32_t second_read);
47
 
static int _mi_cmp_buffer(File file, const unsigned char *buff, my_off_t filepos,
48
 
                          uint32_t length);
 
38
                                 uint second_read);
 
39
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
 
40
                          uint length);
49
41
 
50
42
/* Play it safe; We have a small stack when using threads */
51
43
#undef my_alloca
52
44
#undef my_afree
53
 
#define my_alloca(A) malloc((A))
54
 
#define my_afree(A) free((A))
 
45
#define my_alloca(A) my_malloc((A),MYF(0))
 
46
#define my_afree(A) my_free((A),MYF(0))
55
47
 
56
48
        /* Interface function from MI_INFO */
57
49
 
69
61
    1  error.
70
62
*/
71
63
 
72
 
bool mi_dynmap_file(MI_INFO *info, my_off_t size)
 
64
my_bool mi_dynmap_file(MI_INFO *info, my_off_t size)
73
65
{
 
66
  DBUG_ENTER("mi_dynmap_file");
74
67
  if (size > (my_off_t) (~((size_t) 0)) - MEMMAP_EXTRA_MARGIN)
75
68
  {
76
 
    return(1);
 
69
    DBUG_PRINT("warning", ("File is too large for mmap"));
 
70
    DBUG_RETURN(1);
77
71
  }
78
72
  /*
79
73
    I wonder if it is good to use MAP_NORESERVE. From the Linux man page:
83
77
      mapping. When swap space is not reserved one might get SIGSEGV
84
78
      upon a write if no physical memory is available.
85
79
  */
86
 
  info->s->file_map= (unsigned char*)
 
80
  info->s->file_map= (uchar*)
87
81
                  my_mmap(0, (size_t)(size + MEMMAP_EXTRA_MARGIN),
88
82
                          info->s->mode==O_RDONLY ? PROT_READ :
89
83
                          PROT_READ | PROT_WRITE,
90
84
                          MAP_SHARED | MAP_NORESERVE,
91
85
                          info->dfile, 0L);
92
 
  if (info->s->file_map == (unsigned char*) MAP_FAILED)
 
86
  if (info->s->file_map == (uchar*) MAP_FAILED)
93
87
  {
94
88
    info->s->file_map= NULL;
95
 
    return(1);
 
89
    DBUG_RETURN(1);
96
90
  }
97
91
#if defined(HAVE_MADVISE)
98
92
  madvise((char*) info->s->file_map, size, MADV_RANDOM);
99
93
#endif
100
94
  info->s->mmaped_length= size;
101
 
  return(0);
 
95
  DBUG_RETURN(0);
102
96
}
103
97
 
104
98
 
116
110
{
117
111
  if (info->s->file_map)
118
112
  {
119
 
    my_munmap((char*) info->s->file_map,
120
 
              (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN);
 
113
    VOID(my_munmap((char*) info->s->file_map,
 
114
                   (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN));
121
115
    mi_dynmap_file(info, size);
122
116
  }
123
117
}
139
133
    0  ok
140
134
*/
141
135
 
142
 
size_t mi_mmap_pread(MI_INFO *info, unsigned char *Buffer,
 
136
size_t mi_mmap_pread(MI_INFO *info, uchar *Buffer,
143
137
                    size_t Count, my_off_t offset, myf MyFlags)
144
138
{
 
139
  DBUG_PRINT("info", ("mi_read with mmap %d\n", info->dfile));
145
140
  if (info->s->concurrent_insert)
146
141
    rw_rdlock(&info->s->mmap_lock);
147
142
 
170
165
 
171
166
        /* wrapper for my_pread in case if mmap isn't used */
172
167
 
173
 
size_t mi_nommap_pread(MI_INFO *info, unsigned char *Buffer,
 
168
size_t mi_nommap_pread(MI_INFO *info, uchar *Buffer,
174
169
                       size_t Count, my_off_t offset, myf MyFlags)
175
170
{
176
171
  return my_pread(info->dfile, Buffer, Count, offset, MyFlags);
193
188
    !=0  error.  In this case return error from pwrite
194
189
*/
195
190
 
196
 
size_t mi_mmap_pwrite(MI_INFO *info, const unsigned char *Buffer,
 
191
size_t mi_mmap_pwrite(MI_INFO *info, const uchar *Buffer,
197
192
                      size_t Count, my_off_t offset, myf MyFlags)
198
193
{
 
194
  DBUG_PRINT("info", ("mi_write with mmap %d\n", info->dfile));
199
195
  if (info->s->concurrent_insert)
200
196
    rw_rdlock(&info->s->mmap_lock);
201
197
 
226
222
 
227
223
        /* wrapper for my_pwrite in case if mmap isn't used */
228
224
 
229
 
size_t mi_nommap_pwrite(MI_INFO *info, const unsigned char *Buffer,
 
225
size_t mi_nommap_pwrite(MI_INFO *info, const uchar *Buffer,
230
226
                      size_t Count, my_off_t offset, myf MyFlags)
231
227
{
232
228
  return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
233
229
}
234
230
 
235
231
 
236
 
int _mi_write_dynamic_record(MI_INFO *info, const unsigned char *record)
 
232
int _mi_write_dynamic_record(MI_INFO *info, const uchar *record)
237
233
{
238
234
  ulong reclength=_mi_rec_pack(info,info->rec_buff,record);
239
235
  return (write_dynamic_record(info,info->rec_buff,reclength));
240
236
}
241
237
 
242
 
int _mi_update_dynamic_record(MI_INFO *info, my_off_t pos, const unsigned char *record)
 
238
int _mi_update_dynamic_record(MI_INFO *info, my_off_t pos, const uchar *record)
243
239
{
244
 
  uint32_t length=_mi_rec_pack(info,info->rec_buff,record);
 
240
  uint length=_mi_rec_pack(info,info->rec_buff,record);
245
241
  return (update_dynamic_record(info,pos,info->rec_buff,length));
246
242
}
247
243
 
248
 
int _mi_write_blob_record(MI_INFO *info, const unsigned char *record)
 
244
int _mi_write_blob_record(MI_INFO *info, const uchar *record)
249
245
{
250
 
  unsigned char *rec_buff;
 
246
  uchar *rec_buff;
251
247
  int error;
252
248
  ulong reclength,reclength2,extra;
253
249
 
262
258
    return -1;
263
259
  }
264
260
#endif
265
 
  if (!(rec_buff=(unsigned char*) my_alloca(reclength)))
 
261
  if (!(rec_buff=(uchar*) my_alloca(reclength)))
266
262
  {
267
263
    my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
268
264
    return(-1);
269
265
  }
270
266
  reclength2= _mi_rec_pack(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
271
267
                           record);
272
 
  assert(reclength2 <= reclength);
 
268
  DBUG_PRINT("info",("reclength: %lu  reclength2: %lu",
 
269
                     reclength, reclength2));
 
270
  DBUG_ASSERT(reclength2 <= reclength);
273
271
  error=write_dynamic_record(info,rec_buff+ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
274
272
                             reclength2);
275
273
  my_afree(rec_buff);
277
275
}
278
276
 
279
277
 
280
 
int _mi_update_blob_record(MI_INFO *info, my_off_t pos, const unsigned char *record)
 
278
int _mi_update_blob_record(MI_INFO *info, my_off_t pos, const uchar *record)
281
279
{
282
 
  unsigned char *rec_buff;
 
280
  uchar *rec_buff;
283
281
  int error;
284
282
  ulong reclength,extra;
285
283
 
294
292
    return -1;
295
293
  }
296
294
#endif
297
 
  if (!(rec_buff=(unsigned char*) my_alloca(reclength)))
 
295
  if (!(rec_buff=(uchar*) my_alloca(reclength)))
298
296
  {
299
297
    my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
300
298
    return(-1);
317
315
 
318
316
        /* Write record to data-file */
319
317
 
320
 
static int write_dynamic_record(MI_INFO *info, const unsigned char *record,
 
318
static int write_dynamic_record(MI_INFO *info, const uchar *record,
321
319
                                ulong reclength)
322
320
{
323
321
  int flag;
324
322
  ulong length;
325
323
  my_off_t filepos;
 
324
  DBUG_ENTER("write_dynamic_record");
326
325
 
327
326
  flag=0;
328
327
 
344
343
        reclength + MI_MAX_DYN_BLOCK_HEADER)
345
344
    {
346
345
      my_errno=HA_ERR_RECORD_FILE_FULL;
347
 
      return(1);
 
346
      DBUG_RETURN(1);
348
347
    }
349
348
  }
350
349
 
355
354
    if (_mi_write_part_record(info,filepos,length,
356
355
                              (info->append_insert_at_end ?
357
356
                               HA_OFFSET_ERROR : info->s->state.dellink),
358
 
                              (unsigned char**) &record,&reclength,&flag))
 
357
                              (uchar**) &record,&reclength,&flag))
359
358
      goto err;
360
359
  } while (reclength);
361
360
 
362
 
  return(0);
 
361
  DBUG_RETURN(0);
363
362
err:
364
 
  return(1);
 
363
  DBUG_RETURN(1);
365
364
}
366
365
 
367
366
 
374
373
{
375
374
  MI_BLOCK_INFO block_info;
376
375
  ulong tmp;
 
376
  DBUG_ENTER("_mi_find_writepos");
377
377
 
378
378
  if (info->s->state.dellink != HA_OFFSET_ERROR &&
379
379
      !info->append_insert_at_end)
385
385
    if (!(_mi_get_block_info(&block_info,info->dfile,info->s->state.dellink) &
386
386
           BLOCK_DELETED))
387
387
    {
 
388
      DBUG_PRINT("error",("Delete link crashed"));
388
389
      my_errno=HA_ERR_WRONG_IN_RECORD;
389
 
      return(-1);
 
390
      DBUG_RETURN(-1);
390
391
    }
391
392
    info->s->state.dellink=block_info.next_filepos;
392
393
    info->state->del--;
407
408
        (info->s->base.max_data_file_length - tmp))
408
409
    {
409
410
      my_errno=HA_ERR_RECORD_FILE_FULL;
410
 
      return(-1);
 
411
      DBUG_RETURN(-1);
411
412
    }
412
413
    if (tmp > MI_MAX_BLOCK_LENGTH)
413
414
      tmp=MI_MAX_BLOCK_LENGTH;
416
417
    info->s->state.split++;
417
418
    info->update|=HA_STATE_WRITE_AT_END;
418
419
  }
419
 
  return(0);
 
420
  DBUG_RETURN(0);
420
421
} /* _mi_find_writepos */
421
422
 
422
423
 
427
428
  a big block.
428
429
*/
429
430
 
430
 
static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
 
431
static my_bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info)
431
432
{
 
433
  DBUG_ENTER("unlink_deleted_block");
432
434
  if (block_info->filepos == info->s->state.dellink)
433
435
  {
434
436
    /* First deleted block;  We can just use this ! */
441
443
    /* Unlink block from the previous block */
442
444
    if (!(_mi_get_block_info(&tmp,info->dfile,block_info->prev_filepos)
443
445
          & BLOCK_DELETED))
444
 
      return(1);                                /* Something is wrong */
 
446
      DBUG_RETURN(1);                           /* Something is wrong */
445
447
    mi_sizestore(tmp.header+4,block_info->next_filepos);
446
448
    if (info->s->file_write(info, tmp.header+4,8,
447
449
                  block_info->prev_filepos+4, MYF(MY_NABP)))
448
 
      return(1);
 
450
      DBUG_RETURN(1);
449
451
    /* Unlink block from next block */
450
452
    if (block_info->next_filepos != HA_OFFSET_ERROR)
451
453
    {
452
454
      if (!(_mi_get_block_info(&tmp,info->dfile,block_info->next_filepos)
453
455
            & BLOCK_DELETED))
454
 
        return(1);                              /* Something is wrong */
 
456
        DBUG_RETURN(1);                         /* Something is wrong */
455
457
      mi_sizestore(tmp.header+12,block_info->prev_filepos);
456
458
      if (info->s->file_write(info, tmp.header+12,8,
457
459
                    block_info->next_filepos+12,
458
460
                    MYF(MY_NABP)))
459
 
        return(1);
 
461
        DBUG_RETURN(1);
460
462
    }
461
463
  }
462
464
  /* We now have one less deleted block */
471
473
  */
472
474
  if (info->nextpos == block_info->filepos)
473
475
    info->nextpos+=block_info->block_len;
474
 
  return(0);
 
476
  DBUG_RETURN(0);
475
477
}
476
478
 
477
479
 
494
496
                                       my_off_t filepos)
495
497
{
496
498
  MI_BLOCK_INFO block_info;
 
499
  DBUG_ENTER("update_backward_delete_link");
497
500
 
498
501
  if (delete_block != HA_OFFSET_ERROR)
499
502
  {
501
504
    if (_mi_get_block_info(&block_info,info->dfile,delete_block)
502
505
        & BLOCK_DELETED)
503
506
    {
504
 
      unsigned char buff[8];
 
507
      uchar buff[8];
505
508
      mi_sizestore(buff,filepos);
506
509
      if (info->s->file_write(info,buff, 8, delete_block+12, MYF(MY_NABP)))
507
 
        return(1);                              /* Error on write */
 
510
        DBUG_RETURN(1);                         /* Error on write */
508
511
    }
509
512
    else
510
513
    {
511
514
      my_errno=HA_ERR_WRONG_IN_RECORD;
512
 
      return(1);                                /* Wrong delete link */
 
515
      DBUG_RETURN(1);                           /* Wrong delete link */
513
516
    }
514
517
  }
515
 
  return(0);
 
518
  DBUG_RETURN(0);
516
519
}
517
520
 
518
521
        /* Delete datarecord from database */
519
522
        /* info->rec_cache.seek_not_done is updated in cmp_record */
520
523
 
521
524
static int delete_dynamic_record(MI_INFO *info, my_off_t filepos,
522
 
                                 uint32_t second_read)
 
525
                                 uint second_read)
523
526
{
524
 
  uint32_t length,b_type;
 
527
  uint length,b_type;
525
528
  MI_BLOCK_INFO block_info,del_block;
526
529
  int error;
527
 
  bool remove_next_block;
 
530
  my_bool remove_next_block;
 
531
  DBUG_ENTER("delete_dynamic_record");
528
532
 
529
533
  /* First add a link from the last block to the new one */
530
534
  error= update_backward_delete_link(info, info->s->state.dellink, filepos);
540
544
        MI_MIN_BLOCK_LENGTH)
541
545
    {
542
546
      my_errno=HA_ERR_WRONG_IN_RECORD;
543
 
      return(1);
 
547
      DBUG_RETURN(1);
544
548
    }
545
549
    /* Check if next block is a delete block */
546
550
    del_block.second_read=0;
557
561
    mi_int3store(block_info.header+1,length);
558
562
    mi_sizestore(block_info.header+4,info->s->state.dellink);
559
563
    if (b_type & BLOCK_LAST)
560
 
      memset(block_info.header+12, 255, 8);
 
564
      bfill(block_info.header+12,8,255);
561
565
    else
562
566
      mi_sizestore(block_info.header+12,block_info.next_filepos);
563
 
    if (info->s->file_write(info,(unsigned char*) block_info.header,20,filepos,
 
567
    if (info->s->file_write(info,(uchar*) block_info.header,20,filepos,
564
568
                  MYF(MY_NABP)))
565
 
      return(1);
 
569
      DBUG_RETURN(1);
566
570
    info->s->state.dellink = filepos;
567
571
    info->state->del++;
568
572
    info->state->empty+=length;
573
577
      error=1;
574
578
  } while (!(b_type & BLOCK_LAST));
575
579
 
576
 
  return(error);
 
580
  DBUG_RETURN(error);
577
581
}
578
582
 
579
583
 
583
587
                          my_off_t filepos,     /* points at empty block */
584
588
                          ulong length,         /* length of block */
585
589
                          my_off_t next_filepos,/* Next empty block */
586
 
                          unsigned char **record,       /* pointer to record ptr */
 
590
                          uchar **record,       /* pointer to record ptr */
587
591
                          ulong *reclength,     /* length of *record */
588
592
                          int *flag)            /* *flag == 0 if header */
589
593
{
590
594
  ulong head_length,res_length,extra_length,long_block,del_length;
591
 
  unsigned char *pos,*record_end;
 
595
  uchar *pos,*record_end;
592
596
  my_off_t  next_delete_block;
593
 
  unsigned char temp[MI_SPLIT_LENGTH+MI_DYN_DELETE_BLOCK_HEADER];
 
597
  uchar temp[MI_SPLIT_LENGTH+MI_DYN_DELETE_BLOCK_HEADER];
 
598
  DBUG_ENTER("_mi_write_part_record");
594
599
 
595
600
  next_delete_block=HA_OFFSET_ERROR;
596
601
 
605
610
  if (length == *reclength+ 3 + long_block)
606
611
  {
607
612
    /* Block is exactly of the right length */
608
 
    temp[0]=(unsigned char) (1+ *flag)+(unsigned char) long_block;      /* Flag is 0 or 6 */
 
613
    temp[0]=(uchar) (1+ *flag)+(uchar) long_block;      /* Flag is 0 or 6 */
609
614
    if (long_block)
610
615
    {
611
616
      mi_int3store(temp+1,*reclength);
631
636
        temp[0]=13;
632
637
        mi_int4store(temp+1,*reclength);
633
638
        mi_int3store(temp+5,length-head_length);
634
 
        mi_sizestore((unsigned char*) temp+8,next_filepos);
 
639
        mi_sizestore((uchar*) temp+8,next_filepos);
635
640
      }
636
641
      else
637
642
      {
638
643
        head_length=5+8+long_block*2;
639
 
        temp[0]=5+(unsigned char) long_block;
 
644
        temp[0]=5+(uchar) long_block;
640
645
        if (long_block)
641
646
        {
642
647
          mi_int3store(temp+1,*reclength);
643
648
          mi_int3store(temp+4,length-head_length);
644
 
          mi_sizestore((unsigned char*) temp+7,next_filepos);
 
649
          mi_sizestore((uchar*) temp+7,next_filepos);
645
650
        }
646
651
        else
647
652
        {
648
653
          mi_int2store(temp+1,*reclength);
649
654
          mi_int2store(temp+3,length-head_length);
650
 
          mi_sizestore((unsigned char*) temp+5,next_filepos);
 
655
          mi_sizestore((uchar*) temp+5,next_filepos);
651
656
        }
652
657
      }
653
658
    }
654
659
    else
655
660
    {
656
661
      head_length=3+8+long_block;
657
 
      temp[0]=11+(unsigned char) long_block;
 
662
      temp[0]=11+(uchar) long_block;
658
663
      if (long_block)
659
664
      {
660
665
        mi_int3store(temp+1,length-head_length);
661
 
        mi_sizestore((unsigned char*) temp+4,next_filepos);
 
666
        mi_sizestore((uchar*) temp+4,next_filepos);
662
667
      }
663
668
      else
664
669
      {
665
670
        mi_int2store(temp+1,length-head_length);
666
 
        mi_sizestore((unsigned char*) temp+3,next_filepos);
 
671
        mi_sizestore((uchar*) temp+3,next_filepos);
667
672
      }
668
673
    }
669
674
  }
671
676
  {                                     /* Block with empty info last */
672
677
    head_length=4+long_block;
673
678
    extra_length= length- *reclength-head_length;
674
 
    temp[0]= (unsigned char) (3+ *flag)+(unsigned char) long_block; /* 3,4 or 9,10 */
 
679
    temp[0]= (uchar) (3+ *flag)+(uchar) long_block; /* 3,4 or 9,10 */
675
680
    if (long_block)
676
681
    {
677
682
      mi_int3store(temp+1,*reclength);
678
 
      temp[4]= (unsigned char) (extra_length);
 
683
      temp[4]= (uchar) (extra_length);
679
684
    }
680
685
    else
681
686
    {
682
687
      mi_int2store(temp+1,*reclength);
683
 
      temp[3]= (unsigned char) (extra_length);
 
688
      temp[3]= (uchar) (extra_length);
684
689
    }
685
690
    length=       *reclength+head_length;       /* Write only what is needed */
686
691
  }
 
692
  DBUG_DUMP("header",(uchar*) temp,head_length);
687
693
 
688
694
        /* Make a long block for one write */
689
695
  record_end= *record+length-head_length;
690
696
  del_length=(res_length ? MI_DYN_DELETE_BLOCK_HEADER : 0);
691
 
  memcpy(*record - head_length, temp, head_length);
 
697
  bmove((uchar*) (*record-head_length),(uchar*) temp,head_length);
692
698
  memcpy(temp,record_end,(size_t) (extra_length+del_length));
693
 
  memset(record_end, 0, extra_length);
 
699
  bzero((uchar*) record_end,extra_length);
694
700
 
695
701
  if (res_length)
696
702
  {
717
723
    pos[0]= '\0';
718
724
    mi_int3store(pos+1,res_length);
719
725
    mi_sizestore(pos+4,info->s->state.dellink);
720
 
    memset(pos+12, 255, 8);                     /* End link */
 
726
    bfill(pos+12,8,255);                        /* End link */
721
727
    next_delete_block=info->s->state.dellink;
722
728
    info->s->state.dellink= filepos+length+extra_length;
723
729
    info->state->del++;
730
736
    if (info->update & HA_STATE_EXTEND_BLOCK)
731
737
    {
732
738
      info->update&= ~HA_STATE_EXTEND_BLOCK;
733
 
      if (my_block_write(&info->rec_cache,(unsigned char*) *record-head_length,
 
739
      if (my_block_write(&info->rec_cache,(uchar*) *record-head_length,
734
740
                         length+extra_length+del_length,filepos))
735
741
      goto err;
736
742
    }
737
 
    else if (my_b_write(&info->rec_cache,(unsigned char*) *record-head_length,
 
743
    else if (my_b_write(&info->rec_cache,(uchar*) *record-head_length,
738
744
                        length+extra_length+del_length))
739
745
      goto err;
740
746
  }
741
747
  else
742
748
  {
743
749
    info->rec_cache.seek_not_done=1;
744
 
    if (info->s->file_write(info,(unsigned char*) *record-head_length,length+extra_length+
 
750
    if (info->s->file_write(info,(uchar*) *record-head_length,length+extra_length+
745
751
                  del_length,filepos,info->s->write_flag))
746
752
      goto err;
747
753
  }
748
 
  memcpy(record_end, temp, extra_length + del_length);
 
754
  memcpy(record_end,temp,(size_t) (extra_length+del_length));
749
755
  *record=record_end;
750
756
  *reclength-=(length-head_length);
751
757
  *flag=6;
758
764
      goto err;
759
765
  }
760
766
 
761
 
  return(0);
 
767
  DBUG_RETURN(0);
762
768
err:
763
 
  return(1);
 
769
  DBUG_PRINT("exit",("errno: %d",my_errno));
 
770
  DBUG_RETURN(1);
764
771
} /*_mi_write_part_record */
765
772
 
766
773
 
767
774
        /* update record from datafile */
768
775
 
769
 
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, unsigned char *record,
 
776
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *record,
770
777
                                 ulong reclength)
771
778
{
772
779
  int flag;
773
 
  uint32_t error;
 
780
  uint error;
774
781
  ulong length;
775
782
  MI_BLOCK_INFO block_info;
 
783
  DBUG_ENTER("update_dynamic_record");
776
784
 
777
785
  flag=block_info.second_read=0;
778
786
  /*
798
806
    if ((error=_mi_get_block_info(&block_info,info->dfile,filepos))
799
807
        & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR | BLOCK_FATAL_ERROR))
800
808
    {
 
809
      DBUG_PRINT("error",("Got wrong block info"));
801
810
      if (!(error & BLOCK_FATAL_ERROR))
802
811
        my_errno=HA_ERR_WRONG_IN_RECORD;
803
812
      goto err;
828
837
          & (BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
829
838
             BLOCK_FATAL_ERROR))
830
839
      {
 
840
        DBUG_PRINT("error",("Got wrong block info"));
831
841
        if (!(error & BLOCK_FATAL_ERROR))
832
842
          my_errno=HA_ERR_WRONG_IN_RECORD;
833
843
        goto err;
835
845
      length=(ulong) (block_info.filepos-filepos) + block_info.block_len;
836
846
      if (length < reclength)
837
847
      {
838
 
        uint32_t tmp=MY_ALIGN(reclength - length + 3 +
 
848
        uint tmp=MY_ALIGN(reclength - length + 3 +
839
849
                          test(reclength >= 65520L),MI_DYN_ALIGN_SIZE);
840
850
        /* Don't create a block bigger than MI_MAX_BLOCK_LENGTH */
841
 
        tmp= cmin(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
 
851
        tmp= min(length+tmp, MI_MAX_BLOCK_LENGTH)-length;
842
852
        /* Check if we can extend this block */
843
853
        if (block_info.filepos + block_info.block_len ==
844
854
            info->state->data_file_length &&
846
856
            info->s->base.max_data_file_length-tmp)
847
857
        {
848
858
          /* extend file */
 
859
          DBUG_PRINT("info",("Extending file with %d bytes",tmp));
849
860
          if (info->nextpos == info->state->data_file_length)
850
861
            info->nextpos+= tmp;
851
862
          info->state->data_file_length+= tmp;
868
879
              BLOCK_DELETED)
869
880
          {
870
881
            /* Use; Unlink it and extend the current block */
 
882
            DBUG_PRINT("info",("Extending current block"));
871
883
            if (unlink_deleted_block(info,&del_block))
872
884
              goto err;
873
885
            if ((length+=del_block.block_len) > MI_MAX_BLOCK_LENGTH)
883
895
 
884
896
              if (update_backward_delete_link(info, info->s->state.dellink,
885
897
                                              next_pos))
886
 
                return(1);
 
898
                DBUG_RETURN(1);
887
899
 
888
900
              /* create delete link for data that didn't fit into the page */
889
901
              del_block.header[0]=0;
890
902
              mi_int3store(del_block.header+1, rest_length);
891
903
              mi_sizestore(del_block.header+4,info->s->state.dellink);
892
 
              memset(del_block.header+12, 255, 8);
893
 
              if (info->s->file_write(info,(unsigned char*) del_block.header,20, next_pos,
 
904
              bfill(del_block.header+12,8,255);
 
905
              if (info->s->file_write(info,(uchar*) del_block.header,20, next_pos,
894
906
                            MYF(MY_NABP)))
895
 
                return(1);
 
907
                DBUG_RETURN(1);
896
908
              info->s->state.dellink= next_pos;
897
909
              info->s->state.split++;
898
910
              info->state->del++;
921
933
  if (block_info.next_filepos != HA_OFFSET_ERROR)
922
934
    if (delete_dynamic_record(info,block_info.next_filepos,1))
923
935
      goto err;
924
 
  return(0);
 
936
  DBUG_RETURN(0);
925
937
err:
926
 
  return(1);
 
938
  DBUG_RETURN(1);
927
939
}
928
940
 
929
941
 
930
942
        /* Pack a record. Return new reclength */
931
943
 
932
 
uint32_t _mi_rec_pack(MI_INFO *info, register unsigned char *to,
933
 
                  register const unsigned char *from)
 
944
uint _mi_rec_pack(MI_INFO *info, register uchar *to,
 
945
                  register const uchar *from)
934
946
{
935
947
  uint          length,new_length,flag,bit,i;
936
 
  unsigned char         *pos,*end,*startpos,*packpos;
 
948
  uchar         *pos,*end,*startpos,*packpos;
937
949
  enum en_fieldtype type;
938
950
  register MI_COLUMNDEF *rec;
939
951
  MI_BLOB       *blob;
 
952
  DBUG_ENTER("_mi_rec_pack");
940
953
 
941
954
  flag=0 ; bit=1;
942
955
  startpos=packpos=to; to+= info->s->base.pack_bits; blob=info->blobs;
955
968
        {
956
969
          char *temp_pos;
957
970
          size_t tmp_length=length-portable_sizeof_char_ptr;
958
 
          memcpy(to,from,tmp_length);
959
 
          memcpy(&temp_pos,from+tmp_length,sizeof(char*));
960
 
          memcpy(to + tmp_length, temp_pos, blob->length);
 
971
          memcpy((uchar*) to,from,tmp_length);
 
972
          memcpy_fixed(&temp_pos,from+tmp_length,sizeof(char*));
 
973
          memcpy(to+tmp_length,temp_pos,(size_t) blob->length);
961
974
          to+=tmp_length+blob->length;
962
975
        }
963
976
        blob++;
964
977
      }
965
978
      else if (type == FIELD_SKIP_ZERO)
966
979
      {
967
 
        if (memcmp(from,zero_string,length) == 0)
 
980
        if (memcmp((uchar*) from,zero_string,length) == 0)
968
981
          flag|=bit;
969
982
        else
970
983
        {
971
 
          memcpy(to, from, length);
972
 
          to+=length;
 
984
          memcpy((uchar*) to,from,(size_t) length); to+=length;
973
985
        }
974
986
      }
975
987
      else if (type == FIELD_SKIP_ENDSPACE ||
976
988
               type == FIELD_SKIP_PRESPACE)
977
989
      {
978
 
        pos= (unsigned char*) from; end= (unsigned char*) from + length;
 
990
        pos= (uchar*) from; end= (uchar*) from + length;
979
991
        if (type == FIELD_SKIP_ENDSPACE)
980
992
        {                                       /* Pack trailing spaces */
981
993
          while (end > from && *(end-1) == ' ')
992
1004
        {
993
1005
          if (rec->length > 255 && new_length > 127)
994
1006
          {
995
 
            to[0]= (unsigned char) ((new_length & 127) + 128);
996
 
            to[1]= (unsigned char) (new_length >> 7);
 
1007
            to[0]= (uchar) ((new_length & 127) + 128);
 
1008
            to[1]= (uchar) (new_length >> 7);
997
1009
            to+=2;
998
1010
          }
999
1011
          else
1000
 
            *to++= (unsigned char) new_length;
1001
 
          memcpy(to, pos, new_length);
1002
 
          to+=new_length;
 
1012
            *to++= (uchar) new_length;
 
1013
          memcpy((uchar*) to,pos,(size_t) new_length); to+=new_length;
1003
1014
          flag|=bit;
1004
1015
        }
1005
1016
        else
1006
1017
        {
1007
 
          memcpy(to, from, length);
1008
 
          to+=length;
 
1018
          memcpy(to,from,(size_t) length); to+=length;
1009
1019
        }
1010
1020
      }
1011
1021
      else if (type == FIELD_VARCHAR)
1012
1022
      {
1013
 
        uint32_t pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
1014
 
        uint32_t tmp_length;
 
1023
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
 
1024
        uint tmp_length;
1015
1025
        if (pack_length == 1)
1016
1026
        {
1017
 
          tmp_length= (uint) *(unsigned char*) from;
 
1027
          tmp_length= (uint) *(uchar*) from;
1018
1028
          *to++= *from;
1019
1029
        }
1020
1030
        else
1022
1032
          tmp_length= uint2korr(from);
1023
1033
          store_key_length_inc(to,tmp_length);
1024
1034
        }
1025
 
        memcpy(to, from+pack_length, tmp_length);
 
1035
        memcpy(to, from+pack_length,tmp_length);
1026
1036
        to+= tmp_length;
1027
1037
        continue;
1028
1038
      }
1029
1039
      else
1030
1040
      {
1031
 
        memcpy(to, from, length);
1032
 
        to+=length;
 
1041
        memcpy(to,from,(size_t) length); to+=length;
1033
1042
        continue;                               /* Normal field */
1034
1043
      }
1035
1044
      if ((bit= bit << 1) >= 256)
1036
1045
      {
1037
 
        *packpos++= (unsigned char) flag;
 
1046
        *packpos++= (uchar) flag;
1038
1047
        bit=1; flag=0;
1039
1048
      }
1040
1049
    }
1041
1050
    else
1042
1051
    {
1043
 
      memcpy(to, from, length);
1044
 
      to+=length;
 
1052
      memcpy(to,from,(size_t) length); to+=length;
1045
1053
    }
1046
1054
  }
1047
1055
  if (bit != 1)
1048
 
    *packpos= (unsigned char) flag;
 
1056
    *packpos= (uchar) flag;
1049
1057
  if (info->s->calc_checksum)
1050
 
    *to++= (unsigned char) info->checksum;
1051
 
  return((uint) (to-startpos));
 
1058
    *to++= (uchar) info->checksum;
 
1059
  DBUG_PRINT("exit",("packed length: %d",(int) (to-startpos)));
 
1060
  DBUG_RETURN((uint) (to-startpos));
1052
1061
} /* _mi_rec_pack */
1053
1062
 
1054
1063
 
1058
1067
  Returns 0 if record is ok.
1059
1068
*/
1060
1069
 
1061
 
bool _mi_rec_check(MI_INFO *info,const unsigned char *record, unsigned char *rec_buff,
1062
 
                      ulong packed_length, bool with_checksum)
 
1070
my_bool _mi_rec_check(MI_INFO *info,const uchar *record, uchar *rec_buff,
 
1071
                      ulong packed_length, my_bool with_checksum)
1063
1072
{
1064
1073
  uint          length,new_length,flag,bit,i;
1065
 
  unsigned char         *pos,*end,*packpos,*to;
 
1074
  uchar         *pos,*end,*packpos,*to;
1066
1075
  enum en_fieldtype type;
1067
1076
  register MI_COLUMNDEF *rec;
 
1077
  DBUG_ENTER("_mi_rec_check");
1068
1078
 
1069
1079
  packpos=rec_buff; to= rec_buff+info->s->base.pack_bits;
1070
1080
  rec=info->s->rec;
1077
1087
    {
1078
1088
      if (type == FIELD_BLOB)
1079
1089
      {
1080
 
        uint32_t blob_length=
 
1090
        uint blob_length=
1081
1091
          _mi_calc_blob_length(length-portable_sizeof_char_ptr,record);
1082
1092
        if (!blob_length && !(flag & bit))
1083
1093
          goto err;
1086
1096
      }
1087
1097
      else if (type == FIELD_SKIP_ZERO)
1088
1098
      {
1089
 
        if (memcmp(record,zero_string,length) == 0)
 
1099
        if (memcmp((uchar*) record,zero_string,length) == 0)
1090
1100
        {
1091
1101
          if (!(flag & bit))
1092
1102
            goto err;
1097
1107
      else if (type == FIELD_SKIP_ENDSPACE ||
1098
1108
               type == FIELD_SKIP_PRESPACE)
1099
1109
      {
1100
 
        pos= (unsigned char*) record; end= (unsigned char*) record + length;
 
1110
        pos= (uchar*) record; end= (uchar*) record + length;
1101
1111
        if (type == FIELD_SKIP_ENDSPACE)
1102
1112
        {                                       /* Pack trailing spaces */
1103
1113
          while (end > record && *(end-1) == ' ')
1117
1127
          if (rec->length > 255 && new_length > 127)
1118
1128
          {
1119
1129
            /* purecov: begin inspected */
1120
 
            if (to[0] != (unsigned char) ((new_length & 127) + 128) ||
1121
 
                to[1] != (unsigned char) (new_length >> 7))
 
1130
            if (to[0] != (uchar) ((new_length & 127) + 128) ||
 
1131
                to[1] != (uchar) (new_length >> 7))
1122
1132
              goto err;
1123
1133
            to+=2;
1124
1134
            /* purecov: end */
1125
1135
          }
1126
 
          else if (*to++ != (unsigned char) new_length)
 
1136
          else if (*to++ != (uchar) new_length)
1127
1137
            goto err;
1128
1138
          to+=new_length;
1129
1139
        }
1132
1142
      }
1133
1143
      else if (type == FIELD_VARCHAR)
1134
1144
      {
1135
 
        uint32_t pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
1136
 
        uint32_t tmp_length;
 
1145
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec->length -1);
 
1146
        uint tmp_length;
1137
1147
        if (pack_length == 1)
1138
1148
        {
1139
 
          tmp_length= (uint) *(unsigned char*) record;
 
1149
          tmp_length= (uint) *(uchar*) record;
1140
1150
          to+= 1+ tmp_length;
1141
1151
          continue;
1142
1152
        }
1164
1174
  if (packed_length != (uint) (to - rec_buff) + test(info->s->calc_checksum) ||
1165
1175
      (bit != 1 && (flag & ~(bit - 1))))
1166
1176
    goto err;
1167
 
  if (with_checksum && ((unsigned char) info->checksum != (unsigned char) *to))
 
1177
  if (with_checksum && ((uchar) info->checksum != (uchar) *to))
1168
1178
  {
 
1179
    DBUG_PRINT("error",("wrong checksum for row"));
1169
1180
    goto err;
1170
1181
  }
1171
 
  return(0);
 
1182
  DBUG_RETURN(0);
1172
1183
 
1173
1184
err:
1174
 
  return(1);
 
1185
  DBUG_RETURN(1);
1175
1186
}
1176
1187
 
1177
1188
 
1180
1191
        /* Returns -1 and my_errno =HA_ERR_RECORD_DELETED if reclength isn't */
1181
1192
        /* right. Returns reclength (>0) if ok */
1182
1193
 
1183
 
ulong _mi_rec_unpack(register MI_INFO *info, register unsigned char *to, unsigned char *from,
 
1194
ulong _mi_rec_unpack(register MI_INFO *info, register uchar *to, uchar *from,
1184
1195
                     ulong found_length)
1185
1196
{
1186
 
  uint32_t flag,bit,length,rec_length,min_pack_length;
 
1197
  uint flag,bit,length,rec_length,min_pack_length;
1187
1198
  enum en_fieldtype type;
1188
 
  unsigned char *from_end,*to_end,*packpos;
 
1199
  uchar *from_end,*to_end,*packpos;
1189
1200
  register MI_COLUMNDEF *rec,*end_field;
 
1201
  DBUG_ENTER("_mi_rec_unpack");
1190
1202
 
1191
1203
  to_end=to + info->s->base.reclength;
1192
1204
  from_end=from+found_length;
1193
 
  flag= (unsigned char) *from; bit=1; packpos=from;
 
1205
  flag= (uchar) *from; bit=1; packpos=from;
1194
1206
  if (found_length < info->s->base.min_pack_length)
1195
1207
    goto err;
1196
1208
  from+= info->s->base.pack_bits;
1205
1217
    {
1206
1218
      if (type == FIELD_VARCHAR)
1207
1219
      {
1208
 
        uint32_t pack_length= HA_VARCHAR_PACKLENGTH(rec_length-1);
 
1220
        uint pack_length= HA_VARCHAR_PACKLENGTH(rec_length-1);
1209
1221
        if (pack_length == 1)
1210
1222
        {
1211
 
          length= (uint) *(unsigned char*) from;
 
1223
          length= (uint) *(uchar*) from;
1212
1224
          if (length > rec_length-1)
1213
1225
            goto err;
1214
1226
          *to= *from++;
1230
1242
      if (flag & bit)
1231
1243
      {
1232
1244
        if (type == FIELD_BLOB || type == FIELD_SKIP_ZERO)
1233
 
          memset(to, 0, rec_length);
 
1245
          bzero((uchar*) to,rec_length);
1234
1246
        else if (type == FIELD_SKIP_ENDSPACE ||
1235
1247
                 type == FIELD_SKIP_PRESPACE)
1236
1248
        {
1238
1250
          {
1239
1251
            if (from + 1 >= from_end)
1240
1252
              goto err;
1241
 
            length= (*from & 127)+ ((uint) (unsigned char) *(from+1) << 7); from+=2;
 
1253
            length= (*from & 127)+ ((uint) (uchar) *(from+1) << 7); from+=2;
1242
1254
          }
1243
1255
          else
1244
1256
          {
1245
1257
            if (from == from_end)
1246
1258
              goto err;
1247
 
            length= (unsigned char) *from++;
 
1259
            length= (uchar) *from++;
1248
1260
          }
1249
1261
          min_pack_length--;
1250
1262
          if (length >= rec_length ||
1252
1264
            goto err;
1253
1265
          if (type == FIELD_SKIP_ENDSPACE)
1254
1266
          {
1255
 
            memcpy(to, from, length);
1256
 
            memset(to+length, ' ', rec_length-length);
 
1267
            memcpy(to,(uchar*) from,(size_t) length);
 
1268
            bfill((uchar*) to+length,rec_length-length,' ');
1257
1269
          }
1258
1270
          else
1259
1271
          {
1260
 
            memset(to, ' ', rec_length-length);
1261
 
            memcpy(to + rec_length - length, from, length);
 
1272
            bfill((uchar*) to,rec_length-length,' ');
 
1273
            memcpy(to+rec_length-length,(uchar*) from,(size_t) length);
1262
1274
          }
1263
1275
          from+=length;
1264
1276
        }
1265
1277
      }
1266
1278
      else if (type == FIELD_BLOB)
1267
1279
      {
1268
 
        uint32_t size_length=rec_length- portable_sizeof_char_ptr;
 
1280
        uint size_length=rec_length- portable_sizeof_char_ptr;
1269
1281
        ulong blob_length=_mi_calc_blob_length(size_length,from);
1270
1282
        ulong from_left= (ulong) (from_end - from);
1271
1283
        if (from_left < size_length ||
1272
1284
            from_left - size_length < blob_length ||
1273
1285
            from_left - size_length - blob_length < min_pack_length)
1274
1286
          goto err;
1275
 
        memcpy(to, from, size_length);
 
1287
        memcpy((uchar*) to,(uchar*) from,(size_t) size_length);
1276
1288
        from+=size_length;
1277
 
        memcpy(to+size_length, &from, sizeof(char*));
 
1289
        memcpy_fixed((uchar*) to+size_length,(uchar*) &from,sizeof(char*));
1278
1290
        from+=blob_length;
1279
1291
      }
1280
1292
      else
1283
1295
          min_pack_length--;
1284
1296
        if (min_pack_length + rec_length > (uint) (from_end - from))
1285
1297
          goto err;
1286
 
        memcpy(to, from, rec_length);
1287
 
        from+=rec_length;
 
1298
        memcpy(to,(uchar*) from,(size_t) rec_length); from+=rec_length;
1288
1299
      }
1289
1300
      if ((bit= bit << 1) >= 256)
1290
1301
      {
1291
 
        flag= (unsigned char) *++packpos; bit=1;
 
1302
        flag= (uchar) *++packpos; bit=1;
1292
1303
      }
1293
1304
    }
1294
1305
    else
1296
1307
      if (min_pack_length > (uint) (from_end - from))
1297
1308
        goto err;
1298
1309
      min_pack_length-=rec_length;
1299
 
      memcpy(to, from, rec_length);
 
1310
      memcpy(to, (uchar*) from, (size_t) rec_length);
1300
1311
      from+=rec_length;
1301
1312
    }
1302
1313
  }
1303
1314
  if (info->s->calc_checksum)
1304
1315
    from++;
1305
1316
  if (to == to_end && from == from_end && (bit == 1 || !(flag & ~(bit-1))))
1306
 
    return(found_length);
 
1317
    DBUG_RETURN(found_length);
1307
1318
 
1308
1319
err:
1309
1320
  my_errno= HA_ERR_WRONG_IN_RECORD;
1310
 
  return(MY_FILE_ERROR);
 
1321
  DBUG_PRINT("error",("to_end: 0x%lx -> 0x%lx  from_end: 0x%lx -> 0x%lx",
 
1322
                      (long) to, (long) to_end, (long) from, (long) from_end));
 
1323
  DBUG_DUMP("from",(uchar*) info->rec_buff,info->s->base.min_pack_length);
 
1324
  DBUG_RETURN(MY_FILE_ERROR);
1311
1325
} /* _mi_rec_unpack */
1312
1326
 
1313
1327
 
1314
1328
        /* Calc length of blob. Update info in blobs->length */
1315
1329
 
1316
 
ulong _my_calc_total_blob_length(MI_INFO *info, const unsigned char *record)
 
1330
ulong _my_calc_total_blob_length(MI_INFO *info, const uchar *record)
1317
1331
{
1318
1332
  ulong length;
1319
1333
  MI_BLOB *blob,*end;
1329
1343
}
1330
1344
 
1331
1345
 
1332
 
ulong _mi_calc_blob_length(uint32_t length, const unsigned char *pos)
 
1346
ulong _mi_calc_blob_length(uint length, const uchar *pos)
1333
1347
{
1334
1348
  switch (length) {
1335
1349
  case 1:
1336
 
    return (uint) (unsigned char) *pos;
 
1350
    return (uint) (uchar) *pos;
1337
1351
  case 2:
1338
1352
    return (uint) uint2korr(pos);
1339
1353
  case 3:
1347
1361
}
1348
1362
 
1349
1363
 
1350
 
void _my_store_blob_length(unsigned char *pos,uint32_t pack_length,uint32_t length)
 
1364
void _my_store_blob_length(uchar *pos,uint pack_length,uint length)
1351
1365
{
1352
1366
  switch (pack_length) {
1353
1367
  case 1:
1354
 
    *pos= (unsigned char) length;
 
1368
    *pos= (uchar) length;
1355
1369
    break;
1356
1370
  case 2:
1357
1371
    int2store(pos,length);
1400
1414
    -1          Error
1401
1415
*/
1402
1416
 
1403
 
int _mi_read_dynamic_record(MI_INFO *info, my_off_t filepos, unsigned char *buf)
 
1417
int _mi_read_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *buf)
1404
1418
{
1405
1419
  int block_of_record;
1406
 
  uint32_t b_type, left_length= 0;
1407
 
  unsigned char *to= NULL;
 
1420
  uint b_type, left_length= 0;
 
1421
  uchar *to= NULL;
1408
1422
  MI_BLOCK_INFO block_info;
1409
1423
  File file;
 
1424
  DBUG_ENTER("mi_read_dynamic_record");
1410
1425
 
1411
1426
  if (filepos != HA_OFFSET_ERROR)
1412
1427
  {
1449
1464
        goto panic;                     /* Wrong linked record */
1450
1465
      /* copy information that is already read */
1451
1466
      {
1452
 
        uint32_t offset= (uint) (block_info.filepos - filepos);
1453
 
        uint32_t prefetch_len= (sizeof(block_info.header) - offset);
 
1467
        uint offset= (uint) (block_info.filepos - filepos);
 
1468
        uint prefetch_len= (sizeof(block_info.header) - offset);
1454
1469
        filepos+= sizeof(block_info.header);
1455
1470
 
1456
1471
        if (prefetch_len > block_info.data_len)
1457
1472
          prefetch_len= block_info.data_len;
1458
1473
        if (prefetch_len)
1459
1474
        {
1460
 
          memcpy(to, block_info.header + offset, prefetch_len);
 
1475
          memcpy((uchar*) to, block_info.header + offset, prefetch_len);
1461
1476
          block_info.data_len-= prefetch_len;
1462
1477
          left_length-= prefetch_len;
1463
1478
          to+= prefetch_len;
1475
1490
          there is no equivalent without seeking. We are at the right
1476
1491
          position already. :(
1477
1492
        */
1478
 
        if (info->s->file_read(info, (unsigned char*) to, block_info.data_len,
 
1493
        if (info->s->file_read(info, (uchar*) to, block_info.data_len,
1479
1494
                               filepos, MYF(MY_NABP)))
1480
1495
          goto panic;
1481
1496
        left_length-=block_info.data_len;
1486
1501
 
1487
1502
    info->update|= HA_STATE_AKTIV;      /* We have a aktive record */
1488
1503
    fast_mi_writeinfo(info);
1489
 
    return(_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
 
1504
    DBUG_RETURN(_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
1490
1505
                MY_FILE_ERROR ? 0 : -1);
1491
1506
  }
1492
1507
  fast_mi_writeinfo(info);
1493
 
  return(-1);                   /* Wrong data to read */
 
1508
  DBUG_RETURN(-1);                      /* Wrong data to read */
1494
1509
 
1495
1510
panic:
1496
1511
  my_errno=HA_ERR_WRONG_IN_RECORD;
1497
1512
err:
1498
 
  _mi_writeinfo(info,0);
1499
 
  return(-1);
 
1513
  VOID(_mi_writeinfo(info,0));
 
1514
  DBUG_RETURN(-1);
1500
1515
}
1501
1516
 
1502
1517
        /* compare unique constraint between stored rows */
1503
1518
 
1504
1519
int _mi_cmp_dynamic_unique(MI_INFO *info, MI_UNIQUEDEF *def,
1505
 
                           const unsigned char *record, my_off_t pos)
 
1520
                           const uchar *record, my_off_t pos)
1506
1521
{
1507
 
  unsigned char *rec_buff,*old_record;
 
1522
  uchar *rec_buff,*old_record;
1508
1523
  int error;
 
1524
  DBUG_ENTER("_mi_cmp_dynamic_unique");
1509
1525
 
1510
1526
  if (!(old_record=my_alloca(info->s->base.reclength)))
1511
 
    return(1);
 
1527
    DBUG_RETURN(1);
1512
1528
 
1513
1529
  /* Don't let the compare destroy blobs that may be in use */
1514
1530
  rec_buff=info->rec_buff;
1519
1535
    error=mi_unique_comp(def, record, old_record, def->null_are_equal);
1520
1536
  if (info->s->base.blobs)
1521
1537
  {
1522
 
    void * rec_buff_ptr= mi_get_rec_buff_ptr(info, info->rec_buff);
1523
 
    if (rec_buff_ptr != NULL)
1524
 
      free(rec_buff_ptr);
 
1538
    my_free(mi_get_rec_buff_ptr(info, info->rec_buff), MYF(MY_ALLOW_ZERO_PTR));
1525
1539
    info->rec_buff=rec_buff;
1526
1540
  }
1527
1541
  my_afree(old_record);
1528
 
  return(error);
 
1542
  DBUG_RETURN(error);
1529
1543
}
1530
1544
 
1531
1545
 
1532
1546
        /* Compare of record one disk with packed record in memory */
1533
1547
 
1534
 
int _mi_cmp_dynamic_record(register MI_INFO *info, register const unsigned char *record)
 
1548
int _mi_cmp_dynamic_record(register MI_INFO *info, register const uchar *record)
1535
1549
{
1536
 
  uint32_t flag,reclength,b_type;
 
1550
  uint flag,reclength,b_type;
1537
1551
  my_off_t filepos;
1538
 
  unsigned char *buffer;
 
1552
  uchar *buffer;
1539
1553
  MI_BLOCK_INFO block_info;
 
1554
  DBUG_ENTER("_mi_cmp_dynamic_record");
1540
1555
 
1541
1556
  if (info->opt_flag & WRITE_CACHE_USED)
1542
1557
  {
1543
1558
    info->update&= ~(HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK);
1544
1559
    if (flush_io_cache(&info->rec_cache))
1545
 
      return(-1);
 
1560
      DBUG_RETURN(-1);
1546
1561
  }
1547
1562
  info->rec_cache.seek_not_done=1;
1548
1563
 
1553
1568
  {                                             /* If check isn't disabled  */
1554
1569
    if (info->s->base.blobs)
1555
1570
    {
1556
 
      if (!(buffer=(unsigned char*) my_alloca(info->s->base.pack_reclength+
 
1571
      if (!(buffer=(uchar*) my_alloca(info->s->base.pack_reclength+
1557
1572
                                     _my_calc_total_blob_length(info,record))))
1558
 
        return(-1);
 
1573
        DBUG_RETURN(-1);
1559
1574
    }
1560
1575
    reclength=_mi_rec_pack(info,buffer,record);
1561
1576
    record= buffer;
1601
1616
  my_errno=0;
1602
1617
err:
1603
1618
  if (buffer != info->rec_buff)
1604
 
    my_afree((unsigned char*) buffer);
1605
 
  return(my_errno);
 
1619
    my_afree((uchar*) buffer);
 
1620
  DBUG_RETURN(my_errno);
1606
1621
}
1607
1622
 
1608
1623
 
1609
1624
        /* Compare file to buffert */
1610
1625
 
1611
 
static int _mi_cmp_buffer(File file, const unsigned char *buff, my_off_t filepos,
1612
 
                          uint32_t length)
 
1626
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
 
1627
                          uint length)
1613
1628
{
1614
 
  uint32_t next_length;
1615
 
  unsigned char temp_buff[IO_SIZE*2];
 
1629
  uint next_length;
 
1630
  uchar temp_buff[IO_SIZE*2];
 
1631
  DBUG_ENTER("_mi_cmp_buffer");
1616
1632
 
1617
1633
  next_length= IO_SIZE*2 - (uint) (filepos & (IO_SIZE-1));
1618
1634
 
1628
1644
  }
1629
1645
  if (my_pread(file,temp_buff,length,filepos,MYF(MY_NABP)))
1630
1646
    goto err;
1631
 
  return(memcmp(buff,temp_buff,length));
 
1647
  DBUG_RETURN(memcmp(buff,temp_buff,length));
1632
1648
err:
1633
 
  return(1);
 
1649
  DBUG_RETURN(1);
1634
1650
}
1635
1651
 
1636
1652
 
1668
1684
    != 0        Error
1669
1685
*/
1670
1686
 
1671
 
int _mi_read_rnd_dynamic_record(MI_INFO *info, unsigned char *buf,
 
1687
int _mi_read_rnd_dynamic_record(MI_INFO *info, uchar *buf,
1672
1688
                                register my_off_t filepos,
1673
 
                                bool skip_deleted_blocks)
 
1689
                                my_bool skip_deleted_blocks)
1674
1690
{
1675
1691
  int block_of_record, info_read, save_errno;
1676
 
  uint32_t left_len,b_type;
1677
 
  unsigned char *to= NULL;
 
1692
  uint left_len,b_type;
 
1693
  uchar *to= NULL;
1678
1694
  MI_BLOCK_INFO block_info;
1679
1695
  MYISAM_SHARE *share=info->s;
 
1696
  DBUG_ENTER("_mi_read_rnd_dynamic_record");
1680
1697
 
1681
1698
  info_read=0;
1682
1699
 
1683
1700
  if (info->lock_type == F_UNLCK)
1684
1701
  {
 
1702
#ifndef UNSAFE_LOCKING
 
1703
    if (share->tot_locks == 0)
 
1704
    {
 
1705
      if (my_lock(share->kfile,F_RDLCK,0L,F_TO_EOF,
 
1706
                  MYF(MY_SEEK_NOT_DONE) | info->lock_wait))
 
1707
        DBUG_RETURN(my_errno);
 
1708
    }
 
1709
#else
1685
1710
    info->tmp_lock_type=F_RDLCK;
 
1711
#endif
1686
1712
  }
1687
1713
  else
1688
1714
    info_read=1;                                /* memory-keyinfoblock is ok */
1709
1735
    }
1710
1736
    if (info->opt_flag & READ_CACHE_USED)
1711
1737
    {
1712
 
      if (_mi_read_cache(&info->rec_cache,(unsigned char*) block_info.header,filepos,
 
1738
      if (_mi_read_cache(&info->rec_cache,(uchar*) block_info.header,filepos,
1713
1739
                         sizeof(block_info.header),
1714
1740
                         (!block_of_record && skip_deleted_blocks ?
1715
1741
                          READING_NEXT : 0) | READING_HEADER))
1721
1747
      if (info->opt_flag & WRITE_CACHE_USED &&
1722
1748
          info->rec_cache.pos_in_file < filepos + MI_BLOCK_INFO_HEADER_LENGTH &&
1723
1749
          flush_io_cache(&info->rec_cache))
1724
 
        return(my_errno);
 
1750
        DBUG_RETURN(my_errno);
1725
1751
      info->rec_cache.seek_not_done=1;
1726
1752
      b_type=_mi_get_block_info(&block_info,info->dfile,filepos);
1727
1753
    }
1764
1790
 
1765
1791
    /* copy information that is already read */
1766
1792
    {
1767
 
      uint32_t offset=(uint) (block_info.filepos - filepos);
1768
 
      uint32_t tmp_length= (sizeof(block_info.header) - offset);
 
1793
      uint offset=(uint) (block_info.filepos - filepos);
 
1794
      uint tmp_length= (sizeof(block_info.header) - offset);
1769
1795
      filepos=block_info.filepos;
1770
1796
 
1771
1797
      if (tmp_length > block_info.data_len)
1772
1798
        tmp_length= block_info.data_len;
1773
1799
      if (tmp_length)
1774
1800
      {
1775
 
        memcpy(to, block_info.header+offset,tmp_length);
 
1801
        memcpy((uchar*) to, block_info.header+offset,tmp_length);
1776
1802
        block_info.data_len-=tmp_length;
1777
1803
        left_len-=tmp_length;
1778
1804
        to+=tmp_length;
1784
1810
    {
1785
1811
      if (info->opt_flag & READ_CACHE_USED)
1786
1812
      {
1787
 
        if (_mi_read_cache(&info->rec_cache,(unsigned char*) to,filepos,
 
1813
        if (_mi_read_cache(&info->rec_cache,(uchar*) to,filepos,
1788
1814
                           block_info.data_len,
1789
1815
                           (!block_of_record && skip_deleted_blocks) ?
1790
1816
                           READING_NEXT : 0))
1797
1823
            block_info.filepos + block_info.data_len &&
1798
1824
            flush_io_cache(&info->rec_cache))
1799
1825
          goto err;
1800
 
        /* my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0)); */
1801
 
        if (my_read(info->dfile,(unsigned char*) to,block_info.data_len,MYF(MY_NABP)))
 
1826
        /* VOID(my_seek(info->dfile,filepos,MY_SEEK_SET,MYF(0))); */
 
1827
        if (my_read(info->dfile,(uchar*) to,block_info.data_len,MYF(MY_NABP)))
1802
1828
        {
1803
1829
          if (my_errno == -1)
1804
1830
            my_errno= HA_ERR_WRONG_IN_RECORD;   /* Unexpected end of file */
1824
1850
  fast_mi_writeinfo(info);
1825
1851
  if (_mi_rec_unpack(info,buf,info->rec_buff,block_info.rec_len) !=
1826
1852
      MY_FILE_ERROR)
1827
 
    return(0);
1828
 
  return(my_errno);                     /* Wrong record */
 
1853
    DBUG_RETURN(0);
 
1854
  DBUG_RETURN(my_errno);                        /* Wrong record */
1829
1855
 
1830
1856
panic:
1831
1857
  my_errno=HA_ERR_WRONG_IN_RECORD;              /* Something is fatal wrong */
1832
1858
err:
1833
1859
  save_errno=my_errno;
1834
 
  _mi_writeinfo(info,0);
1835
 
  return(my_errno=save_errno);
 
1860
  VOID(_mi_writeinfo(info,0));
 
1861
  DBUG_RETURN(my_errno=save_errno);
1836
1862
}
1837
1863
 
1838
1864
 
1839
1865
        /* Read and process header from a dynamic-record-file */
1840
1866
 
1841
 
uint32_t _mi_get_block_info(MI_BLOCK_INFO *info, File file, my_off_t filepos)
 
1867
uint _mi_get_block_info(MI_BLOCK_INFO *info, File file, my_off_t filepos)
1842
1868
{
1843
 
  uint32_t return_val=0;
1844
 
  unsigned char *header=info->header;
 
1869
  uint return_val=0;
 
1870
  uchar *header=info->header;
1845
1871
 
1846
1872
  if (file >= 0)
1847
1873
  {
1850
1876
      pointer set to the end of the header after this function.
1851
1877
      my_pread() may leave the file pointer untouched.
1852
1878
    */
1853
 
    my_seek(file,filepos,MY_SEEK_SET,MYF(0));
 
1879
    VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
1854
1880
    if (my_read(file, header, sizeof(info->header),MYF(0)) !=
1855
1881
        sizeof(info->header))
1856
1882
      goto err;
1857
1883
  }
 
1884
  DBUG_DUMP("header",header,MI_BLOCK_INFO_HEADER_LENGTH);
1858
1885
  if (info->second_read)
1859
1886
  {
1860
1887
    if (info->header[0] <= 6 || info->header[0] == 13)
1878
1905
    info->prev_filepos=mi_sizekorr(header+12);
1879
1906
#if SIZEOF_OFF_T == 4
1880
1907
    if ((mi_uint4korr(header+4) != 0 &&
1881
 
         (mi_uint4korr(header+4) != UINT32_MAX ||
1882
 
          info->next_filepos != UINT32_MAX) ||
 
1908
         (mi_uint4korr(header+4) != (ulong) ~0 ||
 
1909
          info->next_filepos != (ulong) ~0)) ||
1883
1910
        (mi_uint4korr(header+12) != 0 &&
1884
 
         (mi_uint4korr(header+12) != UINT32_MAX ||
1885
 
          info->prev_filepos != UINT32_MAX))
 
1911
         (mi_uint4korr(header+12) != (ulong) ~0 ||
 
1912
          info->prev_filepos != (ulong) ~0)))
1886
1913
      goto err;
1887
1914
#endif
1888
1915
    return return_val | BLOCK_DELETED;          /* Deleted block */