apply-patches.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 file type for ${path}/${patch}, skipping";
  72. return 0
  73. ;;
  74. esac
  75. echo ""
  76. echo "Applying $patch using ${type}: "
  77. if [ ! -e "${path}/$patch" ] ; then
  78. echo "Error: missing patch file ${path}/$patch"
  79. exit 1
  80. fi
  81. echo $patch >> ${builddir}/.applied_patches_list
  82. ${uncomp} "${path}/$patch" | patch -g0 -p1 -E -d "${builddir}" -t -N
  83. if [ $? != 0 ] ; then
  84. echo "Patch failed! Please fix ${patch}!"
  85. exit 1
  86. fi
  87. }
  88. function scan_patchdir {
  89. local path=$1
  90. shift 1
  91. patches=${@-*}
  92. # If there is a series file, use it instead of using ls sort order
  93. # to apply patches. Skip line starting with a dash.
  94. if [ -e "${path}/series" ] ; then
  95. for i in `grep -Ev "^#" ${path}/series 2> /dev/null` ; do
  96. apply_patch "$path" "$i"
  97. done
  98. else
  99. for i in `cd $path; ls -d $patches 2> /dev/null` ; do
  100. if [ -d "${path}/$i" ] ; then
  101. scan_patchdir "${path}/$i"
  102. elif echo "$i" | grep -q -E "\.tar(\..*)?$|\.tbz2?$|\.tgz$" ; then
  103. unpackedarchivedir="$builddir/.patches-$(basename $i)-unpacked"
  104. rm -rf "$unpackedarchivedir" 2> /dev/null
  105. mkdir "$unpackedarchivedir"
  106. tar -C "$unpackedarchivedir" -xaf "${path}/$i"
  107. scan_patchdir "$unpackedarchivedir"
  108. else
  109. apply_patch "$path" "$i"
  110. fi
  111. done
  112. fi
  113. }
  114. scan_patchdir "$patchdir" "$patchpattern"
  115. # Check for rejects...
  116. if [ "`find $builddir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] ; then
  117. echo "Aborting. Reject files found."
  118. exit 1
  119. fi
  120. # Remove backup files
  121. find $builddir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;