]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
improve code for volume selection for encrypted volumes
authorAlain Spineux <alain@baculasystems.com>
Thu, 24 Nov 2022 12:22:10 +0000 (13:22 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:57:00 +0000 (13:57 +0200)
bacula/src/cats/cats.h
bacula/src/cats/sql_find.c
bacula/src/dird/dird.h
bacula/src/dird/next_vol.c
bacula/src/dird/ua_output.c
bacula/src/dird/ua_status.c
bacula/src/stored/askdir.c

index 241fa8ddd6ef6bbda7e985cc1e9222a92bfd5fab..84c1650bea1782544630080973400f6a9eac9529 100644 (file)
@@ -71,6 +71,17 @@ typedef enum {
 
 void append_filter(POOLMEM **buf, char *cond);
 
+/* Flags for find_next_volume_for_append() & sql_find */
+enum {
+  fnv_create_vol    = true,
+  fnv_no_create_vol = false,
+  fnv_prune         = true,
+  fnv_no_prune      = false,
+  fnv_not_encrypted = false,
+  fnv_encrypted     = true,
+  fnv_encrypted_any = -1 /* don't care if the volume is encrypted or not */
+};
+
 /* ==============================================================
  *
  *  What follows are definitions that are used "globally" for all
index bc5139391efa2014bca696477e032a480075963d..3456d5a0f9963da05f97a08c834b7e23fbac66ae 100644 (file)
@@ -405,6 +405,7 @@ bool BDB::bdb_find_last_jobid(JCR *jcr, const char *Name, JOB_DBR *jr)
  */
 int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr)
 {
+   POOL_MEM volencrypted(PM_FNAME);
    SQL_ROW row = NULL;
    int numrows;
    const char *order;
@@ -418,6 +419,10 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr)
 
    if (item == -1) {       /* find oldest volume */
       /* Find oldest volume */
+      if (mr->VolEncrypted != fnv_encrypted_any) {
+         /* we have a requirement on VolEncrypted */
+         Mmsg(volencrypted, "AND (VolStatus!='Append' OR VolEncrypted=%d) ", mr->VolEncrypted);
+      }
       Mmsg(cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
          "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
          "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
@@ -430,15 +435,14 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr)
          "FROM Media WHERE PoolId=%s AND MediaType='%s' "
          " AND (VolStatus IN ('Full', 'Append', 'Used') OR (VolStatus IN ('Recycle', 'Purged', 'Used') AND Recycle=1)) "
          " AND Enabled=1 "
-         " AND (VolStatus != 'Append' OR VolEncrypted=%d) "
+         "%s" /* volencrypted */
          "ORDER BY LastWritten LIMIT 1",
-         edit_int64(mr->PoolId, ed1), esc_type, mr->VolEncrypted);
+         edit_int64(mr->PoolId, ed1), esc_type, volencrypted.c_str());
      item = 1;
    } else {
       POOL_MEM changer(PM_FNAME);
       POOL_MEM voltype(PM_FNAME);
       POOL_MEM exclude(PM_FNAME);
-      POOL_MEM volencrypted(PM_FNAME);
       /* Find next available volume */
       /* ***FIXME***
        * replace switch with
@@ -470,7 +474,7 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr)
          order = sql_media_order_most_recently_written[bdb_get_type_index()];    /* take most recently written */
       }
       if (strcmp(mr->VolStatus, "Append") == 0) {
-         Mmsg(volencrypted, " AND VolEncrypted=%d", mr->VolEncrypted);
+         Mmsg(volencrypted, "AND VolEncrypted=%d", mr->VolEncrypted);
       }
       if (mr->VolType == 0) {
          Mmsg(voltype, "");
@@ -493,10 +497,10 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr)
          "VolEncrypted "
          "FROM Media WHERE PoolId=%s AND MediaType='%s' AND Enabled=1 "
          "AND VolStatus='%s' "
-         "%s "
-         "%s "
-         "%s "
-         "%s "
+         "%s " /* volencrypted */
+         "%s "  /* voltype */
+         "%s "  /* changer */
+         "%s "  /* exclude */
          "%s LIMIT %d",
          edit_int64(mr->PoolId, ed1), esc_type,
          esc_status, volencrypted.c_str(),
index 311b69a8fdf32e7c04cc6fa8b46c606f2ccb794c..c4cc7deb2d24109171dd7326b2b02cf0f3aba3a6 100644 (file)
@@ -60,14 +60,6 @@ struct del_ctx {
    int tot_ids;                       /* total to process */
 };
 
-/* Flags for find_next_volume_for_append() */
-enum {
-  fnv_create_vol    = true,
-  fnv_no_create_vol = false,
-  fnv_prune         = true,
-  fnv_no_prune      = false
-};
-
 typedef struct {
    char    *plugin_name;
    POOLMEM *content;
index 91e0cd32edb622625e0be1268199e1860f5814a6..981527ea87d82c21a2b68bab238bfadfb3b3a36f 100644 (file)
@@ -121,11 +121,7 @@ int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, int index,
    /* Make sure we don't send two times the same volume in the same session */
    set_volume_to_exclude_list(jcr, index, mr);
 
-   if (vol_encrypted == 1) {
-      mr->VolEncrypted = 1;
-   } else {
-      mr->VolEncrypted = 0;
-   }
+   mr->VolEncrypted = vol_encrypted; /* 0=no, 1=yes, -1=any */
 
    /*
     * Find the Next Volume for Append
index d5d61cf7f893d887ef12ce50c6cf802af92be44e..9af03dfee7e911f8f6046a76ef6198baddc9e850 100644 (file)
@@ -1199,7 +1199,7 @@ static bool list_nextvol(UAContext *ua, int ndays)
       set_storageid_in_mr(store.store, &mr);
       /* no need to set ScratchPoolId, since we use fnv_no_create_vol */
       if (!find_next_volume_for_append(jcr, &mr, 1, fnv_no_create_vol, fnv_prune,
-            -1 /* no protect */, -1 /* vol_encrypted*/, errmsg)) {
+            -1 /* no protect */, fnv_encrypted_any, errmsg)) {
          ua->error_msg(_("Could not find next Volume for Job %s (Pool=%s, Level=%s).%s\n"),
                        job->name(), pr.Name, level_to_str(edl, sizeof(edl), run->level), errmsg.c_str());
       } else {
index e454ad9faefdeb0dd02a9451a23d2b1d37cfb7ba..c83c613ccc1f4033bd0dd963f022567b1a96bde9 100644 (file)
@@ -778,7 +778,8 @@ static void prt_runtime(UAContext *ua, sched_pkt *sp, int novolume, OutputWriter
          } else {
             Dmsg0(250, "call find_next_volume_for_append\n");
             /* no need to set ScratchPoolId, since we use fnv_no_create_vol */
-            ok = find_next_volume_for_append(jcr, &mr, 1, fnv_no_create_vol, fnv_no_prune, -1, -1, tmp);
+            ok = find_next_volume_for_append(jcr, &mr, 1, fnv_no_create_vol,
+                    fnv_no_prune, -1, fnv_encrypted_any, tmp);
          }
       }
       if (!ok) {
index 31c8f2ebe58bad4c44afd1834c8280c95b211e81..3476fa0f5f41f66f4b1fdf54d83bbd53a0bbf016 100644 (file)
@@ -343,7 +343,7 @@ bool dir_find_next_appendable_volume(DCR *dcr)
        bash_spaces(dcr->pool_name);
        dir->fsend(Find_media, jcr->JobId, vol_index, dcr->pool_name, dcr->media_type,
                   dcr->dev->dev_type, can_create, dcr->dev->use_protect(),
-                  dcr->dev->use_volume_encryption());
+                  dcr->dev->use_volume_encryption()?1:0);
        unbash_spaces(dcr->media_type);
        unbash_spaces(dcr->pool_name);
        Dmsg1(dbglvl, ">dird %s", dir->msg);