]> git.cworth.org Git - apitrace/blobdiff - trace_file.cpp
Merge branch 'master' into compression
[apitrace] / trace_file.cpp
index 10e6565925449f3b820088587cdad1453139dea5..74cc3829e2b4ce5a18a3d69beb614e90b87ed6de 100644 (file)
@@ -6,10 +6,59 @@
 #include <zlib.h>
 #include <snappy.h>
 
+#include "os.hpp"
+
 #include <iostream>
+#include <set>
 
 using namespace Trace;
 
+#define SNAPPY_BYTE1 'a'
+#define SNAPPY_BYTE2 't'
+
+static void cleanupHandler(int sig);
+
+class FileCleanup
+{
+public:
+    FileCleanup()
+    {
+        OS::CatchInterrupts(cleanupHandler);
+    }
+
+    ~FileCleanup()
+    {
+        flush();
+        m_files.clear();
+    }
+
+    void addFile(Trace::File *file)
+    {
+        m_files.insert(file);
+    }
+    void removeFile(Trace::File *file)
+    {
+        m_files.erase(file);
+    }
+
+    void flush()
+    {
+        std::set<Trace::File*>::const_iterator itr;
+        for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
+            (*itr)->flush(File::FlushDeep);
+        }
+    }
+
+private:
+    std::set<Trace::File*> m_files;
+};
+static FileCleanup s_cleaner;
+
+static void cleanupHandler(int sig)
+{
+    s_cleaner.flush();
+}
+
 File::File(const std::string &filename,
            File::Mode mode)
     : m_filename(filename),
@@ -49,6 +98,10 @@ bool File::open(const std::string &filename, File::Mode mode)
     }
     m_isOpened = rawOpen(filename, mode);
     m_mode = mode;
+
+    if (m_isOpened) {
+        s_cleaner.addFile(this);
+    }
     return m_isOpened;
 }
 
@@ -73,12 +126,13 @@ void File::close()
     if (m_isOpened) {
         rawClose();
         m_isOpened = false;
+        s_cleaner.removeFile(this);
     }
 }
 
-void File::flush()
+void File::flush(FlushType type)
 {
-    rawFlush();
+    rawFlush(type);
 }
 
 int File::getc()
@@ -104,6 +158,23 @@ bool File::isZLibCompressed(const std::string &filename)
     return (byte1 == 0x1f && byte2 == 0x8b);
 }
 
+
+bool File::isSnappyCompressed(const std::string &filename)
+{
+    std::fstream stream(filename.c_str(),
+                        std::fstream::binary | std::fstream::in);
+    if (!stream.is_open())
+        return false;
+
+    unsigned char byte1, byte2;
+    stream >> byte1;
+    stream >> byte2;
+    stream.close();
+
+    return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
+}
+
+
 ZLibFile::ZLibFile(const std::string &filename,
                    File::Mode mode)
     : File(filename, mode),
@@ -145,7 +216,7 @@ void ZLibFile::rawClose()
     }
 }
 
-void ZLibFile::rawFlush()
+void ZLibFile::rawFlush(FlushType type)
 {
     gzflush(m_gzFile, Z_SYNC_FLUSH);
 }
@@ -179,7 +250,17 @@ bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
 
     //read in the initial buffer if we're reading
     if (m_stream.is_open() && mode == File::Read) {
+        // read the snappy file identifier
+        unsigned char byte1, byte2;
+        m_stream >> byte1;
+        m_stream >> byte2;
+        assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
+
         flushCache();
+    } else if (m_stream.is_open() && mode == File::Write) {
+        // write the snappy file identifier
+        m_stream << SNAPPY_BYTE1;
+        m_stream << SNAPPY_BYTE2;
     }
     return m_stream.is_open();
 }
@@ -237,6 +318,8 @@ bool SnappyFile::rawRead(void *buffer, int length)
             sizeToRead -= chunkSize;
             if (sizeToRead > 0)
                 flushCache();
+            if (!m_cacheSize)
+                break;
         }
     }
 
@@ -260,8 +343,11 @@ void SnappyFile::rawClose()
     m_cachePtr = NULL;
 }
 
-void SnappyFile::rawFlush()
+void SnappyFile::rawFlush(FlushType type)
 {
+    if (type == FlushDeep) {
+        flushCache();
+    }
     m_stream.flush();
 }