Advertisement
s243a

trim_puppy_functions.sh

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