场景
新版本某个文件有版本更新,软链接指向文件发生了改变,制作patch成的补丁文件里面,软链接文件会被制作成内容为指向文件的普通文件,打补丁的时候文件名相同的链接文件和普通文件会有冲突,这时候需要删掉因为冲突产生的"*.rej"文件和软链接文件,再重新建立软链接。
原来文件
[root@localhost ln_test]# ls -l test-old/
total 4
-rw-r--r-- 1 root root 5 Jun 20 14:28 test.1.2.txt
lrwxrwxrwx 1 root root 12 Jun 20 14:31 test.txt -> test.1.2.txt
新文件
[root@localhost ln_test]# ls test1/ -l
total 4
-rw-r--r-- 1 root root 10 Jun 19 16:50 SafeAgent.1.3.jar
lrwxrwxrwx 1 root root 17 Jun 20 14:27 SafeAgent.jar -> SafeAgent.1.3.jar
制作补丁文件
[root@localhost ln_test]# diff -Naur test-old/ test-new/ > test.patch
打补丁(失败)
[root@localhost ln_test]# cd test-old
[root@localhost test-old]# patch -p1 < ../test.patch
patching file test.1.2.txt
patching file test.1.3.txt
File test.txt is not a regular file -- refusing to patch
1 out of 1 hunk ignored -- saving rejects to file test.txt.rej
[root@localhost test-old]# ls -l
total 8
-rw-r--r-- 1 root root 10 Jun 20 14:34 test.1.3.txt
lrwxrwxrwx 1 root root 12 Jun 20 14:31 test.txt -> test.1.2.txt
-rw-r--r-- 1 root root 124 Jun 20 14:34 test.txt.rej
解决问题:
[root@localhost test-old]# cd ../
[root@localhost ln_test]# ls
test-new test-old test.patch test.sh
[root@localhost ln_test]# ./test.sh
==== ln -s ././test.1.3.txt test.txt
/data/work/aarch64/ln_test
[root@localhost ln_test]# ls test-old -l
total 4
-rw-r--r-- 1 root root 10 Jun 20 14:34 test.1.3.txt
lrwxrwxrwx 1 root root 16 Jun 20 14:36 test.txt -> ././test.1.3.txt
附上脚本teset.sh
#!/bin/bash
find -name *.rej | while read file
do
soft_ln=$(echo ${file%.*})
if [ -L ${soft_ln} ];then
ln_file_dir=$(echo ${file%/*})
rm -f ${file}
cd ${ln_file_dir}
ln_file=$(echo ${soft_ln##*/})
prefix=$(echo ${ln_file%.*})
real_file=$(ls -l ${ln_file} | awk '{print $11}')
rm -f ${ln_file}
rm -f ${real_file}
if [ -d "$(echo ${ln_file%/*})" ];then
real_file_dir=$(echo ${file%/*})
else
real_file_dir=.
fi
new_file=$(ls -l "${real_file_dir}/${prefix}"* | awk '{print $9}')
echo "==== ln -s ${real_file_dir}/${new_file} ${ln_file}"
ln -s ${real_file_dir}/${new_file} ${ln_file}
cd -
fi
done
如有其他解决办法,欢迎讨论。。。。。。