[Title] Do not execute "ln" if target and linkpath are different device. execute...
authordonghyuk.yang <donghyuk.yang@samsung.com>
Thu, 5 Sep 2013 01:02:18 +0000 (10:02 +0900)
committerdonghyuk.yang <donghyuk.yang@samsung.com>
Thu, 5 Sep 2013 01:02:18 +0000 (10:02 +0900)
src/build_server/BuildJob.rb
src/build_server/MultiBuildJob.rb
src/build_server/RegisterPackageJob.rb
src/builder/Builder.rb
src/common/BuildComm.rb
src/common/FileUtil.rb
src/pkg_server/client.rb
src/pkg_server/packageServer.rb

index 3682166c8dbb3d98df2f7c759d01add4b0a4114d..e6c43414726773f24d9c7b88d17f700b33f1da40 100644 (file)
@@ -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
 
index 44a8bba453de0e2430fe82a5530e98194bed2bb4..d5f244a7a355dbd367b156a0c2a7922f8deb6d7b 100644 (file)
@@ -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
 
index e0b0a41d72d6383963f48621d724380dec3a774b..b32be61c14ff9f09ef2580bd421e8fcc5b87e4d0 100644 (file)
@@ -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
index f4048a296d6b7adec012fa5dca042218c735220b..df987a888a5ea67498b170db33fa23f190fa32c3 100644 (file)
@@ -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
index fab444ff566c65b5c489f6b0f1d5e49988e77576..b495e1025ce19a0411e7668ccc7108bd7b563616 100644 (file)
@@ -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
index e39483e69fe7045470fc321f2539f079a9f43727..ee749f11933ff91c29c0e83f381454c4eb964ca2 100644 (file)
@@ -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
index c985b4c7c6c64f175991a239fdb5e2b973763b4c..eda648323a8fbd626fd720a57659cba095b29670 100644 (file)
@@ -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
index 47519fb2e8e6a1c3e18c74fc56bdc4b5a589ccd1..021a24013434ca42c4efe657148f05f0077e474f 100644 (file)
@@ -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