s243a

sandbox.awk (4)

Jan 27th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 3.97 KB | None | 0 0
  1. #!/usr/bin/gawk -f
  2.     function remove_parentheses(s1,s){
  3.       s=s1
  4.       sub(/^[(]/,"",s)
  5.       sub(/[)]$/,"",s)
  6.       return
  7.     }
  8.     function remove_quotes(s1,s){
  9.       s=s1
  10.       sub(/^["']/,"",s)
  11.       sub(/["']$/,"",s)
  12.       return
  13.     }
  14.     function mount(MNT_PT,MNT_PATH){
  15.       system("mkdir -p " MNT_PT)
  16.       system("mount " MNT_PATH " " MNT_PT 2>/dev/null)
  17.     }
  18.     function mount_if_valid(MNT_PT,MNT_PATH,PROOT,PDRV                  ,F_MNT_PATH,D_MNT_PATH){
  19.           if (system("[ -f " MNT_PATH " ] && exit 0 || exit 1") == 0){
  20.             F_MNT_PATH=MNT_PATH
  21.           } else if (system("[ -f " PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0){
  22.            F_MNT_PATH=PROOT "/" MNT_PATH
  23.          } else if (system("[ -f " PDRV "/" PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0){
  24.            F_MNT_PATH=PROOT "/" MNT_PATH
  25.           } else if (system("[ -d " MNT_PATH " ] && exit 0 || exit 1") == 0){
  26.             D_MNT_PATH=MNT_PATH
  27.          } else if (system("[ -d " PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0){
  28.            D_MNT_PATH=PROOT "/" MNT_PATH
  29.          } else if (system("[ -d " PDRV "/" PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0 ){
  30.            D_MNT_PATH=PROOT "/" MNT_PATH
  31.          } else {
  32.            F_MNT_PATH=""
  33.            D_MNT_PATH=""
  34.         }
  35.         if (length(F_MNT_PATH)>0){
  36.             mount(MNT_PT,F_MNT_PATH)
  37.             if (system("cat /proc/mounts | grep -c -m1 " MNT_PT) > 0){
  38.               result=sucess }
  39.             else {
  40.               result=fail }
  41.         }
  42.         else if (length(D_MNT_PATH)>0) {
  43.            result=directory
  44.         } else {
  45.           result=fail
  46.         }
  47.         return result    
  48.     }
  49.     BEGIN {OFS=" "}
  50.     /^\s*$/ {next}
  51.     /==mount==/ { mode=1 }
  52.     /==losetup==/ { mode=2 }
  53.     /==branches==/ { mode=3 }
  54.     !/^[\s]*$/ {
  55.     if (mode == 1) {
  56.       # get list of mount points, types, and devices - index is $3 (mount points)
  57.       mountdev[$2]=$1
  58.       mounttypes[$2]=$3
  59.      
  60.       field2[$2]=$3
  61.     } else if (mode == 2) {
  62.       # get list of loop devices and files - index is $1 (loop devs)
  63.       sub(/:/,"",$1)
  64.       loopdev_full[$1]=remove_parentheses($3)
  65.       sub(/.*[/]/,"",$3); sub(/)/,"",$3)
  66.       loopdev[$1]=$3
  67.     } else if (mode == 3) {
  68.       # map mount types to loop files if mount devices is a loop
  69.       for (m in mountdev) {
  70.         if ( loopdev[mountdev[m]] != "" ){
  71.              BNAME=loopdev[mountdev[m]]
  72.              sub(/.*[/]/,"",BNAME)    
  73.              field2[m]=BNAME
  74.              mountpath[m]=loopdev_full[mountdev[m]]
  75.              system("echo 'BNAME=" BNAME " m=" m " mountpath[m]=" mountpath[m] "' > /dev/stderr")
  76.         }
  77.        
  78.       }
  79.       # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  80.       mode=4
  81.     } else if (mode==4) {
  82.       $1=remove_quotes($1)
  83.       # print the branches and its mappings
  84.       system( "echo 'AWK (mode==4):" $0 "' >/dev/stderr" )      
  85.       if ( !($1 in field2)){
  86.         MNT_PT=$1
  87.         MNT_PATH=$2
  88.         if (length(MNT_PATH)==0){
  89.           MNT_PATH=$1
  90.         }
  91.         mountpath[$1]=MNT_PATH
  92.        
  93.         if (length(MNT_PATH) ==0){
  94.            next
  95.         }
  96.         mnt_status=mount_if_valid(MNT_PT,MNT_PATH,PDRV)
  97.         if(mnt_status == "fail"){
  98.             next
  99.         }
  100.         BNAME=MNT_PATH
  101.         sub(/.*[/]/,"",BNAME)    
  102.          field2[MNT_PT]=BNAME
  103.       }
  104.       if (NF>2){
  105.          STATE=$3
  106.       } else {
  107.            STATE="on"
  108.       }      
  109.       if ( STATE !~ /(on|off)/){
  110.           STATE="on"
  111.       }        
  112.       start=length($1)-MAX_STR_LEN
  113.       if (start<1) start=1
  114.       #field1[$1]=substr($1,start)
  115.       field1[$1]=$1
  116.         #out1=field1[$1] " " field2[$1] " " STATE
  117.         #out2=$1 " " mounttypes[$1] " " mountpath[$1]
  118.         #print out1
  119.         #print out2 > OUTFILE
  120.       if (length(mounttypes[$1]) ==0 ){
  121.           mounttypes[$1]="none"
  122.       }
  123.       print field1[$1],field2[$1],STATE
  124.       print field1[$1],field2[$1],"off",$1,mounttypes[$1],mountpath[$1] > OUTFILE
  125.     }
  126.   }
Add Comment
Please, Sign In to add comment