From 58fe9674814899056ed7e264fde3ccee8eec3210 Mon Sep 17 00:00:00 2001 From: hickert Date: Wed, 7 Sep 2005 09:05:28 +0000 Subject: [PATCH] Better deletion of partition entries disk, and partitiontables git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@1313 594d385d-05f5-0310-b6e9-bd551577e9d8 --- plugins/admin/FAI/class_faiManagement.inc | 2 +- plugins/admin/FAI/class_faiPartitionTable.inc | 171 +++++++++--- .../FAI/class_faiPartitionTableEntry.inc | 250 +++++++++++++----- 3 files changed, 324 insertions(+), 99 deletions(-) diff --git a/plugins/admin/FAI/class_faiManagement.inc b/plugins/admin/FAI/class_faiManagement.inc index 800964b6c..2738653e8 100644 --- a/plugins/admin/FAI/class_faiManagement.inc +++ b/plugins/admin/FAI/class_faiManagement.inc @@ -330,7 +330,7 @@ class faiManagement extends plugin $action = ""; - $action .= ""; + $action .= ""; /* Attach objects */ foreach($this->objects as $key => $value){ diff --git a/plugins/admin/FAI/class_faiPartitionTable.inc b/plugins/admin/FAI/class_faiPartitionTable.inc index 8d657ca60..277562087 100644 --- a/plugins/admin/FAI/class_faiPartitionTable.inc +++ b/plugins/admin/FAI/class_faiPartitionTable.inc @@ -37,6 +37,7 @@ class faiPartitionTable extends plugin $ldap->cd ($this->dn); $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*")); while($object = $ldap->fetch()){ + $this->disks[$object['cn'][0]]['status'] = "edited"; $this->disks[$object['cn'][0]]['dn'] = $object['dn']; $this->disks[$object['cn'][0]]['cn'] = $object['cn'][0]; if(isset($object['description'][0])){ @@ -65,6 +66,7 @@ class faiPartitionTable extends plugin } /* Append fetched partitions */ + $partition['status']="edited"; $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; } } @@ -76,13 +78,17 @@ class faiPartitionTable extends plugin /* Fill templating stuff */ $smarty= get_smarty(); $display= ""; - + /* Add Disk to this Partitionset * This code adds a new HDD to the disks * A new Dialog will be opened */ if(isset($_POST['AddDisk'])){ - $this->dialog = new faiPartitionTableEntry($this->config,$this->dn); + $usedDiskNames =array(); + foreach($this->disks as $key=>$disk){ + $usedDiskNames[]= $key; + } + $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); $this->is_dialog = true; } @@ -90,7 +96,13 @@ class faiPartitionTable extends plugin * Open dialog which allows us to edit the selected entry */ if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){ - $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$this->disks[$_POST['disks']]); + $usedDiskNames =array(); + foreach($this->disks as $key=>$disk){ + if($key != $_POST['disks']){ + $usedDiskNames[]= $key; + } + } + $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$_POST['disks']]); $this->is_dialog = true; } @@ -113,6 +125,23 @@ class faiPartitionTable extends plugin } }else{ $disk = $this->dialog->save(); + if(isset($disk['rename'])){ + if($this->disks[$disk['rename']['from']]['status']=="edited"){ + $this->disks[$disk['rename']['from']]['status']="delete"; + }else{ + unset($this->disks[$disk['rename']['from']]); + } + + foreach($disk['partitions'] as $key => $val){ + if($disk['partitions'][$key]['status']!="delete"){ + $disk['partitions'][$key]['status']= "new"; + } + } + + $disk['status']="new"; + $disk['cn']= $disk['rename']['to']; + } + $this->disks[$disk['cn']]=$disk; unset($this->dialog); $this->dialog = NULL; @@ -120,6 +149,28 @@ class faiPartitionTable extends plugin } } + /* Delete selected disk drive from list + * Assign delete status for all its partitions + */ + if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){ + $disk = $_POST['disks']; + if($this->disks[$disk]['status']=="edited"){ + $this->disks[$disk."-delete"]=$this->disks[$disk]; + unset($this->disks[$disk]); + $disk = $disk."-delete"; + $this->disks[$disk]['status']="delete"; + foreach($this->disks[$disk]['partitions'] as $name => $value ){ + if($value['status']=="edited"){ + $this->disks[$disk]['partitions'][$name]['status']="delete"; + }else{ + unset($this->disks[$disk]['partitions'][$name]); + } + } + }else{ + unset($this->disks[$disk]); + } + } + /* Display dialog if one is defined */ if(isset($this->dialog)){ @@ -151,11 +202,18 @@ class faiPartitionTable extends plugin */ $a_return = array(); foreach($this->disks as $key => $disk){ - $cnt = count( $disk['partitions']); - if(!empty($disk['description'])){ - $a_return[$key]= $disk['cn']." [".$disk['description']."] - "._("Number of Partitions")." :".$cnt; - }else{ - $a_return[$key]= $disk['cn']." - "._("Number of Partitions")." :".$cnt; + if($disk['status'] != "delete"){ + $cnt=0; + foreach($disk['partitions'] as $val){ + if($val['status']!="delete"){ + $cnt ++; + } + } + if(!empty($disk['description'])){ + $a_return[$key]= $disk['cn']." [".$disk['description']."] - "._("Number of Partitions")." :".$cnt; + }else{ + $a_return[$key]= $disk['cn']." - "._("Number of Partitions")." :".$cnt; + } } } return($a_return); @@ -203,6 +261,7 @@ class faiPartitionTable extends plugin /* Save to LDAP */ function save() { + plugin::save(); /* Save current settings. * 1 : We must save the partition table, with its description and cn @@ -210,36 +269,63 @@ class faiPartitionTable extends plugin * 3 : Save partitions for each disk */ - /* remove old entry */ $ldap = $this->config->get_ldap_link(); - $ldap->cat($this->dn); - if($ldap->count()){ - $ldap->cd ($this->dn); - $ldap->rmdir_recursive($this->dn); + + if($this->new){ + $ldap->cd($this->dn); + $ldap->create_missing_trees($this->dn); + $ldap->cd($this->dn); + $ldap->add($this->attrs); + show_ldap_error($ldap->get_error()); + }else{ + /* Add partition table to ldap + */ + $ldap->cd($this->dn); + $ldap->modify($this->attrs); show_ldap_error($ldap->get_error()); } - - /* Add partition table to ldap - */ - $ldap->cd($this->dn); - $ldap->create_missing_trees($this->dn); - $ldap->cd($this->dn); - $ldap->add($this->attrs); - show_ldap_error($ldap->get_error()); + + /* Sort entries, because we must delete entries with status="delete" first */ + $order = array(); + foreach($this->disks as $key => $disk){ + if($disk['status'] == "delete"){ + $order[$key] = $disk; + } + } + foreach($this->disks as $key => $disk){ + if($disk['status'] != "delete"){ + $order[$key] = $disk; + } + } + /* Append all disks to ldap */ - foreach($this->disks as $cn=>$disk){ - $disk_cn = "cn=".$cn.",".$this->dn; + foreach($order as $cn=>$disk){ + $disk_dn = "cn=".$disk['cn'].",".$this->dn; $disk_attrs['cn'] = $disk['cn']; $disk_attrs['description'] = $disk['description']; - $disk_attrs['objectClass']=array("top","FAIclass","FAIpartitionDisk"); + $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk"); + + if($disk['status'] == "delete"){ + $ldap->cd($disk_dn); + $ldap->rmdir_recursive($disk_dn); + }elseif($disk['status']== "edited"){ + $ldap->cd($disk_dn); + $ldap->modify($disk_attrs); + }elseif($disk['status']== "new"){ + $ldap->cd($disk_dn); + $ldap->create_missing_trees($disk_dn); + $ldap->cd($disk_dn); + $ldap->add($disk_attrs); + }else{ + print_red("unknown status while saving disks"); + } - $ldap->cd($disk_cn); - $ldap->create_missing_trees($disk_cn); - $ldap->cd($disk_cn); - $ldap->add($disk_attrs); - show_ldap_error($ldap->get_error()); + //print $disk_dn; + //print_a($disk_attrs); + show_ldap_error($ldap->get_error()); + if($disk['status']!="delete") /* Add all partitions */ foreach($disk['partitions'] as $key => $partition){ $partition_attrs = array(); @@ -251,14 +337,29 @@ class faiPartitionTable extends plugin } } - $partition_dn = "cn=".$partition_attrs['cn'].",".$disk_cn; + $partition_dn = "cn=".$partition_attrs['cn'].",".$disk_dn; $partition_attrs['objectClass'] = array("top","FAIclass","FAIpartitionEntry"); + + unset($partition_attrs['status']); + unset($partition_attrs['old_cn']); + + //print $partition_dn; + //print_a($partition); + //print_a($partition_attrs); - $ldap->cd($partition_dn); - $ldap->create_missing_trees($partition_dn); - $ldap->cd($partition_dn); - $ldap->add($partition_attrs); - show_ldap_error($ldap->get_error()); + if(($partition['status'] == "delete")&&($disk['status']!="new")){ + $ldap->cd($partition_dn); + $ldap->rmdir_recursive($partition_dn); + }elseif($partition['status'] == "new"){ + $ldap->cd($partition_dn); + $ldap->create_missing_trees($partition_dn); + $ldap->cd($partition_dn); + $ldap->add($partition_attrs); + }elseif($partition['status'] == "edited"){ + $ldap->cd($partition_dn); + $ldap->modify($partition_attrs); + } + show_ldap_error($ldap->get_error()); } } $this->handle_post_events("add"); diff --git a/plugins/admin/FAI/class_faiPartitionTableEntry.inc b/plugins/admin/FAI/class_faiPartitionTableEntry.inc index c652ee965..c56e71d95 100644 --- a/plugins/admin/FAI/class_faiPartitionTableEntry.inc +++ b/plugins/admin/FAI/class_faiPartitionTableEntry.inc @@ -12,36 +12,74 @@ class faiPartitionTableEntry extends plugin var $attributes = array("DISK_cn","DISK_description"); var $objectclasses= array(); - var $DISK_cn = ""; - var $DISK_description = ""; + var $DISK_cn = ""; + var $DISK_description = ""; var $partitions = array(); var $UsedAttrs = array(); var $AvailableDiskNames = array(); var $is_edit = false; var $old_cn = ""; + var $status = "new"; + var $deletePartitions = array(); - function faiPartitionTableEntry ($config, $dn= NULL,$disk=false) + function faiPartitionTableEntry ($config, $dn= NULL,$usedDiskNames=array(),$disk=false) { plugin::plugin ($config, $dn); $this->UsedAttrs = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize", "FAImountOptions","FAIfsOptions","FAIpartitionFlags"); + /* Generate available disknames, to avoid duplicates in cn */ $this->AvailableDiskNames = array(); foreach(array("a","b","c","d","e","f","g","h") as $ts){ - $this->AvailableDiskNames["hd".$ts]= "hd".$ts; + if(!in_array("hd".$ts,$usedDiskNames)){ + $this->AvailableDiskNames["hd".$ts]= "hd".$ts; + } } + /* Default status is new */ + $this->status = "new"; + + /* We want to edit an entry */ if($disk){ + + /* Set disk status */ + $this->status = $disk['status']; + + /* Walk through partitions */ foreach($disk['partitions'] as $name => $values){ - foreach($this->UsedAttrs as $attr){ - if(!isset($values[$attr])){ - $disk['partitions'][$name][$attr]=""; + + /* If a partition is already marked as delete, attach it to deletePartitions only. */ + if($values['status'] == "delete"){ + unset($disk['partitions'][$name]); + $this->deletePartitions[]=$values; + }else{ + + /* Set status, to know which partition must be deleted from ldap + new : Neu partition entry // save + edited : Update partition entry in ldap + deleted: Remove partition from ldap + */ + + /* If status is not new, set to edit mode. + * New means that this partition currently wasn't saved to ldap. + */ + if($disk['partitions'][$name]['status']!="new"){ + $disk['partitions'][$name]['status']="edited"; + } + + $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn']; + + /* Assign empty attributes, if attribute is missing */ + foreach($this->UsedAttrs as $attr){ + if(!isset($values[$attr])){ + $disk['partitions'][$name][$attr]=""; + } } } } - + /* Set default attributes */ $this->DISK_cn = $disk['cn']; $this->DISK_description = $disk['description']; $this->partitions = $disk['partitions']; @@ -57,7 +95,7 @@ class faiPartitionTableEntry extends plugin $s_action = ""; $s_entry = ""; $display = ""; - + /* Assign values * And Checkbox selection */ @@ -87,15 +125,20 @@ class faiPartitionTableEntry extends plugin * with partitions order. */ if($s_action == "remove"){ - unset($this->partitions[$s_entry]); + if($this->partitions[$s_entry]['status'] == "edited"){ + $this->deletePartitions[]= $this->partitions[$s_entry]; + unset($this->partitions[$s_entry]); + }else{ + unset($this->partitions[$s_entry]); + } $tmp= array(); foreach($this->partitions as $part){ $tmp[count($tmp)+1]=$part; } $this->partitions = $tmp; - } + /* To add a partitions we only append an empty * array to the already defined partitions. */ @@ -103,6 +146,8 @@ class faiPartitionTableEntry extends plugin foreach($this->UsedAttrs as $attr){ $tmp[$attr] = ""; } + $tmp["old_cn"] = ""; + $tmp['status']="new"; $this->partitions[count($this->partitions)+1]=$tmp; } @@ -120,11 +165,11 @@ class faiPartitionTableEntry extends plugin /* The user can't change a diskname * if we are currently in edit mode for the specified disk */ - if($this->is_edit){ - $smarty->assign("DISK_cnACL"," disabled "); - }else{ +// if($this->is_edit){ +// $smarty->assign("DISK_cnACL"," disabled "); +// }else{ $smarty->assign("DISK_cnACL",""); - } +// } /* Fetch template and show the result */ @@ -160,49 +205,50 @@ class faiPartitionTableEntry extends plugin * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2 */ foreach($this->partitions as $key => $part){ + if($part['status']!="delete"){ + /* Generate Partition select box + */ + $PartitionType = ""; - /* Generate Partition select box - */ - $PartitionType = ""; - - - /* Generate fsType select box - */ - $FAIfsType= ""; + foreach($FAIfsTypes as $type){ + if($part['FAIfsType'] == $type){ + $FAIfsType .=""; + }else{ + $FAIfsType .=""; + } + } + $FAIfsType.=""; + + $str .= ""; + $str .= "".($key).""; + $str .= ""; + $str .= ""; + $str .= "".$PartitionType.""; + $str .= "".$FAIfsType.""; + $str .= ""; + $str .= ""; + $str .= ""; + $str .= ""; + if($part['FAIpartitionFlags']!=false){ + $str .= ""; }else{ - $FAIfsType .=""; + $str .= ""; } - } - $FAIfsType.=""; - - $str .= ""; - $str .= "".($key).""; - $str .= ""; - $str .= ""; - $str .= "".$PartitionType.""; - $str .= "".$FAIfsType.""; - $str .= ""; - $str .= ""; - $str .= ""; - $str .= ""; - if($part['FAIpartitionFlags']!=false){ - $str .= ""; - }else{ - $str .= ""; + $str .= ""; + $str .= ""; } - $str .= ""; - $str .= ""; } $str.=""; return($str); @@ -252,11 +298,27 @@ class faiPartitionTableEntry extends plugin /* check every partition. * if there is an invalid value defined, append an errorstr to message */ + + /* Array that contain every partitionname mountpoint etc already assigned */ + $alreadyUsed = array(); + foreach($this->UsedAttrs as $attrs){ + $alreadyUsed[$attrs] = array(); + } + foreach($this->partitions as $key => $part){ + if((empty($part['cn']))||(preg_match("/[^0-9a-z]/",$part['cn']))){ - $message[]=sprintf(_("Please enter a valid name for partition %s." ),($key)); + $message[]=sprintf(_("Please enter a valid name for partition %s."),($key)); + } + + if(in_array($part['cn'],$alreadyUsed['cn'])){ + $message[]=sprintf(_("please enter a unique name for partition %s"),($key)); } + if(in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint'])){ + $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key)); + } + if(empty($part['description'])){ $message[]=sprintf(_("Please enter a valid description for partition %s." ),($key)); } @@ -265,6 +327,12 @@ class faiPartitionTableEntry extends plugin $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key)); } + if($part['FAIfsType'] == "swap"){ + if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){ + $message[]=sprintf(_("Fils system type 'swap' is already used, change file system type for partition %s."),$key); + } + } + $tmp = split("-",$part['FAIpartitionSize']); if(count($tmp)!=2){ $message[]=sprintf(_("Please enter a range for partition size, 'from-to', for partition %s."),($key)); @@ -272,6 +340,11 @@ class faiPartitionTableEntry extends plugin $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); }elseif($tmp[0]>=$tmp[1]){ $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); + }elseif($this->checkSize($part['FAIpartitionSize'],$alreadyUsed['FAIpartitionSize'],$key)){ + $message[]=$this->checkSize($part['FAIpartitionSize'],$alreadyUsed['FAIpartitionSize'],$key); + } + foreach($this->UsedAttrs as $attrs){ + $alreadyUsed[$attrs][$key] = $part[$attrs]; } } @@ -291,7 +364,30 @@ class faiPartitionTableEntry extends plugin return ($message); } - + + + /* This function checks the selected partition sizes + * If a size setting overlap with another, it returns an error msg + */ + function checkSize($current,$others,$ckey){ + $tmp = split("-",$current); + $from = $tmp[0]; + $to = $tmp[1]; + foreach($others as $key=> $other){ + $tmp = split("-",$other); + $oth_from = $tmp[0]; + $oth_to = $tmp[1]; + /* surrounds another entry */ + if( (($from <= $oth_from)&&($to >= $oth_to))|| // inside + (($from >= $oth_from)&&($to <= $oth_to))|| // outside + (($to >= $oth_from)&&($to <= $oth_to)) || // overlap at right + (($from >= $oth_from)&&($from <= $oth_to)) ){ // overlap left + $message =sprintf( _("Size settings for partition %s, overlap with size settings for partition %s."),$ckey,$key); + return($message); + } + } + return(false); + } function save() { @@ -299,19 +395,47 @@ class faiPartitionTableEntry extends plugin * If the disk was edited and not a new one, we reset the name of the disk * to keep the correct index. */ - - if($this->is_edit){ - $tmp['cn'] = $this->old_cn; - }else{ - $tmp['cn'] = $this->DISK_cn; + + /* Check partition rename + */ + foreach($this->partitions as $key => $part){ + if($part['cn'] != $part['old_cn']){ + if($part['status'] == "edited"){ + + $tmp = $part; + $tmp['status']= "delete"; + $tmp['cn'] = $part['old_cn']; + + $this->deletePartitions[] = $tmp; + } + $this->partitions[$key]['old_cn']=$part['cn']; + $this->partitions[$key]['status']="new"; + } } - + + $tmp = array(); + $tmp['cn'] = $this->DISK_cn; + + /* Attach partitions */ foreach($this->partitions as $key=>$val) { $this->partitions[$key]['FAIpartitionNr']=$key; } - + + /* Attach deleted */ + foreach($this->deletePartitions as $key=>$val) { + $this->partitions[$key."-delete"]=$val; + $this->partitions[$key."-delete"]['status']="delete"; + } + $tmp['description'] = $this->DISK_description; $tmp['partitions'] = $this->partitions; + $tmp['status'] = $this->status; + + /* If hdd name has changed, tell partitionTable to rename it */ + if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){ + $tmp['rename']['from'] = $this->old_cn; + $tmp['rename']['to'] = $this->DISK_cn; + } return($tmp); } -- 2.30.2