apply-patches.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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', '.zip', '.Z', '.diff*' or '.patch*', it will
  27. # 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. if [ ! -d "${builddir}" ] ; then
  36. echo "Aborting. '${builddir}' is not a directory."
  37. exit 1
  38. fi
  39. if [ ! -d "${patchdir}" ] ; then
  40. echo "Aborting. '${patchdir}' is not a directory."
  41. exit 1
  42. fi
  43. # Remove any rejects present BEFORE patching - Because if there are
  44. # any, even if patches are well applied, at the end it will complain
  45. # about rejects in builddir.
  46. find ${builddir}/ '(' -name '*.rej' -o -name '.*.rej' ')' -print0 | \
  47. xargs -0 -r rm -f
  48. function apply_patch {
  49. path=$1
  50. patch=$2
  51. case "$patch" in
  52. *.gz)
  53. type="gzip"; uncomp="gunzip -dc"; ;;
  54. *.bz)
  55. type="bzip"; uncomp="bunzip -dc"; ;;
  56. *.bz2)
  57. type="bzip2"; uncomp="bunzip2 -dc"; ;;
  58. *.zip)
  59. type="zip"; uncomp="unzip -d"; ;;
  60. *.Z)
  61. type="compress"; uncomp="uncompress -c"; ;;
  62. *.diff*)
  63. type="diff"; uncomp="cat"; ;;
  64. *.patch*)
  65. type="patch"; uncomp="cat"; ;;
  66. *)
  67. echo "Unsupported format file for ${patch}, skip it";
  68. return 0;
  69. ;;
  70. esac
  71. echo ""
  72. echo "Applying $patch using ${type}: "
  73. echo $patch >> ${builddir}/.applied_patches_list
  74. ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}"
  75. if [ $? != 0 ] ; then
  76. echo "Patch failed! Please fix ${patch}!"
  77. exit 1
  78. fi
  79. }
  80. function scan_patchdir {
  81. path=$1
  82. shift 1
  83. patches=${@-*}
  84. # If there is a series file, use it instead of using ls sort order
  85. # to apply patches. Skip line starting with a dash.
  86. if [ -e "${path}/series" ] ; then
  87. for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
  88. apply_patch "$path" "$i" || exit 1
  89. done
  90. else
  91. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  92. if [ -d "${path}/$i" ] ; then
  93. scan_patchdir "${path}/$i"
  94. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  95. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  96. rm -rf "$unpackedarchivedir" 2> /dev/null
  97. mkdir "$unpackedarchivedir"
  98. tar -C "$unpackedarchivedir" -xaf "${path}/$i"
  99. scan_patchdir "$unpackedarchivedir"
  100. else
  101. apply_patch "$path" "$i" || exit 1
  102. fi
  103. done
  104. fi
  105. }
  106. scan_patchdir "$patchdir" "$patchpattern"
  107. # Check for rejects...
  108. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  109. echo "Aborting. Reject files found."
  110. exit 1
  111. fi
  112. # Remove backup files
  113. find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;