From: donghyuk.yang Date: Thu, 5 Sep 2013 01:02:18 +0000 (+0900) Subject: [Title] Do not execute "ln" if target and linkpath are different device. execute... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=55c7efee0f147c3f8f294a7ea5d2151e9d40ee23;p=sdk%2Ftools%2Fsdk-build.git [Title] Do not execute "ln" if target and linkpath are different device. execute "cp" instead of it. --- diff --git a/src/build_server/BuildJob.rb b/src/build_server/BuildJob.rb index 3682166..e6c4341 100644 --- a/src/build_server/BuildJob.rb +++ b/src/build_server/BuildJob.rb @@ -41,6 +41,7 @@ require "utils.rb" require "ReverseBuildChecker.rb" require "CommonJob.rb" require "BuildServerException.rb" +require "FileUtil" class BuildJob < CommonJob @@ -706,7 +707,10 @@ class BuildJob < CommonJob base_package_name= File.basename(local_path, "#{cos}#{ext}") @log.info( "Copying compatible package:#{local_path}", Log::LV_USER) @log.info( "Creating package file ... #{base_package_name}#{@os}#{ext}", Log::LV_USER) - FileUtils.ln local_path, "#{@source_path}/#{base_package_name}#{@os}#{ext}", :force => true + #FileUtils.ln local_path, "#{@source_path}/#{base_package_name}#{@os}#{ext}", :force => true + if not FileUtil.safeLink(local_path, "#{@source_path}/#{base_package_name}#{@os}#{ext}") then + @log.error("Failed to link file [#{local_path}] to [#{@source_path}/#{base_package_name}#{@os}#{ext}]") + end else @log.info( "Downloading compatible package:#{pkg_name}(#{cos})", Log::LV_USER) loc = @pkgsvr_client.download(pkg_name, cos, false) @@ -790,7 +794,10 @@ class BuildJob < CommonJob binpkg_path_list.each do |file| @result_files.push File.basename(file) @log.info( " * #{file}", Log::LV_USER) - FileUtils.ln(file,"#{dst_path}/", :force => true) + #FileUtils.ln(file,"#{dst_path}/", :force => true) + if not FileUtil.safeLink(file,"#{dst_path}/") then + @log.error("Failed to link file [#{file}] to [#{dst_path}]") + end end end diff --git a/src/build_server/MultiBuildJob.rb b/src/build_server/MultiBuildJob.rb index 44a8bba..d5f244a 100644 --- a/src/build_server/MultiBuildJob.rb +++ b/src/build_server/MultiBuildJob.rb @@ -39,6 +39,7 @@ require "RemoteBuilder.rb" require "JobLog.rb" require "mail.rb" require "CommonJob.rb" +require "FileUtil" class MultiBuildJob < CommonJob @@ -501,7 +502,10 @@ class MultiBuildJob < CommonJob binpkg_path_list.each do |file| @result_files.push File.basename(file) @log.info( " * #{file}", Log::LV_USER) - FileUtils.ln(file,"#{dst_path}/", :force => true) + #FileUtils.ln(file,"#{dst_path}/", :force => true) + if not FileUtil.safeLink(file,"#{dst_path}/") then + @log.error("Failed to link file [#{file}] to [#{dst_path}]") + end end end diff --git a/src/build_server/RegisterPackageJob.rb b/src/build_server/RegisterPackageJob.rb index e0b0a41..b32be61 100644 --- a/src/build_server/RegisterPackageJob.rb +++ b/src/build_server/RegisterPackageJob.rb @@ -40,6 +40,7 @@ require "ReverseBuildChecker.rb" require "CommonJob.rb" require "JobLog.rb" require "BuildServerException.rb" +require "FileUtil" class RegisterPackageJob < CommonJob @@ -172,7 +173,10 @@ class RegisterPackageJob < CommonJob FileUtils.mv(@local_path, @file_path) FileUtils.rm_rf("#{File.dirname(@local_path)}") else - FileUtils.ln(@local_path, @file_path, :force => true) + #FileUtils.ln(@local_path, @file_path, :force => true) + if not FileUtil.safeLink(@local_path, @file_path) then + @log.error("Failed to link file [#{@local_path}] to [#{@file_path}]") + end end # check if the os is supported by build server @@ -418,7 +422,10 @@ class RegisterPackageJob < CommonJob # make new package file for compatible OS newfile = "#{@pkg_name}_#{@pkg_version}_#{os}.zip" @log.info( "Copying #{@filename} to #{newfile}" ) - FileUtils.ln(@file_path,"#{@source_path}/#{newfile}",:force => true) + #FileUtils.ln(@file_path,"#{@source_path}/#{newfile}",:force => true) + if not FileUtil.safeLink(@file_path,"#{@source_path}/#{newfile}") then + @log.error("Failed to link file [#{@file_path}] to [#{@source_path}/#{newfile}]") + end # reverse check if not @no_reverse then diff --git a/src/builder/Builder.rb b/src/builder/Builder.rb index f4048a2..df987a8 100644 --- a/src/builder/Builder.rb +++ b/src/builder/Builder.rb @@ -33,6 +33,7 @@ require "client" require "utils" require "PackageManifest" require "log" +require "FileUtil" class Builder private_class_method :new @@ -592,7 +593,11 @@ VERSION=\"#{version}\" " return true end - FileUtils.ln(src, tar,:force => true) + #FileUtils.ln(src, tar,:force => true) + if not FileUtil.safeLink(src, tar) then + @log.error("Failed to link file #{src} to #{tar}") + return false + end end return true @@ -651,7 +656,11 @@ VERSION=\"#{version}\" " return true end - FileUtils.ln(src, tar,:force => true) + #FileUtils.ln(src, tar,:force => true) + if not FileUtil.safeLink(src, tar) then + @log.error("Failed to link file #{src} to #{tar}") + return false + end end return true end diff --git a/src/common/BuildComm.rb b/src/common/BuildComm.rb index fab444f..b495e10 100644 --- a/src/common/BuildComm.rb +++ b/src/common/BuildComm.rb @@ -27,6 +27,7 @@ Contributors: =end $LOAD_PATH.unshift File.dirname(__FILE__) +$LOAD_PATH.unshift File.dirname(File.dirname(__FILE__))+"/common" require "log" require "socket" require 'timeout' @@ -34,6 +35,7 @@ require "net/ftp" require 'thread' require "FileTransferViaFTP" require "FileTransferViaDirect" +require "FileUtil" ATTEMPTS = ["first", "second", "third"] @@ -295,9 +297,12 @@ class BuildCommServer # if hit , touch and copy FileUtils.touch cache_file - FileUtils.ln(cache_file, dst_file, :force => true) - - found = true + #FileUtils.ln(cache_file, dst_file, :force => true) + if (FileUtil.safeLink(cache_file, dst_file)) then + found = true + else + @log.error "Failed to link file [#{cache_file}] to [#{dst_file}]" + end end # refresh cache dir @@ -320,8 +325,11 @@ class BuildCommServer file_name = File.basename(dst_file) cache_file = "#{@cache_dir}/#{file_name}" @download_cache_mutex.synchronize do - # copy & touch - FileUtils.ln(dst_file, cache_file, :force => true) + # copy & touch + #FileUtils.ln(dst_file, cache_file, :force => true) + if not FileUtil.safeLink(dst_file, cache_file) then + @log.error "Failed to link file [#{dst_file}] to [#{cache_file}]" + end FileUtils.touch cache_file end end diff --git a/src/common/FileUtil.rb b/src/common/FileUtil.rb index e39483e..ee749f1 100644 --- a/src/common/FileUtil.rb +++ b/src/common/FileUtil.rb @@ -29,5 +29,37 @@ class FileUtil return false end end - -end \ No newline at end of file + + public + def FileUtil.safeLink(target, linkname, force = true) + # linkname can be a file or a directory + # + # if linkname does not exist, it sould be a filename. + # - File.dirname(linkpath) should exists and be a directory. + # if linkname does exist, it should be a directory. + # - if it is a file, FileUtils.ln() will be a error. + # - if it is a file, FileUtils.cp() will be finished successfully. + linkpath = linkname + if not File.exist? linkname then + linkpath = File.dirname(linkname) + end + + # both target and linkpath should exist. + if not File.exist? target and not File.exist? linkpath then + return false + end + + # compare device between target and linkpath + # - execute ln if it is same device + # - execute cp if it is different device + target_device = File.stat(target).dev + linkpath_device = File.stat(linkpath).dev + if target_device.eql? linkpath_device then + FileUtils.ln(target, linkname, :force => force) + else + FileUtils.cp(target, linkname) + end + + return true + end +end diff --git a/src/pkg_server/client.rb b/src/pkg_server/client.rb index c985b4c..eda6483 100644 --- a/src/pkg_server/client.rb +++ b/src/pkg_server/client.rb @@ -47,8 +47,7 @@ require 'digest' require "SnapshotController" require "ChangeLogController" require 'PackageServerConstants' - - +require "FileUtil" $update_mutex = Mutex.new $get_snapshot_mutex = Mutex.new @@ -436,8 +435,10 @@ class Client @log.info "Entering cache sync",Log::LV_USER cached_filepath = get_cached_filepath(filename, pkg_checksum, pkg_size) if not cached_filepath.nil? and File.exist? cached_filepath then - FileUtils.ln(cached_filepath, location, :force => true) - downloaded_file_path = File.join(location, File.basename(cached_filepath)) + #FileUtils.ln(cached_filepath, location, :force => true) + if (FileUtil.safeLink(cached_filepath, location)) then + downloaded_file_path = File.join(location, File.basename(cached_filepath)) + end end ensure Utils.file_unlock(lock) if not lock.nil? @@ -461,7 +462,11 @@ class Client @log.info " [path: #{@download_path}]" if not File.exist? cachefile then - FileUtils.ln(filepath, @download_path, :force => true) + #FileUtils.ln(filepath, @download_path, :force => true) + if not FileUtil.safeLink(filepath, @download_path) then + @log.info "Failed to link file [#{filepath}] to " + @log.info " [#{@download_path}]" + end end if not File.exist? cachefile then diff --git a/src/pkg_server/packageServer.rb b/src/pkg_server/packageServer.rb index 47519fb..021a240 100644 --- a/src/pkg_server/packageServer.rb +++ b/src/pkg_server/packageServer.rb @@ -208,9 +208,15 @@ class PackageServer # move file to package server binary_pkg_file_path_list.each do |l| if test_flag then - FileUtils.ln( l, "#{distribution.location}/temp/#{File.basename(l)}", :force => true ) + #FileUtils.ln( l, "#{distribution.location}/temp/#{File.basename(l)}", :force => true ) + if not FileUtil.safeLink( l, "#{distribution.location}/temp/#{File.basename(l)}") then + @log.error("Failed to link file [#{l}] to [#{distribution.location}/temp/#{File.basename(l)}]") + end else - FileUtils.ln( l, "#{distribution.location}/binary/#{File.basename(l)}", :force => true ) + #FileUtils.ln( l, "#{distribution.location}/binary/#{File.basename(l)}", :force => true ) + if not FileUtil.safeLink( l, "#{distribution.location}/binary/#{File.basename(l)}") then + @log.error("Failed to link file [#{l}] to [#{distribution.location}/binary/#{File.basename(l)}]") + end end end @@ -219,11 +225,17 @@ class PackageServer if test_flag then src_file = File.join(distribution.location, l[0]) dest_file = File.join(distribution.location, "temp", l[1]) - FileUtils.ln( src_file, dest_file, :force => true ) + #FileUtils.ln( src_file, dest_file, :force => true ) + if not FileUtil.safeLink( src_file, dest_file) then + @log.error("Failed to link file [#{src_file}] to [#{dest_file}]") + end else src_file = File.join(distribution.location, l[0]) dest_file = File.join(distribution.location, "binary", l[1]) - FileUtils.ln( src_file, dest_file, :force => true ) + #FileUtils.ln( src_file, dest_file, :force => true ) + if not FileUtil.safeLink( src_file, dest_file) then + @log.error("Failed to link file [#{src_file}] to [#{dest_file}]") + end end end