]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/recycle.c
kes Extend new GUI api code to tree commands.
[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    Bacula® - The Network Backup Solution
12
13    Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
14
15    The main author of Bacula is Kern Sibbald, with contributions from
16    many others, a complete list can be found in the file AUTHORS.
17    This program is Free Software; you can redistribute it and/or
18    modify it under the terms of version two of the GNU General Public
19    License as published by the Free Software Foundation plus additions
20    that are listed in the file LICENSE.
21
22    This program is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30    02110-1301, USA.
31
32    Bacula® is a registered trademark of John Walker.
33    The licensor of Bacula is the Free Software Foundation Europe
34    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35    Switzerland, email:ftf@fsfeurope.org.
36 */
37
38 #include "bacula.h"
39 #include "dird.h"
40 #include "ua.h"
41
42 struct s_oldest_ctx {
43    uint32_t MediaId;
44    char LastWritten[30];
45 };
46
47 static int oldest_handler(void *ctx, int num_fields, char **row)
48 {
49    struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
50
51    if (row[0]) {
52       oldest->MediaId = str_to_int64(row[0]);
53       bstrncpy(oldest->LastWritten, row[1]?row[1]:"", sizeof(oldest->LastWritten));
54       Dmsg1(100, "New oldest %s\n", row[1]?row[1]:"");
55    }
56    return 1;
57 }
58
59 /* Forward referenced functions */
60
61 bool find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
62 {
63    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
64    if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
65       jcr->MediaId = mr->MediaId;
66       Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
67       pm_strcpy(jcr->VolumeName, mr->VolumeName);
68       return true;
69    }
70    return false;
71 }
72
73
74 /*
75  *   Look for oldest Purged volume
76  */
77 bool recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
78 {
79    struct s_oldest_ctx oldest;
80    char ed1[50];
81    POOLMEM *query = get_pool_memory(PM_EMSG);
82    const char *select =
83           "SELECT MediaId,LastWritten FROM Media "
84           "WHERE PoolId=%s AND Recycle=1 AND VolStatus='Purged' "
85           "AND Enabled=1 AND MediaType='%s' %s"
86           "ORDER BY LastWritten ASC,MediaId LIMIT 1";
87
88    Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
89    oldest.MediaId = 0;
90    if (InChanger) {
91       char changer[100];
92       bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
93          edit_int64(mr->StorageId, ed1));
94       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, changer);
95    } else {
96       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
97    }
98
99    if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
100       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
101       Dmsg0(100, "return 0  recycle_oldest_purged_volume query\n");
102       free_pool_memory(query);
103       return false;
104    }
105    free_pool_memory(query);
106    Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
107    if (oldest.MediaId != 0) {
108       mr->MediaId = oldest.MediaId;
109       if (db_get_media_record(jcr, jcr->db, mr)) {
110          if (recycle_volume(jcr, mr)) {
111             Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
112             Dmsg1(100, "return 1  recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
113             return true;
114          }
115       }
116       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
117    }
118    Dmsg0(100, "return 0  recycle_oldest_purged_volume end\n");
119    return false;
120 }
121
122 /*
123  * Recycle the specified volume
124  */
125 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
126 {
127    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
128    mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
129    mr->VolBytes = 1;
130    mr->FirstWritten = mr->LastWritten = 0;
131    mr->RecycleCount++;
132    mr->set_first_written = true;
133    return db_update_media_record(jcr, jcr->db, mr);
134 }