summaryrefslogblamecommitdiffstats
path: root/helper/fileutil.inc
blob: 9f5c110feffeeca9ff2fd7a5ca49df6501e44c9c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
 

                              









                                                         
                                                                                                          



                                                                                

                      
                         
                                                                                                                                          
                                                                       
                                                                                                           
                                                                               
                                                                    
                                   
                                                                                                                                         
                                                                                                                

 

                                                
                                                                                                  
                        
                                                     

                                                                                   
                              
                                                       
                                                                                                                                                
                                                        
                                                                                                                                                





                                                                                          



                                                                                                    
                                                                                                                                   
                                                                                                        
                                                                            




                                                        
 

                      
                                      
                                                                               
                        
                                                        



                                                       
                                                        


                                                                                          
                               



                                      
                                                          
                                                       
                                                           
                                                       
                    
                                                                                                





                                                
                    







                                                                         
                                    

                                                               
                                                                                                                 






                                                                                    
                                    

                                                               
                                                                                                                
                                  





                                                                         
                                    

                                                               
                                                                                                             
                                  





                                                                                              
 
 




                                                                                                    
                                                                                                            

 



                                                                                                 

















                                                                                                                           
 
 
 
                                 
        



                                                                                                       
                      



                                                                                                                     
                                   
                                                                         
                                                               
                                                                
                         

                                                            

 

                                 
                                                                 

                                                                                                                    

                                                              
                                                   

                                                            
 
#
# copy list of files using tar
tarcopy () {
	if [ $# -gt 0 -a "x$1" == "x-i" ]; then
		shift
		local IGNORE_ERROR="--ignore-failed-read"
	else
		local IGNORE_ERROR=
	fi
	if [ "x$IGNORE_TAR_ERROR" != "x" ]; then
		unset IGNORE_TAR_ERROR
		IGNORE_ERROR="--ignore-failed-read"
	fi
	[ $# -ne 2 ] && perror "Sanity check failed: tarcopy needs exactly two params, but $# were given."
	local FROM=$(trim "$1")
	local TO=$(trim "$2")
	if [ -z "$FROM" ]; then
		pwarning "tarcopy called with empty input list (dest was '$TO')"
		return
	fi
	local SHORT=$FROM
	[ ${#SHORT} -gt 30 ] && SHORT=$(echo "$SHORT" | sed ':a;N;$!ba;s/\n/ /g' | cut -c-25)...$(echo "$SHORT" | cut -c$[${#SHORT} - 4]-)
	[ -z "$TO" ] && perror "tarcopy called with empty destination."
	[ ! -d "$TO" ] && { mkdir -p "$TO" || perror "could not create destination "$TO" for tar-copy."; } 
	# TODO count files copied? would remove the need to do it everywhere :)
	tar $IGNORE_ERROR -cpP $FROM | tar -xp -C "$TO" 2> /dev/null
	local PS=(${PIPESTATUS[*]})
	[ "x$IGNORE_ERROR" == "x" -a "x${PS[0]}" != "x0" ] && perror "packing-part of tar-copy from '$SHORT' to '$TO' failed. (${PS[0]})"
	[ "x${PS[1]}" != "x0" ] && perror "unpacking-part of tar-copy from '$SHORT' to '$TO' failed. (${PS[1]})"
}

# get all files of required packages by a module
list_packet_files() {
	[ -z "$REQUIRED_CONTENT_PACKAGES" ] && pinfo "No required packages for $TOOL"  && return 1
	local PACKAGE=""
	for PACKAGE in $REQUIRED_CONTENT_PACKAGES; do
		local OPTIONAL="$(echo "$PACKAGE" | cut -c 1)"
		[ "x$OPTIONAL" = "x@" ] && PACKAGE="$(echo "$PACKAGE" | cut -c 2-)"
		local FILES=""
		if [ "$PACKET_HANDLER" = "dpkg" ]; then
			FILES="$(dpkg -L "$PACKAGE" | grep "^/" | grep -v -E 'share/(man|doc)|/var/run|/var/log'; echo ":###:${PIPESTATUS[0]}")"
		elif [ "$PACKET_HANDLER" = "rpm" ]; then
			FILES="$(rpm -ql "$PACKAGE" | grep "^/" | grep -v -E 'share/(man|doc)|/var/run|/var/log'; echo ":###:${PIPESTATUS[0]}")"
		fi
		# ugly hack to get our return value
		#local LPRET=$(echo "$FILES" | tail -1 | sed 's/^.*:###:\([0-9]*\)$/\1/g')
		#FILES=$(echo "$FILES" | sed 's/^\(.*\):###:[0-9]*$/\1/g')
		local LPRET=$(echo "$FILES" | awk -F ':###:' '{printf $2}')
		FILES=$(echo "$FILES" | awk -F ':###:' '{print $1}')
		if [ "x$LPRET" != "x0" -a "x$OPTIONAL" != "x@" ]; then
			pdebug "FILES: '$FILES'"
			perror "dpkg/rpm exited with code '$LPRET' for required package ${PACKAGE}."
		fi
		[ "x$LPRET" != "x0" ] && pwarning "dpkg/rpm exited with code '$LPRET' for optional package ${PACKAGE}." && continue
		[ -z "$FILES" ] && pwarning "list_packet_files empty for packet ${PACKAGE}." && continue
		pdebug "Packet $PACKAGE has $(echo $FILES | wc -w) files..."
		for FILE in $FILES; do
			[ ! -d "$FILE" ] && echo "$FILE"
		done
	done
}
#
# Conveniance function
#
# install all dependencies of a module
# goes through all package as given in the variable REQUIRED_INSTALLED_PACKAGES
install_dependencies() {
	[ -z "$REQUIRED_INSTALLED_PACKAGES" ] && return 
	install_packages "$REQUIRED_INSTALLED_PACKAGES"
}
# 
# install given packet through system's packet manager
# uses PACKET_HANDLER as determined in helper/system.inc
#
install_packages() {
	[ $# -eq 0 ] && perror "Sanity check failed: no argument given to install_package"
	local PACKAGE_LIST="$@"
	local INSTALLED_PACKAGES=""

	for PKG in ${PACKAGE_LIST}; do
		# check if installed
		if [ "x$PACKET_HANDLER" == "xdpkg" ]; then
			dpkg -L ${PKG} > /dev/null 2>&1
		elif [ "x$PACKET_HANDLER" == "xrpm" ]; then
			rpm -ql ${PKG} > /dev/null 2>&1
		else
			perror "No packet manager / handler determined, this should not happen!"
		fi

		local LRET=$?
		if [ "x$LRET" == "x0" ]; then
			# package installed
			pdebug "$PKG installed!"
		else
			# package not installed
			pdebug "$PKG not installed!"
			if [ "x$PACKET_MANAGER" == "xapt" ]; then
				apt-get install -y ${PKG}
				local IRET=$?
				if [ "x$IRET" == "x0" ]; then
					# $PGK was installed successfully
					INSTALLED_PACKAGES+="$PKG "
				else
					# PKG was not installed
					# TODO error handling
					perror "install_packages: apt-get failed with '$IRET' for package '$PKG'"
				fi
			elif [ "x$PACKET_MANAGER" == "xzypper" ]; then
				zypper --no-refresh --non-interactive install ${PKG}
				local IRET=$?
				if [ "x$IRET" == "x0" ]; then
					# $PGK was installed successfully
					INSTALLED_PACKAGES+="$PKG "
				else
					# PKG was not installed
					# TODO error handling
					perror "install_packages: zypper failed with '$IRET' for package '$PKG'"
				fi
			elif [ "x$PACKET_MANAGER" == "xyum" ]; then
				yum --assumeyes install ${PKG}
				local IRET=$?	
				if [ "x$IRET" == "x0" ]; then
					# $PGK was installed successfully
					INSTALLED_PACKAGES+="$PKG "
				else
					# PKG was not installed
					# TODO error handling
					perror "install_packages: yum failed with '$IRET' for package '$PKG'"
				fi
			else
				perror "No packet manager determined, this should not happen!"
			fi
		fi
	done
	[ ! -z "$INSTALLED_PACKAGES" ] && pinfo "Packages installed: ${INSTALLED_PACKAGES}"
}

#
# copies static data files from <MODULE>/data/ to <TARGET_BUILD_DIR>
#
copy_static_data() {
	[ ! -d "${MODULE_DIR}/data" ] && pinfo "${MODULE} has no static 'data' directory." && return
	cp -r "${MODULE_DIR}/data/"* ${TARGET_BUILD_DIR} || perror "Could not copy static data of ${MODULE}"
}

copy_system_files() {
	[ ! -z "$REQUIRED_SYSTEM_FILES" ] && tarcopy "$REQUIRED_SYSTEM_FILES" "$TARGET_BUILD_DIR"
}

calc_size() {

	local CURRENT_BUILD_SIZE=$(du -bc "${TARGET_BUILD_DIR}" | awk 'END {print $1}')

	[ ! -z "${BUILD_SIZE[$MODULE]}" ] && local OLD_MODULE_SIZE=${BUILD_SIZE[$MODULE]} || local OLD_MODULE_SIZE=0
	local diff=$((CURRENT_BUILD_SIZE-TARGET_BUILD_SIZE+OLD_MODULE_SIZE))

	if [ -z "${BUILD_SIZE[$MODULE]}" ]; then 
		echo "BUILD_SIZE[$MODULE]=${diff}" >> "${ROOT_DIR}/logs/${TARGET}.size"
	else
		sed -i "s/^BUILD_SIZE\[${MODULE}\]=.*$/BUILD_SIZE\[${MODULE}\]=${diff}/g" "${ROOT_DIR}/logs/${TARGET}.size"
	fi
	
	MODULE_BUILD_SIZE=$(echo $diff | awk '{ sum=$1; hum[1024^3]="GB"; hum[1024^2]="MB"; hum[1024]="KB"; 
					for (x=1024^3; x>=1024; x/=1024){ 
        					if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break }
					}
				}')
}

#
# generate initramfs of directory
# usage:
#	generate_initramfs <source_dir> <files> <destination_dir/filename>
# example:
#	generate_initramfs "./server/boot/stage32_sqfs" "./mnt/openslx.sqfs" "./server/boot/initramfs2"
#	generate_initramfs "./server/build/stage31" "." "./server/boot/initramfs"
generate_initramfs() {
	[ $# -ne 3 ] && perror "Sanity check failed: generate_initramfs needs exactly two params, but $# were given."
        cd "$1" || perror "Cannot cd to '$1'"
	
        find $2 | cpio --format="newc" --create | gzip -9 > "$3"
        local PS=(${PIPESTATUS[*]})
        [ "x${PS[0]}" != "x0" ] && perror "'find $2' in '$(pwd)' failed."
        [ "x${PS[1]}" != "x0" ] && perror "cpio create failed."
        [ "x${PS[2]}" != "x0" ] && perror "gzip to '$3' failed."
        cd - &> /dev/null
        pinfo "Created initramfs of $1 at $3"
	pinfo "Size: $(du -bsh "$3" | awk 'END {print $1}')"
}

# generates squashfs of directory
# usage:
#	generate_squashfs <source_dir> <destination_dir/filename>
generate_squashfs() {
	[ $# -ne 2 ] && perror "Sanity check failed: generate_squashfs needs exactly two params, but $# were given."
	[ -d "$1" ] || perror "$1 is not a directory."
	mksquashfs "$1" "$2" -comp xz -b 1M -no-recovery >&6 \
	        || perror "mksquashfs failed ($?)."
	pinfo "Created squashfs of $1 at $2"
	pinfo "Size: $(du -bsh "$2" | awk 'END {print $1}')"
}