From 10b3158464d931ccfa758eb648ca2e9b72aa0a86 Mon Sep 17 00:00:00 2001 From: paulr Date: Sun, 11 Jul 2010 19:55:39 +1000 Subject: [PATCH] lots of code changes... --- bin/pbdc.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- lib/lib.php | 46 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/bin/pbdc.php b/bin/pbdc.php index b7b8d07..c1ea23d 100644 --- a/bin/pbdc.php +++ b/bin/pbdc.php @@ -52,7 +52,7 @@ function init() echo "This command will init the data for $changer_name\n"; - if(file_exists("$BASE_DATA/$changer_name.db")) { + if(file_exists("$BASE_DATA/$changer_name.db")&&check_init($changer_name)) { //echo "Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:"; $r = readline("Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:"); if($r == "y") { @@ -73,7 +73,31 @@ function init() if($rb == "") $rb = 1; if($rc == "") $rc = 20; - db_getDB($changer_name); + conf_setVal($changer_name, "automountdir", "$ra"); + conf_setVal($changer_name, "ndrives", "$rb"); + conf_setVal($changer_name, "tapesize", "$rc"); + + // check if they were set + echo "got: ".conf_getVal($changer_name, "automountdir").", and ".conf_getVal($changer_name, "tapesize").", and ".conf_getVal($changer_name, "ndrives")."\n"; +} + +function check_init_and_fail($changer) +{ + $lk = conf_getVal($changer, "automountdir"); + if(!$lk) { + echo "DB not init'd yet, please run init first\n"; + exit(0); + } +} + +function check_init($changer) +{ + $lk = conf_getVal($changer, "automountdir"); + if(!$lk) { + return false; + } + + return true; } function add_disk() @@ -82,6 +106,8 @@ function add_disk() // creating pbdc/changer_name/stuff. global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv; + check_init_and_fail($argv[1]); + $dh = opendir("/dev/disk/by-uuid"); $disk = ""; @@ -123,6 +149,32 @@ function add_disk() // now we try and init the disk // we have to get automount directory config - //$k = mkdir(); + $dir = conf_getVal($argv[1], "automountdir"); + if(!is_dir($dir)) { + echo "cant find automount directory, $dir\n"; + } + $mkd = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/"; + $k = mkdir($mkd, 0700, true); + + if(!is_dir($mkd)) { + echo "Error, couldn't create directory, check permissions on $mkd?\n"; + } + + $ts = conf_getVal($argv[1], "tapesize"); + $max = (int)($disk[$i]["size"]/$ts); + + $k = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:")); + if($k > $max) { + echo "Sorry, max number of tapes is $max, creating $max instead\n"; + } + + $did = add_diskToDB($argv[1], basename($disk[$i]["syml"])); + + if(!$did) { + echo "Disk already exists, doing nothing\n"; + exit(0); + } + + //echo "did: $did\n"; } ?> \ No newline at end of file diff --git a/lib/lib.php b/lib/lib.php index 42d30a5..26ca7ed 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -5,7 +5,7 @@ function db_getDB($changer_name) global $BASE_DATA, $BASE_LIB, $BACULA_USER; try { - $dbobject = new PDO("sqlite:$BASE_DATA/$changer_name"."db"); + $dbobject = new PDO("sqlite:$BASE_DATA/$changer_name".".db"); } catch(PDOException $exep) { echo "Cannot open sqlitedb, ".$exep->getMessage()."
"; return; @@ -33,12 +33,12 @@ function db_createDB($dbobject) } } -function conf_getVal($name) +function conf_getVal($changer, $name) { - $db = db_getDB(); + $db = db_getDB($changer); - $sql = "select conf_val from conf where conf_name=='$name'"; - error_log("from getval: ".$sql); + $sql = "select config_value from config where config_name=='$name'"; + //error_log("from getval: ".$sql); $res = $db->query($sql); @@ -47,22 +47,46 @@ function conf_getVal($name) $val = false; foreach($res as $row) { - $val = $row["conf_val"]; + $val = $row["config_value"]; } return $val; } -function conf_setVal($name, $value) +function conf_setVal($changer, $name, $value) { - $db = db_getDB(); + $db = db_getDB($changer); - $sql = "delete from conf where conf_name=='$name'"; + $sql = "delete from config where config_name=='$name'"; $db->query($sql); - $sql = "insert into conf values ('$name', '$value')"; + $sql = "insert into config values ('$name', '$value')"; $db->query($sql); - error_log("from setval: ".$sql); + //error_log("from setval: ".$sql); +} + +function add_diskToDB($changer, $diskid) +{ + $db = db_getDB($changer); + + $sql = "select * from disk_list where disk_name=='$diskid'"; + $res = $db->query($sql); + if(!$res) return false; + + $val = false; + foreach($res as $row) { + $val = $row["disk_id"]; + } + + if(!$val) { + $sql = "insert into disk_list values(NULL, '$diskid')"; + $db->query($sql); + return $db->lastInsertId(); + } else { + echo "Disk already existed in db, will create more tapes?\n"; + } + + return $val; } ?> \ No newline at end of file -- 1.7.0.4