* should be in find.c
*
* Kern Sibbald, March 2000
+ *
+ * Version $Id$
*/
/*
char file[MAXSTRING];
char spath[MAXSTRING];
char buf[MAXSTRING];
- Dmsg0(20, "get_file_from_catalog\n");
+ Dmsg1(20, "Enter get_file_from_catalog fname=%s \n", fname);
/* Find path without the filename */
for (p=l=fname; *p; p++) {
db_escape_string(buf, spath, pnl);
fdbr->PathId = db_get_path_record(mdb, buf);
- Dmsg1(50, "db_get_path_record PathId=%d\n", fdbr->PathId);
+ Dmsg2(50, "db_get_path_record PathId=%d path=%s\n", fdbr->PathId, buf);
id = db_get_file_record(mdb, fdbr);
int db_get_file_record(B_DB *mdb, FILE_DBR *fdbr)
{
SQL_ROW row;
+ int stat = 0;
- P(mdb->mutex);
+ db_lock(mdb);
Mmsg(&mdb->cmd,
"SELECT FileId, LStat, MD5 from File where File.JobId=%d and File.PathId=%d and \
File.FilenameId=%d", fdbr->JobId, fdbr->PathId, fdbr->FilenameId);
+ Dmsg3(050, "Get_file_record JobId=%d FilenameId=%d PathId=%d\n",
+ fdbr->JobId, fdbr->FilenameId, fdbr->PathId);
+
+ Dmsg1(100, "Query=%s\n", mdb->cmd);
+
if (QUERY_DB(mdb, mdb->cmd)) {
mdb->num_rows = sql_num_rows(mdb);
+ Dmsg1(050, "get_file_record num_rows=%d\n", (int)mdb->num_rows);
/*
* Note, we can find more than one File record with the same
*/
if (mdb->num_rows > 1) {
Emsg1(M_WARNING, 0, _("get_file_record want 1 got rows=%d\n"), mdb->num_rows);
- Emsg1(M_WARNING, 0, "%s\n", mdb->cmd);
+ Emsg1(M_ERROR, 0, "%s", mdb->cmd);
}
if (mdb->num_rows >= 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
- Emsg1(M_ERROR, 0, "Error fetching row: %s\n", sql_strerror(mdb));
+ Mmsg1(&mdb->errmsg, "Error fetching row: %s\n", sql_strerror(mdb));
} else {
fdbr->FileId = (FileId_t)strtod(row[0], NULL);
strncpy(fdbr->LStat, row[1], sizeof(fdbr->LStat));
fdbr->LStat[sizeof(fdbr->LStat)] = 0;
strncpy(fdbr->MD5, row[2], sizeof(fdbr->MD5));
fdbr->MD5[sizeof(fdbr->MD5)] = 0;
- sql_free_result(mdb);
- V(mdb->mutex);
- return 1;
+ stat = 1;
}
}
-
sql_free_result(mdb);
}
- V(mdb->mutex);
- return 0; /* failed */
+ db_unlock(mdb);
+ return stat;
}
static int db_get_filename_record(B_DB *mdb, char *fname)
{
SQL_ROW row;
- int FilenameId;
+ int FilenameId = 0;
if (*fname == 0) {
Mmsg0(&mdb->errmsg, _("Null name given to db_get_filename_record\n"));
Emsg0(M_ABORT, 0, mdb->errmsg);
}
- P(mdb->mutex);
+ db_lock(mdb);
Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name=\"%s\"", fname);
if (QUERY_DB(mdb, mdb->cmd)) {
if (mdb->num_rows > 1) {
Mmsg1(&mdb->errmsg, _("More than one Filename!: %d\n"), (int)(mdb->num_rows));
- Emsg0(M_FATAL, 0, mdb->errmsg);
- } else if (mdb->num_rows == 1) {
+ Emsg1(M_WARNING, 0, _("get_filename_record want 1 got rows=%d\n"), mdb->num_rows);
+ Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
+ }
+ if (mdb->num_rows >= 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
- Emsg0(M_FATAL, 0, mdb->errmsg);
- }
- FilenameId = atoi(row[0]);
- if (FilenameId <= 0) {
- Mmsg2(&mdb->errmsg, _("Create db Filename record %s found bad record: %d\n"),
- mdb->cmd, FilenameId);
- Emsg0(M_ERROR, 0, mdb->errmsg);
+ Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
+ } else {
+ FilenameId = atoi(row[0]);
+ if (FilenameId <= 0) {
+ Mmsg2(&mdb->errmsg, _("Get DB Filename record %s found bad record: %d\n"),
+ mdb->cmd, FilenameId);
+ Emsg1(M_ERROR, 0, "%s", mdb->errmsg);
+ FilenameId = 0;
+ }
}
- sql_free_result(mdb);
- V(mdb->mutex);
- return FilenameId;
-
}
sql_free_result(mdb);
}
- V(mdb->mutex);
- return 0; /* failed */
+ db_unlock(mdb);
+ return FilenameId;
}
/* Get path record
static int db_get_path_record(B_DB *mdb, char *path)
{
SQL_ROW row;
- int PathId;
- /*******FIXME***** move into mdb record and allocate */
- static int cached_id = 0;
- static char cached_path[MAXSTRING];
+ uint32_t PathId = 0;
if (*path == 0) {
Emsg0(M_ABORT, 0, _("Null path given to db_get_path_record\n"));
}
- if (cached_id != 0 && strcmp(cached_path, path) == 0) {
- return cached_id;
+
+ db_lock(mdb);
+
+ if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
+ db_unlock(mdb);
+ return mdb->cached_path_id;
}
- P(mdb->mutex);
Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path=\"%s\"", path);
if (QUERY_DB(mdb, mdb->cmd)) {
if (mdb->num_rows > 1) {
Mmsg1(&mdb->errmsg, _("More than one Path!: %s\n"),
edit_uint64(mdb->num_rows, ed1));
- Emsg0(M_FATAL, 0, mdb->errmsg);
} else if (mdb->num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
- Emsg0(M_FATAL, 0, mdb->errmsg);
- }
- PathId = atoi(row[0]);
- if (PathId != cached_id) {
- cached_id = PathId;
- strcpy(cached_path, path);
+ } else {
+ PathId = atoi(row[0]);
+ if (PathId <= 0) {
+ Mmsg2(&mdb->errmsg, _("Get DB path record %s found bad record: %d\n"),
+ mdb->cmd, PathId);
+ PathId = 0;
+ } else {
+ /* Cache path */
+ if (PathId != mdb->cached_path_id) {
+ mdb->cached_path_id = PathId;
+ mdb->cached_path = check_pool_memory_size(mdb->cached_path,
+ strlen(path)+1);
+ strcpy(mdb->cached_path, path);
+ }
+ }
}
- sql_free_result(mdb);
- V(mdb->mutex);
- return PathId;
}
-
sql_free_result(mdb);
}
- V(mdb->mutex);
- return 0; /* failed */
+ db_unlock(mdb);
+ return PathId;
}
{
SQL_ROW row;
- P(mdb->mutex);
+ db_lock(mdb);
if (jr->JobId == 0) {
Mmsg(&mdb->cmd, "SELECT VolSessionId, VolSessionTime, \
-PoolId, StartTime, EndTime, JobFiles, JobBytes, StartDay, Job \
+PoolId, StartTime, EndTime, JobFiles, JobBytes, JobTDate, Job \
FROM Job WHERE Job=\"%s\"", jr->Job);
} else {
Mmsg(&mdb->cmd, "SELECT VolSessionId, VolSessionTime, \
-PoolId, StartTime, EndTime, JobFiles, JobBytes, StartDay, Job \
+PoolId, StartTime, EndTime, JobFiles, JobBytes, JobTDate, Job \
FROM Job WHERE JobId=%d", jr->JobId);
}
if (!QUERY_DB(mdb, mdb->cmd)) {
- V(mdb->mutex);
+ db_unlock(mdb);
return 0; /* failed */
}
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(&mdb->errmsg, _("No Job found for JobId %d\n"), jr->JobId);
sql_free_result(mdb);
- V(mdb->mutex);
+ db_unlock(mdb);
return 0; /* failed */
}
strcpy(jr->cEndTime, row[4]);
jr->JobFiles = atol(row[5]);
jr->JobBytes = (uint64_t)strtod(row[6], NULL);
- jr->StartDay = (btime_t)strtod(row[7], NULL);
+ jr->JobTDate = (btime_t)strtod(row[7], NULL);
strcpy(jr->Job, row[8]);
sql_free_result(mdb);
- V(mdb->mutex);
+ db_unlock(mdb);
return 1;
}
int stat = 0;
int i;
- P(mdb->mutex);
+ db_lock(mdb);
Mmsg(&mdb->cmd,
"SELECT VolumeName FROM JobMedia,Media WHERE JobMedia.JobId=%d \
AND JobMedia.MediaId=Media.MediaId", JobId);
}
sql_free_result(mdb);
}
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}
{
int stat = 0;
- P(mdb->mutex);
+ db_lock(mdb);
Mmsg(&mdb->cmd, "SELECT count(*) from Pool");
stat = get_sql_record_max(mdb);
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}
int i = 0;
uint32_t *id;
- P(mdb->mutex);
+ db_lock(mdb);
*ids = NULL;
Mmsg(&mdb->cmd, "SELECT PoolId FROM Pool");
if (QUERY_DB(mdb, mdb->cmd)) {
Mmsg(&mdb->errmsg, _("Pool id select failed: ERR=%s\n"), sql_strerror(mdb));
stat = 0;
}
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}
SQL_ROW row;
int stat = 0;
- P(mdb->mutex);
+ db_lock(mdb);
if (pdbr->PoolId != 0) { /* find by id */
Mmsg(&mdb->cmd,
"SELECT PoolId, Name, NumVols, MaxVols, UseOnce, UseCatalog, AcceptAnyVolume, \
+AutoPrune, Recycle, VolRetention, \
PoolType, LabelFormat FROM Pool WHERE Pool.PoolId=%d", pdbr->PoolId);
} else { /* find by name */
Mmsg(&mdb->cmd,
"SELECT PoolId, Name, NumVols, MaxVols, UseOnce, UseCatalog, AcceptAnyVolume, \
+AutoPrune, Recycle, VolRetention, \
PoolType, LabelFormat FROM Pool WHERE Pool.Name=\"%s\"", pdbr->Name);
}
char ed1[30];
Mmsg1(&mdb->errmsg, _("More than one Pool!: %s\n"),
edit_uint64(mdb->num_rows, ed1));
- Emsg0(M_ERROR, 0, mdb->errmsg);
} else if (mdb->num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
- Emsg0(M_ERROR, 0, mdb->errmsg);
} else {
pdbr->PoolId = atoi(row[0]);
strcpy(pdbr->Name, row[1]);
pdbr->UseOnce = atoi(row[4]);
pdbr->UseCatalog = atoi(row[5]);
pdbr->AcceptAnyVolume = atoi(row[6]);
- strcpy(pdbr->PoolType, row[7]);
- if (row[8]) {
- strcpy(pdbr->LabelFormat, row[8]);
+ pdbr->AutoPrune = atoi(row[7]);
+ pdbr->Recycle = atoi(row[8]);
+ pdbr->VolRetention = (btime_t)strtod(row[9], NULL);
+ strcpy(pdbr->PoolType, row[10]);
+ if (row[11]) {
+ strcpy(pdbr->LabelFormat, row[11]);
} else {
pdbr->LabelFormat[0] = 0;
}
}
sql_free_result(mdb);
}
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}
{
int stat = 0;
- P(mdb->mutex);
+ db_lock(mdb);
Mmsg(&mdb->cmd, "SELECT count(*) from Media");
stat = get_sql_record_max(mdb);
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}
int i = 0;
uint32_t *id;
- P(mdb->mutex);
+ db_lock(mdb);
*ids = NULL;
Mmsg(&mdb->cmd, "SELECT MediaId FROM Media");
if (QUERY_DB(mdb, mdb->cmd)) {
Mmsg(&mdb->errmsg, _("Media id select failed: ERR=%s\n"), sql_strerror(mdb));
stat = 0;
}
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}
SQL_ROW row;
int stat = 0;
- P(mdb->mutex);
+ db_lock(mdb);
if (mr->MediaId == 0 && mr->VolumeName[0] == 0) {
Mmsg(&mdb->cmd, "SELECT count(*) from Media");
mr->MediaId = get_sql_record_max(mdb);
- V(mdb->mutex);
+ db_unlock(mdb);
return 1;
}
if (mr->MediaId != 0) { /* find by id */
Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes,\
-MediaType,VolStatus,PoolId \
+MediaType,VolStatus,PoolId,VolRetention,Recycle \
FROM Media WHERE MediaId=%d", mr->MediaId);
} else { /* find by name */
Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes,\
-MediaType,VolStatus,PoolId \
+MediaType,VolStatus,PoolId,VolRetention,Recycle \
FROM Media WHERE VolumeName=\"%s\"", mr->VolumeName);
}
char ed1[30];
Mmsg1(&mdb->errmsg, _("More than one Volume!: %s\n"),
edit_uint64(mdb->num_rows, ed1));
- Emsg0(M_ERROR, 0, mdb->errmsg);
} else if (mdb->num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
- Emsg0(M_ERROR, 0, mdb->errmsg);
} else {
/* return values */
mr->MediaId = atoi(row[0]);
strcpy(mr->MediaType, row[11]);
strcpy(mr->VolStatus, row[12]);
mr->PoolId = atoi(row[13]);
+ mr->VolRetention = (btime_t)strtod(row[14], NULL);
+ mr->Recycle = atoi(row[15]);
stat = mr->MediaId;
}
} else {
}
sql_free_result(mdb);
}
- V(mdb->mutex);
+ db_unlock(mdb);
return stat;
}