Write the depslog version in binary instead of text.
authorNico Weber <thakis@chromium.org>
Tue, 9 Apr 2013 00:18:30 +0000 (17:18 -0700)
committerEvan Martin <martine@danga.com>
Tue, 9 Apr 2013 04:13:20 +0000 (21:13 -0700)
This way, it doubles as a byte-order marker. The header is now exactly one
line in a hex editor, and it's still relatively easy to look at the version
in a text editor.

src/deps_log.cc

index 5590a32..8946e32 100644 (file)
 #include "state.h"
 #include "util.h"
 
-namespace {
-const char kFileSignature[] = "# ninja deps v%d\n";
+// The version is stored as 4 bytes after the signature and also serves as a
+// byte order mark. Signature and version combined are 16 bytes long.
+const char kFileSignature[] = "# ninjadeps\n";
 const int kCurrentVersion = 1;
-}  // anonymous namespace
 
 DepsLog::~DepsLog() {
   Close();
@@ -47,7 +47,11 @@ bool DepsLog::OpenForWrite(const string& path, string* err) {
   fseek(file_, 0, SEEK_END);
 
   if (ftell(file_) == 0) {
-    if (fprintf(file_, kFileSignature, kCurrentVersion) < 0) {
+    if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, file_) < 1) {
+      *err = strerror(errno);
+      return false;
+    }
+    if (fwrite(&kCurrentVersion, 4, 1, file_) < 1) {
       *err = strerror(errno);
       return false;
     }
@@ -137,7 +141,10 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
     return false;
   }
   int version = 0;
-  sscanf(buf, kFileSignature, &version);
+  if (fread(&version, 4, 1, f) < 1) {
+    *err = strerror(errno);
+    return false;
+  }
   if (version != kCurrentVersion) {
     *err = "bad deps log signature or version; starting over";
     fclose(f);