[Title] Validate package after downloading
authordonghyuk.yang <donghyuk.yang@samsung.com>
Thu, 14 Mar 2013 15:18:33 +0000 (00:18 +0900)
committerdonghyuk.yang <donghyuk.yang@samsung.com>
Thu, 14 Mar 2013 15:18:33 +0000 (00:18 +0900)
src/pkg_server/client.rb

index db0ac3e9504f77775e2de91cf9f3165909ba1649..e59564bf3468f9eaa906b211414a85d29e9630a3 100644 (file)
@@ -325,7 +325,12 @@ class Client
                                end
 
                                file_path = File.join(loc, filename)
-                               add_file_to_cache(file_path)
+                               if validatePkgFile(file_path, pkg_checksum, pkg_size) then
+                                       add_file_to_cache(file_path)
+                               else
+                                       @log.error "File Validation Failed!!"
+                                       return nil                                              
+                               end                                             
                        else
                                @log.info "Cached #{pkg_name} package file.. OK"
                        end
@@ -340,6 +345,17 @@ class Client
                return file_local_path
        end
 
+       private
+       def validatePkgFile(filepath, orig_checksum, orig_size)
+               checksum = Digest::SHA256.file(filepath).hexdigest
+               size = File.size filepath
+               if checksum.to_s.eql? orig_checksum and size.to_s.eql? orig_size then
+                       return true
+               else
+                       return false                            
+               end                             
+       end     
+
        private
        def get_file_from_cache(filename, pkg_checksum, pkg_size, location)
                downloaded_file_path = nil
@@ -397,14 +413,13 @@ class Client
 
        private
        def get_cached_filepath(pkg_filename, pkg_checksum, pkg_size)
-
                cached_filepath = "#{@download_path}/#{pkg_filename}"
                if File.exist? cached_filepath then
-                       FileUtils.touch cached_filepath
-                       checksum = Digest::SHA256.file(cached_filepath).hexdigest
-                       size = File.size cached_filepath
-                       if checksum.to_s.eql? pkg_checksum and size.to_s.eql? pkg_size then
+                       if validatePkgFile(cached_filepath, pkg_checksum, pkg_size) then
+                               FileUtils.touch cached_filepath                                 
                                return cached_filepath
+                       else
+                               return nil
                        end
                end
                return nil