2
1

apply-patches.sh 4.5 KB

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