reGen2 Linux Release Metatool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
8.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. # Copyright 2010-2012, Asio Software Technologies
  3. # Distributed under the terms of the GNU General Public License v3
  4. #-------------------------------------------------------------------------------
  5. # Applies a diff file to an original
  6. # Parameters: %original_dir% %patch%
  7. #-------------------------------------------------------------------------------
  8. function applyPatch() {
  9. local DESTDIR="${1}"
  10. local PATCH="${2}"
  11. local PREFIX
  12. for PREFIX in {0..4}; do
  13. if run "patch -d ${DESTDIR} --dry-run -p ${PREFIX} -i ${PATCH} -s"; then
  14. run "patch -d ${DESTDIR} -p ${PREFIX} -i ${PATCH}" && return 0
  15. fi
  16. done
  17. return 1
  18. }
  19. #-------------------------------------------------------------------------------
  20. # Completely removes log file
  21. #-------------------------------------------------------------------------------
  22. function cleanLog() {
  23. rm -rf ${LOGFILE} &> /dev/null
  24. }
  25. #-------------------------------------------------------------------------------
  26. # Completely removes whole temp directory
  27. #-------------------------------------------------------------------------------
  28. function cleanTemp() {
  29. rm -rf ${STOREDIR} &> /dev/null
  30. }
  31. #-------------------------------------------------------------------------------
  32. # Downloads files into specified or current directory
  33. # Parameters: %url% [%dest_directory%]
  34. #-------------------------------------------------------------------------------
  35. function download() {
  36. local DEST FILENAME RESULT URL
  37. DEST="${2}"
  38. FILENAME="${1##*/}"
  39. URL="${1}"
  40. if isSet DEST; then
  41. DEST="${DEST}/${FILENAME}"
  42. else
  43. DEST="./${FILENAME}"
  44. fi
  45. logMessage "Downloading ${URL} to ${DEST}" "DEBUG"
  46. echo -ne " ${STAR_GREEN} ${FILENAME}: "
  47. wget --progress=dot -c -t ${FETCHTRIES} -T ${FETCHTIMEOUT} -O "${DEST}" \
  48. ${URL} 2>&1 | grep --line-buffered "%" | sed -u -e "s/[\.\,]//g" | \
  49. awk '{printf("\b\b\b\b%4s", $2)}'
  50. RESULT=${PIPESTATUS[0]}
  51. echo -ne "\b\b\b\b"
  52. if [ ${RESULT} -ne 0 ]; then
  53. logOutput "Unable to download ${URL}! Exit code: ${RESULT}"
  54. echo -e "${COLOR_RED}${COLOR_BOLD}ERROR${COLOR_WHITE}${COLOR_NORMAL}"
  55. else
  56. echo -e "${COLOR_GREEN}${COLOR_BOLD}DONE${COLOR_WHITE}${COLOR_NORMAL}"
  57. fi
  58. return ${RESULT}
  59. }
  60. #-------------------------------------------------------------------------------
  61. # Generates a hash file
  62. # Parameters: %digestfile% %filelist%
  63. #-------------------------------------------------------------------------------
  64. function generateDigests() {
  65. if [ "${DIGESTS}" != "" ]; then
  66. local OUTPUT="${1}"
  67. shift
  68. local FILELIST="${@}"
  69. rm -f ${OUTPUT}
  70. for FILE in ${FILELIST}; do
  71. [ ! -f ${FILE} ] && return 1
  72. for DIGEST in ${DIGESTS}; do
  73. shash -a ${DIGEST} ${FILE} >> ${OUTPUT}
  74. done
  75. done
  76. fi
  77. return 0
  78. }
  79. #-------------------------------------------------------------------------------
  80. # Creates all components of the specified directories assuming thay are empty
  81. # Parameters: %directory%
  82. #-------------------------------------------------------------------------------
  83. function makeCleanDirectory() {
  84. rm -rf "${@}"
  85. makeDirectory "${@}"
  86. return ${?}
  87. }
  88. #-------------------------------------------------------------------------------
  89. # Creates all 'core' directories necessary for EzBuild to operate
  90. #-------------------------------------------------------------------------------
  91. function makeCoreDirectories() {
  92. local DIRS="${BINDIR} ${BUILDDIR} ${LOGFILE%/*} ${OVERLAYSDIR} \
  93. ${PACKAGESDIR} ${PORTAGESDIR} ${STOREDIR} ${TEMPDIR}"
  94. for DIR in ${DIRS}; do
  95. makeDirectory ${DIR} || return 1
  96. done
  97. return 0
  98. }
  99. #-------------------------------------------------------------------------------
  100. # Creates base device nodes and symlinks in specified directory
  101. # Parameters: %directory%
  102. #-------------------------------------------------------------------------------
  103. function makeCoreNodes() {
  104. local DIR="${1}"
  105. makeNode ${DIR} 600 console c 5 1 || return 1
  106. makeNode ${DIR} 600 kmsg c 1 11 || return 1
  107. makeNode ${DIR} 777 null c 1 3 || return 1
  108. makeNode ${DIR} 666 ptmx c 5 2 || return 1
  109. makeNode ${DIR} 644 ptyp0 c 2 0 || return 1
  110. makeNode ${DIR} 666 random c 1 8 || return 1
  111. makeNode ${DIR} 666 tty c 5 0 || return 1
  112. makeNode ${DIR} 666 tty0 c 4 0 || return 1
  113. makeNode ${DIR} 644 ttyp0 c 3 0 || return 1
  114. makeNode ${DIR} 666 urandom c 1 9 || return 1
  115. makeNode ${DIR} 666 zero c 1 5 || return 1
  116. for n in {0..3}; do
  117. makeNode ${DIR} 600 ttyS${n} c 4 $(( 64 + $n )) || return 1
  118. done
  119. run "ln -sfv /proc/self/fd ${DIR}/fd" || return 1
  120. run "ln -sfv /proc/self/fd/0 ${DIR}/stdin" || return 1
  121. run "ln -sfv /proc/self/fd/1 ${DIR}/stdout" || return 1
  122. run "ln -sfv /proc/self/fd/2 ${DIR}/stderr" || return 1
  123. run "ln -sfv /proc/kcore ${DIR}/core" || return 1
  124. return 0
  125. }
  126. #-------------------------------------------------------------------------------
  127. # Creates all components of the specified directories
  128. # Parameters: %directory%
  129. #-------------------------------------------------------------------------------
  130. function makeDirectory() {
  131. local OUTPUT RESULT
  132. OUTPUT=$(install -d "${@}" 2>&1)
  133. RESULT=${?}
  134. if [ ${RESULT} -ne 0 ]; then
  135. logOutput "${OUTPUT}"
  136. fi
  137. return ${RESULT}
  138. }
  139. #-------------------------------------------------------------------------------
  140. # Creates a symbolic link between two files in specified target directory
  141. # Parameters: %source% %target_dir%
  142. #-------------------------------------------------------------------------------
  143. function makeLink() {
  144. local DESTDIR OUTPUT RESULT
  145. DESTDIR="${2%/*}"
  146. [[ ! -d ${DESTDIR} ]] && makeDirectory "${DESTDIR}"
  147. OUTPUT=$(ln -sfn "${1}" "${2}" 2>&1)
  148. RESULT=${?}
  149. if [ ${RESULT} -ne 0 ]; then
  150. logOutput "${OUTPUT}"
  151. fi
  152. return ${RESULT}
  153. }
  154. #-------------------------------------------------------------------------------
  155. # Makes block or character special file with specified mode bits
  156. # Parameters: %directory% %chmod% %name% %type% [%major%] [%minor%]
  157. #-------------------------------------------------------------------------------
  158. function makeNode() {
  159. local DIRECTORY="${1}"
  160. local CHMOD="${2}"
  161. local NAME="${3}"
  162. shift 3
  163. if [ ! -d ${DIRECTORY} ]; then
  164. makeDirectory ${DIRECTORY} || return 1
  165. fi
  166. cd ${DIRECTORY}
  167. rm -rf ${NAME}
  168. run "mknod ${NAME} ${*}" || return 1
  169. run "chmod ${CHMOD} ${NAME}" || return 1
  170. return 0
  171. }
  172. #-------------------------------------------------------------------------------
  173. # Merges data from source overlay into destination directory
  174. # Parameters: %src_directory$ %dest_directory% %files_to_copy% %files_to_skip%
  175. # %overwrite%
  176. #-------------------------------------------------------------------------------
  177. function mergeOverlay() {
  178. local DST="${2}"
  179. local FTC="${3}"
  180. local FTS="${4}"
  181. local ORW="${5}"
  182. local SRC="${1}"
  183. local TOPDIR="$(pwd)"
  184. makeCleanDirectory ${TEMPDIR}/ebuilds || return 1
  185. cd ${SRC}
  186. saveIFS ","
  187. if [ "$(toUpper ${FTC})" == "ALL" ]; then
  188. run "cp -apf *-* ${TEMPDIR}/ebuilds/"
  189. run "cp -apf eclass ${TEMPDIR}/ebuilds/"
  190. run "cp -apf licenses ${TEMPDIR}/ebuilds/"
  191. run "cp -apf virtual ${TEMPDIR}/ebuilds/"
  192. else
  193. for ITEM in ${FTC}; do
  194. run "cp -apf --parents ${ITEM} ${TEMPDIR}/ebuilds/" || return 1
  195. done
  196. fi
  197. if [ "$(toUpper ${FTS})" != "NONE" ] && [ "$(toUpper ${FTS})" != "N/A" ]; then
  198. for ITEM in ${FTS}; do
  199. rm -rf ${TEMPDIR}/ebuilds/${ITEM}
  200. done
  201. fi
  202. restoreIFS
  203. cd ${TEMPDIR}/ebuilds
  204. find -type d -name ".svn" -o -name "CVS" | xargs rm -rf {}
  205. while read CAT; do
  206. cd ${CAT}
  207. while read PACK; do
  208. if [ -e ${DST}/${CAT}/${PACK} ]; then
  209. if isEnabled ${ORW}; then
  210. rm -rf ${DST}/${CAT}/${PACK}
  211. else
  212. continue
  213. fi
  214. fi
  215. if [ ! -d ${DST}/${CAT} ]; then
  216. makeDirectory ${DST}/${CAT} || return 1
  217. fi
  218. run "cp -apf ${PACK} ${DST}/${CAT}/${PACK}" || return 1
  219. done < <(find * -maxdepth 0)
  220. cd ..
  221. done < <(find * -maxdepth 0 -type d)
  222. cd ${TOPDIR}
  223. rm -rf ${TEMPDIR}/ebuilds
  224. return 0
  225. }
  226. #-------------------------------------------------------------------------------
  227. # Extracts any tar based archive into specified directory
  228. # Parameters: %tarball% %dest_directory%
  229. #-------------------------------------------------------------------------------
  230. function unpack() {
  231. local DST EXT OUTPUT RESULT SRC
  232. SRC="${1}"
  233. DST="${2}"
  234. EXT=$(toLower "${SRC##*.}")
  235. if [ ! -d ${DST} ]; then
  236. makeDirectory ${DST} || return 1
  237. fi
  238. case "${EXT}" in
  239. bz2)
  240. if [ -e /usr/bin/pbzip2 ]; then
  241. OUTPUT=$(pbzip2 -dc "${SRC}" 2>&1 | tar xpf - -C "${DST}" 2>&1)
  242. else
  243. OUTPUT=$(tar xjpf "${SRC}" -C "${DST}" 2>&1)
  244. fi
  245. RESULT=${?}
  246. ;;
  247. gz|xz|z)
  248. OUTPUT=$(tar xpf "${SRC}" -C "${DST}" 2>&1)
  249. RESULT=${?}
  250. ;;
  251. *)
  252. OUTPUT="Unrecognized source compression for ${SRC}"
  253. RESULT=1
  254. ;;
  255. esac
  256. if [ ${RESULT} -ne 0 ]; then
  257. logOutput "${OUTPUT}"
  258. fi
  259. return ${RESULT}
  260. }