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












                                                                   


                                                                                                          



                                                                                

                      
                         
                                                                                                                                          
                                                                       
                                                                                                           
                                        
                                   

                                                                                                                

 

                                                
                                                                                          


                                                      
                                                                                                                                    
                                                           
                                                                                                                                                        





                                                                                          
                                                                                                                         
                                                                                            




                                                        
 
                                      
                                                                         




                                                                                                                      
                                                                                                                                    

          







                                                                                                          
 
                                                                                                                      
 
                                 
        




                                                                                                       
                      



                                                                                                                     
                                   
                                                                         
                                                               
                                                                
            
                                             

 

                                 
                                                                 

                                                                                                                    

                                                              
                                                   
                                            
 
# one time jobs

# determine packet manager:
if [ ! -z "$(which apt-get)" ]; then
	PACKET_MANAGER="apt"
elif [ ! -z "$(which zypper)" ]; then
	PACKET_MANAGER="zypper"
else
	perror "Could not determine this platform's packet manager"
fi

#

# copy list of files using tar
tarcopy () {
	[ $# -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."; } 
	tar -cp $FROM | tar -xp -C "$TO"
	local PS=(${PIPESTATUS[*]})
	[ "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_PACKAGES" ] && pinfo "No required packages for $TOOL"  && return 1
	for PACKAGE in $REQUIRED_PACKAGES; do
		local FILES=""
		if [ "$PACKET_MANAGER" = "apt" ]; then
			FILES="$(dpkg -L "$PACKAGE" | grep -v -E 'share/(man|doc)|/var/run|/var/log'; echo ":###:${PIPESTATUS[0]}")"
		elif [ "$PACKET_MANAGER" = "zypper" ]; then
			FILES="$(rpm -ql "$PACKAGE" | grep -v -E 'share/(doc|man)|/var/run|/var/log' | grep -v share/man; 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}')
		[ "x$LPRET" != "x0" ] && pwarning "dpkg/rpm exited with code '$LPRET' for packet ${PACKAGE}." && return 1
		[ -z "$FILES" ] && pwarning "list_packet_files empty for packet ${PACKAGE}."
		for FILE in $FILES; do
			[ ! -d "$FILE" ] && echo "$FILE"
		done
	done
}
#
# install all dependencies of a module
# goes through all package as given in the variable REQUIRED_DEPENDENCIES
install_dependencies() {
	[ -z "$REQUIRED_DEPENDENCIES" ] && return
	if [ "$PACKET_MANAGER" = "apt" ]; then
		apt-get install -y $REQUIRED_DEPENDENCIES || perror "Could not apt-get install $REQUIRED_DEPENDENCIES"
	elif [ "$PACKET_MANAGER" = "zypper" ]; then
		zypper --no-refresh install -y -n $REQUIRED_DEPENDENCIES || perror "Could not zypper install $REQUIRED_DEPENDENCIES"
	fi
}
#
# 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}"
}


######################################################################################################################
#
# 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 -
        pinfo "Created initramfs of $1 at $3"
}

# 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"
}