*/
/*
- Copyright (C) 2002 Kern Sibbald and John Walker
+ Copyright (C) 2002-2004 Kern Sibbald and John Walker
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
if (row[0]) {
- Dmsg2(100, "oldest_handler %s %s\n", row[0], row[1]);
- }
- /* Find oldest Media record */
- if (row[1] && strcmp(row[1], oldest->LastWritten) < 0) {
- oldest->MediaId = atoi(row[0]);
- strcpy(oldest->LastWritten, row[1]);
- Dmsg1(100, "New oldest %s\n", row[1]);
+ oldest->MediaId = str_to_int64(row[0]);
+ bstrncpy(oldest->LastWritten, row[1]?row[1]:"", sizeof(oldest->LastWritten));
+ Dmsg1(100, "New oldest %s\n", row[1]?row[1]:"");
}
return 1;
}
/* Forward referenced functions */
-int find_recycled_volume(JCR *jcr, MEDIA_DBR *mr)
+int find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
{
- strcpy(mr->VolStatus, "Recycle");
- if (db_find_next_volume(jcr, jcr->db, 1, mr)) {
+ bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
+ if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
jcr->MediaId = mr->MediaId;
- Dmsg1(20, "Find_next_vol MediaId=%d\n", jcr->MediaId);
- strcpy(jcr->VolumeName, mr->VolumeName);
+ Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
+ pm_strcpy(jcr->VolumeName, mr->VolumeName);
return 1;
}
return 0;
/*
* Look for oldest Purged volume
*/
-int recycle_a_volume(JCR *jcr, MEDIA_DBR *mr)
+int recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
{
struct s_oldest_ctx oldest;
POOLMEM *query = get_pool_memory(PM_EMSG);
- char *select =
-"SELECT MediaId, LastWritten FROM Media "
-"WHERE PoolId=%d AND Recycle=1 AND VolStatus=\"Purged\" "
-"AND MediaType=\"%s\"";
+ const char *select =
+ "SELECT MediaId,LastWritten FROM Media "
+ "WHERE PoolId=%u AND Recycle=1 AND VolStatus='Purged' "
+ "AND MediaType='%s' %s"
+ "ORDER BY LastWritten ASC,MediaId LIMIT 1";
- Dmsg0(100, "Enter recycle_a_volume\n");
+ Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
oldest.MediaId = 0;
- strcpy(oldest.LastWritten, "9999-99-99 99:99:99");
- Mmsg(&query, select, mr->PoolId, mr->MediaType);
+ if (InChanger) {
+ Mmsg(query, select, mr->PoolId, mr->MediaType, "AND InChanger=1 ");
+ } else {
+ Mmsg(query, select, mr->PoolId, mr->MediaType, "");
+ }
+
if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
- Dmsg0(100, "Exit 0 recycle_a_volume query\n");
+ Dmsg0(100, "return 0 recycle_oldest_purged_volume query\n");
free_pool_memory(query);
return 0;
}
if (oldest.MediaId != 0) {
mr->MediaId = oldest.MediaId;
if (db_get_media_record(jcr, jcr->db, mr)) {
- strcpy(mr->VolStatus, "Recycle");
- if (db_update_media_record(jcr, jcr->db, mr)) {
- Jmsg(jcr, M_INFO, 0, "Recycled volume %s\n", mr->VolumeName);
- Dmsg1(100, "Exit 1 recycle_a_volume Vol=%s\n", mr->VolumeName);
+ if (recycle_volume(jcr, mr)) {
+ Jmsg(jcr, M_INFO, 0, "Recycled volume \"%s\"\n", mr->VolumeName);
+ Dmsg1(100, "return 1 recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
return 1;
}
}
Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
}
- Dmsg0(100, "Exit 0 recycle_a_volume end\n");
+ Dmsg0(100, "return 0 recycle_oldest_purged_volume end\n");
return 0;
}
+
+/*
+ * Recycle the specified volume
+ */
+int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
+{
+ bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
+ mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
+ mr->VolBytes = 1;
+ mr->FirstWritten = mr->LastWritten = 0;
+ return db_update_media_record(jcr, jcr->db, mr);
+}