]> granicus.if.org Git - file/commitdiff
PR/351: Fix compilation of empty files.
authorChristos Zoulas <christos@zoulas.com>
Sun, 4 May 2014 18:57:34 +0000 (18:57 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sun, 4 May 2014 18:57:34 +0000 (18:57 +0000)
ChangeLog
src/apprentice.c

index 17567b3cfa738a714fce76fa25f17d8d92e44b9f..6873a5fca139eda809f52d8150eed49876e60efb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2014-05-04  14:55  Christos Zoulas <christos@zoulas.com>
+
+       * PR/351: Fix compilation of empty files 
+
 2014-04-30  17:39  Christos Zoulas <christos@zoulas.com>
 
        * Fix integer formats: We don't specify 'l' or
index d570663fca20c44f02b441e3c4f8d30df8d532fa..90fd91e718b4a545c7d8d5c32bdd849ae52e5d43 100644 (file)
@@ -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) {