X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Fcats%2Fsql_get.c;h=981f10ab789af3d22aa99133de4f2ba420291346;hb=9052e93bbb96b3b99b9a4c412ff4c9bf903c9096;hp=857927ac3463883474621fe843b84758970de1cb;hpb=27c6094b93454811a6e966558ec187f8ad5a229c;p=bacula%2Fbacula diff --git a/bacula/src/cats/sql_get.c b/bacula/src/cats/sql_get.c index 857927ac34..981f10ab78 100644 --- a/bacula/src/cats/sql_get.c +++ b/bacula/src/cats/sql_get.c @@ -1,3 +1,30 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2000-2007 Free Software Foundation Europe e.V. + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of John Walker. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ /* * Bacula Catalog Database Get record interface routines * Note, these routines generally get a record by id or @@ -9,26 +36,6 @@ * Version $Id$ */ -/* - Copyright (C) 2000-2005 Kern Sibbald - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of - the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public - License along with this program; if not, write to the Free - Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, - MA 02111-1307, USA. - - */ - /* The following is necessary so that we do not include * the dummy external definition of DB. @@ -53,13 +60,6 @@ static int db_get_filename_record(JCR *jcr, B_DB *mdb); static int db_get_path_record(JCR *jcr, B_DB *mdb); -/* Imported subroutines */ -extern void print_result(B_DB *mdb); -extern int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd); -extern void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname); - - - /* * Given a full filename (with path), look up the File record * (with attributes) in the database. @@ -141,7 +141,7 @@ int db_get_file_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr, FILE_DBR *fdbr) } else { fdbr->FileId = (FileId_t)str_to_int64(row[0]); bstrncpy(fdbr->LStat, row[1], sizeof(fdbr->LStat)); - bstrncpy(fdbr->SIG, row[2], sizeof(fdbr->SIG)); + bstrncpy(fdbr->Digest, row[2], sizeof(fdbr->Digest)); stat = 1; } } else { @@ -169,7 +169,7 @@ static int db_get_filename_record(JCR *jcr, B_DB *mdb) int FilenameId = 0; mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2); - db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl); + db_escape_string(jcr, mdb, mdb->esc_name, mdb->fname, mdb->fnl); Mmsg(mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name); if (QUERY_DB(jcr, mdb, mdb->cmd)) { @@ -213,7 +213,7 @@ static int db_get_path_record(JCR *jcr, B_DB *mdb) uint32_t PathId = 0; mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2); - db_escape_string(mdb->esc_name, mdb->path, mdb->pnl); + db_escape_string(jcr, mdb, mdb->esc_name, mdb->path, mdb->pnl); if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl && strcmp(mdb->cached_path, mdb->path) == 0) { @@ -262,10 +262,10 @@ static int db_get_path_record(JCR *jcr, B_DB *mdb) /* * Get Job record for given JobId or Job name - * Returns: 0 on failure - * 1 on success + * Returns: false on failure + * true on success */ -int db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr) +bool db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr) { SQL_ROW row; char ed1[50]; @@ -274,25 +274,27 @@ int db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr) if (jr->JobId == 0) { Mmsg(mdb->cmd, "SELECT VolSessionId,VolSessionTime," "PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus," -"Type,Level,ClientId " +"Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId," +"SchedTime,RealEndTime " "FROM Job WHERE Job='%s'", jr->Job); } else { Mmsg(mdb->cmd, "SELECT VolSessionId,VolSessionTime," "PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus," -"Type,Level,ClientId " +"Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId," +"SchedTime,RealEndTime " "FROM Job WHERE JobId=%s", edit_int64(jr->JobId, ed1)); } if (!QUERY_DB(jcr, mdb, mdb->cmd)) { db_unlock(mdb); - return 0; /* failed */ + return false; /* failed */ } if ((row = sql_fetch_row(mdb)) == NULL) { Mmsg1(mdb->errmsg, _("No Job found for JobId %s\n"), edit_int64(jr->JobId, ed1)); sql_free_result(mdb); db_unlock(mdb); - return 0; /* failed */ + return false; /* failed */ } jr->VolSessionId = str_to_uint64(row[0]); @@ -304,14 +306,27 @@ int db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr) jr->JobBytes = str_to_int64(row[6]); jr->JobTDate = str_to_int64(row[7]); bstrncpy(jr->Job, row[8]!=NULL?row[8]:"", sizeof(jr->Job)); - jr->JobStatus = (int)*row[9]; - jr->JobType = (int)*row[10]; - jr->JobLevel = (int)*row[11]; + jr->JobStatus = row[9]!=NULL?(int)*row[9]:JS_FatalError; + jr->JobType = row[10]!=NULL?(int)*row[10]:JT_BACKUP; + jr->JobLevel = row[11]!=NULL?(int)*row[11]:L_NONE; jr->ClientId = str_to_uint64(row[12]!=NULL?row[12]:(char *)""); + bstrncpy(jr->Name, row[13]!=NULL?row[13]:"", sizeof(jr->Name)); + jr->PriorJobId = str_to_uint64(row[14]!=NULL?row[14]:(char *)""); + bstrncpy(jr->cRealEndTime, row[15]!=NULL?row[15]:"", sizeof(jr->cRealEndTime)); + if (jr->JobId == 0) { + jr->JobId = str_to_int64(row[16]); + } + jr->FileSetId = str_to_int64(row[17]); + bstrncpy(jr->cSchedTime, row[3]!=NULL?row[18]:"", sizeof(jr->cSchedTime)); + bstrncpy(jr->cRealEndTime, row[3]!=NULL?row[19]:"", sizeof(jr->cRealEndTime)); + jr->StartTime = str_to_utime(jr->cStartTime); + jr->SchedTime = str_to_utime(jr->cSchedTime); + jr->EndTime = str_to_utime(jr->cEndTime); + jr->RealEndTime = str_to_utime(jr->cRealEndTime); sql_free_result(mdb); db_unlock(mdb); - return 1; + return true; } /* @@ -390,7 +405,8 @@ int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS db_lock(mdb); Mmsg(mdb->cmd, "SELECT VolumeName,MediaType,FirstIndex,LastIndex,StartFile," -"JobMedia.EndFile,StartBlock,JobMedia.EndBlock,Copy,Stripe" +"JobMedia.EndFile,StartBlock,JobMedia.EndBlock,Copy," +"Slot,StorageId" " FROM JobMedia,Media WHERE JobMedia.JobId=%s" " AND JobMedia.MediaId=Media.MediaId ORDER BY VolIndex,JobMediaId", edit_int64(JobId, ed1)); @@ -398,14 +414,16 @@ int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS Dmsg1(130, "VolNam=%s\n", mdb->cmd); if (QUERY_DB(jcr, mdb, mdb->cmd)) { mdb->num_rows = sql_num_rows(mdb); - Dmsg1(130, "Num rows=%d\n", mdb->num_rows); + Dmsg1(200, "Num rows=%d\n", mdb->num_rows); if (mdb->num_rows <= 0) { Mmsg1(mdb->errmsg, _("No volumes found for JobId=%d\n"), JobId); stat = 0; } else { stat = mdb->num_rows; + DBId_t *SId = NULL; if (stat > 0) { *VolParams = Vols = (VOL_PARAMS *)malloc(stat * sizeof(VOL_PARAMS)); + SId = (DBId_t *)malloc(stat * sizeof(DBId_t)); } for (i=0; i < stat; i++) { if ((row = sql_fetch_row(mdb)) == NULL) { @@ -414,6 +432,7 @@ int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS stat = 0; break; } else { + DBId_t StorageId; bstrncpy(Vols[i].VolumeName, row[0], MAX_NAME_LENGTH); bstrncpy(Vols[i].MediaType, row[1], MAX_NAME_LENGTH); Vols[i].FirstIndex = str_to_uint64(row[2]); @@ -422,10 +441,27 @@ int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS Vols[i].EndFile = str_to_uint64(row[5]); Vols[i].StartBlock = str_to_uint64(row[6]); Vols[i].EndBlock = str_to_uint64(row[7]); - Vols[i].Copy = str_to_uint64(row[8]); - Vols[i].Stripe = str_to_uint64(row[9]); +// Vols[i].Copy = str_to_uint64(row[8]); + Vols[i].Slot = str_to_uint64(row[9]); + StorageId = str_to_uint64(row[10]); + Vols[i].Storage[0] = 0; + SId[i] = StorageId; + } + } + for (i=0; i < stat; i++) { + if (SId[i] != 0) { + Mmsg(mdb->cmd, "SELECT Name from Storage WHERE StorageId=%s", + edit_int64(SId[i], ed1)); + if (QUERY_DB(jcr, mdb, mdb->cmd)) { + if ((row = sql_fetch_row(mdb)) && row[0]) { + bstrncpy(Vols[i].Storage, row[0], MAX_NAME_LENGTH); + } + } } } + if (SId) { + free(SId); + } } sql_free_result(mdb); } @@ -546,13 +582,13 @@ bool db_get_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr) Mmsg(mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,AcceptAnyVolume," "AutoPrune,Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles," -"MaxVolBytes,PoolType,LabelType,LabelFormat FROM Pool WHERE Pool.PoolId=%s", +"MaxVolBytes,PoolType,LabelType,LabelFormat,RecyclePoolId FROM Pool WHERE Pool.PoolId=%s", edit_int64(pdbr->PoolId, ed1)); } else { /* find by name */ Mmsg(mdb->cmd, "SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,AcceptAnyVolume," "AutoPrune,Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles," -"MaxVolBytes,PoolType,LabelType,LabelFormat FROM Pool WHERE Pool.Name='%s'", +"MaxVolBytes,PoolType,LabelType,LabelFormat,RecyclePoolId FROM Pool WHERE Pool.Name='%s'", pdbr->Name); } @@ -585,18 +621,24 @@ bool db_get_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr) bstrncpy(pdbr->PoolType, row[14]!=NULL?row[14]:"", sizeof(pdbr->PoolType)); pdbr->LabelType = str_to_int64(row[15]); bstrncpy(pdbr->LabelFormat, row[16]!=NULL?row[16]:"", sizeof(pdbr->LabelFormat)); + pdbr->RecyclePoolId = str_to_int64(row[17]); ok = true; } - } else { - Mmsg(mdb->errmsg, _("Pool record not found in Catalog.\n")); } sql_free_result(mdb); - } else { - Mmsg(mdb->errmsg, _("Pool record not found in Catalog.\n")); } if (ok) { - Mmsg(mdb->cmd, "SELECT count(*) from Pool"); - pdbr->NumVols = get_sql_record_max(jcr, mdb); + uint32_t NumVols; + Mmsg(mdb->cmd, "SELECT count(*) from Media WHERE PoolId=%s", + edit_int64(pdbr->PoolId, ed1)); + NumVols = get_sql_record_max(jcr, mdb); + Dmsg2(400, "Actual NumVols=%d Pool NumVols=%d\n", NumVols, pdbr->NumVols); + if (NumVols != pdbr->NumVols) { + pdbr->NumVols = NumVols; + db_update_pool_record(jcr, mdb, pdbr); + } + } else { + Mmsg(mdb->errmsg, _("Pool record not found in Catalog.\n")); } db_unlock(mdb); return ok; @@ -640,7 +682,7 @@ int db_get_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cdbr) } else { cdbr->ClientId = str_to_int64(row[0]); bstrncpy(cdbr->Name, row[1]!=NULL?row[1]:"", sizeof(cdbr->Name)); - bstrncpy(cdbr->Uname, row[2]!=NULL?row[1]:"", sizeof(cdbr->Uname)); + bstrncpy(cdbr->Uname, row[2]!=NULL?row[2]:"", sizeof(cdbr->Uname)); cdbr->AutoPrune = str_to_int64(row[3]); cdbr->FileRetention = str_to_int64(row[4]); cdbr->JobRetention = str_to_int64(row[5]); @@ -729,7 +771,7 @@ int db_get_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr) edit_int64(fsr->FileSetId, ed1)); } else { /* find by name */ Mmsg(mdb->cmd, - "SELECT FileSetId,FileSet,CreateTime,MD5 FROM FileSet " + "SELECT FileSetId,FileSet,MD5,CreateTime FROM FileSet " "WHERE FileSet='%s' ORDER BY CreateTime DESC LIMIT 1", fsr->FileSet); } @@ -779,24 +821,25 @@ int db_get_num_media_records(JCR *jcr, B_DB *mdb) /* * This function returns a list of all the Media record ids for - * the current Pool. + * the current Pool with the correct Media Type. * The caller must free ids if non-NULL. * - * Returns 0: on failure - * 1: on success + * Returns false: on failure + * true: on success */ -int db_get_media_ids(JCR *jcr, B_DB *mdb, uint32_t PoolId, int *num_ids, uint32_t *ids[]) +bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, int *num_ids, uint32_t *ids[]) { SQL_ROW row; - int stat = 0; int i = 0; uint32_t *id; char ed1[50]; + bool ok = false; db_lock(mdb); *ids = NULL; - Mmsg(mdb->cmd, "SELECT MediaId FROM Media WHERE PoolId=%s", - edit_int64(PoolId, ed1)); + Mmsg(mdb->cmd, "SELECT DISTINCT MediaId FROM Media WHERE PoolId=%s " + " AND MediaType='%s'", + edit_int64(mr->PoolId, ed1), mr->MediaType); if (QUERY_DB(jcr, mdb, mdb->cmd)) { *num_ids = sql_num_rows(mdb); if (*num_ids > 0) { @@ -807,41 +850,82 @@ int db_get_media_ids(JCR *jcr, B_DB *mdb, uint32_t PoolId, int *num_ids, uint32_ *ids = id; } sql_free_result(mdb); - stat = 1; + ok = true; } else { Mmsg(mdb->errmsg, _("Media id select failed: ERR=%s\n"), sql_strerror(mdb)); Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg); - stat = 0; + ok = false; } db_unlock(mdb); - return stat; + return ok; +} + + +/* + * This function returns a list of all the DBIds that are returned + * for the query. + * + * Returns false: on failure + * true: on success + */ +bool db_get_query_dbids(JCR *jcr, B_DB *mdb, POOL_MEM &query, dbid_list &ids) +{ + SQL_ROW row; + int i = 0; + bool ok = false; + + db_lock(mdb); + ids.num_ids = 0; + if (QUERY_DB(jcr, mdb, query.c_str())) { + ids.num_ids = sql_num_rows(mdb); + if (ids.num_ids > 0) { + if (ids.max_ids < ids.num_ids) { + free(ids.DBId); + ids.DBId = (DBId_t *)malloc(ids.num_ids * sizeof(DBId_t)); + } + while ((row = sql_fetch_row(mdb)) != NULL) { + ids.DBId[i++] = str_to_uint64(row[0]); + } + } + sql_free_result(mdb); + ok = true; + } else { + Mmsg(mdb->errmsg, _("query dbids failed: ERR=%s\n"), sql_strerror(mdb)); + Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg); + ok = false; + } + db_unlock(mdb); + return ok; } + /* Get Media Record * - * Returns: 0 on failure - * id on success + * Returns: false: on failure + * true: on success */ -int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) +bool db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) { SQL_ROW row; - int stat = 0; char ed1[50]; + bool ok = false; db_lock(mdb); if (mr->MediaId == 0 && mr->VolumeName[0] == 0) { Mmsg(mdb->cmd, "SELECT count(*) from Media"); mr->MediaId = get_sql_record_max(jcr, mdb); db_unlock(mdb); - return 1; + return true; } if (mr->MediaId != 0) { /* find by id */ Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks," "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes," "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs," "MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger," - "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId " + "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId," + "Enabled,LocationId,RecycleCount,InitialWrite," + "ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime " "FROM Media WHERE MediaId=%s", edit_int64(mr->MediaId, ed1)); } else { /* find by name */ @@ -849,7 +933,9 @@ int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes," "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs," "MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger," - "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId " + "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId," + "Enabled,LocationId,RecycleCount,InitialWrite," + "ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime " "FROM Media WHERE VolumeName='%s'", mr->VolumeName); } @@ -898,7 +984,17 @@ int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) bstrncpy(mr->cLabelDate, row[27]!=NULL?row[27]:"", sizeof(mr->cLabelDate)); mr->LabelDate = (time_t)str_to_utime(mr->cLabelDate); mr->StorageId = str_to_int64(row[28]); - stat = mr->MediaId; + mr->Enabled = str_to_int64(row[29]); + mr->LocationId = str_to_int64(row[30]); + mr->RecycleCount = str_to_int64(row[31]); + bstrncpy(mr->cInitialWrite, row[32]!=NULL?row[32]:"", sizeof(mr->cInitialWrite)); + mr->InitialWrite = (time_t)str_to_utime(mr->cInitialWrite); + mr->ScratchPoolId = str_to_int64(row[33]); + mr->RecyclePoolId = str_to_int64(row[34]); + mr->VolReadTime = str_to_int64(row[35]); + mr->VolWriteTime = str_to_int64(row[36]); + + ok = true; } } else { if (mr->MediaId != 0) { @@ -919,7 +1015,7 @@ int db_get_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) mr->VolumeName); } } db_unlock(mdb); - return stat; + return ok; }