apply-patches.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #! /bin/bash
  2. # A little script I whipped up to make it easy to
  3. # patch source trees and have sane error handling
  4. # -Erik
  5. #
  6. # (c) 2002 Erik Andersen <andersen@codepoet.org>
  7. #
  8. # Parameters:
  9. # - the build directory, optional, default value is '.'. The place where are
  10. # the package sources.
  11. # - the patch directory, optional, default '../kernel-patches'. The place
  12. # where are the scripts you want to apply.
  13. # - other parameters are the patch name patterns, optional, default value is
  14. # '*'. Pattern(s) describing the patch names you want to apply.
  15. #
  16. # The script will look recursively for patches from the patch directory. If a
  17. # file is named 'series' then only patches mentionned into it will be applied.
  18. # If not, the script will look for file names matching pattern(s). If the name
  19. # ends with '.tar.*', '.tbz2' or '.tgz', the file is considered as an archive
  20. # and will be uncompressed into a directory named
  21. # '.patches-name_of_the_archive-unpacked'. It's the turn of this directory to
  22. # be scanned with '*' as pattern. Remember that scanning is recursive. Other
  23. # files than series file and archives are considered as a patch.
  24. #
  25. # Once a patch is found, the script will try to apply it. If its name doesn't
  26. # end with '.gz', '.bz', '.bz2', '.xz', '.zip', '.Z', '.diff*' or '.patch*',
  27. # it will be skipped. If necessary, the patch will be uncompressed before being
  28. # applied. The list of the patches applied is stored in '.applied_patches_list'
  29. # file in the build directory.
  30. # Set directories from arguments, or use defaults.
  31. builddir=${1-.}
  32. patchdir=${2-../kernel-patches}
  33. shift 2
  34. patchpattern=${@-*}
  35. # use a well defined sorting order
  36. export LC_COLLATE=C
  37. if [ ! -d "${builddir}" ] ; then
  38. echo "Aborting. '${builddir}' is not a directory."
  39. exit 1
  40. fi
  41. if [ ! -d "${patchdir}" ] ; then
  42. echo "Aborting. '${patchdir}' is not a directory."
  43. exit 1
  44. fi
  45. # Remove any rejects present BEFORE patching - Because if there are
  46. # any, even if patches are well applied, at the end it will complain
  47. # about rejects in builddir.
  48. find ${builddir}/ '(' -name '*.rej' -o -name '.*.rej' ')' -print0 | \
  49. xargs -0 -r rm -f
  50. function apply_patch {
  51. path=$1
  52. patch=$2
  53. case "$patch" in
  54. *.gz)
  55. type="gzip"; uncomp="gunzip -dc"; ;;
  56. *.bz)
  57. type="bzip"; uncomp="bunzip -dc"; ;;
  58. *.bz2)
  59. type="bzip2"; uncomp="bunzip2 -dc"; ;;
  60. *.xz)
  61. type="xz"; uncomp="unxz -dc"; ;;
  62. *.zip)
  63. type="zip"; uncomp="unzip -d"; ;;
  64. *.Z)
  65. type="compress"; uncomp="uncompress -c"; ;;
  66. *.diff*)
  67. type="diff"; uncomp="cat"; ;;
  68. *.patch*)
  69. type="patch"; uncomp="cat"; ;;
  70. *)
  71. echo "Unsupported format file for ${patch}, skip it";
  72. return 0;
  73. ;;
  74. esac
  75. echo ""
  76. echo "Applying $patch using ${type}: "
  77. echo $patch >> ${builddir}/.applied_patches_list
  78. ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" -t
  79. if [ $? != 0 ] ; then
  80. echo "Patch failed! Please fix ${patch}!"
  81. exit 1
  82. fi
  83. }
  84. function scan_patchdir {
  85. path=$1
  86. shift 1
  87. patches=${@-*}
  88. # If there is a series file, use it instead of using ls sort order
  89. # to apply patches. Skip line starting with a dash.
  90. if [ -e "${path}/series" ] ; then
  91. for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
  92. apply_patch "$path" "$i" || exit 1
  93. done
  94. else
  95. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  96. if [ -d "${path}/$i" ] ; then
  97. scan_patchdir "${path}/$i"
  98. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  99. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  100. rm -rf "$unpackedarchivedir" 2> /dev/null
  101. mkdir "$unpackedarchivedir"
  102. tar -C "$unpackedarchivedir" -xaf "${path}/$i"
  103. scan_patchdir "$unpackedarchivedir"
  104. else
  105. apply_patch "$path" "$i" || exit 1
  106. fi
  107. done
  108. fi
  109. }
  110. scan_patchdir "$patchdir" "$patchpattern"
  111. # Check for rejects...
  112. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  113. echo "Aborting. Reject files found."
  114. exit 1
  115. fi
  116. # Remove backup files
  117. find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;