Skip to content

Commit

Permalink
Merge branch 'bareos-15.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco van Wieringen committed Jun 24, 2016
2 parents dfda827 + a8727a6 commit 9c5d8bf
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 65 deletions.
4 changes: 3 additions & 1 deletion src/cats/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ bool db_get_base_file_list(JCR *jcr, B_DB *mdb, bool use_md5,
DB_RESULT_HANDLER *result_handler,void *ctx);
int db_get_path_record(JCR *jcr, B_DB *mdb);
bool db_get_pool_record(JCR *jcr, B_DB *db, POOL_DBR *pdbr);
bool db_get_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sdbr);
bool db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr);
int db_get_job_volume_names(JCR *jcr, B_DB *mdb, JobId_t JobId, POOLMEM *&VolumeNames);
bool db_get_file_attributes_record(JCR *jcr, B_DB *mdb, char *fname, JOB_DBR *jr, FILE_DBR *fdbr);
Expand All @@ -100,7 +101,8 @@ int db_get_num_media_records(JCR *jcr, B_DB *mdb);
int db_get_num_pool_records(JCR *jcr, B_DB *mdb);
int db_get_pool_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids);
bool db_get_client_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids);
bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, POOL_MEM &volumes, int *num_ids, uint32_t **ids);
int db_get_storage_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids);
bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, POOL_MEM &volumes, int *num_ids, DBId_t **ids);
int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS **VolParams);
bool db_get_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cdbr);
bool db_get_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr);
Expand Down
173 changes: 129 additions & 44 deletions src/cats/sql_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static int db_get_filename_record(JCR *jcr, B_DB *mdb)
int db_get_path_record(JCR *jcr, B_DB *mdb)
{
SQL_ROW row;
uint32_t PathId = 0;
DBId_t PathId = 0;
int num_rows;

mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
Expand Down Expand Up @@ -351,13 +351,13 @@ bool db_get_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)

/**
* Find VolumeNames for a given JobId
* Returns: 0 on error or no Volumes found
* number of volumes on success
* Volumes are concatenated in VolumeNames
* separated by a vertical bar (|) in the order
* that they were written.
* Returns: 0 on error or no Volumes found
* number of volumes on success
* Volumes are concatenated in VolumeNames
* separated by a vertical bar (|) in the order
* that they were written.
*
* Returns: number of volumes on success
* Returns: number of volumes on success
*/
int db_get_job_volume_names(JCR *jcr, B_DB *mdb, JobId_t JobId, POOLMEM *&VolumeNames)
{
Expand Down Expand Up @@ -413,11 +413,11 @@ int db_get_job_volume_names(JCR *jcr, B_DB *mdb, JobId_t JobId, POOLMEM *&Volume

/**
* Find Volume parameters for a give JobId
* Returns: 0 on error or no Volumes found
* number of volumes on success
* List of Volumes and start/end file/blocks (malloced structure!)
* Returns: 0 on error or no Volumes found
* number of volumes on success
* List of Volumes and start/end file/blocks (malloced structure!)
*
* Returns: number of volumes on success
* Returns: number of volumes on success
*/
int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS **VolParams)
{
Expand Down Expand Up @@ -460,6 +460,7 @@ int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS
} else {
DBId_t StorageId;
uint32_t StartBlock, EndBlock, StartFile, EndFile;

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]);
Expand Down Expand Up @@ -498,8 +499,6 @@ int db_get_job_volume_parameters(JCR *jcr, B_DB *mdb, JobId_t JobId, VOL_PARAMS
return retval;
}



/**
* Get the number of pool records
*
Expand All @@ -519,25 +518,62 @@ int db_get_num_pool_records(JCR *jcr, B_DB *mdb)

/**
* This function returns a list of all the Pool record ids.
* The caller must free ids if non-NULL.
*
* Returns 0: on failure
* 1: on success
*/
int db_get_pool_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids)
{
SQL_ROW row;
int retval = 0;
int i = 0;
DBId_t *id;

db_lock(mdb);
*ids = NULL;
Mmsg(mdb->cmd, "SELECT PoolId FROM Pool");
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
*num_ids = sql_num_rows(mdb);
if (*num_ids > 0) {
id = (DBId_t *)malloc(*num_ids * sizeof(DBId_t));
while ((row = sql_fetch_row(mdb)) != NULL) {
id[i++] = str_to_uint64(row[0]);
}
*ids = id;
}
sql_free_result(mdb);
retval = 1;
} else {
Mmsg(mdb->errmsg, _("Pool id select failed: ERR=%s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
retval = 0;
}
db_unlock(mdb);
return retval;
}

/**
* This function returns a list of all the Storage record ids.
* The caller must free ids if non-NULL.
*
* Returns 0: on failure
* 1: on success
*/
int db_get_pool_ids(JCR *jcr, B_DB *mdb, int *num_ids, uint32_t *ids[])
int db_get_storage_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids)
{
SQL_ROW row;
int retval = 0;
int i = 0;
uint32_t *id;
DBId_t *id;

db_lock(mdb);
*ids = NULL;
Mmsg(mdb->cmd, "SELECT PoolId FROM Pool");
Mmsg(mdb->cmd, "SELECT StorageId FROM Storage");
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
*num_ids = sql_num_rows(mdb);
if (*num_ids > 0) {
id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
id = (DBId_t *)malloc(*num_ids * sizeof(DBId_t));
while ((row = sql_fetch_row(mdb)) != NULL) {
id[i++] = str_to_uint64(row[0]);
}
Expand All @@ -546,7 +582,7 @@ int db_get_pool_ids(JCR *jcr, B_DB *mdb, int *num_ids, uint32_t *ids[])
sql_free_result(mdb);
retval = 1;
} else {
Mmsg(mdb->errmsg, _("Pool id select failed: ERR=%s\n"), sql_strerror(mdb));
Mmsg(mdb->errmsg, _("Storage id select failed: ERR=%s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
retval = 0;
}
Expand All @@ -556,25 +592,25 @@ int db_get_pool_ids(JCR *jcr, B_DB *mdb, int *num_ids, uint32_t *ids[])

/**
* This function returns a list of all the Client record ids.
* The caller must free ids if non-NULL.
* The caller must free ids if non-NULL.
*
* Returns false: on failure
* true: on success
* Returns false: on failure
* true: on success
*/
bool db_get_client_ids(JCR *jcr, B_DB *mdb, int *num_ids, uint32_t *ids[])
bool db_get_client_ids(JCR *jcr, B_DB *mdb, int *num_ids, DBId_t **ids)
{
bool retval = false;
SQL_ROW row;
int i = 0;
uint32_t *id;
DBId_t *id;

db_lock(mdb);
*ids = NULL;
Mmsg(mdb->cmd, "SELECT ClientId FROM Client ORDER BY Name");
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
*num_ids = sql_num_rows(mdb);
if (*num_ids > 0) {
id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
id = (DBId_t *)malloc(*num_ids * sizeof(DBId_t));
while ((row = sql_fetch_row(mdb)) != NULL) {
id[i++] = str_to_uint64(row[0]);
}
Expand All @@ -593,7 +629,7 @@ bool db_get_client_ids(JCR *jcr, B_DB *mdb, int *num_ids, uint32_t *ids[])
/**
* Get Pool Record
* If the PoolId is non-zero, we get its record,
* otherwise, we search on the PoolName
* otherwise, we search on the PoolName
*
* Returns: false on failure
* true on success
Expand Down Expand Up @@ -664,8 +700,8 @@ bool db_get_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr)

if (ok) {
uint32_t NumVols;
Mmsg(mdb->cmd, "SELECT count(*) from Media WHERE PoolId=%s",
edit_int64(pdbr->PoolId, ed1));

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) {
Expand All @@ -680,10 +716,59 @@ bool db_get_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pdbr)
return ok;
}

/**
* Get Storage Record
* If the StorageId is non-zero, we get its record, otherwise, we search on the StorageName
*
* Returns: false on failure
* true on success
*/
bool db_get_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sdbr)
{
SQL_ROW row;
bool ok = false;
char ed1[50];
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];

db_lock(mdb);
if (sdbr->StorageId != 0) { /* find by id */
Mmsg(mdb->cmd,
"SELECT StorageId,Name,AutoChanger FROM Storage WHERE Storage.StorageId=%s",
edit_int64(sdbr->StorageId, ed1));
} else { /* find by name */
mdb->db_escape_string(jcr, esc, sdbr->Name, strlen(sdbr->Name));
Mmsg(mdb->cmd,
"SELECT StorageId,Name,Autochanger FROM Storage WHERE Storage.Name='%s'", esc);
}
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
num_rows = sql_num_rows(mdb);
if (num_rows > 1) {
char ed1[30];
Mmsg1(mdb->errmsg, _("More than one Storage!: %s\n"),
edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
} else if (num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
} else {
sdbr->StorageId = str_to_int64(row[0]);
bstrncpy(sdbr->Name, (row[1] != NULL) ? row[1] : "", sizeof(sdbr->Name));
sdbr->AutoChanger = str_to_int64(row[2]);
ok = true;
}
}
sql_free_result(mdb);
}

db_unlock(mdb);
return ok;
}

/**
* Get Client Record
* If the ClientId is non-zero, we get its record,
* otherwise, we search on the Client Name
* If the ClientId is non-zero, we get its record, otherwise, we search on the Client Name
*
* Returns: false on failure
* true on success
Expand All @@ -699,14 +784,14 @@ bool db_get_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cdbr)
db_lock(mdb);
if (cdbr->ClientId != 0) { /* find by id */
Mmsg(mdb->cmd,
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
"FROM Client WHERE Client.ClientId=%s",
edit_int64(cdbr->ClientId, ed1));
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
"FROM Client WHERE Client.ClientId=%s",
edit_int64(cdbr->ClientId, ed1));
} else { /* find by name */
mdb->db_escape_string(jcr, esc, cdbr->Name, strlen(cdbr->Name));
Mmsg(mdb->cmd,
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
"FROM Client WHERE Client.Name='%s'", esc);
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
"FROM Client WHERE Client.Name='%s'", esc);
}

if (QUERY_DB(jcr, mdb, mdb->cmd)) {
Expand Down Expand Up @@ -735,6 +820,7 @@ bool db_get_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cdbr)
} else {
Mmsg(mdb->errmsg, _("Client record not found in Catalog.\n"));
}

db_unlock(mdb);
return retval;
}
Expand All @@ -759,7 +845,9 @@ bool db_get_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
num_rows = sql_num_rows(mdb);

/* If more than one, report error, but return first row */
/*
* If more than one, report error, but return first row
*/
if (num_rows > 1) {
Mmsg1(mdb->errmsg, _("More than one Counter!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
Expand Down Expand Up @@ -793,7 +881,6 @@ bool db_get_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
return retval;
}


/**
* Get FileSet Record
* If the FileSetId is non-zero, we get its record,
Expand Down Expand Up @@ -874,11 +961,11 @@ int db_get_num_media_records(JCR *jcr, B_DB *mdb)
* Returns false: on failure
* true: on success
*/
bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, POOL_MEM &volumes, int *num_ids, uint32_t *ids[])
bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, POOL_MEM &volumes, int *num_ids, DBId_t **ids)
{
SQL_ROW row;
int i = 0;
uint32_t *id;
DBId_t *id;
char ed1[50];
bool ok = false;
char esc[MAX_NAME_LENGTH * 2 + 1];
Expand Down Expand Up @@ -938,7 +1025,7 @@ bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, POOL_MEM &volumes, int
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
*num_ids = sql_num_rows(mdb);
if (*num_ids > 0) {
id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
id = (DBId_t *)malloc(*num_ids * sizeof(DBId_t));
while ((row = sql_fetch_row(mdb)) != NULL) {
id[i++] = str_to_uint64(row[0]);
}
Expand All @@ -955,13 +1042,11 @@ bool db_get_media_ids(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr, POOL_MEM &volumes, int
return ok;
}


/**
* This function returns a list of all the DBIds that are returned
* for the query.
* This function returns a list of all the DBIds that are returned for the query.
*
* Returns false: on failure
* true: on success
* Returns false: on failure
* true: on success
*/
bool db_get_query_dbids(JCR *jcr, B_DB *mdb, POOL_MEM &query, dbid_list &ids)
{
Expand Down
15 changes: 9 additions & 6 deletions src/cats/sql_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,20 @@ void db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
} else {
if (mdbr->VolumeName[0] != 0) {
Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolStatus,Enabled,"
"VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten "
"FROM Media WHERE Media.VolumeName='%s'", esc);
"VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten,Storage.Name AS Storage "
"FROM Media,Storage WHERE Media.StorageId=Storage.StorageId "
"AND Media.VolumeName='%s'", esc);
} else if (mdbr->PoolId > 0) {
Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolStatus,Enabled,"
"VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten "
"FROM Media WHERE Media.PoolId=%s ORDER BY MediaId",
"VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten,Storage.Name AS Storage "
"FROM Media,Storage WHERE Media.StorageId=Storage.StorageId "
"AND Media.PoolId=%s ORDER BY MediaId",
edit_int64(mdbr->PoolId, ed1));
} else {
Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolStatus,Enabled,"
"VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten "
"FROM Media ORDER BY MediaId");
"VolBytes,VolFiles,VolRetention,Recycle,Slot,InChanger,MediaType,LastWritten,Storage.Name AS Storage "
"FROM Media,Storage WHERE Media.StorageId=Storage.StorageId "
"ORDER BY MediaId");
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/dird/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ SCHEDRES *select_enable_disable_schedule_resource(UAContext *ua, bool enable);
bool select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
bool select_media_dbr(UAContext *ua, MEDIA_DBR *mr);
bool select_pool_dbr(UAContext *ua, POOL_DBR *pr, const char *argk = "pool");
bool select_storage_dbr(UAContext *ua, STORAGE_DBR *pr, const char *argk = "storage");
bool select_client_dbr(UAContext *ua, CLIENT_DBR *cr);

void start_prompt(UAContext *ua, const char *msg);
Expand All @@ -386,6 +387,7 @@ drive_number_t get_storage_drive(UAContext *ua, STORERES *store);
slot_number_t get_storage_slot(UAContext *ua, STORERES *store);
int get_media_type(UAContext *ua, char *MediaType, int max_media);
bool get_pool_dbr(UAContext *ua, POOL_DBR *pr, const char *argk = "pool");
bool get_storage_dbr(UAContext *ua, STORAGE_DBR *sr, const char *argk = "storage");
bool get_client_dbr(UAContext *ua, CLIENT_DBR *cr);
POOLRES *get_pool_resource(UAContext *ua);
JOBRES *get_restore_job(UAContext *ua);
Expand Down
Loading

0 comments on commit 9c5d8bf

Please sign in to comment.