]> git.pond.sub.org Git - empserver/blobdiff - include/file.h
Generation numbers to catch write back of stale copies
[empserver] / include / file.h
index 3011a13f87443d42b7f2271b37f37df824135f74..58146d31cdb8e73106fb145a0cb6ae7b7fb82fae 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -26,9 +26,9 @@
  *  ---
  *
  *  file.h: Describes Empire tables (`files' for historical reasons)
- * 
+ *
  *  Known contributors to this file:
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2008
  */
 
 #ifndef FILE_H
@@ -40,26 +40,56 @@ struct empfile {
     /* Members with immutable values */
     int uid;                   /* Table ID */
     char *name;                        /* Empire name (e.g., "treaty") */
-    char *file;                        /* if backed by file, file name relative to
-                                  data directory */
+    char *file;                        /* file name, relative to gamedir for
+                                  game state, to builtindir for config */
     struct castr *cadef;       /* table column selectors (column meta-data) */
     int size;                  /* size of a table entry */
     int flags;                 /* only EFF_IMMUTABLE immutable, see below
                                   for use of remaining bits */
+
     /* Members whose values are fixed when the cache is mapped */
     char *cache;               /* pointer to cache */
     int csize;                 /* cache size, in entries */
-    /* and flags bit EFF_MEM */
+    /* flags bits EFF_MEM, EFF_PRIVATE, EFF_NOTIME also fixed then */
+
     /* Members whose values may vary throughout operation */
     int baseid;                        /* id of first entry in cache */
     int cids;                  /* # entries in cache */
     int fids;                  /* # entries in table */
     int fd;                    /* file descriptor, -1 if not open */
-    /* and flags bit EFF_RDONLY */
-    /* User callbacks */
-    void (*init)(int, void *); /* called after entry creation, unless null */
-    int (*postread)(int, void *); /* called after read, unless null */
-    int (*prewrite)(int, void *); /* called before write, unless null */
+    /* flags bit EFF_CUSTOM also varies */
+
+    /* User callbacks, may all be null */
+    /*
+     * Called after element initialization.  ELT is the element.
+     */
+    void (*oninit)(void *elt);
+    /*
+     * Called after read.  ID is the element id, and ELT is the
+     * element read.  May modify the element.  Modifications are
+     * visible to caller of ef_read(), but have no effect on the file.
+     */
+    void (*postread)(int id, void *elt);
+    /*
+     * Called before write.  ID is the element id, OLD is the element
+     * being updated (null unless it is cached) and ELT is the element
+     * being written.  May modify the element.  Modifications will be
+     * visible to caller of ef_write() and are written to the file.
+     */
+    void (*prewrite)(int id, void *old, void *elt);
+    /*
+     * Called after table size changed, with file type as argument.
+     * Return -1 and set errno to make the operation fail.
+     */
+    int (*onresize)(int type);
+};
+
+struct emptypedstr {
+    signed ef_type: 8;
+    unsigned seqno: 12;
+    unsigned generation: 12;
+    int uid;
+    time_t timestamp;
 };
 
 /*
@@ -68,33 +98,41 @@ struct empfile {
  * The remaining flags record how the table is being used.
  */
 /* Immutable flags, fixed at compile-time */
+/* Dereferencing entry address cast to struct emptypedstr * is safe */
+#define EFF_TYPED      bit(0)
 /*
  * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner /
  * group of such a table's entries can be safely obtained by
- * dereferencing entry address cast to struct empobj *.
+ * dereferencing the entry's address cast to struct empobj *.
  */
-#define EFF_XY         bit(0)
-#define EFF_OWNER      bit(1)
-#define EFF_GROUP      bit(2)
+#define EFF_XY         bit(1)
+#define EFF_OWNER      bit(2)
+#define EFF_GROUP      bit(3)
 /* Table is allocated statically */
-#define EFF_STATIC     bit(3)
+#define EFF_STATIC     bit(4)
+/* Table has a sentinel (all zero, not counted as elt), implies EFF_MEM */
+#define EFF_SENTINEL   bit(5)
 /* All the immutable flags */
-#define EFF_IMMUTABLE  (EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
+#define EFF_IMMUTABLE \
+    (EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
 /* Flags set when table contents is mapped */
 /* Table is entirely in memory */
 #define EFF_MEM                bit(8)
-/* Table is read-only */
-#define EFF_RDONLY     bit(9)
+/* Table is privately mapped: changes don't affect the underlying file */
+#define EFF_PRIVATE    bit(9)
 /* Table is customized (configuration tables only) */
 #define EFF_CUSTOM     bit(10)
+/* Don't update timestamps */
+#define EFF_NOTIME     bit(11)
 /* Transient flags, only occur in argument of ef_open() */
 /* Create table file, clobbering any existing file */
-#define EFF_CREATE     bit(11)
+#define EFF_CREATE     bit(16)
 
 /*
  * Empire `file types'
  * These are really table IDs.  Some tables are backed by files, some
- * are compiled into the server.
+ * are compiled into the server, some initialized from configuration
+ * files.
  */
 enum {
     /* Error value */
@@ -129,8 +167,10 @@ enum {
     EF_NUKE_CHR,
     EF_NEWS_CHR,
     EF_INFRASTRUCTURE,
+    EF_UPDATES,                        /* not actually static */
     EF_TABLE,
-    EF_META,
+    EF_VERSION,
+    EF_META,                   /* not really configuration */
     /* Symbol tables */
     EF_AGREEMENT_STATUS,
     EF_LAND_CHR_FLAGS,
@@ -164,24 +204,30 @@ enum {
 
 extern struct castr *ef_cadef(int);
 extern int ef_read(int, int, void *);
+extern void ef_make_stale(void);
+extern void ef_mark_fresh(int, void *);
 extern void *ef_ptr(int, int);
 extern char *ef_nameof(int);
 extern time_t ef_mtime(int);
-extern int ef_open(int, int);
-extern int ef_check(int);
+extern int ef_open(int, int, int);
+extern int ef_open_view(int, int);
 extern int ef_close(int);
 extern int ef_flush(int);
+extern void ef_blank(int, int, void *);
 extern int ef_write(int, int, void *);
+extern void ef_set_uid(int, void *, int);
 extern int ef_extend(int, int);
 extern int ef_ensure_space(int, int, int);
+extern int ef_truncate(int, int);
 extern int ef_nelem(int);
 extern int ef_flags(int);
 extern int ef_byname(char *);
 extern int ef_byname_from(char *, int *);
-extern void ef_init(void);
 extern int ef_verify(void);
 extern int ef_elt_byname(int, char *);
 
 extern struct empfile empfile[EF_MAX + 1];
+extern void empfile_init(void);
+extern void empfile_fixup(void);
 
 #endif