~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_dynrec.c

  • Committer: Monty Taylor
  • Date: 2008-07-01 14:33:36 UTC
  • mto: (28.1.12 backport_patch)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: monty@inaugust.com-20080701143336-8uihm7dhpu92rt0q
Somehow missed moving password.c. Duh.

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