--- freem/src/global_bltin.c 2025/04/08 16:46:11 1.9
+++ freem/src/global_bltin.c 2025/04/08 20:00:56 1.10
@@ -1,5 +1,5 @@
/*
- * $Id: global_bltin.c,v 1.9 2025/04/08 16:46:11 snw Exp $
+ * $Id: global_bltin.c,v 1.10 2025/04/08 20:00:56 snw Exp $
* freem database engine
*
*
@@ -24,6 +24,9 @@
* along with FreeM. If not, see .
*
* $Log: global_bltin.c,v $
+ * 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
* Add global file header and offsets
*
@@ -56,6 +59,7 @@
#include
#include "mpsdef.h"
+#include "journal.h"
#include "global_bltin.h"
global_handle *global_handles_head;
@@ -244,7 +248,7 @@ void gbl_close_all(void)
}
} /* gbl_close_all() */
-int gbl_write_header(global_handle *g)
+int gbl_write_initial_header(global_handle *g)
{
global_header hdr;
unsigned long old_position;
@@ -268,7 +272,7 @@ int gbl_write_header(global_handle *g)
lseek (g->fd, 0, SEEK_SET);
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);
}
@@ -276,8 +280,78 @@ int gbl_write_header(global_handle *g)
gbl_unlock (g);
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() */
+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)
{
while (1) {
@@ -298,13 +372,16 @@ int gbl_create(global_handle *g)
g->last_block = 0;
g->use_count = 1;
- gbl_write_header (g);
+ gbl_write_initial_header (g);
return OK;
} /* gbl_create() */
short gbl_open(global_handle *g, short action)
{
+ int result;
+ global_header h;
+
if (g->opened == FALSE) {
while (1) {
errno = 0;
@@ -318,7 +395,7 @@ short gbl_open(global_handle *g, short a
case EMFILE:
case ENFILE:
- close_all_globals ();
+ gbl_close_all ();
continue;
}
@@ -334,6 +411,15 @@ short gbl_open(global_handle *g, short a
}
else {
g->opened = TRUE;
+ result = gbl_read_header (g, &h);
+
+ if (result == GBL_HDR_OK) {
+ g->opened = TRUE;
+ }
+ else {
+ gbl_close (g);
+ return FALSE;
+ }
}
}
@@ -1360,6 +1446,9 @@ s20:
stcpy0 (&block[++addr], data, (long) datal);
lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
write (g->fd, block, BLOCKLEN);
+
+ gbl_update_tid (g);
+
break;
@@ -3584,26 +3673,13 @@ short g_numeric (char *str)
} /* end g_numeric() */
+
+/* DEPRECATED: use gbl_close_all() instead */
void close_all_globals (void)
{
- register int i;
-
- 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;
-
- }
-
- }
-
+ gbl_close_all ();
+
return;
-
} /* end close_all_globals() */
static void panic (void)