From: Christos Zoulas Date: Sun, 4 May 2014 18:57:34 +0000 (+0000) Subject: PR/351: Fix compilation of empty files. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a33f92c042221cbbbffc0896dbe531e1a7983450;p=file PR/351: Fix compilation of empty files. --- diff --git a/ChangeLog b/ChangeLog index 17567b3c..6873a5fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2014-05-04 14:55 Christos Zoulas + + * PR/351: Fix compilation of empty files + 2014-04-30 17:39 Christos Zoulas * Fix integer formats: We don't specify 'l' or diff --git a/src/apprentice.c b/src/apprentice.c index d570663f..90fd91e7 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.206 2014/04/30 21:41:02 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.207 2014/05/04 18:57:35 christos Exp $") #endif /* lint */ #include "magic.h" @@ -2793,10 +2793,6 @@ error: return NULL; } -private const uint32_t ar[] = { - MAGICNO, VERSIONNO -}; - /* * handle an mmaped file. */ @@ -2810,6 +2806,10 @@ apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn) char *dbname; int rv = -1; uint32_t i; + union { + struct magic m; + uint32_t h[2 + MAGIC_SETS]; + } hdr; dbname = mkdbname(ms, fn, 1); @@ -2821,24 +2821,16 @@ apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn) file_error(ms, errno, "cannot open `%s'", dbname); goto out; } + memset(&hdr, 0, sizeof(hdr)); + hdr.h[0] = MAGICNO; + hdr.h[1] = VERSIONNO; + memcpy(hdr.h + 2, map->nmagic, nm); - if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) { - file_error(ms, errno, "error writing `%s'", dbname); - goto out; - } - - if (write(fd, map->nmagic, nm) != (ssize_t)nm) { + if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) { file_error(ms, errno, "error writing `%s'", dbname); goto out; } - assert(nm + sizeof(ar) < m); - - if (lseek(fd, (off_t)m, SEEK_SET) != (off_t)m) { - file_error(ms, errno, "error seeking `%s'", dbname); - goto out; - } - for (i = 0; i < MAGIC_SETS; i++) { len = m * map->nmagic[i]; if (write(fd, map->magic[i], len) != (ssize_t)len) {