Better determined what should be backed up
[backups/.git] / main.cc
diff --git a/main.cc b/main.cc
index e2864fda9603de454d0eb41d1257dc2b9a55c9e3..07868bcd65199d14e2a43da88dbf3740018bb672 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -62,7 +62,7 @@ void populate_set( istream &in, SET &files ) {
 
 template<class SET>
 void partition_sets( const SET &current, const SET &old,
-                     SET &added, SET &common, SET &deleted  ) {
+                     SET &added, SET &common, SET &old_common, SET &deleted  ) {
   FileDataNameCmp cmp;
 
   set_difference( current.begin(), current.end(),
@@ -79,6 +79,11 @@ void partition_sets( const SET &current, const SET &old,
                   old.begin(),     old.end(),
                   inserter( common, common.begin() ),
                   cmp );
+
+  set_union(      old.begin(),    old.end(),
+                  common.begin(), common.end(),
+                  inserter( old_common, old_common.begin() ),
+                  cmp );
 }
 
 int main() {
@@ -93,23 +98,8 @@ int main() {
   }
 
   // Now divide the two sets into three sets (added, deleted and common )
-  file_set added, deleted, common;
-  partition_sets( current, backed_up, added, common, deleted );
-
-  { // This little block will copy the last_backup_date from the second set to the first
-    FileDataNameCmp cmp;
-
-    file_set common_with_dates;
-    set_union( backed_up.begin(), backed_up.end(),
-               current.begin(),   current.end(),
-               inserter( common_with_dates, common_with_dates.begin() ),
-               cmp );
-
-    file_set::iterator i = common.begin(), j = common_with_dates.begin();
-    for( ; i != common.end(); ++i, ++j ) {
-      (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
-    }
-  }
+  file_set added, deleted, common, old_common;
+  partition_sets( current, backed_up, added, common, old_common, deleted );
 
   // Now find the list of files to backup.
   file_set backups;
@@ -117,14 +107,15 @@ int main() {
   // backup all added files
   copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
 
-  // backup common files that have changed since the last backup date.
-  for( file_set::iterator i = common.begin(); i != common.end(); ++i ) {
-    if( (*i)->getLastBackupDate() < (*i)->getModifiedDate() ) {
-      backups.insert( *i );
-    }
+  // Backup files that have been modified
+  file_set::iterator i = common.begin(), j = old_common.begin();
+  for( ; i != common.end(); ++i, ++j ) {
+    (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
+
+    if( needs_backup( *j, *i ) ) backups.insert( *i );
   }
 
-  // Now, sort the backups by filesize and build a list of up to SIZE
+  // Now, sort the backups by filesize and build a list that'll fit on a DVD
   file_vector backups_s;
   copy( backups.begin(), backups.end(), back_inserter( backups_s ) );