src/strip.vala
[app.mailtrimmer] / src / strip.vala
index 3a19ff5..340d852 100644 (file)
@@ -1,5 +1,13 @@
 /**
 
+ ** check left to do:  
+  - range scans on maildir
+  - see how replacing the links works in the resulting email via thunderbird etc..
+  - some checksum issues (see dupelicates?? suspect 0byte issues?)  -- seems ok now?
+
+
+
   needs to scan 2 things
   a) our mailfort email database
        point it at the top directory, containing YEAR/MONTH/DAY.... directories.
@@ -362,8 +370,25 @@ public class Strip : GLib.Object {
        
        // initialize it with known data..
        // that should wipe out dupes.
-       var filesize = this.query("SELECT filesize FROM Attachment WHERE id = %d".printf(
-                       int.parse(sid)));       
+       var matches = this.execute("SELECT count(id) as nid FROM Attachment WHERE id = %d".printf(
+                       int.parse(sid)));  
+                GLib.error("Got Matches :%s", matches);
+                
+               if (matches=="0") {      
+                  GLib.error("Failed to find id  :%s", sid);
+                  return;
+               }
+       
+       
+       // initialize it with known data..
+       // that should wipe out dupes.
+       var filesize = this.execute("SELECT filesize FROM Attachment WHERE id = %d".printf(
+                       int.parse(sid)));  
+
+               if (filesize=="") {      
+                  GLib.debug("Ignoring record id (missing in database) :%s", sid);
+                  return;
+               }
                if (int.parse(filesize) < 1) {
                GLib.debug("Could not get filesize from id :%s = %s", sid,filesize);
                Posix.exit(0);
@@ -549,7 +574,10 @@ public class Strip : GLib.Object {
                        }
                        GLib.debug("Creating file %s", target_fn);
                        if (!FileUtils.test (target_fn, FileTest.EXISTS)) {
-                               Posix.link(fn, target_fn);
+                               var from = File.new_for_path (fn);
+                               var to =  File.new_for_path (target_fn);
+                               from.copy(to, 0, null);
+
                        }
                } else { 
                        GLib.debug("Skipping extraction %s", target_fn);
@@ -578,7 +606,7 @@ public class Strip : GLib.Object {
                
                
        
-       var rc=  this.mysql.query(str);         
+       var rc=  this.mysql.query(str); 
        if ( rc != 0 ) {
 
                    GLib.debug("ERROR %u: Query failed: %s\n", this.mysql.errno(), this.mysql.error());
@@ -588,6 +616,8 @@ public class Strip : GLib.Object {
 
         var rs = mysql.use_result();
         
+        GLib.debug("got %d rows", (int)rs.num_rows());
+        
         var got_row = false;
                string[] row;
                string ret = "";
@@ -597,7 +627,8 @@ public class Strip : GLib.Object {
                
                }
                if (!need_return) {
-                       return "";
+                       GLib.debug("got %s", got_row ? "=Nothing=" : ret);
+                       return got_row ? "" : ret;
                }
                if (!got_row) {
                         GLib.debug("ERROR : no rows returned");
@@ -752,8 +783,8 @@ public class Strip : GLib.Object {
                
                var oldest = new  DateTime.now_local();
                oldest = oldest.add_months(-1 * StripApplication.opt_age_oldest);
-               var rtspan = oldest.difference(oldest) / GLib.TimeSpan.DAY;
-
+               var rtspan = mailtime.difference(oldest) / GLib.TimeSpan.DAY;
+               GLib.debug("Checking oldest %d days difference", (int)rtspan   );
                if (rtspan < 0) {
                        GLib.debug("skip(2) file is %d days older than %d months", (int)rtspan, StripApplication.opt_age_oldest);
                        return;
@@ -834,6 +865,7 @@ public class Strip : GLib.Object {
         GLib.debug("finished writing output %d", file_size);
 
         //
+        outstream = null;
         
          
        this.used_space_after += file_size;
@@ -842,12 +874,17 @@ public class Strip : GLib.Object {
        if (StripApplication.opt_is_replacing) {
                Posix.unlink(path +"/" + name);         
                GLib.debug("copy tmp file %s to %s" , tmpfile, path +"/" + name);               
-               Posix.link(tmpfile, path +"/" + name);
-               Posix.unlink(tmpfile);
-               var nf = File.new_for_path(path +"/" + name);
+               
+               // link will not work, as we are doing it accross file systems
+                       var from = File.new_for_path (tmpfile);
+                       var nf =  File.new_for_path (path +"/" + name);
+                       from.copy(nf, 0, null);
+                       
+
                var newfileinfo = nf.query_info(GLib.FileAttribute.TIME_MODIFIED,GLib.FileQueryInfoFlags.NONE,null);
                newfileinfo.set_modification_time(mod_time);
                nf.set_attributes_from_info(newfileinfo,FileQueryInfoFlags.NONE);
+               Posix.unlink(tmpfile);
                }
        this.processed++;
        
@@ -875,7 +912,7 @@ public class Strip : GLib.Object {
                var oldest = new  DateTime.now_local();
                        oldest = oldest.add_months(-1 * StripApplication.opt_age_oldest);
                        
-                       GLib.debug("Checking directory %s is older than min: %d/%d/%d", subpath, oldest.get_year() , oldest.get_month(), oldest.get_day_of_month() );                                   
+                       //GLib.debug("Checking directory %s is older than min: %d/%d/%d", subpath, oldest.get_year() , oldest.get_month(), oldest.get_day_of_month() );                                 
                        
                        if (year < oldest.get_year()) {
                                GLib.debug("Skip directory %s is older than min year: %d", subpath, oldest.get_year());
@@ -893,7 +930,7 @@ public class Strip : GLib.Object {
                var newest = new  DateTime.now_local();
                        newest = newest.add_months(-1 * StripApplication.opt_age_newest);
                        
-                       GLib.debug("Checking directory %s is newer than max: %d/%d/%d", subpath, newest.get_year() , newest.get_month(), newest.get_day_of_month() );                                   
+                       //GLib.debug("Checking directory %s is newer than max: %d/%d/%d", subpath, newest.get_year() , newest.get_month(), newest.get_day_of_month() );                                 
                        
                        if (year > newest.get_year()) {
                                GLib.debug("Skip directory %s is newer than max year: %d", subpath, newest.get_year());
@@ -947,17 +984,21 @@ public class Strip : GLib.Object {
                 continue;
             }
             
-
+            var ds = next_file.get_display_name();
             if (next_file.get_file_type() != FileType.DIRECTORY) {
                 
-                if (next_file.get_display_name()[0] == ',') {
+                
+                
+                if (ds[0] == ',') {
                        continue;
                }
                // other files to ignore?
-                
-                this.scan_file(basepath + subpath , next_file.get_display_name());
+                if (Regex.match_simple (".tgz$", ds)) {
+                       continue;
+               }
+                this.scan_file(basepath + subpath , ds);
                                if(this.has_replaced) {
-                                this.report_state("After scanning %s/%s".printf(basepath + subpath , next_file.get_display_name()));
+                                this.report_state("After scanning %s/%s".printf(basepath + subpath , ds));
                        }
                 continue;
             }
@@ -968,7 +1009,7 @@ public class Strip : GLib.Object {
 
 
         
-            var ds = next_file.get_display_name();
+
             // not really needed?? - we are storing attachments in a seperate location now...
             if (ds[0] == '.') {
                 next_file = null;