]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/recycle.c
Added code to detect that no files were inserted into the
[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-2004 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       oldest->MediaId = str_to_int64(row[0]);
46       bstrncpy(oldest->LastWritten, row[1]?row[1]:"", sizeof(oldest->LastWritten));
47       Dmsg1(100, "New oldest %s\n", row[1]?row[1]:"");
48    }
49    return 1;
50 }
51
52 /* Forward referenced functions */
53
54 int find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
55 {
56    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
57    if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
58       jcr->MediaId = mr->MediaId;
59       Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
60       pm_strcpy(jcr->VolumeName, mr->VolumeName);
61       return 1;
62    }
63    return 0;
64 }
65
66
67 /*
68  *   Look for oldest Purged volume
69  */
70 int recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
71 {
72    struct s_oldest_ctx oldest;
73    char ed1[50];
74    POOLMEM *query = get_pool_memory(PM_EMSG);
75    const char *select =
76           "SELECT MediaId,LastWritten FROM Media "
77           "WHERE PoolId=%s AND Recycle=1 AND VolStatus='Purged' "
78           "AND MediaType='%s' %s"
79           "ORDER BY LastWritten ASC,MediaId LIMIT 1";
80
81    Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
82    oldest.MediaId = 0;
83    if (InChanger) {
84       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, 
85            "AND InChanger=1 ");
86    } else {
87       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
88    }
89
90    if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
91       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
92       Dmsg0(100, "return 0  recycle_oldest_purged_volume query\n");
93       free_pool_memory(query);
94       return 0;
95    }
96    free_pool_memory(query);
97    Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
98    if (oldest.MediaId != 0) {
99       mr->MediaId = oldest.MediaId;
100       if (db_get_media_record(jcr, jcr->db, mr)) {
101          if (recycle_volume(jcr, mr)) {
102             Jmsg(jcr, M_INFO, 0, "Recycled volume \"%s\"\n", mr->VolumeName);
103             Dmsg1(100, "return 1  recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
104             return 1;
105          }
106       }
107       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
108    }
109    Dmsg0(100, "return 0  recycle_oldest_purged_volume end\n");
110    return 0;
111 }
112
113 /*
114  * Recycle the specified volume
115  */
116 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
117 {
118    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
119    mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
120    mr->VolBytes = 1;
121    mr->FirstWritten = mr->LastWritten = 0;
122    return db_update_media_record(jcr, jcr->db, mr);
123 }