2
1

apply-patches.sh 4.7 KB

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