[25104] users/jberry/mpwa/app/models/port_pkg_file.rb

source_changes at macosforge.org source_changes at macosforge.org
Sun May 13 20:14:32 PDT 2007


Revision: 25104
          http://trac.macosforge.org/projects/macports/changeset/25104
Author:   jberry at macports.org
Date:     2007-05-13 20:14:32 -0700 (Sun, 13 May 2007)

Log Message:
-----------
Bit of optimization to reading of file blobs. Verify the file digest as we read the file to ensure it's still valid

Modified Paths:
--------------
    users/jberry/mpwa/app/models/port_pkg_file.rb

Modified: users/jberry/mpwa/app/models/port_pkg_file.rb
===================================================================
--- users/jberry/mpwa/app/models/port_pkg_file.rb	2007-05-14 01:28:18 UTC (rev 25103)
+++ users/jberry/mpwa/app/models/port_pkg_file.rb	2007-05-14 03:14:32 UTC (rev 25104)
@@ -3,6 +3,9 @@
 require 'file_blob'
 require 'mpwa-conf'
 
+class PortPkgFileException < RuntimeError
+end
+
 class PortPkgFile < ActiveRecord::Base
     belongs_to :port_pkg
     before_destroy { |f| FileBlob.delete_all "port_pkg_file_id = #{f.id}" }
@@ -50,8 +53,8 @@
             
             # Create a new bob
             blob = FileBlob.create(:port_pkg_file => self, :data => buf, :sequence => seq)
+
             length = length + buf.length
-            
             seq = seq + 1
         end
         
@@ -65,18 +68,33 @@
     end
     
     def write_to_file(file)
+        # Create a digester so that we can verify the digest
+        sha256 = Digest::SHA256.new
+
+        # Page in the blobs, writing to file as we go
+        length = 0
         seq = 0
-        while (blob = FileBlob.find(:first, :conditions => "port_pkg_file_id=#{self.id} and sequence=#{seq}"))
-            file.write(blob.data)
+        while (length < self.length)
+            blob = FileBlob.find(:first, :conditions => "port_pkg_file_id=#{self.id} and sequence=#{seq}")
+            raise PortPkgFileException, "file blob missing segment" if !blob
+            
+            buf = blob.data
+            sha256.update buf
+            
+            file.write(buf)
+            length = length + buf.length
+            
             seq = seq + 1
         end
+        
+        # Verify the digest
+        raise PortPkgFileException, "digest mismatch while reading pkg_file #{self.id}" if sha256.hexdigest != self.sha256
     end
     
     def data()
         StringIO.open("rw") do |f|
             write_to_file(f)
-            f.rewind
-            f.read
+            f.string
         end
     end
     

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20070513/3238576d/attachment.html


More information about the macports-changes mailing list