|
From: <il...@pr...> - 2005-01-29 04:38:06
|
Update of /cvsroot/meshdb/src/mailt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23692 Modified Files: index.c index.h mail.inc mkill.c Log Message: Fixed machine portability of index file. Types are now explicitly sized and always treated as being big endian. Writing on a big endian machine and reading (using mail.inc) on a little endian machine works; the other way around hasn't been tested. Index: index.c =================================================================== RCS file: /cvsroot/meshdb/src/mailt/index.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- index.c 24 Nov 2003 05:57:18 -0000 1.3 +++ index.c 29 Jan 2005 04:37:45 -0000 1.4 @@ -46,6 +46,7 @@ { struct mail *m; struct mail0 *m0; + uint32_t version; m = mmap(NULL, msgs * sizeof (struct mail), readonly ? PROT_READ : PROT_READ|PROT_WRITE, @@ -55,9 +56,10 @@ return NULL; } m0 = (struct mail0 *)m; - if (msgs != 1 && m0->version != MAIL0_VERSION) { - fprintf(stderr, "index: bad version number %d, expected %d\n", - (int)m0->version, MAIL0_VERSION); + version = betoh32(m0->version); + if (msgs != 1 && version != MAIL0_VERSION) { + fprintf(stderr, "index: bad version number %ld, expected %d\n", + (long)version, MAIL0_VERSION); munmap(m, msgs * sizeof (struct mail)); return NULL; } @@ -83,8 +85,9 @@ struct mail *mail; { struct mail0 *m0 = (struct mail0 *)mail; + uint32_t msgs = betoh32(m0->msgs); - return index_unloadn(mail, m0->msgs); + return index_unloadn(mail, msgs); } /* Map all mail entries into memory */ @@ -94,13 +97,13 @@ int readonly; { struct mail0 *m0; - int msgs; + uint32_t msgs; /* Map just the first record to get the number of messages */ m0 = (struct mail0 *)index_loadn(fd, 1, readonly); if (m0 == NULL) return NULL; - msgs = m0->msgs; + msgs = betoh32(m0->msgs); /* Now, map the lot */ index_unloadn((struct mail *)m0, 1); return index_loadn(fd, msgs, readonly); @@ -123,13 +126,13 @@ return -1; /* Initialise the mail0 record */ m0 = (struct mail0 *)m; - m0->version = MAIL0_VERSION; - m0->size = sizeof (struct mail); - m0->end = 0; - m0->date = 0; - m0->msgs = 1; - m0->child = 0; - m0->flags = 0; + m0->version = betoh32(MAIL0_VERSION); + m0->size = betoh32(sizeof (struct mail)); + m0->end = betoh64(0); + m0->date = betoh32(0); + m0->msgs = betoh32(1); + m0->child = betoh32(0); + m0->flags = betoh32(0); /* Unload the index */ index_unloadn(m, 1); return 0; @@ -142,8 +145,9 @@ int fd; { void *base; + int64_t end = betoh64(m0->end); - base = mmap(NULL, m0->end, PROT_READ, MAP_PRIVATE, fd, 0); + base = mmap(NULL, end, PROT_READ, MAP_PRIVATE, fd, 0); if (base == MAP_FAILED) { perror("mmap mbox"); return NULL; @@ -157,6 +161,7 @@ struct mail0 *m0; cstring_t base; { - if (munmap((void *)base, m0->end)) + int64_t end = betoh64(m0->end); + if (munmap((void *)base, end)) perror("munmap mbox"); } Index: index.h =================================================================== RCS file: /cvsroot/meshdb/src/mailt/index.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- index.h 10 Aug 2002 03:45:14 -0000 1.1.1.1 +++ index.h 29 Jan 2005 04:37:45 -0000 1.2 @@ -5,35 +5,40 @@ * The first record (mail0) is treated specially. */ -typedef unsigned long msgid_t; +#include <sys/types.h> +#include <inttypes.h> + +#include "../geo/compat/compat.h" + +typedef uint32_t msgid_t; typedef const char *cstring_t; typedef char *string_t; struct mail { - size_t hdrlen; /* incl. last blank line */ - size_t bodylen; - off_t pos; /* start of headers in file */ - time_t date; + uint32_t hdrlen; /* incl. last blank line */ + uint32_t bodylen; + int64_t pos; /* start of headers in file */ + uint32_t date; char from[40]; /* Sender's name */ char subject[60]; /* Subject text */ msgid_t parent, child, sibling; /* Thread tree */ - int flags; + uint32_t flags; #define FLAGS_KILLED 0x0001 /* Message has been killed */ }; /* First entry */ struct mail0 { - size_t size; /* size of each entry */ - size_t version; /* version check */ + uint32_t size; /* size of each entry */ + uint32_t version; /* version check */ #define MAIL0_VERSION 1 - off_t end; /* total size of mbox */ - time_t date; /* index update time */ + int64_t end; /* total size of mbox */ + uint32_t date; /* index update time */ char ign4[40]; char ign5[60]; msgid_t msgs; /* number of messages */ msgid_t child; /* first unparented child */ msgid_t ign8; - int flags; + uint32_t flags; }; int index_create(int fd); Index: mail.inc =================================================================== RCS file: /cvsroot/meshdb/src/mailt/mail.inc,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- mail.inc 10 Aug 2002 03:45:14 -0000 1.1.1.1 +++ mail.inc 29 Jan 2005 04:37:45 -0000 1.2 @@ -3,15 +3,20 @@ /* Utility functions to access mail indices */ - $FMT = "Lhdrlen/Lbodylen/Lpos/Ldate/a40from" - ."/a60subject/Lparent/Lchild/Lsibling/Lflags"; - $FMT0 = "Lsize/Lversion/Lend/Ldate/a40ign4" - ."/a60ign5/Lmsgs/Lchild/Lign8/Lflags"; - $FMTSZ = 132; + $FMT = "Nhdrlen/Nbodylen/Nposhi/Nposlo/Ndate/a40from" + ."/a60subject/Nparent/Nchild/Nsibling/Nflags"; + $FMT0 = "Nsize/Nversion/Nendhi/Nendlo/Ndate/a40ign4" + ."/a60ign5/Nmsgs/Nchild/Nign8/Nflags"; + $FMTSZ = 136; $KILLED = 1; - /* off_t is a 64-bit value. (php probably doesnt handle it) */ - function quad($lo, $hi) { + /* Construct a 64 bit value from two 32 bit values. This + * should work on systems where PHP's int is 64 bits, and + * won't cause any problems on 32 bit machines as long as the + * high word is 0. PHP doesn't have an integral type that we + * can safely hold 64 bits in, let alone have unpack() support + * it. */ + function quad($hi, $lo) { return ($hi << 32) | $lo; } @@ -27,7 +32,7 @@ || $m0['version'] != 1) { die("$index: bad format"); } - /* $m0["end"] = quad($m0["endlo"], $m0["endhi"]); */ + $m0["end"] = quad($m0["endhi"], $m0["endlo"]); fseek($idxf, 0); $idx = fread($idxf, $FMTSZ * $m0["msgs"]); fclose($idxf); @@ -50,7 +55,7 @@ if ($i < 1 || $i >= $o["m0"]["msgs"]) die("bad message ID ".$i); $m = unpack($FMT, substr($o["idx"], $FMTSZ * $i, $FMTSZ)); - /* $m["pos"] = quad($m["poslo"], $m["poshi"]); */ + $m["pos"] = quad($m["poshi"], $m["poslo"]); $m["o"] = $o; return $m; } Index: mkill.c =================================================================== RCS file: /cvsroot/meshdb/src/mailt/mkill.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- mkill.c 24 Nov 2003 06:23:01 -0000 1.2 +++ mkill.c 29 Jan 2005 04:37:45 -0000 1.3 @@ -22,6 +22,7 @@ struct open o; struct mail0 *m0; struct mail *m; + uint32_t msgs; msgid_t i, kid, *sp, lasti; cstring_t mbox, mid, midend; int error = 0; @@ -68,6 +69,7 @@ if (mid == NULL) { /* List killed messages */ + msgs = betoh32(m0->msgs); for (i = 1; i < m0->msgs; i++) if (m[i].flags & FLAGS_KILLED) { if (nflag) |