Diff for /freem/src/global_bltin.c between versions 1.10 and 1.12

version 1.10, 2025/04/08 20:00:56 version 1.12, 2025/04/09 00:43:07
Line 24 Line 24
  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.   *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
  *   *
  *   $Log$   *   $Log$
    *   Revision 1.12  2025/04/09 00:43:07  snw
    *   Exit with fatal error if a header mismatch found
    *
    *   Revision 1.11  2025/04/08 21:41:13  snw
    *   Make insert, update, and splitp global handler functions take a ptr to a global_handle instead of a file descriptor
    *
  *   Revision 1.10  2025/04/08 20:00:56  snw   *   Revision 1.10  2025/04/08 20:00:56  snw
  *   Global handler now uses a header file and maintains the last journaling transaction ID   *   Global handler now uses a header file and maintains the last journaling transaction ID
  *   *
Line 65 Line 71
 global_handle *global_handles_head;  global_handle *global_handles_head;
   
 static void b_free (short filedes, unsigned long blknbr);  static void b_free (short filedes, unsigned long blknbr);
 static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr);  static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr);
 static void update (short filedes, char *ins_key, long keyl);  static void update (global_handle *g, char *ins_key, long keyl);
 static void insert (int filedes, char *ins_key, long key_len, unsigned long blknbr);  static void insert (global_handle *g, char *ins_key, long key_len, unsigned long blknbr);
 static void scanpblk (char *block, long *adr, long *fnd);  static void scanpblk (char *block, long *adr, long *fnd);
 static void scandblk (char *block, long *adr, long *fnd);  static void scandblk (char *block, long *adr, long *fnd);
 static void getnewblk (int filedes, unsigned long *blknbr);  static void getnewblk (int filedes, unsigned long *blknbr);
Line 291  int gbl_write_header(global_handle *g, g Line 297  int gbl_write_header(global_handle *g, g
     if (g->opened == FALSE) {      if (g->opened == FALSE) {
         return FALSE;          return FALSE;
     }      }
       
     gbl_lock (g, 1);      gbl_lock (g, 1);
     old_position = lseek (g->fd, 0, SEEK_CUR);      old_position = lseek (g->fd, 0, SEEK_CUR);
     lseek (g->fd, 0, SEEK_SET);      lseek (g->fd, 0, SEEK_SET);
Line 303  int gbl_write_header(global_handle *g, g Line 309  int gbl_write_header(global_handle *g, g
   
     lseek (g->fd, old_position, SEEK_SET);      lseek (g->fd, old_position, SEEK_SET);
     gbl_unlock (g);      gbl_unlock (g);
   
       gbl_read_header (g, &g->header);
           
     return TRUE;          return TRUE;    
 } /* gbl_write_header() */  } /* gbl_write_header() */
Line 380  int gbl_create(global_handle *g) Line 388  int gbl_create(global_handle *g)
 short gbl_open(global_handle *g, short action)  short gbl_open(global_handle *g, short action)
 {  {
     int result;      int result;
     global_header h;  
           
     if (g->opened == FALSE) {      if (g->opened == FALSE) {
         while (1) {          while (1) {
Line 411  short gbl_open(global_handle *g, short a Line 418  short gbl_open(global_handle *g, short a
         }          }
         else {          else {
             g->opened = TRUE;              g->opened = TRUE;
             result = gbl_read_header (g, &h);              result = gbl_read_header (g, &g->header);
               
             if (result == GBL_HDR_OK) {              if (result == GBL_HDR_OK) {
                 g->opened = TRUE;                  g->opened = TRUE;
             }              }
             else {              else {                
                 gbl_close (g);                                  gbl_close (g);
                   set_io (UNIX);
                   
                   switch (result) {
                       
                       case GBL_HDR_BADMAGIC:
                           fprintf (stderr, "gbl_open:  bad magic value in %s [FATAL]\n", g->global_name);
                           exit (1);
                           break;
   
                       case GBL_HDR_BADVERSION:
                           fprintf (stderr, "gbl_open:  global version is %d in %s (must be %d) [FATAL]\n", g->header.format_version, g->global_name, GBL_FORMAT_VERSION);
                           exit (1);
                           break;
   
                   }
                           
                 return FALSE;                  return FALSE;
             }              }
         }          }
Line 1360  s10:            { Line 1383  s10:            {
                 lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                  lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
                 write (g->fd, block, BLOCKLEN);                  write (g->fd, block, BLOCKLEN);
   
                 if (traceadr[trx] == 0) update (g->fd, compactkey, keyl);                  if (traceadr[trx] == 0) update (g, compactkey, keyl);
   
                 break;                  break;
             }              }
Line 2171  p_empty:  /* entry if pointer block goes Line 2194  p_empty:  /* entry if pointer block goes
                         if (addr == 0) {        /* update of pointer */                          if (addr == 0) {        /* update of pointer */
                             traceadr[trx] = 0;                              traceadr[trx] = 0;
                                                           
                             update (g->fd, &block[2], (long) UNSIGN (block[0]));                              update (g, &block[2], (long) UNSIGN (block[0]));
                         }                          }
   
                         trx = trxsav;                          trx = trxsav;
Line 2217  p_empty:  /* entry if pointer block goes Line 2240  p_empty:  /* entry if pointer block goes
                 write (g->fd, block, BLOCKLEN);                  write (g->fd, block, BLOCKLEN);
                 if (addr < 3) {         /* update of pointer */                  if (addr < 3) {         /* update of pointer */
                 traceadr[trx] = 0;                  traceadr[trx] = 0;
                 update (g->fd, &block[2], (long) UNSIGN (block[0]));                  update (g, &block[2], (long) UNSIGN (block[0]));
                 }                  }
             }              }
   
Line 2580  splitd:    /* split data block in two se Line 2603  splitd:    /* split data block in two se
         addr = 0;          addr = 0;
         blknbr = newblk;          blknbr = newblk;
                   
         insert (g->fd, compactkey, keyl, newblk);          insert (g, compactkey, keyl, newblk);
                   
         /* up-date LL-PTR of RL-block */          /* up-date LL-PTR of RL-block */
         if ((other = right * 65536 + right1 * 256 + right2)) {          if ((other = right * 65536 + right1 * 256 + right2)) {
Line 2626  splitd:    /* split data block in two se Line 2649  splitd:    /* split data block in two se
         blknbr = newblk;          blknbr = newblk;
         traceadr[trx] = (-1);           /* inhibit second update of pointers */          traceadr[trx] = (-1);           /* inhibit second update of pointers */
                   
         insert (g->fd, compactkey, keyl, newblk);          insert (g, compactkey, keyl, newblk);
                   
         if (addr < 3) {                 /* update of pointer */          if (addr < 3) {                 /* update of pointer */
             traceadr[trx] = 0;              traceadr[trx] = 0;
                           
             update (g->fd, compactkey, keyl);              update (g, compactkey, keyl);
         }          }
   
         /* other is ***always*** zero !!!          /* other is ***always*** zero !!!
Line 2745  splitd:    /* split data block in two se Line 2768  splitd:    /* split data block in two se
             offset = limit;              offset = limit;
             /* insert new block in pointer structure */              /* insert new block in pointer structure */
                   
             insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk);              insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
                   
             /* up-date LL-PTR of RL-block */              /* up-date LL-PTR of RL-block */
             if (other != 0) {              if (other != 0) {
Line 2787  splitd:    /* split data block in two se Line 2810  splitd:    /* split data block in two se
             traceblk[trx] = (blknbr = newblk);              traceblk[trx] = (blknbr = newblk);
                           
             /* insert new block in pointer structure */              /* insert new block in pointer structure */
             insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk);              insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
                           
             /* up-date LL-PTR of RL-block */              /* up-date LL-PTR of RL-block */
             if (other != 0) {              if (other != 0) {
Line 2827  spltex: Line 2850  spltex:
  *   'addr' there I would like to insert, if possible (which is not)   *   'addr' there I would like to insert, if possible (which is not)
  *   'offset' filled up to this limit   *   'offset' filled up to this limit
  */   */
 static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr)    static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr)       
 {  {
   
     char block0[BLOCKLEN];      char block0[BLOCKLEN];
Line 2840  static void splitp (short filedes, char Line 2863  static void splitp (short filedes, char
   
     hdr_offset = sizeof (global_header);      hdr_offset = sizeof (global_header);
           
     getnewblk (filedes, &newblk);       /* get a new block */      getnewblk (g->fd, &newblk); /* get a new block */
           
     if (*blknbr == ROOT) {              /* ROOT overflow is special */      if (*blknbr == ROOT) {              /* ROOT overflow is special */
   
Line 2875  static void splitp (short filedes, char Line 2898  static void splitp (short filedes, char
         block0[NRBLK + 2] = i % 256;          block0[NRBLK + 2] = i % 256;
         block0[BTYP] = POINTER;          block0[BTYP] = POINTER;
                   
         lseek (filedes, hdr_offset + ROOT, SEEK_SET);          lseek (g->fd, hdr_offset + ROOT, SEEK_SET);
         write (filedes, block0, BLOCKLEN);          write (g->fd, block0, BLOCKLEN);
                   
         /* shift trace_stack */          /* shift trace_stack */
         j = trx++;          j = trx++;
Line 2893  static void splitp (short filedes, char Line 2916  static void splitp (short filedes, char
         traceblk[1] = newblk;          traceblk[1] = newblk;
         *blknbr = newblk;          *blknbr = newblk;
                   
         getnewblk (filedes, &newblk);   /* get a new block */          getnewblk (g->fd, &newblk);     /* get a new block */
   
     }      }
   
Line 2947  static void splitp (short filedes, char Line 2970  static void splitp (short filedes, char
         block0[LLPTR + 1] = (*blknbr) % 65536 / 256;          block0[LLPTR + 1] = (*blknbr) % 65536 / 256;
         block0[LLPTR + 2] = (*blknbr) % 256;          block0[LLPTR + 2] = (*blknbr) % 256;
   
         lseek (filedes, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET);          lseek (g->fd, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET);
         write (filedes, block0, BLOCKLEN);          write (g->fd, block0, BLOCKLEN);
   
         (*offs) = limit;          (*offs) = limit;
                   
         insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk);          insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
                   
         /* up-date LL-PTR of RL-block */          /* up-date LL-PTR of RL-block */
         if (other != 0) {          if (other != 0) {
   
             lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);              lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
             read (filedes, block0, BLOCKLEN);              read (g->fd, block0, BLOCKLEN);
                           
             block0[LLPTR] = newblk / 65536;              block0[LLPTR] = newblk / 65536;
             block0[LLPTR + 1] = newblk % 65536 / 256;              block0[LLPTR + 1] = newblk % 65536 / 256;
             block0[LLPTR + 2] = newblk % 256;              block0[LLPTR + 2] = newblk % 256;
                           
             lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);              lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
             write (filedes, block0, BLOCKLEN);              write (g->fd, block0, BLOCKLEN);
   
         }          }
   
Line 2990  static void splitp (short filedes, char Line 3013  static void splitp (short filedes, char
         (*addr) -= limit;          (*addr) -= limit;
         (*offs) -= limit;          (*offs) -= limit;
                   
         lseek (filedes, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET);          lseek (g->fd, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET);
         write (filedes, block, BLOCKLEN);          write (g->fd, block, BLOCKLEN);
         stcpy0 (block, block0, (long) BLOCKLEN);          stcpy0 (block, block0, (long) BLOCKLEN);
                   
         (*blknbr) = newblk;          (*blknbr) = newblk;
                   
         insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk);          insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
                   
         /* up-date LL-PTR of RL-block */          /* up-date LL-PTR of RL-block */
         if (other != 0) {          if (other != 0) {
   
             lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);              lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
             read (filedes, block0, BLOCKLEN);              read (g->fd, block0, BLOCKLEN);
                           
             block0[LLPTR] = newblk / 65536;              block0[LLPTR] = newblk / 65536;
             block0[LLPTR + 1] = newblk % 65536 / 256;              block0[LLPTR + 1] = newblk % 65536 / 256;
             block0[LLPTR + 2] = newblk % 256;              block0[LLPTR + 2] = newblk % 256;
                           
             lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);              lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
             write (filedes, block0, BLOCKLEN);              write (g->fd, block0, BLOCKLEN);
   
         }          }
   
Line 3024  static void splitp (short filedes, char Line 3047  static void splitp (short filedes, char
  *  ins_key:    key to be inserted   *  ins_key:    key to be inserted
  *  keyl:       length of that key   *  keyl:       length of that key
  */   */
 static void update (short filedes, char *ins_key, long keyl)  static void update (global_handle *g, char *ins_key, long keyl)
 {  {
     long offset;      long offset;
     long addr;      long addr;
Line 3042  static void update (short filedes, char Line 3065  static void update (short filedes, char
         blknbr = traceblk[trx];          blknbr = traceblk[trx];
         addr = traceadr[trx];          addr = traceadr[trx];
                   
         lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);          lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
         read (filedes, block, BLOCKLEN);          read (g->fd, block, BLOCKLEN);
                   
         {          {
             long    oldkeyl;              long    oldkeyl;
Line 3070  static void update (short filedes, char Line 3093  static void update (short filedes, char
             else if (j < 0) {           /* we need more space */              else if (j < 0) {           /* we need more space */
                           
                 /* block too small */                  /* block too small */
                 if ((offset - j) > DATALIM) splitp (filedes, block, &addr, &offset, &blknbr);                  if ((offset - j) > DATALIM) splitp (g, block, &addr, &offset, &blknbr);
                                   
                 i = offset;                  i = offset;
                 offset -= j;                  offset -= j;
Line 3093  static void update (short filedes, char Line 3116  static void update (short filedes, char
             while (i < keyl) block[j++] = ins_key[i++];              while (i < keyl) block[j++] = ins_key[i++];
                           
             /* block pointed to remains the same */              /* block pointed to remains the same */
             lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);              lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
             write (filedes, block, BLOCKLEN);              write (g->fd, block, BLOCKLEN);
   
         }          }
                   
         lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);          lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
         read (filedes, block, BLOCKLEN);          read (g->fd, block, BLOCKLEN);
   
     }      }
   
Line 3114  static void update (short filedes, char Line 3137  static void update (short filedes, char
  *  key_len:    length of that key   *  key_len:    length of that key
  *  blknbr:     key points to this block   *  blknbr:     key points to this block
  */   */
 static void insert (int filedes, char *ins_key, long key_len, unsigned long blknbr)     /* insert pointer */  static void insert (global_handle *g, char *ins_key, long key_len, unsigned long blknbr)        /* insert pointer */
 {  {
     unsigned long blk;      unsigned long blk;
     char block[BLOCKLEN];      char block[BLOCKLEN];
Line 3131  static void insert (int filedes, char *i Line 3154  static void insert (int filedes, char *i
     blk = traceblk[trx];      blk = traceblk[trx];
     addr = traceadr[trx];      addr = traceadr[trx];
   
     lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);      lseek (g->fd, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
     read (filedes, block, BLOCKLEN);      read (g->fd, block, BLOCKLEN);
           
     offset = UNSIGN (block[OFFS]) * 256 +      offset = UNSIGN (block[OFFS]) * 256 +
     UNSIGN (block[OFFS + 1]);      UNSIGN (block[OFFS + 1]);
Line 3144  static void insert (int filedes, char *i Line 3167  static void insert (int filedes, char *i
   
     needed = key_len + 2 + PLEN;      needed = key_len + 2 + PLEN;
           
     if ((offset + needed) > DATALIM) splitp (filedes, block, &addr, &offset, &blk);      if ((offset + needed) > DATALIM) splitp (g, block, &addr, &offset, &blk);
           
     /*  insert key */      /*  insert key */
     i = (offset += needed);      i = (offset += needed);
Line 3168  static void insert (int filedes, char *i Line 3191  static void insert (int filedes, char *i
     block[i++] = blknbr % 65536 / 256;      block[i++] = blknbr % 65536 / 256;
     block[i] = blknbr % 256;      block[i] = blknbr % 256;
   
     lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);      lseek (g->fd, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
     write (filedes, block, BLOCKLEN);      write (g->fd, block, BLOCKLEN);
           
     trx = trxsav;      trx = trxsav;
           

Removed from v.1.10  
changed lines
  Added in v.1.12


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>