Merge branch 'master' of git.irl.cs.ucla.edu:ndn/chronoshare
diff --git a/src/action-item.proto b/src/action-item.proto
index 3218ef8..c7eca9c 100644
--- a/src/action-item.proto
+++ b/src/action-item.proto
@@ -5,18 +5,19 @@
     UPDATE = 0;
     DELETE = 1;
   }
-  required ActionType action = 3;
+  required ActionType action = 0;
 
-  required string filename = 4;
-  required uint64 version = 5;
-  required uint32 timestamp = 6;
+  required string filename = 1;
+  required uint64 version = 2;
+  required uint32 timestamp = 3;
+  optional uint64 seg_num = 4;
+  optional bytes  file_hash = 5;
 
-  optional bytes  file_hash = 7;
-  // optional uint32 atime = 8;
-  optional uint32 mtime = 9;
-  // optional uint32 ctime = 10;
-  optional uint32 mode  = 11;
+  optional uint32 mtime = 6;
+  optional uint32 mode  = 7;
+  optional uint32 atime = 8;
+  optional uint32 ctime = 9;
 
-  optional bytes  parent_device_name = 12;
-  optional uint64 parent_seq_no = 13;
+  // optional bytes  parent_device_name = 12;
+  // optional uint64 parent_seq_no = 13;
 }
diff --git a/src/db-helper.cc b/src/db-helper.cc
index 0e08612..4313dfd 100644
--- a/src/db-helper.cc
+++ b/src/db-helper.cc
@@ -103,6 +103,7 @@
     file_mtime  TIMESTAMP,                                              \n\
     file_ctime  TIMESTAMP,                                              \n\
     file_chmod  INTEGER,                                                \n\
+    file_seg_num INTEGER, /* NULL if action is \"delete\" */            \n\
                                                                         \n\
     parent_device_id INTEGER,                                           \n\
     parent_seq_no    INTEGER,                                           \n\
diff --git a/src/sync-core.cc b/src/sync-core.cc
index c64ce90..9e5d0f4 100644
--- a/src/sync-core.cc
+++ b/src/sync-core.cc
@@ -331,7 +331,8 @@
   // find the actuall difference and invoke callback on the actual difference
   HashPtr oldHash = m_rootHash;
   m_rootHash = m_log->RememberStateInStateLog();
-  SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash);
+  // get diff with both new SeqNo and old SeqNo
+  SyncStateMsgPtr diff = m_log->FindStateDifferences(*oldHash, *m_rootHash, true);
 
   if (diff->state_size() > 0)
   {
diff --git a/src/sync-log.cc b/src/sync-log.cc
index 1e17957..eb421a9 100644
--- a/src/sync-log.cc
+++ b/src/sync-log.cc
@@ -304,13 +304,13 @@
 }
 
 SyncStateMsgPtr
-SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
+SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq)
 {
-  return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
+  return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash), includeOldSeq);
 }
 
 SyncStateMsgPtr
-SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
+SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq)
 {
   sqlite3_stmt *stmt;
 
@@ -370,6 +370,7 @@
     {
       SyncState *state = msg->add_state ();
 
+      // set name
       state->set_name (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
 
       // locator is optional, so must check if it is null
@@ -378,6 +379,23 @@
         state->set_locator (reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 1)), sqlite3_column_bytes (stmt, 1));
       }
 
+      // set old seq
+      if (includeOldSeq)
+      {
+        if (sqlite3_column_type (stmt, 2) == SQLITE_NULL)
+        {
+          // old seq is zero; we always have an initial action of zero seq
+          // other's do not need to fetch this action
+          state->set_old_seq(0);
+        }
+        else
+        {
+          sqlite3_int64 oldSeqNo = sqlite3_column_int64 (stmt, 2);
+          state->set_old_seq(oldSeqNo);
+        }
+      }
+
+      // set new seq
       if (sqlite3_column_type (stmt, 3) == SQLITE_NULL)
         {
         state->set_type (SyncState::DELETE);
diff --git a/src/sync-log.h b/src/sync-log.h
index aed3bce..549d759 100644
--- a/src/sync-log.h
+++ b/src/sync-log.h
@@ -69,10 +69,10 @@
 
   // How difference is exposed will be determined later by the actual protocol
   SyncStateMsgPtr
-  FindStateDifferences (const std::string &oldHash, const std::string &newHash);
+  FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq = false);
 
   SyncStateMsgPtr
-  FindStateDifferences (const Hash &oldHash, const Hash &newHash);  
+  FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq = false);  
 
   //-------- only used in test -----------------
   sqlite3_int64
diff --git a/src/sync-state.proto b/src/sync-state.proto
index 26095e1..410726b 100644
--- a/src/sync-state.proto
+++ b/src/sync-state.proto
@@ -11,6 +11,7 @@
 
   optional uint64 seq = 3;
   optional bytes locator = 4;
+  optional uint64 old_seq = 5;
 }
 
 message SyncStateMsg
diff --git a/test/test-sync-core.cc b/test/test-sync-core.cc
index 1fbe4e6..354e2b4 100644
--- a/test/test-sync-core.cc
+++ b/test/test-sync-core.cc
@@ -13,6 +13,19 @@
 void callback(const SyncStateMsgPtr &msg)
 {
   BOOST_CHECK(msg->state_size() > 0);
+  int size = msg->state_size();
+  int index = 0;
+  while (index < size)
+  {
+    SyncState state = msg->state(index);
+    BOOST_CHECK(state.has_old_seq());
+    BOOST_CHECK(state.old_seq() >= 0);
+    if (state.seq() != 0)
+    {
+      BOOST_CHECK(state.old_seq() != state.seq()); 
+    }
+    index++;
+  }
 }
 
 void checkRoots(const HashPtr &root1, const HashPtr &root2)