SHARE
TWEET

mv_or_copy_file.sh

s243a Mar 12th, 2019 (edited) 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #trap remove_fille_descriptpors EXIT
  2. #trap remove_fille_descriptpors SIGKILL
  3. #trap remove_fille_descriptpors SIGTERM
  4.  
  5. remove_fille_descriptpors(){
  6. exec 9>&-
  7. exec 10>&-
  8. exec 11>&-
  9. umount /mnt/wksfs #2>&1 #| xargs -0 echo ) || echo "umount error"
  10. }
  11.  
  12. set_source_dest_ops(){
  13.    ssdo_pos_args=()
  14.    ssdo_pos_arg_count=0
  15.    ssdo_arg_count=0
  16.    for i in "$@"; do
  17.    echo "i=$i"
  18.    case $i in
  19.    -p1=*|--source-prefix=*)
  20.       ssdo_s_prefix=${i#*=}; ;;
  21.    -p2=*|--destination-prefix)
  22.       ssdo_d_prefix=${i#*=}; ;;
  23.    --)
  24.       arg_count+=1
  25.       break; ;;
  26.    -*|--*)
  27.       ssdo_flags+=( "$i" ); ;;
  28.    *)
  29.       ssdo_pos_args+=( "$i" )
  30.       ssdo_post_arg_count+=1
  31.       ;;
  32.    esac
  33.       arg_count+=1
  34.    done
  35.    ssdo_curdir=${curdir:-`pwd`}
  36.    s_prefix=${s_prefix:-$curdir}
  37.    d_prefix=${d_prefix:-$s_prefix}
  38.    ssdo_tail_args=( ${@:arg_count:} )  #TODO should we quote this?
  39. }  
  40. set_move_prefix(){
  41.    smp_pos_args=()
  42.    smp_pos_arg_count=0
  43.    smp_arg_count=0
  44.    for i in "$@"; do
  45.    case $i in
  46.    -m=*|--move-prefix=*)
  47.       move_prefix="${i#*=}"; ;;
  48.    --)
  49.       arg_count+=1
  50.       break; ;;
  51.    -*|--*)
  52.       smp_flags+=( "$i" ); ;;
  53.    *)
  54.       smp_pos_args+=( "$i" )
  55.       smp_pos_arg_count+=1
  56.       ;;
  57.    esac
  58.       arg_count+=1
  59.    done
  60.    if [ -z "$move_prefix" ]; then
  61.      echo "ussage: set_move_prefix move_prefix"
  62.    fi  
  63. }
  64. set_full_paths(){
  65.    if [ -z "$1" ] || [ -z "$2" ]; then
  66.      echo "ussage: mv_files_within_curdir fm_folder to_folder"
  67.      return 1 #Anthing other than 0 is an error.          
  68.    fi
  69.    s_folder=${1}
  70.    d_folder=${2}
  71.    m_folder=${3}
  72.    if [ "${s_folder:0:1}" = "/" ]; then
  73.      s_folder_fullpath=$s_folder
  74.      s_prefix=$s_folder_fullpath
  75.      s_sub_folder="/"
  76.    elif [ "${s_folder:0:1}" == "." ]; then
  77.      if [ -z "$s_prefix" ]; then
  78.        s_prefix=`cwd`
  79.      fi
  80.      s_folder_fullpath=$s_prefix/${s_folder:2:}
  81.      s_prefix=$s_folder_fullpath
  82.      s_sub_folder=${s_folder:1:}  
  83.    else
  84.      s_folder_fullpath=$s_prefix/$s_folder
  85.      s_sub_folder=/$s_folder      
  86.    fi
  87.    if [ "${d_folder:0:1}" = "/" ]; then
  88.      d_folder_fullpath=$d_folder
  89.      d_prefix=$d_folder_fullpath
  90.      d_sub_folder="/"
  91.    elif [ "${d_folder:0:1}" = "." ]; then
  92.      if [ -z "$s_prefix" ]; then
  93.        d_prefix="`pwd`"
  94.      fi    
  95.      d_folder_fullpath=$d_prefix/${d_folder:2:}
  96.      d_prefix=$d_folder_fullpath
  97.      d_sub_folder=${d_folder:1:}      
  98.    else
  99.      d_folder_fullpath=$d_prefix/$d_folder      
  100.      s_sub_folder=/$s_folder  
  101.    fi
  102.    if [ ! -z "$m_folder" ]; then
  103.      if [ "${m_folder:0:1}" = "/" ]; then
  104.        m_folder_fullpath="$m_folder"
  105.        m_prefix="$m_folder_fullpath"
  106.        m_sub_folder="/"      
  107.      elif [ "${m_folder:0:1}" = "." ]; then
  108.        if [ -z "$m_prefix" ]; then
  109.          m_prefix="`pwd`"
  110.        fi
  111.        m_folder_fullpath=$m_prefix/${m_folder:2:}
  112.        m_prefix=$m_folder_fullpath
  113.        m_sub_folder=${d_folder:1:}        
  114.      else
  115.        m_folder_fullpath=$m_prefix/$m_folder
  116.        m_sub_folder=${m_folder:1:}              
  117.      fi  
  118.    fi
  119. }  
  120. mv_or_copy_files(){
  121.    bla=$@
  122.    echo "======================================"
  123.    set_source_dest_ops ${bla[@]}
  124.    #eval "set ${pos_args[@]}" # the variable pos_args is set in set_source_dest_ops
  125.    echo "======================================"
  126.    set_full_paths ${ssdo_pos_args[@]}  
  127.    #set_move_prefix ${ssdo_flags[@]}
  128.    echo "======================================"
  129.    #set -x
  130.    if [ -d "$s_folder_fullpath" ] ; then
  131.      
  132.       #sed -e "s#$xcurdir\/kernel-modules\/lib\/#\/lib\/#g"  | \ sdfdsf
  133.       #rm /tmp/foo_10
  134.       exec 10<> /tmp/foo_10
  135.       #( IFS=; find $s_folder_fullpath -print0 -type f -name "*" 1>&10 )
  136.       echo "find $s_folder_fullpath  -type f -name "'*'" "
  137.         #read -p "Press enter to continue" <&1
  138.       #( find $s_folder_fullpath  -type l -name "*" 1>&10 )
  139.    echo "--------------------------------------"      
  140. #      while IFS= read  -r -d $'\0' fm_filePath_prefixed <&10 ; do #IFS= read -r -d ''
  141.        while IFS=$'\0' read  -r -d $'\0' -u10 fm_filePath_prefixed ; do
  142.         #set -x
  143.         echo "===== mv_or_copy_files(); fm_filePath_prefixed=$fm_filePath_prefixed===="
  144.         echo "fm_filePath_prefixed=$fm_filePath_prefixed"
  145.         echo "${fm_filePath_prefixed[@]}"
  146.         echo "s_prefix=$s_prefix"
  147.         fm_filePath=${fm_filePath_prefixed#"$s_prefix"}
  148.         to_filePath_prefixed=$d_prefix$fm_filePath
  149.         move_filePath_prefixed=$m_prefix$fm_filePath
  150.         #set +x
  151.         #read -p "Press enter to continue" <&1
  152.        
  153.         if [ -d "$to_filePath_prefixed" ]; then
  154.           set -x
  155.           mkdir -p "$to_filePath_prefixed" #added by s243a 18-03-12
  156.           set +x
  157.         else
  158.           mkdir -p "`dirname  $to_filePath_prefixed`" # Moved from within velow if statment (s243a)
  159.           if [ ! -z "$m_folder" ] && [ -f "$move_filePath_prefixed" ]; then
  160.             if [ ! -f "$to_filePath_prefixed" ]; then
  161.               #mkdir -p `dirname  $to_filePath_prefixed` #Moved outside of if statment (s243 18-03-12)
  162.               mv "$move_filePath_prefixed" "$to_filePath_prefixed"
  163.             else
  164.               set -x
  165.               cp -a -u  "$fm_filePath_prefixed" "$to_filePath_prefixed"
  166.               rm "$fm_filePath_prefixed"
  167.               set +x
  168.             fi
  169.           else
  170.               set -x
  171.               mkdir -p "`dirname $to_filePath_prefixed`"
  172.               cp -a -u  "$fm_filePath_prefixed" "$to_filePath_prefixed"  
  173.               set +x    
  174.           fi
  175.         fi       
  176.       done 10< <( find "$s_folder_fullpath" -name '*' -print0 ) #Removed -type f option (s243a 18-03-12)
  177.       #https://blog.famzah.net/2016/10/20/bash-process-null-terminated-results-piped-from-external-commands/
  178.       exec 10>&-
  179.    fi
  180.    #set +x
  181. }
  182. apply_white_out_files(){
  183.    bla=$@
  184.    #echo "====LN#170===apply_white_out_files===="
  185.    set_source_dest_ops ${bla[@]}
  186.    #eval "set ${pos_args[@]}" # the variable pos_args is set in set_source_dest_ops
  187.    #echo "====LN#173===apply_white_out_files===="
  188.    set_full_paths ${ssdo_pos_args[@]}  
  189.    #set_move_prefix ${ssdo_flags[@]}
  190.    #echo "====LN#176===apply_white_out_files===="    
  191.    
  192.    echo "s_folder_fullpath=$s_folder_fullpath"
  193.    echo "s_prefix=$s_prefix"
  194.    echo "d_prefix=$d_prefix"
  195.    #read -p "Press enter to continue" <&1
  196.    set -x  
  197.    exec 11<> /tmp/mv_or_copy_files/fd_11
  198.    while IFS=$'\0' read  -r -d $'\0' -u11 fm_filePath_prefixed ; do
  199.      wh_fname=`basename "$fm_filePath_prefixed"`
  200.      echo "===== apply_white_out_files(); fm_filePath_prefixed=$fm_filePath_prefixed===="
  201.      set -x
  202.      case "$wh_fname" in
  203.      '.wh..wh..opq')
  204.        fm_dirPath_prefixed=`dirname "$fm_filePath_prefixed"`
  205.        fm_dirPath=${fm_dirPath_prefixed#"$s_prefix"}
  206.        to_dirPath_prefixed=$d_prefix$fm_dirPath
  207.  
  208.        rm -rf "$to_dirPath_prefixed"
  209.        cp -r "$fm_dirPath_prefixed" "$to_dirPath_prefixed"
  210.        rm "$to_dirPath_prefixed/$wh_fname"                
  211.        #read -p "Press enter to continue" <&1
  212.        ;;
  213.      '.wh..wh.plnk'|'.wh..wh.aufs') #https://unix.stackexchange.com/questions/92287/aufs-whiteout-removal
  214.        fm_dirPath_prefixed=`dirname "$fm_filePath_prefixed"`
  215.        fm_dirPath=${fm_dirPath_prefixed#"$s_prefix"}
  216.        to_dirPath_prefixed=$d_prefix$fm_dirPath
  217.        rm "$to_dirPath_prefixed/$wh_fname"
  218.        #read -p "Press enter to continue" <&1
  219.        ;;
  220.      *)
  221.        fm_dirPath_prefixed=`dirname "$fm_filePath_prefixed"`
  222.        fm_dirPath=${fm_dirPath_prefixed#"$s_prefix"}
  223.        to_dirPath_prefixed="$d_prefix$fm_dirPath"
  224.        fname="${wh_fname#.wh.}" #https://lwn.net/Articles/324291/
  225.        if [ -d "$to_dirPath_prefixed/$fname" ]; then
  226.          rm -rf "$to_dirPath_prefixed/$fname"  
  227.        else
  228.          rm "$to_dirPath_prefixed/$fname"
  229.        fi
  230.        rm "$to_dirPath_prefixed/$wh_fname"    
  231.        #read -p "Press enter to continue" <&1                    
  232.        ;;
  233.      esac
  234.      set +x
  235.            
  236.    done 11< <( find "$s_folder_fullpath" -type f -name '.wh*' -print0 ) #https://blog.famzah.net/2016/10/20/bash-process-null-terminated-results-piped-from-external-commands/
  237.       exec 11>&-
  238.    #set +x
  239. }
  240. merge_sfs_and_save_fils(){
  241.   local sfs_dir=$1
  242.   local target_dir=$2
  243.   local savedir=${3:-$sfs_dir/tazpupsave}
  244.  
  245.   mkdir -p "$target_dir"
  246.   mount_point=/mnt/wksfs
  247.   mkdir -p /mnt/wksfs
  248.  
  249.   #rm /tmp/foo_9
  250.   exec 9<> /tmp/mv_or_copy_files/fd_9
  251.   #set -x
  252.   ( find "$sfs_dir" -maxdepth 1 -type f -name "*.sfs" | \
  253.     grep -v -e "^$sfs_dir"/'f.*' | \
  254.     grep -v -e "^$sfs_dir"/'z.*' 1>&9 )
  255.   #set +x
  256.   #while read file1 <&9 ; do #https://stlocal variables bashackoverflow.com/questions/6911520/read-command-in-bash-script-is-being-skipped
  257.    while read -r -u9 file1 ; do
  258.     mount -v -t squashfs "$file1" "$mount_point" #2>&1 | xargs echo
  259.     #mount -t squashfs "$file1" /mnt/wksfs
  260.     echo "mount -t squashfs $file1 /mnt/wksfs"
  261.     read -p "Press enter to continue" <&1
  262.     if [ 1 -eq 1 ]; then
  263.       echo "mv_or_copy_files /mnt/wksfs $target_dir"
  264.       mv_or_copy_files /mnt/wksfs "$target_dir"
  265.     fi
  266.     echo "mounted $file1"
  267.     #read -p "Press enter to continue"
  268.  
  269.     umount /mnt/wksfs
  270.   done 9< <( find $sfs_dir -maxdepth 1 -type f -name "*.sfs" | \
  271.               grep -v -e "^$sfs_dir"/'f.*' | \
  272.               grep -v -e "^$sfs_dir"/'z.*' )
  273.   exec 9>&-
  274.   #read -p "Press enter to continue"
  275.   echo "mv_or_copy_files $savedir $target_dir"
  276.   mv_or_copy_files "$savedir" "$target_dir"
  277. }
  278. mkdir -p /tmp/mv_or_copy_files
  279. remove_fille_descriptpors
  280. trap remove_fille_descriptpors EXIT SIGKILL SIGTERM
  281. #trap remove_fille_descriptpors SIGKILL
  282. #trap remove_fille_descriptpors SIGTERM
  283. root=/initrd/mnt/dev_save/slacko64save
  284. sfs_dir=/mnt/sdb1/TazPup/64/a1
  285. target_dir=$root/tazpup-builder/slitaz-rootfs-new
  286. savedir=$sfs_dir/tazpupsave
  287.  
  288.  
  289. merge_sfs_and_save_fils "$sfs_dir" "$target_dir"
  290. apply_white_out_files "$savedir" "$target_dir"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top