]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/recycle.c
correct date
[bacula/bacula] / bacula / src / dird / recycle.c
1 /*
2  *
3  *   Bacula Director -- Automatic Recycling of Volumes
4  *      Recycles Volumes that have been purged
5  *
6  *     Kern Sibbald, May MMII
7  *
8  *   Version $Id$
9  */
10
11 /*
12    Copyright (C) 2002 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "dird.h"
33 #include "ua.h"
34
35 struct s_oldest_ctx {
36    uint32_t MediaId;
37    char LastWritten[30];
38 };
39
40 static int oldest_handler(void *ctx, int num_fields, char **row)
41 {
42    struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
43
44    if (row[0]) {
45       Dmsg2(100, "oldest_handler %s %s\n", row[0], row[1]);
46    }
47    /* Find oldest Media record */
48    if (row[1] && strcmp(row[1], oldest->LastWritten) < 0) {
49       oldest->MediaId = atoi(row[0]);
50       bstrncpy(oldest->LastWritten, row[1], sizeof(oldest->LastWritten));
51       Dmsg1(100, "New oldest %s\n", row[1]);
52    }
53    return 1;
54 }
55
56 /* Forward referenced functions */
57
58 int find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
59 {
60    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
61    if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
62       jcr->MediaId = mr->MediaId;
63       Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
64       pm_strcpy(&jcr->VolumeName, mr->VolumeName);
65       return 1;
66    }
67    return 0;
68 }
69
70
71 /*
72  *   Look for oldest Purged volume
73  */
74 int recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
75 {
76    struct s_oldest_ctx oldest;
77    POOLMEM *query = get_pool_memory(PM_EMSG);
78    char *select1 =
79           "SELECT MediaId,LastWritten FROM Media "
80           "WHERE PoolId=%u AND Recycle=1 AND VolStatus='Purged' "
81           "AND MediaType='%s' AND InChanger=1";
82    char *select2 =
83           "SELECT MediaId,LastWritten FROM Media "
84           "WHERE PoolId=%u AND Recycle=1 AND VolStatus='Purged' "
85           "AND MediaType='%s'";
86
87
88    Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
89    oldest.MediaId = 0;
90    bstrncpy(oldest.LastWritten, "9999-99-99 99:99:99", sizeof(oldest.LastWritten));
91    if (InChanger) {
92       Mmsg(&query, select1, mr->PoolId, mr->MediaType);
93    } else {
94       Mmsg(&query, select2, mr->PoolId, mr->MediaType);
95    }
96
97    if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
98       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
99       Dmsg0(100, "Exit 0  recycle_oldest_purged_volume query\n");
100       free_pool_memory(query);
101       return 0;
102    }
103    free_pool_memory(query);
104    Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
105    if (oldest.MediaId != 0) {
106       mr->MediaId = oldest.MediaId;
107       if (db_get_media_record(jcr, jcr->db, mr)) {
108          if (recycle_volume(jcr, mr)) {
109             Jmsg(jcr, M_INFO, 0, "Recycled volume \"%s\"\n", mr->VolumeName);
110             Dmsg1(100, "Exit 1  recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
111             return 1;
112          }
113       }
114       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
115    }
116    Dmsg0(100, "Exit 0  recycle_oldest_purged_volume end\n");
117    return 0;    
118 }
119
120 /*
121  * Recycle the specified volume
122  */
123 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
124 {
125    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
126    mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
127    mr->VolBytes = 1;
128    mr->FirstWritten = mr->LastWritten = 0;
129    return db_update_media_record(jcr, jcr->db, mr);
130 }