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

version 1.9, 2025/04/08 16:46:11 version 1.10, 2025/04/08 20:00:56
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.10  2025/04/08 20:00:56  snw
    *   Global handler now uses a header file and maintains the last journaling transaction ID
    *
  *   Revision 1.9  2025/04/08 16:46:11  snw   *   Revision 1.9  2025/04/08 16:46:11  snw
  *   Add global file header and offsets   *   Add global file header and offsets
  *   *
Line 56 Line 59
 #include <errno.h>  #include <errno.h>
   
 #include "mpsdef.h"  #include "mpsdef.h"
   #include "journal.h"
 #include "global_bltin.h"  #include "global_bltin.h"
   
 global_handle *global_handles_head;  global_handle *global_handles_head;
Line 244  void gbl_close_all(void) Line 248  void gbl_close_all(void)
     }      }
 } /* gbl_close_all() */  } /* gbl_close_all() */
   
 int gbl_write_header(global_handle *g)  int gbl_write_initial_header(global_handle *g)
 {  {
     global_header hdr;      global_header hdr;
     unsigned long old_position;      unsigned long old_position;
Line 268  int gbl_write_header(global_handle *g) Line 272  int gbl_write_header(global_handle *g)
     lseek (g->fd, 0, SEEK_SET);      lseek (g->fd, 0, SEEK_SET);
   
     if (write (g->fd, &hdr, sizeof (global_header)) == -1) {      if (write (g->fd, &hdr, sizeof (global_header)) == -1) {
         snprintf (msg, 256, "error %d writing global header for %s", errno, g->global_name);          snprintf (msg, sizeof (msg), "error %d writing global header for %s", errno, g->global_name);
         m_fatal (msg);          m_fatal (msg);
     }      }
           
Line 276  int gbl_write_header(global_handle *g) Line 280  int gbl_write_header(global_handle *g)
     gbl_unlock (g);      gbl_unlock (g);
   
     return TRUE;      return TRUE;
   } /* gbl_write_initial_header() */
   
   
   int gbl_write_header(global_handle *g, global_header *hdr)
   {
       unsigned long old_position;
       char msg[256];
       
       if (g->opened == FALSE) {
           return FALSE;
       }
   
       gbl_lock (g, 1);
       old_position = lseek (g->fd, 0, SEEK_CUR);
       lseek (g->fd, 0, SEEK_SET);
   
       if (write (g->fd, hdr, sizeof (global_header)) == -1) {
           snprintf (msg, sizeof (msg), "error %d writing global header for %s", errno, g->global_name);
           m_fatal (msg);
       }
   
       lseek (g->fd, old_position, SEEK_SET);
       gbl_unlock (g);
       
       return TRUE;    
 } /* gbl_write_header() */  } /* gbl_write_header() */
   
   int gbl_read_header(global_handle *g, global_header *h)
   {
       unsigned long old_position;
       char m[5] = GBL_MAGIC;
   
       
       if (g->opened == FALSE) {
           return GBL_HDR_NOTOPEN;
       }
   
       gbl_lock (g, 1);
       old_position = lseek (g->fd, 0, SEEK_CUR);
       lseek (g->fd, 0, SEEK_SET);
   
       read (g->fd, h, sizeof (global_header));
   
       lseek (g->fd, old_position, SEEK_SET);
       gbl_unlock (g);
   
       if (strncmp (h->magic, m, 5) != 0) {
           return GBL_HDR_BADMAGIC;
       }
       if (h->format_version != GBL_FORMAT_VERSION) {
           return GBL_HDR_BADVERSION;
       }
       if (h->block_size != BLOCKLEN) {
           return GBL_HDR_BADBLOCKSIZE;
       }
   
       return GBL_HDR_OK;                          
   } /* gbl_read_header() */
   
   int gbl_update_tid(global_handle *g)
   {
       global_header h;
   
       if (gbl_read_header (g, &h) != GBL_HDR_OK) {
           return FALSE;
       }
   
       h.last_transaction_id = jnl_tran_id;
   
       return gbl_write_header (g, &h);        
   } /* gbl_update_tid() */
   
 int gbl_create(global_handle *g)  int gbl_create(global_handle *g)
 {  {
     while (1) {      while (1) {
Line 298  int gbl_create(global_handle *g) Line 372  int gbl_create(global_handle *g)
     g->last_block = 0;      g->last_block = 0;
     g->use_count = 1;      g->use_count = 1;
   
     gbl_write_header (g);      gbl_write_initial_header (g);
           
     return OK;      return OK;
 } /* gbl_create() */  } /* gbl_create() */
   
 short gbl_open(global_handle *g, short action)  short gbl_open(global_handle *g, short action)
 {  {
       int result;
       global_header h;
       
     if (g->opened == FALSE) {      if (g->opened == FALSE) {
         while (1) {          while (1) {
             errno = 0;              errno = 0;
Line 318  short gbl_open(global_handle *g, short a Line 395  short gbl_open(global_handle *g, short a
                                           
                 case EMFILE:                  case EMFILE:
                 case ENFILE:                  case ENFILE:
                     close_all_globals ();                      gbl_close_all ();
                     continue;                      continue;
             }              }
                           
Line 334  short gbl_open(global_handle *g, short a Line 411  short gbl_open(global_handle *g, short a
         }          }
         else {          else {
             g->opened = TRUE;              g->opened = TRUE;
               result = gbl_read_header (g, &h);
   
               if (result == GBL_HDR_OK) {
                   g->opened = TRUE;
               }
               else {
                   gbl_close (g);                
                   return FALSE;
               }
         }          }
     }      }
   
Line 1360  s20: Line 1446  s20:
             stcpy0 (&block[++addr], data, (long) datal);              stcpy0 (&block[++addr], data, (long) datal);
             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);
   
               gbl_update_tid (g);
               
             break;              break;
   
   
Line 3584  short g_numeric (char *str) Line 3673  short g_numeric (char *str)
   
 }                                       /* end g_numeric() */  }                                       /* end g_numeric() */
   
   
   /* DEPRECATED: use gbl_close_all() instead */
 void close_all_globals (void)  void close_all_globals (void)
 {                                         {                                       
     register int i;      gbl_close_all ();
       
     for (i = 0; i < NO_GLOBLS; i++) {  
           
         if (oldfil[i][0] != NUL) {  
               
             close (olddes[i]);  
               
             usage[i] = 0;  
             olddes[i] = 0;  
             oldfil[i][0] = NUL;  
   
         }  
   
     }  
   
     return;      return;
   
 }                                       /* end close_all_globals() */  }                                       /* end close_all_globals() */
   
 static void panic (void)  static void panic (void)

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


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