[Title] add cache remove method for client
authorjiil.hyoun <jiil.hyoun@samsung.com>
Tue, 12 Mar 2013 06:24:18 +0000 (15:24 +0900)
committerjiil.hyoun <jiil.hyoun@samsung.com>
Tue, 12 Mar 2013 06:24:18 +0000 (15:24 +0900)
[Type] Enhancement
[Module] Toolchain /
[Priority] Minor
[Jira#]
[Redmine#] 7956
[Problem]
[Cause]
[Solution]
[TestCase]

Change-Id: Idd93dfa19b3a1d0cb6becc6570295d5e35baccf7

src/common/utils.rb
src/pkg_server/client.rb

index 3785ad946e536c2789113e744b2f41498780360e..d549c8367227a553c5c0ecdf0b7c184f0436d245 100644 (file)
@@ -26,6 +26,7 @@ Contributors:
 - S-Core Co., Ltd
 =end
 require 'rbconfig'
+require 'find'
 
 class Utils
        STARTUP_INFO_SIZE = 68
@@ -624,6 +625,21 @@ class Utils
        end
 
 
+       def Utils.get_sub_files_info(path_list) # return [PATH,size,time] list
+               list = []
+               path_list.each do |path|
+                       Find.find(path) do |dep_path|
+                               if (File.exist?(dep_path) and not File.directory? dep_path)
+                                       fsize = File.size(dep_path)
+                                       ftime = File.new(dep_path).ctime
+                                       list.push [dep_path,fsize,ftime]
+                               end
+                       end
+               end
+               return list.sort {|a,b| a[2] <=> b[2]}
+       end
+
+
        def Utils.get_directory_size(path)
 
                os_category = get_os_category(HOST_OS)
index 005cbced56fed996fdb7b8904b386c4a96c6f9fe..366b228e40e8e9236e28fcce048595aa79607ceb 100644 (file)
@@ -130,6 +130,32 @@ class Client
                if not File.exist? @location then FileUtils.mkdir_p "#{@location}" end
                @log.info "Update local package list.. [#{@location}]"
                read_installed_pkg_list()
+
+               #cache_size = 4G
+               @cache_size = 4000 #(MB)
+       end
+
+
+       # make cache clean
+       def clean_cache
+               #get dir info
+               cache_list = Dir.entries(CONFIG_PATH).select{|dir| not dir.start_with? "." and File.directory? CONFIG_PATH + "/#{dir}/downloads" }.map{|dir| CONFIG_PATH + "/#{dir}/downloads"}
+               cache_info_list = Utils.get_sub_files_info(cache_list).reverse
+               cache_size = 0
+               cache_info_list.each {|file| cache_size += file[1]}
+
+               extra_size = cache_size - (@cache_size * 1024 * 1024)
+
+               if extra_size < 0 then return end
+
+               remove_list = []
+               while extra_size > 0 do
+                       old_package = cache_info_list.pop
+                       remove_list.push old_package[0]
+                       extra_size -= old_package[1]
+               end
+
+               FileUtils.rm_rf remove_list
        end
 
        public
@@ -277,6 +303,7 @@ class Client
                        @log.info "Removed old package files.."
                        @log.info "  * #{pkg_files[1..-1].join(", ")}"
                end
+               clean_cache
        end
 
        private