From: <ke...@us...> - 2007-03-30 10:07:32
|
Revision: 4457 http://bacula.svn.sourceforge.net/bacula/?rev=4457&view=rev Author: kerns Date: 2007-03-30 03:07:33 -0700 (Fri, 30 Mar 2007) Log Message: ----------- kes Implement dir_sql() which issues an SQL query. kes Modify bat medialist to use dir_sql() and do some minor C++ formatting as well as eliminating a number of pointers, simplifing the code. Modified Paths: -------------- trunk/bacula/src/dird/ua_dotcmds.c trunk/bacula/src/lib/scan.c trunk/bacula/src/qt-console/console/console.cpp trunk/bacula/src/qt-console/console/console.h trunk/bacula/src/qt-console/medialist/medialist.cpp trunk/bacula/src/qt-console/medialist/medialist.h trunk/bacula/technotes-2.1 Modified: trunk/bacula/src/dird/ua_dotcmds.c =================================================================== --- trunk/bacula/src/dird/ua_dotcmds.c 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/src/dird/ua_dotcmds.c 2007-03-30 10:07:33 UTC (rev 4457) @@ -170,7 +170,7 @@ JCR *jcr = NULL; int a; - bsendmsg(ua, _("The Director will segment fault.\n")); + ua->send_msg(_("The Director will segment fault.\n")); a = jcr->JobId; /* ref NULL pointer */ jcr->JobId = 1000; /* another ref NULL pointer */ return true; @@ -182,7 +182,7 @@ LockRes(); foreach_res(job, R_JOB) { if (acl_access_ok(ua, Job_ACL, job->name())) { - bsendmsg(ua, "%s\n", job->name()); + ua->send_msg("%s\n", job->name()); } } UnlockRes(); @@ -195,7 +195,7 @@ LockRes(); foreach_res(fs, R_FILESET) { if (acl_access_ok(ua, FileSet_ACL, fs->name())) { - bsendmsg(ua, "%s\n", fs->name()); + ua->send_msg("%s\n", fs->name()); } } UnlockRes(); @@ -208,7 +208,7 @@ LockRes(); foreach_res(client, R_CLIENT) { if (acl_access_ok(ua, Client_ACL, client->name())) { - bsendmsg(ua, "%s\n", client->name()); + ua->send_msg("%s\n", client->name()); } } UnlockRes(); @@ -220,7 +220,7 @@ MSGS *msgs = NULL; LockRes(); foreach_res(msgs, R_MSGS) { - bsendmsg(ua, "%s\n", msgs->name()); + ua->send_msg("%s\n", msgs->name()); } UnlockRes(); return true; @@ -232,7 +232,7 @@ LockRes(); foreach_res(pool, R_POOL) { if (acl_access_ok(ua, Pool_ACL, pool->name())) { - bsendmsg(ua, "%s\n", pool->name()); + ua->send_msg("%s\n", pool->name()); } } UnlockRes(); @@ -245,7 +245,7 @@ LockRes(); foreach_res(store, R_STORAGE) { if (acl_access_ok(ua, Storage_ACL, store->name())) { - bsendmsg(ua, "%s\n", store->name()); + ua->send_msg("%s\n", store->name()); } } UnlockRes(); @@ -255,11 +255,11 @@ static bool typescmd(UAContext *ua, const char *cmd) { - bsendmsg(ua, "Backup\n"); - bsendmsg(ua, "Restore\n"); - bsendmsg(ua, "Admin\n"); - bsendmsg(ua, "Verify\n"); - bsendmsg(ua, "Migrate\n"); + ua->send_msg("Backup\n"); + ua->send_msg("Restore\n"); + ua->send_msg("Admin\n"); + ua->send_msg("Verify\n"); + ua->send_msg("Migrate\n"); return true; } @@ -285,7 +285,7 @@ static int client_backups_handler(void *ctx, int num_field, char **row) { UAContext *ua = (UAContext *)ctx; - bsendmsg(ua, "| %s | %s | %s | %s | %s | %s | %s | %s |\n", + ua->send_msg("| %s | %s | %s | %s | %s | %s | %s | %s |\n", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); return 0; } @@ -307,11 +307,12 @@ } if (!acl_access_ok(ua, Client_ACL, ua->argv[1]) || !acl_access_ok(ua, FileSet_ACL, ua->argv[2])) { + ua->error_msg(_("Access to specified Client or FileSet not allowed.\n")); return true; } Mmsg(ua->cmd, client_backups, ua->argv[1], ua->argv[2]); if (!db_sql_query(ua->db, ua->cmd, client_backups_handler, (void *)ua)) { - bsendmsg(ua, _("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db)); + ua->error_msg(_("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db)); return true; } return true; @@ -330,19 +331,26 @@ } pm_strcat(rows, "\t"); } - bsendmsg(ua, rows.c_str()); + ua->send_msg(rows.c_str()); return 0; } static bool sql_cmd(UAContext *ua, const char *cmd) { + int index; if (!open_client_db(ua)) { return true; } - if (!db_sql_query(ua->db, ua->argk[1], sql_handler, (void *)ua)) { - bsendmsg(ua, _("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db)); + index = find_arg_with_value(ua, "query"); + if (index < 0) { + ua->error_msg(_("query keyword not found.\n")); return true; } + if (!db_sql_query(ua->db, ua->argv[index], sql_handler, (void *)ua)) { + Dmsg1(100, "Query failed: ERR=%s\n", db_strerror(ua->db)); + ua->error_msg(_("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db)); + return true; + } return true; } @@ -350,12 +358,12 @@ static bool levelscmd(UAContext *ua, const char *cmd) { - bsendmsg(ua, "Incremental\n"); - bsendmsg(ua, "Full\n"); - bsendmsg(ua, "Differential\n"); - bsendmsg(ua, "Catalog\n"); - bsendmsg(ua, "InitCatalog\n"); - bsendmsg(ua, "VolumeToCatalog\n"); + ua->send_msg("Incremental\n"); + ua->send_msg("Full\n"); + ua->send_msg("Differential\n"); + ua->send_msg("Catalog\n"); + ua->send_msg("InitCatalog\n"); + ua->send_msg("VolumeToCatalog\n"); return true; } @@ -368,6 +376,7 @@ CLIENT *client; STORE *storage; POOL *pool; + char ed1[50]; if (ua->argc != 2 || !ua->argv[1]) { return true; @@ -381,18 +390,18 @@ job = (JOB *)GetResWithName(R_JOB, ua->argv[1]); if (job) { USTORE store; - bsendmsg(ua, "job=%s", job->name()); - bsendmsg(ua, "pool=%s", job->pool->name()); - bsendmsg(ua, "messages=%s", job->messages->name()); - bsendmsg(ua, "client=%s", job->client->name()); + ua->send_msg("job=%s", job->name()); + ua->send_msg("pool=%s", job->pool->name()); + ua->send_msg("messages=%s", job->messages->name()); + ua->send_msg("client=%s", job->client->name()); get_job_storage(&store, job, NULL); - bsendmsg(ua, "storage=%s", store.store->name()); - bsendmsg(ua, "where=%s", job->RestoreWhere?job->RestoreWhere:""); - bsendmsg(ua, "level=%s", level_to_str(job->JobLevel)); - bsendmsg(ua, "type=%s", job_type_to_str(job->JobType)); - bsendmsg(ua, "fileset=%s", job->fileset->name()); - bsendmsg(ua, "enabled=%d", job->enabled); - bsendmsg(ua, "catalog=%s", job->client->catalog->name()); + ua->send_msg("storage=%s", store.store->name()); + ua->send_msg("where=%s", job->RestoreWhere?job->RestoreWhere:""); + ua->send_msg("level=%s", level_to_str(job->JobLevel)); + ua->send_msg("type=%s", job_type_to_str(job->JobType)); + ua->send_msg("fileset=%s", job->fileset->name()); + ua->send_msg("enabled=%d", job->enabled); + ua->send_msg("catalog=%s", job->client->catalog->name()); } } /* Client defaults */ @@ -402,13 +411,13 @@ } client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[1]); if (client) { - bsendmsg(ua, "client=%s", client->name()); - bsendmsg(ua, "address=%s", client->address); - bsendmsg(ua, "fdport=%d", client->FDport); - bsendmsg(ua, "file_retention=%d", client->FileRetention); - bsendmsg(ua, "job_retention=%d", client->JobRetention); - bsendmsg(ua, "autoprune=%d", client->AutoPrune); - bsendmsg(ua, "catalog=%s", client->catalog->name()); + ua->send_msg("client=%s", client->name()); + ua->send_msg("address=%s", client->address); + ua->send_msg("fdport=%d", client->FDport); + ua->send_msg("file_retention=%s", edit_uint64(client->FileRetention, ed1)); + ua->send_msg("job_retention=%s", edit_uint64(client->JobRetention, ed1)); + ua->send_msg("autoprune=%d", client->AutoPrune); + ua->send_msg("catalog=%s", client->catalog->name()); } } /* Storage defaults */ @@ -419,16 +428,16 @@ storage = (STORE *)GetResWithName(R_STORAGE, ua->argv[1]); DEVICE *device; if (storage) { - bsendmsg(ua, "storage=%s", storage->name()); - bsendmsg(ua, "address=%s", storage->address); - bsendmsg(ua, "enabled=%d", storage->enabled); - bsendmsg(ua, "media_type=%s", storage->media_type); - bsendmsg(ua, "sdport=%d", storage->SDport); + ua->send_msg("storage=%s", storage->name()); + ua->send_msg("address=%s", storage->address); + ua->send_msg("enabled=%d", storage->enabled); + ua->send_msg("media_type=%s", storage->media_type); + ua->send_msg("sdport=%d", storage->SDport); device = (DEVICE *)storage->device->first(); - bsendmsg(ua, "device=%s", device->name()); + ua->send_msg("device=%s", device->name()); if (storage->device->size() > 1) { while ((device = (DEVICE *)storage->device->next())) { - bsendmsg(ua, ",%s", device->name()); + ua->send_msg(",%s", device->name()); } } } @@ -440,21 +449,21 @@ } pool = (POOL *)GetResWithName(R_POOL, ua->argv[1]); if (pool) { - bsendmsg(ua, "pool=%s", pool->name()); - bsendmsg(ua, "pool_type=%s", pool->pool_type); - bsendmsg(ua, "label_format=%s", pool->label_format?pool->label_format:""); - bsendmsg(ua, "use_volume_once=%d", pool->use_volume_once); - bsendmsg(ua, "purge_oldest_volume=%d", pool->purge_oldest_volume); - bsendmsg(ua, "recycle_oldest_volume=%d", pool->recycle_oldest_volume); - bsendmsg(ua, "recycle_current_volume=%d", pool->recycle_current_volume); - bsendmsg(ua, "max_volumes=%d", pool->max_volumes); - bsendmsg(ua, "vol_retention=%d", pool->VolRetention); - bsendmsg(ua, "vol_use_duration=%d", pool->VolUseDuration); - bsendmsg(ua, "max_vol_jobs=%d", pool->MaxVolJobs); - bsendmsg(ua, "max_vol_files=%d", pool->MaxVolFiles); - bsendmsg(ua, "max_vol_bytes=%d", pool->MaxVolBytes); - bsendmsg(ua, "auto_prune=%d", pool->AutoPrune); - bsendmsg(ua, "recycle=%d", pool->Recycle); + ua->send_msg("pool=%s", pool->name()); + ua->send_msg("pool_type=%s", pool->pool_type); + ua->send_msg("label_format=%s", pool->label_format?pool->label_format:""); + ua->send_msg("use_volume_once=%d", pool->use_volume_once); + ua->send_msg("purge_oldest_volume=%d", pool->purge_oldest_volume); + ua->send_msg("recycle_oldest_volume=%d", pool->recycle_oldest_volume); + ua->send_msg("recycle_current_volume=%d", pool->recycle_current_volume); + ua->send_msg("max_volumes=%d", pool->max_volumes); + ua->send_msg("vol_retention=%s", edit_uint64(pool->VolRetention, ed1)); + ua->send_msg("vol_use_duration=%s", edit_uint64(pool->VolUseDuration, ed1)); + ua->send_msg("max_vol_jobs=%d", pool->MaxVolJobs); + ua->send_msg("max_vol_files=%d", pool->MaxVolFiles); + ua->send_msg("max_vol_bytes=%s", edit_uint64(pool->MaxVolBytes, ed1)); + ua->send_msg("auto_prune=%d", pool->AutoPrune); + ua->send_msg("recycle=%d", pool->Recycle); } } return true; Modified: trunk/bacula/src/lib/scan.c =================================================================== --- trunk/bacula/src/lib/scan.c 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/src/lib/scan.c 2007-03-30 10:07:33 UTC (rev 4457) @@ -235,9 +235,6 @@ p = strchr(argk[i], '='); if (p) { *p++ = 0; /* terminate keyword and point to value */ - if (strlen(p) > MAX_NAME_LENGTH-1) { - p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */ - } } argv[i] = p; /* save ptr to value or NULL */ } Modified: trunk/bacula/src/qt-console/console/console.cpp =================================================================== --- trunk/bacula/src/qt-console/console/console.cpp 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/src/qt-console/console/console.cpp 2007-03-30 10:07:33 UTC (rev 4457) @@ -159,6 +159,10 @@ return dir_cmd(cmd.toUtf8().data(), results); } +/* + * Send a command to the Director, and return the + * results in a QStringList. + */ bool Console::dir_cmd(const char *cmd, QStringList &results) { int stat; @@ -174,7 +178,36 @@ return true; /* ***FIXME*** return any command error */ } +bool Console::sql_cmd(QString &query, QStringList &results) +{ + return sql_cmd(query.toUtf8().data(), results); +} +/* + * Send an sql query to the Director, and return the + * results in a QStringList. + */ +bool Console::sql_cmd(const char *query, QStringList &results) +{ + int stat; + POOL_MEM cmd(PM_MESSAGE); + + notify(false); + + pm_strcpy(cmd, ".sql query=\""); + pm_strcat(cmd, query); + pm_strcat(cmd, "\""); + write(cmd.c_str()); + while ((stat = read()) > 0) { + strip_trailing_junk(msg()); + results << msg(); + } + notify(true); + discardToPrompt(); + return true; /* ***FIXME*** return any command error */ +} + + /* * Send a job name to the director, and read all the resulting * defaults. @@ -395,8 +428,7 @@ int Console::write(const char *msg) { - m_sock->msglen = strlen(msg); - pm_strcpy(&m_sock->msg, msg); + m_sock->msglen = pm_strcpy(m_sock->msg, msg); m_at_prompt = false; if (commDebug) Pmsg1(000, "send: %s\n", msg); return m_sock->send(); Modified: trunk/bacula/src/qt-console/console/console.h =================================================================== --- trunk/bacula/src/qt-console/console/console.h 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/src/qt-console/console/console.h 2007-03-30 10:07:33 UTC (rev 4457) @@ -79,6 +79,8 @@ void write_dir(const char *buf); bool dir_cmd(const char *cmd, QStringList &results); bool dir_cmd(QString &cmd, QStringList &results); + bool sql_cmd(const char *cmd, QStringList &results); + bool sql_cmd(QString &cmd, QStringList &results); bool authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons); bool is_connected() { return m_sock != NULL; }; const QFont get_font(); Modified: trunk/bacula/src/qt-console/medialist/medialist.cpp =================================================================== --- trunk/bacula/src/qt-console/medialist/medialist.cpp 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/src/qt-console/medialist/medialist.cpp 2007-03-30 10:07:33 UTC (rev 4457) @@ -50,22 +50,29 @@ m_treeWidget = treeWidget; /* our Storage Tree Tree Widget */ m_console = console; createConnections(); - m_populated=false; - m_headerlist = new QStringList(); - m_popupmedia = new QString(""); - m_poollist = new QStringList(); + m_populated = false; } MediaList::~MediaList() { - delete m_headerlist; - delete m_popupmedia; - delete m_poollist; } void MediaList::populateTree() { QTreeWidgetItem *mediatreeitem, *pooltreeitem, *topItem; + QString currentpool(""); + QString resultline; + QStringList results; + const char *query = + "SELECT p.Name,m.VolumeName,m.MediaId,m.VolStatus,m.Enabled,m.VolBytes," + "m.VolFiles,m.VolRetention,m.MediaType,m.LastWritten" + " FROM Media m,Pool p" + " WHERE m.PoolId=p.PoolId" + " ORDER BY p.Name"; + QStringList headerlist = (QStringList() + << "Volume Name" << "Media Id" << "Volume Status" << "Enabled" + << "Volume Bytes" << "Volume Files" << "Volume Retention" + << "Media Type" << "Last Written"); m_treeWidget->clear(); m_treeWidget->setColumnCount(9); @@ -73,6 +80,7 @@ topItem->setText(0, "Pools"); topItem->setData(0, Qt::UserRole, 0); topItem->setExpanded( true ); + #ifdef xxx #include <QSize> ***** FIXME ***** @@ -80,55 +88,38 @@ //topItem->setSizeHint(0,QSize(1050,50)); #endif - /* Start with a list of pools */ - m_poollist->clear(); - m_headerlist->clear(); - //m_headerlist->append("Media Id"); - m_headerlist->append("Volume Name"); - m_headerlist->append("Media Id"); - m_headerlist->append("Volume Status"); - m_headerlist->append("Enabled"); - m_headerlist->append("Volume Bytes"); - m_headerlist->append("Volume Files"); - m_headerlist->append("Volume Retention"); - m_headerlist->append("Media Type"); - m_headerlist->append("Last Written"); - m_treeWidget->setHeaderLabels(*m_headerlist); + m_treeWidget->setHeaderLabels(headerlist); - QString currentpool(""); - QString resultline; - QStringList results; - QString m_cmd(".sql \"select p.name, m.volumename, m.mediaid, m.volstatus, m.enabled, m.volbytes, m.volfiles, m.volretention, m.mediatype, m.lastwritten FROM media m, pool p ORDER BY p.name\""); - if ( m_console->dir_cmd(m_cmd,results)){ - int recordcounter=0; - foreach( resultline, results ){ + if (m_console->sql_cmd(query, results)) { + int recordcounter = 0; + foreach (resultline, results) { QRegExp regex("^Using Catalog"); - if ( regex.indexIn(resultline) < 0 ){ + if (regex.indexIn(resultline) < 0) { QStringList recorditemlist = resultline.split("\t"); - int recorditemcnter=0; + int recorditemcnter = 0; /* Iterate through items in the record */ QString mediarecorditem; - foreach( mediarecorditem, recorditemlist ){ + foreach (mediarecorditem, recorditemlist) { QString trimmeditem = mediarecorditem.trimmed(); - if( trimmeditem != "" ){ - if ( recorditemcnter == 0 ){ - if ( currentpool != trimmeditem.toUtf8().data() ){ - currentpool = trimmeditem.toUtf8().data(); + if (trimmeditem != "") { + if (recorditemcnter == 0) { + if (currentpool != trimmeditem) { + currentpool = trimmeditem; pooltreeitem = new QTreeWidgetItem(topItem); - pooltreeitem->setText(0, trimmeditem.toUtf8().data()); + pooltreeitem->setText(0, trimmeditem); pooltreeitem->setData(0, Qt::UserRole, 1); - pooltreeitem->setExpanded( true ); + pooltreeitem->setExpanded(true); } mediatreeitem = new QTreeWidgetItem(pooltreeitem); } else { mediatreeitem->setData(recorditemcnter-1, Qt::UserRole, 2); - mediatreeitem->setText(recorditemcnter-1, trimmeditem.toUtf8().data()); + mediatreeitem->setText(recorditemcnter-1, trimmeditem); } - recorditemcnter+=1; + recorditemcnter++; } } } - recordcounter+=1; + recordcounter++; } } } @@ -144,17 +135,17 @@ void MediaList::treeItemClicked(QTreeWidgetItem *item, int column) { int treedepth = item->data(column, Qt::UserRole).toInt(); - QString text = item->text(0); - switch (treedepth){ - case 1: - break; - case 2: - /* Can't figure out how to make a right button do this --- Qt::LeftButton, Qt::RightButton, Qt::MidButton */ - *m_popupmedia = text; - QMenu *popup = new QMenu( m_treeWidget ); - connect(popup->addAction("Edit Properties"), SIGNAL(triggered()), this, SLOT(editMedia())); - connect(popup->addAction("Show Jobs On Media"), SIGNAL(triggered()), this, SLOT(showJobs())); - popup->exec(QCursor::pos()); + switch (treedepth) { + case 1: + break; + case 2: + /* Can't figure out how to make a right button do this --- Qt::LeftButton, Qt::RightButton, Qt::MidButton */ + m_popuptext = item->text(0); + QMenu *popup = new QMenu( m_treeWidget ); + connect(popup->addAction("Edit Properties"), SIGNAL(triggered()), this, SLOT(editMedia())); + connect(popup->addAction("Show Jobs On Media"), SIGNAL(triggered()), this, SLOT(showJobs())); + popup->exec(QCursor::pos()); + break; } } @@ -167,19 +158,19 @@ void MediaList::editMedia() { - MediaEdit* edit = new MediaEdit(m_console, *m_popupmedia); + MediaEdit* edit = new MediaEdit(m_console, m_popuptext); edit->show(); } void MediaList::showJobs() { - JobList* joblist = new JobList(m_console, *m_popupmedia); + JobList* joblist = new JobList(m_console, m_popuptext); joblist->show(); } void MediaList::PgSeltreeWidgetClicked() { - if( ! m_populated ){ + if(!m_populated) { populateTree(); m_populated=true; } Modified: trunk/bacula/src/qt-console/medialist/medialist.h =================================================================== --- trunk/bacula/src/qt-console/medialist/medialist.h 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/src/qt-console/medialist/medialist.h 2007-03-30 10:07:33 UTC (rev 4457) @@ -60,10 +60,7 @@ private: Console *m_console; QTreeWidget *m_treeWidget; - QStringList *m_poollist; - QStringList *m_headerlist; - QString *m_popupmedia; - QString *m_cmd; + QString m_popuptext; bool m_populated; void populateTree(); }; Modified: trunk/bacula/technotes-2.1 =================================================================== --- trunk/bacula/technotes-2.1 2007-03-30 01:00:22 UTC (rev 4456) +++ trunk/bacula/technotes-2.1 2007-03-30 10:07:33 UTC (rev 4457) @@ -1,6 +1,11 @@ Technical notes on version 2.1 General: +29Mar07 +kes Implement dir_sql() which issues an SQL query. +kes Modify bat medialist to use dir_sql() and do some minor C++ + formatting as well as eliminating a number of pointers, simplifing + the code. 28Mar07 kes Fix some of the mtx-changer grep commands for tape label readers. kes Convert a number of bsendmsg into the appropriate send/warning/error This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |