Advertisement
s243a

trim_puppy_functions.sh (v2-1)

Mar 29th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.56 KB | None | 0 0
  1. unmount_vfs(){
  2.  umount -l $curdir/$rootfs/dev 2>/dev/null
  3.  umount -l $curdir/$rootfs/sys 2>/dev/null
  4.  umount -l $curdir/$rootfs/proc 2>/dev/null
  5.  #umount /mnt/wktaz 2>/dev/null
  6.  #umount /mnt/wksfs 2>/dev/null
  7.  close_file_descriptors
  8.  
  9. }  
  10. close_file_descriptors(){
  11.   exec 10>&-
  12. }
  13. delete_petspec_fm_file(){
  14.  
  15.   local field_val=$1
  16.   local pkg_rootfs=${2:-"$s_rootfs"}
  17.   local spec_list_name=${3:-"woof-installed-packages"}
  18.   local field=${4:-1} #The first field is the default  
  19.   #TODO if prk_rootfs="/" or "" give warning
  20.   AWK_PRG="BEGIN{FS=\"|\"}
  21.  {
  22.    if ( \$$field != \"$field_val\" ) {
  23.     print
  24.    }
  25.  }"
  26.   #TODO we probably need logic to expand wildards
  27.   local file_spec_list=( "$pkg_rootfs/root/.packages/$spec_list_name" )
  28.  
  29.   for spec_list in "${file_spec_list[@]}"; do
  30.     spec_name=`basename "$spec_list"`
  31.     cat "$spec_list" | awk "$AWK_PRG"  >> "/tmp/trim_puppy/$spec_name"
  32.     echo "about to"
  33.     echo "rm \"$spec_list\"
  34.    mv  \"/tmp/trim_puppy/$spec_name\" \"$spec_list\"
  35.    "
  36.     read -p "Press enter to continue"
  37.     set -x    
  38.     rm "$spec_list"
  39.     mv  "/tmp/trim_puppy/$spec_name" "$spec_list"
  40.     set +x    
  41.     read -p "Press enter to continue"
  42.   done
  43.  
  44. }
  45.  
  46. inst_builtin_fm_dir(){
  47.   local source_dir=$1
  48.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  49.   if [ "$dest_root" = "/" ]; then
  50.      echo "warning about to install package into system rootfs"
  51.      read -p "Press enter to continue"
  52.   fi
  53.  
  54.   local pkg_name=${3:-`basename $1`} #TODO add logic for different location of files.list
  55.  
  56.   source_file_list_prefixed="$source_dir/root/.packages/builtin_files/$pkg_name"
  57.   dest_file_list_prefixed="$dest_root/root/.packages/builtin_files/$pkg_name"
  58.   local spec_list_name=${4:-"woof-installed-packages"}
  59.   local file_spec_list_path="$dest_root/root/.packages/$spec_list_name"
  60.   echo "about to:"
  61.   echo "  cp --remove-destination -arf $source_dir/*
  62.                                 $dest_root/ 2>/dev/null"
  63.   read -p "Press enter to continue"
  64.   cp --remove-destination -arf $source_dir/* \
  65.                                  $dest_root/ 2>/dev/null
  66.   if [ ! -f "$source_file_list_prefixed" ]; then
  67.      cd "$source_dir"
  68.      mkdir -p `dirname "$dest_file_list_prefixed"`
  69.      find . -mindepth 1 -name '*' > "$dest_file_list_prefixed"
  70.   fi
  71.   if [ ! -f "$source_dir/pet.specs" ]; then
  72.      cd "source_dir"
  73.      
  74.      
  75.      dir2pet "$pkg_name"
  76.   fi  
  77.  
  78.   #match_cnt=$( grep -cF "$spec" "$inst_pkg_specs_target" )
  79.   #if [ $match_cnt -eq 0 ]; then
  80.   #  spec >> "$inst_pkg_specs_target"
  81.   #fi
  82.   if [ -f "$source_dir/pet.specs" ]; then
  83.     #All args here except the first are default
  84.     echo "about to:"
  85.     echo "  delete_petspec_fm_file \"$pkg_name\" \"$dest_root\" \"$spec_list_name\" \"1\""
  86.     read -p "Press enter to continue"
  87.     delete_petspec_fm_file "$pkg_name" "$dest_root" "$spec_list_name" "1"
  88.     cat "$source_dir/pet.specs" >> "$file_spec_list_path"
  89.   fi
  90. }
  91. copy_pet_specs(){
  92.   AWK_PRG="BEGIN{FS=\"|\"}
  93.  {
  94.    if ( \$2 == \"$app\" ) {
  95.     print
  96.    }
  97.  }"
  98.   file_spec_list=( "$rootfs_full/root/.packages/"*"-installed-packages" )
  99.   #cat "${file_spec_list[@]}" | awk -F'|' "$AWK_PRG" #'{print $2;}'
  100.   spec=$( cat "${file_spec_list[@]}" | awk "$AWK_PRG"  >> "$inst_pkg_specs_target" )
  101.   match_cnt=$( grep -cF "$spec" "$inst_pkg_specs_target" )
  102.   if [ $match_cnt -eq 0 ]; then
  103.     spec >> "$inst_pkg_specs_target"
  104.   fi
  105. }
  106. copy_built_in(){
  107.   set -x
  108.   app=$1
  109.   file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  110.   if [ ! -f "$file_list" ] && [ $retry -eq 1 ]; then
  111.     file_list="$alt_s_rootfs/root/.packages/builtin_files/$app"
  112.     using_alt_list=1
  113.   else
  114.     using_alt_list=1=0
  115.   fi
  116.   if [ "${target:0:1}" = "/" ]; then
  117.     target_root="$target"      
  118.   else
  119.     target_root="$curdir/$target"    
  120.   fi  
  121.   target_file_list="$target_root/root/.packages/builtin_files/$app"  
  122.   echo "file_list=$file_list"
  123.   mkdir -p /tmp/trim_puppy
  124.   exec 10<> /tmp/trim_puppy/fd_10
  125.   subdir="/"
  126.   mkdir -p /tmp/trim_puppy/
  127.   rm "/tmp/trim_puppy/$app"
  128.   touch "/tmp/trim_puppy/$app"
  129.  
  130.   #mkdir -p
  131.   inst_pkg_specs_target="$target_root/root/.packages/"$arr_name"-installed-packages"
  132.   mkdir -p `dirname "$inst_pkg_specs_target"`
  133.   copy_pet_specs
  134.  
  135.   while IFS=$'\n' read  -r -d $'\n' -u10 line ; do
  136.        line=`echo "$line" | tr -d '[:space:]'`
  137.        if [ ! "${line:0:1}" = "/" ]; then
  138.            line=$subdir$line
  139.        fi
  140.        echo $line>>"/tmp/trim_puppy/$app"
  141.        if [ "${target:0:1}" = "/" ]; then
  142.          target_prefixed="$target$line"
  143.          #target_root="$target" #Defined above      
  144.        else
  145.          #target_root="$curdir/$target" #Defined above
  146.          target_prefixed="$curdir/$target$line"      
  147.        fi  
  148.  
  149.        if [ "${s_rootfs:0:1}" = "/" ]; then
  150.          source_prefixed="$s_rootfs$line"
  151.          cd "$s_rootfs" #this is necessary for the cpio command below    
  152.        else
  153.          source_prefixed="$curdir/$s_rootfs$line"    
  154.          cd "$curdir/$s_rootfs"  #this is necessary for the cpio command below
  155.        fi  
  156.  
  157.     if [ -d "$source_prefixed" ]; then
  158.        subdir="$line"
  159.        echo ".$line" | cpio -pd "$target_root"
  160.        subdir=${subdir%/}/
  161.     else
  162.        target_dir=`dirname $target_prefixed`
  163.        if [ ! -d "$target_dir" ]; then
  164.         source_dir=`dirname $line`
  165.         echo ".$source_dir" | cpio -pd "$target_root"      
  166.        fi
  167.        if [ ! -f "$source_prefixed" ] && [ $retry -eq 1 ]; then
  168.            source_prefixed="$alt_s_rootfs$line"
  169.            cp -a -u "$source_prefixed" "$target_prefixed"
  170.        elif [ ! "$arr_action" = "mv" ]; then
  171.          cp -a -u "$source_prefixed" "$target_prefixed"
  172.        else
  173.          mv -uf "$source_prefixed" "$target_prefixed"
  174.          if [ -f "$source_prefixed" ]; then
  175.            rm "$source_prefixed"
  176.          fi
  177.        fi
  178.     fi
  179.    
  180.   done 10< <( cat "$file_list" )
  181.   exec 10>&-
  182.   set +x
  183.   if [ ! "$arr_action" = "mv" ]; then
  184.     mkdir -p `dirname "$target_file_list"`
  185.     cp -a -u "$file_list" "$target_file_list"
  186.   else
  187.     mv -uf "$file_list" "$target_file_list"
  188.     sed -i "\%|${app}|%d" "$curdir/$s_rootfs/root/.packages/"*"-installed-packages"
  189.     if [ -f "$file_list" ]; then
  190.       rm "$file_list"
  191.     fi
  192.   fi
  193. }
  194. dissect_rootfs(){
  195.   #arr_names=${1:-ALL_ArrNames}
  196.   arry_names_name=${1:-ALL_ArrNames}
  197.   for arr_name in $(eval 'echo "${'$arry_names_name'[@]}"'); do
  198.     eval "arr=( \"\${"$arr_name"[@]}\" )"
  199.     eval "arr_action=\$"$arr_name"_action"
  200.     arr_action=${arr_action:-"$action"}
  201.     #Move is faster but perhaps copy is safer
  202.     arr_action=${arr_action:-cp}
  203.     set -x
  204.     eval 'target="$'$arr_name'_target"'
  205.     target=${target:-"$curdir/$arr_name"}
  206.     set +x
  207.     if [ "${s_rootfs:0:1}" = "/" ]; then
  208.       s_rootfs_prefixed="$s_rootfs"    
  209.     else
  210.       s_rootfs_prefixed="$curdir/$s_rootfs"    
  211.     fi  
  212.     for app in "${arr[@]}"; do
  213.       echo "arr_action=$arr_action"
  214.       case "$arr_action" in
  215.       cp|mv|retry)  
  216.           echo "app=$app"
  217.           copy_built_in "$app"        
  218.           ;;
  219.       #mv)
  220.       #    move_built_in()
  221.       #;;
  222.       #pet) #We might also want to convert to other package formats
  223.       #    mk_pet_fm_built_in()
  224.       #;;
  225.       esac
  226.       if [ -f "/tmp/trim_puppy/$app" ] && [ $chroot_remove_builtin -eq 1 ]; then
  227.         file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  228.         rm "$file_list"
  229.         cp "/tmp/trim_puppy/$app" "$file_list"
  230.       fi
  231.       #chroot "$s_rootfs_prefixed" remove_builtin "$app"
  232.     done
  233.   done
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement