~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_open.c

  • Committer: Monty Taylor
  • Date: 2008-10-05 01:41:06 UTC
  • Revision ID: monty@inaugust.com-20081005014106-bulqe4kp7i6ipts1
Moved qsort declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include "myisamdef.h"
19
19
#include <mystrings/m_ctype.h>
20
 
#include <drizzled/util/test.h>
21
20
 
22
21
static void setup_key_functions(MI_KEYDEF *keyinfo);
23
22
#define get_next_element(to,pos,size) \
63
62
  have an open count of 0.
64
63
******************************************************************************/
65
64
 
66
 
MI_INFO *mi_open(const char *name, int mode, uint32_t open_flags)
 
65
MI_INFO *mi_open(const char *name, int mode, uint open_flags)
67
66
{
68
67
  int lock_error,kfile,open_mode,save_errno,have_rtree=0;
69
 
  uint32_t i,j,len,errpos,head_length,base_pos,offset,info_length,keys,
 
68
  uint i,j,len,errpos,head_length,base_pos,offset,info_length,keys,
70
69
    key_parts,unique_key_parts,fulltext_keys,uniques;
71
70
  char name_buff[FN_REFLEN], org_name[FN_REFLEN], index_name[FN_REFLEN],
72
71
       data_name[FN_REFLEN];
73
 
  unsigned char *disk_cache= NULL;
74
 
  unsigned char *disk_pos, *end_pos;
 
72
  uchar *disk_cache, *disk_pos, *end_pos;
75
73
  MI_INFO info,*m_info,*old_info;
76
74
  MYISAM_SHARE share_buff,*share;
77
75
  ulong rec_per_key_part[HA_MAX_POSSIBLE_KEY*MI_MAX_KEY_SEG];
94
92
    share_buff.state.rec_per_key_part=rec_per_key_part;
95
93
    share_buff.state.key_root=key_root;
96
94
    share_buff.state.key_del=key_del;
97
 
    share_buff.key_cache= multi_key_cache_search((unsigned char*) name_buff,
 
95
    share_buff.key_cache= multi_key_cache_search((uchar*) name_buff,
98
96
                                                 strlen(name_buff));
99
97
 
100
 
    if ((kfile=my_open(name_buff,(open_mode=O_RDWR),MYF(0))) < 0)
 
98
    if ((kfile=my_open(name_buff,(open_mode=O_RDWR) | O_SHARE,MYF(0))) < 0)
101
99
    {
102
100
      if ((errno != EROFS && errno != EACCES) ||
103
101
          mode != O_RDONLY ||
104
 
          (kfile=my_open(name_buff,(open_mode=O_RDONLY),MYF(0))) < 0)
 
102
          (kfile=my_open(name_buff,(open_mode=O_RDONLY) | O_SHARE,MYF(0))) < 0)
105
103
        goto err;
106
104
    }
107
105
    share->mode=open_mode;
144
142
 
145
143
    info_length=mi_uint2korr(share->state.header.header_length);
146
144
    base_pos=mi_uint2korr(share->state.header.base_pos);
147
 
    if (!(disk_cache= (unsigned char*) malloc(info_length+128)))
 
145
    if (!(disk_cache= (uchar*) my_alloca(info_length+128)))
148
146
    {
149
147
      my_errno=ENOMEM;
150
148
      goto err;
380
378
    share->data_file_type=STATIC_RECORD;
381
379
    if (share->options & HA_OPTION_PACK_RECORD)
382
380
      share->data_file_type = DYNAMIC_RECORD;
383
 
    free(disk_cache);
384
 
    disk_cache= NULL;
 
381
    my_afree(disk_cache);
385
382
    mi_setup_functions(share);
386
383
    share->is_log_table= false;
387
384
    thr_lock_init(&share->lock);
513
510
  return(m_info);
514
511
 
515
512
err:
516
 
  if (disk_cache != NULL)
517
 
    free(disk_cache);
518
513
  save_errno=my_errno ? my_errno : HA_ERR_END_OF_FILE;
519
514
  if ((save_errno == HA_ERR_CRASHED) ||
520
515
      (save_errno == HA_ERR_CRASHED_ON_USAGE) ||
522
517
    mi_report_error(save_errno, name);
523
518
  switch (errpos) {
524
519
  case 6:
525
 
    free((unsigned char*) m_info);
 
520
    my_free((uchar*) m_info,MYF(0));
526
521
    /* fall through */
527
522
  case 5:
528
523
    my_close(info.dfile,MYF(0));
530
525
      break;                                    /* Don't remove open table */
531
526
    /* fall through */
532
527
  case 4:
533
 
    free((unsigned char*) share);
 
528
    my_free((uchar*) share,MYF(0));
534
529
    /* fall through */
535
530
  case 3:
536
531
    /* fall through */
 
532
  case 2:
 
533
    my_afree(disk_cache);
 
534
    /* fall through */
537
535
  case 1:
538
536
    my_close(kfile,MYF(0));
539
537
    /* fall through */
547
545
} /* mi_open */
548
546
 
549
547
 
550
 
unsigned char *mi_alloc_rec_buff(MI_INFO *info, ulong length, unsigned char **buf)
 
548
uchar *mi_alloc_rec_buff(MI_INFO *info, ulong length, uchar **buf)
551
549
{
552
 
  uint32_t extra;
 
550
  uint extra;
553
551
  uint32_t old_length= 0;
554
552
 
555
553
  if (! *buf || length > (old_length=mi_get_rec_buff_len(info, *buf)))
556
554
  {
557
 
    unsigned char *newptr = *buf;
 
555
    uchar *newptr = *buf;
558
556
 
559
557
    /* to simplify initial init of info->rec_buf in mi_open and mi_extra */
560
558
    if (length == (ulong) -1)
574
572
            MI_REC_BUFF_OFFSET : 0);
575
573
    if (extra && newptr)
576
574
      newptr-= MI_REC_BUFF_OFFSET;
577
 
    if (!(newptr=(unsigned char*) my_realloc((unsigned char*)newptr, length+extra+8,
 
575
    if (!(newptr=(uchar*) my_realloc((uchar*)newptr, length+extra+8,
578
576
                                     MYF(MY_ALLOW_ZERO_PTR))))
579
577
      return newptr;
580
578
    *((uint32_t *) newptr)= (uint32_t) length;
695
693
   Function to save and store the header in the index file (.MYI)
696
694
*/
697
695
 
698
 
uint32_t mi_state_info_write(File file, MI_STATE_INFO *state, uint32_t pWrite)
 
696
uint mi_state_info_write(File file, MI_STATE_INFO *state, uint pWrite)
699
697
{
700
 
  unsigned char  buff[MI_STATE_INFO_SIZE + MI_STATE_EXTRA_SIZE];
701
 
  unsigned char *ptr=buff;
 
698
  uchar  buff[MI_STATE_INFO_SIZE + MI_STATE_EXTRA_SIZE];
 
699
  uchar *ptr=buff;
702
700
  uint  i, keys= (uint) state->header.keys,
703
701
        key_blocks=state->header.max_block_size_index;
704
702
 
707
705
 
708
706
  /* open_count must be first because of _mi_mark_file_changed ! */
709
707
  mi_int2store(ptr,state->open_count);          ptr +=2;
710
 
  *ptr++= (unsigned char)state->changed; *ptr++= state->sortkey;
 
708
  *ptr++= (uchar)state->changed; *ptr++= state->sortkey;
711
709
  mi_rowstore(ptr,state->state.records);        ptr +=8;
712
710
  mi_rowstore(ptr,state->state.del);            ptr +=8;
713
711
  mi_rowstore(ptr,state->split);                ptr +=8;
735
733
  }
736
734
  if (pWrite & 2)                               /* From isamchk */
737
735
  {
738
 
    uint32_t key_parts= mi_uint2korr(state->header.key_parts);
 
736
    uint key_parts= mi_uint2korr(state->header.key_parts);
739
737
    mi_int4store(ptr,state->sec_index_changed); ptr +=4;
740
738
    mi_int4store(ptr,state->sec_index_used);    ptr +=4;
741
739
    mi_int4store(ptr,state->version);           ptr +=4;
758
756
}
759
757
 
760
758
 
761
 
unsigned char *mi_state_info_read(unsigned char *ptr, MI_STATE_INFO *state)
 
759
uchar *mi_state_info_read(uchar *ptr, MI_STATE_INFO *state)
762
760
{
763
 
  uint32_t i,keys,key_parts,key_blocks;
 
761
  uint i,keys,key_parts,key_blocks;
764
762
  memcpy(&state->header,ptr, sizeof(state->header));
765
763
  ptr +=sizeof(state->header);
766
764
  keys=(uint) state->header.keys;
811
809
}
812
810
 
813
811
 
814
 
uint32_t mi_state_info_read_dsk(File file, MI_STATE_INFO *state, bool pRead)
 
812
uint mi_state_info_read_dsk(File file, MI_STATE_INFO *state, bool pRead)
815
813
{
816
 
  unsigned char buff[MI_STATE_INFO_SIZE + MI_STATE_EXTRA_SIZE];
 
814
  uchar buff[MI_STATE_INFO_SIZE + MI_STATE_EXTRA_SIZE];
817
815
 
818
816
  if (!myisam_single_user)
819
817
  {
834
832
**  store and read of MI_BASE_INFO
835
833
****************************************************************************/
836
834
 
837
 
uint32_t mi_base_info_write(File file, MI_BASE_INFO *base)
 
835
uint mi_base_info_write(File file, MI_BASE_INFO *base)
838
836
{
839
 
  unsigned char buff[MI_BASE_INFO_SIZE], *ptr=buff;
 
837
  uchar buff[MI_BASE_INFO_SIZE], *ptr=buff;
840
838
 
841
839
  mi_sizestore(ptr,base->keystart);                     ptr +=8;
842
840
  mi_sizestore(ptr,base->max_data_file_length);         ptr +=8;
861
859
  mi_int2store(ptr,base->max_key_length);               ptr +=2;
862
860
  mi_int2store(ptr,base->extra_alloc_bytes);            ptr +=2;
863
861
  *ptr++= base->extra_alloc_procent;
864
 
  /* old raid info  slots */
865
 
  *ptr++= 0;
866
 
  mi_int2store(ptr,UINT16_C(0));                        ptr +=2;
867
 
  mi_int4store(ptr,UINT32_C(0));                        ptr +=4;
868
 
 
 
862
  *ptr++= base->raid_type;
 
863
  mi_int2store(ptr,base->raid_chunks);                  ptr +=2;
 
864
  mi_int4store(ptr,base->raid_chunksize);               ptr +=4;
869
865
  memset(ptr, 0, 6);                                    ptr +=6; /* extra */
870
866
  return my_write(file, buff, (size_t) (ptr-buff), MYF(MY_NABP)) != 0;
871
867
}
872
868
 
873
869
 
874
 
unsigned char *my_n_base_info_read(unsigned char *ptr, MI_BASE_INFO *base)
 
870
uchar *my_n_base_info_read(uchar *ptr, MI_BASE_INFO *base)
875
871
{
876
872
  base->keystart = mi_sizekorr(ptr);                    ptr +=8;
877
873
  base->max_data_file_length = mi_sizekorr(ptr);        ptr +=8;
897
893
  base->max_key_length = mi_uint2korr(ptr);             ptr +=2;
898
894
  base->extra_alloc_bytes = mi_uint2korr(ptr);          ptr +=2;
899
895
  base->extra_alloc_procent = *ptr++;
900
 
 
901
 
  /* advance past raid_type (1) raid_chunks (2) and raid_chunksize (4) */
902
 
  ptr+= 7; 
 
896
  base->raid_type= *ptr++;
 
897
  base->raid_chunks= mi_uint2korr(ptr);                 ptr +=2;
 
898
  base->raid_chunksize= mi_uint4korr(ptr);              ptr +=4;
 
899
  /* TO BE REMOVED: Fix for old RAID files */
 
900
  if (base->raid_type == 0)
 
901
  {
 
902
    base->raid_chunks=0;
 
903
    base->raid_chunksize=0;
 
904
  }
903
905
 
904
906
  ptr+=6;
905
907
  return ptr;
909
911
  mi_keydef
910
912
---------------------------------------------------------------------------*/
911
913
 
912
 
uint32_t mi_keydef_write(File file, MI_KEYDEF *keydef)
 
914
uint mi_keydef_write(File file, MI_KEYDEF *keydef)
913
915
{
914
 
  unsigned char buff[MI_KEYDEF_SIZE];
915
 
  unsigned char *ptr=buff;
 
916
  uchar buff[MI_KEYDEF_SIZE];
 
917
  uchar *ptr=buff;
916
918
 
917
 
  *ptr++ = (unsigned char) keydef->keysegs;
 
919
  *ptr++ = (uchar) keydef->keysegs;
918
920
  *ptr++ = keydef->key_alg;                     /* Rtree or Btree */
919
921
  mi_int2store(ptr,keydef->flag);               ptr +=2;
920
922
  mi_int2store(ptr,keydef->block_length);       ptr +=2;
924
926
  return my_write(file, buff, (size_t) (ptr-buff), MYF(MY_NABP)) != 0;
925
927
}
926
928
 
927
 
unsigned char *mi_keydef_read(unsigned char *ptr, MI_KEYDEF *keydef)
 
929
uchar *mi_keydef_read(uchar *ptr, MI_KEYDEF *keydef)
928
930
{
929
931
   keydef->keysegs      = (uint) *ptr++;
930
932
   keydef->key_alg      = *ptr++;               /* Rtree or Btree */
946
948
 
947
949
int mi_keyseg_write(File file, const HA_KEYSEG *keyseg)
948
950
{
949
 
  unsigned char buff[HA_KEYSEG_SIZE];
950
 
  unsigned char *ptr=buff;
 
951
  uchar buff[HA_KEYSEG_SIZE];
 
952
  uchar *ptr=buff;
951
953
  ulong pos;
952
954
 
953
955
  *ptr++= keyseg->type;
967
969
}
968
970
 
969
971
 
970
 
unsigned char *mi_keyseg_read(unsigned char *ptr, HA_KEYSEG *keyseg)
 
972
uchar *mi_keyseg_read(uchar *ptr, HA_KEYSEG *keyseg)
971
973
{
972
974
   keyseg->type         = *ptr++;
973
975
   keyseg->language     = *ptr++;
994
996
  mi_uniquedef
995
997
---------------------------------------------------------------------------*/
996
998
 
997
 
uint32_t mi_uniquedef_write(File file, MI_UNIQUEDEF *def)
 
999
uint mi_uniquedef_write(File file, MI_UNIQUEDEF *def)
998
1000
{
999
 
  unsigned char buff[MI_UNIQUEDEF_SIZE];
1000
 
  unsigned char *ptr=buff;
 
1001
  uchar buff[MI_UNIQUEDEF_SIZE];
 
1002
  uchar *ptr=buff;
1001
1003
 
1002
1004
  mi_int2store(ptr,def->keysegs);               ptr+=2;
1003
 
  *ptr++=  (unsigned char) def->key;
1004
 
  *ptr++ = (unsigned char) def->null_are_equal;
 
1005
  *ptr++=  (uchar) def->key;
 
1006
  *ptr++ = (uchar) def->null_are_equal;
1005
1007
 
1006
1008
  return my_write(file, buff, (size_t) (ptr-buff), MYF(MY_NABP)) != 0;
1007
1009
}
1008
1010
 
1009
 
unsigned char *mi_uniquedef_read(unsigned char *ptr, MI_UNIQUEDEF *def)
 
1011
uchar *mi_uniquedef_read(uchar *ptr, MI_UNIQUEDEF *def)
1010
1012
{
1011
1013
   def->keysegs = mi_uint2korr(ptr);
1012
1014
   def->key     = ptr[2];
1018
1020
**  MI_COLUMNDEF
1019
1021
***************************************************************************/
1020
1022
 
1021
 
uint32_t mi_recinfo_write(File file, MI_COLUMNDEF *recinfo)
 
1023
uint mi_recinfo_write(File file, MI_COLUMNDEF *recinfo)
1022
1024
{
1023
 
  unsigned char buff[MI_COLUMNDEF_SIZE];
1024
 
  unsigned char *ptr=buff;
 
1025
  uchar buff[MI_COLUMNDEF_SIZE];
 
1026
  uchar *ptr=buff;
1025
1027
 
1026
1028
  mi_int2store(ptr,recinfo->type);      ptr +=2;
1027
1029
  mi_int2store(ptr,recinfo->length);    ptr +=2;
1030
1032
  return my_write(file, buff, (size_t) (ptr-buff), MYF(MY_NABP)) != 0;
1031
1033
}
1032
1034
 
1033
 
unsigned char *mi_recinfo_read(unsigned char *ptr, MI_COLUMNDEF *recinfo)
 
1035
uchar *mi_recinfo_read(uchar *ptr, MI_COLUMNDEF *recinfo)
1034
1036
{
1035
1037
   recinfo->type=  mi_sint2korr(ptr);   ptr +=2;
1036
1038
   recinfo->length=mi_uint2korr(ptr);   ptr +=2;
1040
1042
}
1041
1043
 
1042
1044
/**************************************************************************
1043
 
Open data file
 
1045
Open data file with or without RAID
1044
1046
We can't use dup() here as the data file descriptors need to have different
1045
1047
active seek-positions.
1046
1048
 
1051
1053
int mi_open_datafile(MI_INFO *info, MYISAM_SHARE *share,
1052
1054
                     File file_to_dup __attribute__((unused)))
1053
1055
{
1054
 
    info->dfile=my_open(share->data_file_name, share->mode,
 
1056
    info->dfile=my_open(share->data_file_name, share->mode | O_SHARE,
1055
1057
                        MYF(MY_WME));
1056
1058
  return info->dfile >= 0 ? 0 : 1;
1057
1059
}
1059
1061
 
1060
1062
int mi_open_keyfile(MYISAM_SHARE *share)
1061
1063
{
1062
 
  if ((share->kfile=my_open(share->unique_file_name, share->mode,
 
1064
  if ((share->kfile=my_open(share->unique_file_name, share->mode | O_SHARE,
1063
1065
                            MYF(MY_WME))) < 0)
1064
1066
    return 1;
1065
1067
  return 0;