ezbuild/libraries/filesystem

300 lines
9.1 KiB
Plaintext

2012-01-02 12:40:32 +01:00
#!/bin/bash
2012-01-03 11:29:52 +01:00
# Copyright 2010-2012, Asio Software Technologies
2012-01-02 12:40:32 +01:00
# Distributed under the terms of the GNU General Public License v3
2012-01-17 21:36:00 +01:00
#-------------------------------------------------------------------------------
# Applies a diff file to an original
# Parameters: %original_dir% %patch%
#-------------------------------------------------------------------------------
function applyPatch() {
local DESTDIR="${1}"
local PATCH="${2}"
local PREFIX
for PREFIX in {0..4}; do
if run "patch -d ${DESTDIR} --dry-run -p ${PREFIX} -i ${PATCH} -s"; then
run "patch -d ${DESTDIR} -p ${PREFIX} -i ${PATCH}" && return 0
fi
done
return 1
}
2012-01-03 14:48:00 +01:00
#-------------------------------------------------------------------------------
2012-01-04 01:28:10 +01:00
# Completely removes log file
2012-01-03 14:48:00 +01:00
#-------------------------------------------------------------------------------
2012-01-04 11:44:42 +01:00
function cleanLog() {
2012-01-03 14:48:00 +01:00
rm -rf ${LOGFILE} &> /dev/null
}
#-------------------------------------------------------------------------------
2012-01-05 01:49:43 +01:00
# Completely removes whole temp directory
2012-01-03 14:48:00 +01:00
#-------------------------------------------------------------------------------
function cleanTemp() {
2012-01-18 11:10:13 +01:00
rm -rf ${STOREDIR} &> /dev/null
2012-01-03 14:48:00 +01:00
}
#-------------------------------------------------------------------------------
# Downloads files into specified or current directory
2012-01-07 16:00:21 +01:00
# Parameters: %url% [%dest_directory%]
#-------------------------------------------------------------------------------
function download() {
local DEST FILENAME RESULT URL
DEST="${2}"
FILENAME="${1##*/}"
URL="${1}"
if isSet DEST; then
DEST="${DEST}/${FILENAME}"
else
DEST="./${FILENAME}"
fi
2012-01-02 23:38:22 +01:00
logMessage "Downloading ${URL} to ${DEST}" "DEBUG"
echo -ne " ${STAR_GREEN} ${FILENAME}: "
wget --progress=dot -c -t ${FETCHTRIES} -T ${FETCHTIMEOUT} -O "${DEST}" \
${URL} 2>&1 | grep --line-buffered "%" | sed -u -e "s/[\.\,]//g" | \
awk '{printf("\b\b\b\b%4s", $2)}'
RESULT=${PIPESTATUS[0]}
echo -ne "\b\b\b\b"
2012-01-02 23:01:22 +01:00
if [ ${RESULT} -ne 0 ]; then
logOutput "Unable to download ${URL}! Exit code: ${RESULT}"
echo -e "${COLOR_RED}${COLOR_BOLD}ERROR${COLOR_WHITE}${COLOR_NORMAL}"
else
echo -e "${COLOR_GREEN}${COLOR_BOLD}DONE${COLOR_WHITE}${COLOR_NORMAL}"
fi
return ${RESULT}
}
2012-01-23 13:38:33 +01:00
#-------------------------------------------------------------------------------
# Generates a contents file
# Parameters: %contentsfile% %archive%
#-------------------------------------------------------------------------------
function generateContents() {
local OUTPUT="${1}"
local ARCHIVE="${2}"
[ ! -f ${ARCHIVE} ] && return 1
case ${ARCHIVE} in
*.iso)
isoinfo -l -i ${ARCHIVE} > ${OUTPUT} || return 1
;;
*.tar)
tar -tvf ${ARCHIVE} > ${OUTPUT} || return 1
;;
*.tar.gz|*.tar.xz|*.tar.z)
tar -tvzf ${ARCHIVE} > ${OUTPUT} || return 1
;;
*.tar.bz2)
tar -tvjf ${ARCHIVE} > ${OUTPUT} || return 1
;;
esac
return 0
}
2012-01-18 10:51:59 +01:00
#-------------------------------------------------------------------------------
# Generates a hash file
# Parameters: %digestfile% %filelist%
#-------------------------------------------------------------------------------
function generateDigests() {
if [ "${DIGESTS}" != "" ]; then
local OUTPUT="${1}"
shift
local FILELIST="${@}"
rm -f ${OUTPUT}
for FILE in ${FILELIST}; do
[ ! -f ${FILE} ] && return 1
for DIGEST in ${DIGESTS}; do
shash -a ${DIGEST} ${FILE} >> ${OUTPUT}
done
done
fi
return 0
}
2012-01-21 15:36:24 +01:00
#-------------------------------------------------------------------------------
# Creates all components of the specified directories assuming thay are empty
# Parameters: %directory%
#-------------------------------------------------------------------------------
function makeCleanDirectory() {
rm -rf "${@}"
makeDirectory "${@}"
return ${?}
}
2012-01-05 19:21:44 +01:00
#-------------------------------------------------------------------------------
# Creates all 'core' directories necessary for EzBuild to operate
#-------------------------------------------------------------------------------
function makeCoreDirectories() {
local DIRS="${BINDIR} ${BUILDDIR} ${ECACHEDIR} ${LOGFILE%/*} \
${OVERLAYSDIR} ${PACKAGESDIR} ${PORTAGESDIR} ${STOREDIR} ${TEMPDIR}"
2012-01-05 19:21:44 +01:00
for DIR in ${DIRS}; do
makeDirectory ${DIR} || return 1
done
return 0
}
2012-01-22 11:15:10 +01:00
#-------------------------------------------------------------------------------
# Creates base device nodes and symlinks in specified directory
# Parameters: %directory%
#-------------------------------------------------------------------------------
function makeCoreNodes() {
local DIR="${1}"
makeNode ${DIR} 600 console c 5 1 || return 1
makeNode ${DIR} 600 kmsg c 1 11 || return 1
makeNode ${DIR} 777 null c 1 3 || return 1
makeNode ${DIR} 666 ptmx c 5 2 || return 1
makeNode ${DIR} 644 ptyp0 c 2 0 || return 1
makeNode ${DIR} 666 random c 1 8 || return 1
makeNode ${DIR} 666 tty c 5 0 || return 1
makeNode ${DIR} 666 tty0 c 4 0 || return 1
makeNode ${DIR} 644 ttyp0 c 3 0 || return 1
makeNode ${DIR} 666 urandom c 1 9 || return 1
makeNode ${DIR} 666 zero c 1 5 || return 1
for n in {0..3}; do
makeNode ${DIR} 600 ttyS${n} c 4 $(( 64 + $n )) || return 1
done
run "ln -sfv /proc/self/fd ${DIR}/fd" || return 1
run "ln -sfv /proc/self/fd/0 ${DIR}/stdin" || return 1
run "ln -sfv /proc/self/fd/1 ${DIR}/stdout" || return 1
run "ln -sfv /proc/self/fd/2 ${DIR}/stderr" || return 1
run "ln -sfv /proc/kcore ${DIR}/core" || return 1
return 0
}
2012-01-02 12:40:32 +01:00
#-------------------------------------------------------------------------------
# Creates all components of the specified directories
# Parameters: %directory%
#-------------------------------------------------------------------------------
function makeDirectory() {
local OUTPUT RESULT
OUTPUT=$(install -d "${@}" 2>&1)
RESULT=${?}
if [ ${RESULT} -ne 0 ]; then
2012-01-02 12:40:32 +01:00
logOutput "${OUTPUT}"
fi
return ${RESULT}
}
#-------------------------------------------------------------------------------
2012-01-05 01:49:43 +01:00
# Creates a symbolic link between two files in specified target directory
2012-01-07 16:00:21 +01:00
# Parameters: %source% %target_dir%
2012-01-02 12:40:32 +01:00
#-------------------------------------------------------------------------------
function makeLink() {
local DESTDIR OUTPUT RESULT
DESTDIR="${2%/*}"
2012-01-02 12:40:32 +01:00
[[ ! -d ${DESTDIR} ]] && makeDirectory "${DESTDIR}"
OUTPUT=$(ln -sfn "${1}" "${2}" 2>&1)
RESULT=${?}
2012-01-02 23:01:22 +01:00
if [ ${RESULT} -ne 0 ]; then
2012-01-02 12:40:32 +01:00
logOutput "${OUTPUT}"
fi
return ${RESULT}
}
2012-01-03 10:32:12 +01:00
2012-01-08 23:46:05 +01:00
#-------------------------------------------------------------------------------
2012-01-09 11:20:54 +01:00
# Makes block or character special file with specified mode bits
# Parameters: %directory% %chmod% %name% %type% [%major%] [%minor%]
2012-01-08 23:46:05 +01:00
#-------------------------------------------------------------------------------
function makeNode() {
local DIRECTORY="${1}"
2012-01-09 11:20:54 +01:00
local CHMOD="${2}"
local NAME="${3}"
shift 3
2012-01-08 23:46:05 +01:00
if [ ! -d ${DIRECTORY} ]; then
makeDirectory ${DIRECTORY} || return 1
fi
cd ${DIRECTORY}
2012-01-09 11:20:54 +01:00
rm -rf ${NAME}
run "mknod ${NAME} ${*}" || return 1
run "chmod ${CHMOD} ${NAME}" || return 1
2012-01-08 23:46:05 +01:00
return 0
}
2012-01-13 13:36:03 +01:00
#-------------------------------------------------------------------------------
# Merges data from source overlay into destination directory
# Parameters: %src_directory$ %dest_directory% %files_to_copy% %files_to_skip%
# %overwrite%
#-------------------------------------------------------------------------------
function mergeOverlay() {
local DST="${2}"
local FTC="${3}"
local FTS="${4}"
local ORW="${5}"
local SRC="${1}"
local TOPDIR="$(pwd)"
2012-01-18 11:15:11 +01:00
makeCleanDirectory ${TEMPDIR}/ebuilds || return 1
2012-01-13 13:36:03 +01:00
cd ${SRC}
saveIFS ","
if [ "$(toUpper ${FTC})" == "ALL" ]; then
2012-01-18 11:15:11 +01:00
run "cp -apf *-* ${TEMPDIR}/ebuilds/"
run "cp -apf eclass ${TEMPDIR}/ebuilds/"
run "cp -apf licenses ${TEMPDIR}/ebuilds/"
run "cp -apf virtual ${TEMPDIR}/ebuilds/"
2012-01-13 13:36:03 +01:00
else
for ITEM in ${FTC}; do
2012-01-18 11:15:11 +01:00
run "cp -apf --parents ${ITEM} ${TEMPDIR}/ebuilds/" || return 1
2012-01-13 13:36:03 +01:00
done
fi
2012-01-13 13:41:26 +01:00
if [ "$(toUpper ${FTS})" != "NONE" ] && [ "$(toUpper ${FTS})" != "N/A" ]; then
2012-01-13 13:36:03 +01:00
for ITEM in ${FTS}; do
2012-01-18 11:15:11 +01:00
rm -rf ${TEMPDIR}/ebuilds/${ITEM}
2012-01-13 13:36:03 +01:00
done
fi
restoreIFS
2012-01-18 11:15:11 +01:00
cd ${TEMPDIR}/ebuilds
2012-01-13 13:36:03 +01:00
find -type d -name ".svn" -o -name "CVS" | xargs rm -rf {}
while read CAT; do
cd ${CAT}
while read PACK; do
if [ -e ${DST}/${CAT}/${PACK} ]; then
if isEnabled ${ORW}; then
rm -rf ${DST}/${CAT}/${PACK}
else
continue
fi
fi
if [ ! -d ${DST}/${CAT} ]; then
makeDirectory ${DST}/${CAT} || return 1
fi
run "cp -apf ${PACK} ${DST}/${CAT}/${PACK}" || return 1
done < <(find * -maxdepth 0)
cd ..
done < <(find * -maxdepth 0 -type d)
cd ${TOPDIR}
2012-01-18 11:15:11 +01:00
rm -rf ${TEMPDIR}/ebuilds
2012-01-13 13:36:03 +01:00
return 0
}
2012-01-03 10:32:12 +01:00
#-------------------------------------------------------------------------------
# Extracts any tar based archive into specified directory
2012-01-07 16:00:21 +01:00
# Parameters: %tarball% %dest_directory%
2012-01-03 10:32:12 +01:00
#-------------------------------------------------------------------------------
function unpack() {
local DST EXT OUTPUT RESULT SRC
SRC="${1}"
DST="${2}"
EXT=$(toLower "${SRC##*.}")
if [ ! -d ${DST} ]; then
makeDirectory ${DST} || return 1
fi
case "${EXT}" in
bz2)
if [ -e /usr/bin/pbzip2 ]; then
OUTPUT=$(pbzip2 -dc "${SRC}" 2>&1 | tar xpf - -C "${DST}" 2>&1)
else
OUTPUT=$(tar xjpf "${SRC}" -C "${DST}" 2>&1)
fi
RESULT=${?}
;;
gz|xz|z)
OUTPUT=$(tar xpf "${SRC}" -C "${DST}" 2>&1)
RESULT=${?}
;;
*)
OUTPUT="Unrecognized source compression for ${SRC}"
RESULT=1
;;
esac
if [ ${RESULT} -ne 0 ]; then
logOutput "${OUTPUT}"
fi
return ${RESULT}
}