1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2002-2012 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2016 Planets Communications B.V.
6    Copyright (C) 2016-2016 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * Kern Sibbald, May MMII
25  */
26 /**
27  * @file
28  * BAREOS Director -- Automatic Recycling of Volumes
29  *                    Recycles Volumes that have been purged
30  */
31 #include "include/bareos.h"
32 #include "dird.h"
33 #include "dird/autorecycle.h"
34 #include "dird/jcr_private.h"
35 #include "dird/next_vol.h"
36 
37 namespace directordaemon {
38 
39 /* Forward referenced functions */
40 
FindRecycledVolume(JobControlRecord * jcr,bool InChanger,MediaDbRecord * mr,StorageResource * store,const char * unwanted_volumes)41 bool FindRecycledVolume(JobControlRecord* jcr,
42                         bool InChanger,
43                         MediaDbRecord* mr,
44                         StorageResource* store,
45                         const char* unwanted_volumes)
46 {
47   bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
48   SetStorageidInMr(store, mr);
49   if (jcr->db->FindNextVolume(jcr, 1, InChanger, mr, unwanted_volumes)) {
50     jcr->impl->MediaId = mr->MediaId;
51     Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->impl->MediaId);
52     PmStrcpy(jcr->VolumeName, mr->VolumeName);
53     SetStorageidInMr(store, mr);
54 
55     return true;
56   }
57 
58   return false;
59 }
60 
61 /**
62  * Look for oldest Purged volume
63  */
RecycleOldestPurgedVolume(JobControlRecord * jcr,bool InChanger,MediaDbRecord * mr,StorageResource * store,const char * unwanted_volumes)64 bool RecycleOldestPurgedVolume(JobControlRecord* jcr,
65                                bool InChanger,
66                                MediaDbRecord* mr,
67                                StorageResource* store,
68                                const char* unwanted_volumes)
69 {
70   bstrncpy(mr->VolStatus, "Purged", sizeof(mr->VolStatus));
71   SetStorageidInMr(store, mr);
72 
73   if (jcr->db->FindNextVolume(jcr, 1, InChanger, mr, unwanted_volumes)) {
74     Dmsg1(20, "Find_next_vol MediaId=%u\n", mr->MediaId);
75     SetStorageidInMr(store, mr);
76     if (RecycleVolume(jcr, mr)) {
77       Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
78       Dmsg1(100, "return 1  RecycleOldestPurgedVolume Vol=%s\n",
79             mr->VolumeName);
80 
81       return true;
82     }
83   }
84   Dmsg0(100, "return 0  RecycleOldestPurgedVolume end\n");
85 
86   return false;
87 }
88 
89 /**
90  * Recycle the specified volume
91  */
RecycleVolume(JobControlRecord * jcr,MediaDbRecord * mr)92 bool RecycleVolume(JobControlRecord* jcr, MediaDbRecord* mr)
93 {
94   bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
95   mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
96   mr->VolBytes = 1;
97   mr->FirstWritten = mr->LastWritten = 0;
98   mr->RecycleCount++;
99   mr->set_first_written = true;
100   SetStorageidInMr(NULL, mr);
101 
102   return jcr->db->UpdateMediaRecord(jcr, mr);
103 }
104 } /* namespace directordaemon */
105