#!/bin/bash ############################################################################### # Verify preload file meets the necessary requirements for preloading an app # # Copyright 2012 Nintendo. All rights reserved. # # These coded instructions, statements, and computer programs contain # proprietary information of Nintendo of America Inc. and/or Nintendo # Company Ltd., and are protected by Federal copyright law. They may # not be disclosed to third parties or copied or duplicated in any form, # in whole or in part, without the prior written consent of Nintendo. ############################################################################### ############################################################################### # Defines ############################################################################### # 20 megabyte cap MEMORY_CAP=$((20*1024*1024)) PRELOAD_FILE="preload.txt" ############################################################################### # Functions ############################################################################### function display_help { echo -e "Preload File Verification\n" echo -e "This script verifies that the $PRELOAD_FILE file meets"\ "all necessary requirements for preloading an application.\n" echo -e "Usage: preloadcheck.sh [options] [optional path to code folder]\n " echo -e "Options:\n"\ " -h : (help) display options\n"\ " -g : generate preload file" } function check_file_exists_silent { if [ ! -f $working_dir$1 ] then return 1 fi return 0 } function check_file_exists { if ! check_file_exists_silent $1 then echo "Error: Unable to locate the file '$1'" let error_count++ return 1 fi return 0 } function check_text { local text=(`cat $working_dir$2 | tr -cs '[:upper:][:lower:][:punct:][:digit:]' '[\n*]' | grep $1`) if [ ${#text[@]} -eq 1 ]; then return 0; fi if [ ${#text[@]} -eq 0 ] then echo "Error: '$1' not found in the file '$2'" else echo "Error: Multiple instances of '$1' found in the file '$2'" fi let error_count++ return 1 } function get_filesize { if ! check_file_exists_silent $1 then echo 0 else echo `stat -c %s $working_dir$1` fi } function get_content_size { local size=0 preload_contents=($(cat $working_dir$PRELOAD_FILE | tr '\r' ' ')) for i in "${preload_contents[@]}" do let size+=$(get_filesize $i) done echo $size } function append_text { echo -n $1' ' >> $working_dir$2 } ############################################################################### # Main ############################################################################### relative_dir="" working_dir=$PWD generate_file=0 # Process command line arguments for var in "$@" do case "$var" in -h) display_help exit ;; -g) generate_file=1 ;; *) # Relative dir must not start with forward slash, but must # end with one relative_dir=$var [[ $relative_dir = /* ]] && relative_dir=`echo $relative_dir | sed 's/^.//'` [[ $relative_dir != */ ]] && relative_dir="$relative_dir"/ working_dir=$working_dir/$relative_dir break ;; esac done # Ensure working_dir ends with forward slash [[ $working_dir != */ ]] && working_dir="$working_dir"/ if [ $generate_file -eq 1 ] then if check_file_exists_silent $PRELOAD_FILE then echo "Error: '$PRELOAD_FILE' already exists in the directory" exit fi # Create the preload file touch $working_dir$PRELOAD_FILE else if ! check_file_exists $PRELOAD_FILE then exit fi fi error_count=0 # Check that the cos/app xml files exist and are listed in preload if check_file_exists "cos.xml" then if [ $generate_file -eq 0 ] then check_text "cos.xml" $PRELOAD_FILE else append_text "cos.xml" $PRELOAD_FILE fi fi if check_file_exists "app.xml" then if [ $generate_file -eq 0 ] then check_text "app.xml" $PRELOAD_FILE else append_text "app.xml" $PRELOAD_FILE fi fi # If the preload file was generated, get the name of the rpx and add it if [ $generate_file -eq 1 ] then rpx_file=(`find $working_dir -maxdepth 1 -name '*.rpx'`) if [ ${#rpx_file[@]} -eq 1 ] then rpx_file=`basename $rpx_file` file_size=$(get_filesize $rpx_file) if [ $file_size -gt $MEMORY_CAP ] then echo "Warning: '$rpx_file' exceeds the maximum memory size of"\ "$MEMORY_CAP bytes, and will not be added to '$PRELOAD_FILE'" else append_text $rpx_file $PRELOAD_FILE fi else if [ ${#rpx_file[@]} -eq 0 ] then echo "Error: No rpx file exists in the directory" else echo "Error: Multiple rpx files exist in the directory."\ "Unabled to determine which one to add to '$PRELOAD_FILE'" fi let error_count++ unset rpx_file fi else # Retieve the name of the rpx from the preload file rpx_file=(`cat $working_dir$PRELOAD_FILE | tr -cs '[:upper:][:lower:][:punct:][:digit:]' '[\n*]' | grep '\.rpx'`) if [ ${#rpx_file[@]} -eq 1 ] then if ! check_file_exists $rpx_file then unset rpx_file fi else if [ ${#rpx_file[@]} -eq 0 ] then echo "Warning: No rpx file specified in '$PRELOAD_FILE'" # Attempt to retrieve the rpx name from the directory rpx_file=(`find $working_dir -maxdepth 1 -name '*.rpx'`) if [ ${#rpx_file[@]} -eq 1 ] then rpx_file=`basename $rpx_file` fi else echo "Error: Multiple rpx files specified in '$PRELOAD_FILE'" let error_count++ unset rpx_file fi fi fi if [ $generate_file -eq 1 ] then content_size=$(get_content_size) else # Verify that all listed rpls exist in the directory rpl_file=(`cat $working_dir$PRELOAD_FILE | tr -cs '[:upper:][:lower:][:punct:][:digit:]' '[\n*]' | grep '\.rpl'`) for i in "${rpl_file[@]}" do check_file_exists $i done fi # Use dumprpl to return all statically bound rpls and verify that they # exist in the directory and are listed in the preload file. This is a # recurssive process as each rpl may have others statically bound to it rpl_search=($rpx_file) while [ ${#rpl_search[@]} -ne 0 ] do for i in $rpl_search do rpl_temp=("${rpl_temp[@]}" `dumprpl -imports $relative_dir$i | grep 'Imports' |\ tr -cs '[:upper:][:lower:][:punct:][:digit:]' '[\n*]' |\ grep -E 'fimport_\w*|dimport_\w*' | cut -c10- | cut -f1 -d "," | sort -u`) done unset rpl_search for i in "${rpl_temp[@]}" do i=$i".rpl" # Only concerned with rpls that exist in the directory if ! check_file_exists_silent $i; then continue; fi # Check that the rpl is listed in the preload file if [ $generate_file -eq 0 ] then check_text $i $PRELOAD_FILE else file_size=$(get_filesize $i) if [ $(($content_size+$file_size)) -gt $MEMORY_CAP ] then echo "Warning: Unable to add $i to '$PRELOAD_FILE';"\ " maximum content size exceeded" else append_text $i $PRELOAD_FILE content_size=$(($content_size+$file_size)) fi fi # Add any rpls that have not already been dumped to the search list match=$(echo "$rpl_files:0}" | grep -o $i) [[ -z $match ]] && rpl_files=("${rpl_files[@]}" $i) \ && rpl_search=("${rpl_search[@]}" $i) done unset rpl_temp done # Check that total size of all files in the preload file does not exceed max content_size=$(get_content_size) if [ $content_size -gt $MEMORY_CAP ] then echo "Error: The contents of '$PRELOAD_FILE' is $content_size bytes which"\ "exceeds the maximum allowed size of $MEMORY_CAP bytes" let error_count++ fi if [ $error_count -eq 0 ] then echo -e "No errors detected." else echo -e "\nErrors were encountered. Pleae check that $PRELOAD_FILE meets"\ "all necessary\nrequirements." fi