]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_purge.c
tidy up make clean and ensure we have all the create/drop etc files
[bacula/bacula] / bacula / src / dird / ua_purge.c
index b41a095b0247badacf134c04920443f613e634fa..c0ce011d358696664e4921c53d014941302f00df 100644 (file)
@@ -164,7 +164,6 @@ int purgecmd(UAContext *ua, char *cmd)
 {
    CLIENT *client;
    MEDIA_DBR mr;
-   POOL_DBR pr;
    JOB_DBR  jr;
    static char *keywords[] = {
       N_("files"),
@@ -185,11 +184,12 @@ int purgecmd(UAContext *ua, char *cmd)
       NULL};
       
    bsendmsg(ua, _(
-      "This command is DANGEROUS!!!\n"
+      "\nThis command is can be DANGEROUS!!!\n\n"
       "It purges (deletes) all Files from a Job,\n"
       "JobId, Client or Volume; or it purges (deletes)\n"
-      "all Jobs from a Client or Volume. Normally you\n" 
-      "should use the PRUNE command instead.\n"));
+      "all Jobs from a Client or Volume without regard\n"
+      "for retention periods. Normally you should use the\n" 
+      "PRUNE command, which respects retention periods.\n"));
 
    if (!open_db(ua)) {
       return 1;
@@ -205,11 +205,13 @@ int purgecmd(UAContext *ua, char *cmd)
         }
         return 1;
       case 2:                        /* client */
-        client = select_client_resource(ua);
-        purge_files_from_client(ua, client);
+        client = get_client_resource(ua);
+        if (client) {
+           purge_files_from_client(ua, client);
+        }
         return 1;
       case 3:                        /* Volume */
-        if (select_pool_and_media_dbr(ua, &pr, &mr)) {
+        if (select_media_dbr(ua, &mr)) {
            purge_files_from_volume(ua, &mr);
         }
         return 1;
@@ -218,18 +220,20 @@ int purgecmd(UAContext *ua, char *cmd)
    case 1:
       switch(find_arg_keyword(ua, jobs_keywords)) {
       case 0:                        /* client */
-        client = select_client_resource(ua);
-        purge_jobs_from_client(ua, client);
+        client = get_client_resource(ua);
+        if (client) {
+           purge_jobs_from_client(ua, client);
+        }
         return 1;
       case 1:                        /* Volume */
-        if (select_pool_and_media_dbr(ua, &pr, &mr)) {
+        if (select_media_dbr(ua, &mr)) {
            purge_jobs_from_volume(ua, &mr);
         }
         return 1;
       }
    /* Volume */
    case 2:
-      if (select_pool_and_media_dbr(ua, &pr, &mr)) {
+      if (select_media_dbr(ua, &mr)) {
         purge_jobs_from_volume(ua, &mr);
       }
       return 1;
@@ -238,21 +242,19 @@ int purgecmd(UAContext *ua, char *cmd)
    }
    switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
    case 0:                           /* files */
-      client = select_client_resource(ua);
-      if (!client) {
-        return 1;
+      client = get_client_resource(ua);
+      if (client) {
+        purge_files_from_client(ua, client);
       }
-      purge_files_from_client(ua, client);
       break;
    case 1:                           /* jobs */
-      client = select_client_resource(ua);
-      if (!client) {
-        return 1;
+      client = get_client_resource(ua);
+      if (client) {
+        purge_jobs_from_client(ua, client);
       }
-      purge_jobs_from_client(ua, client);
       break;
    case 2:                           /* Volume */
-      if (select_pool_and_media_dbr(ua, &pr, &mr)) {
+      if (select_media_dbr(ua, &mr)) {
         purge_jobs_from_volume(ua, &mr);
       }
       break;
@@ -261,7 +263,7 @@ int purgecmd(UAContext *ua, char *cmd)
 }
 
 /*
- * Prune File records from the database. For any Job which
+ * Purge File records from the database. For any Job which
  * is older than the retention period, we unconditionally delete
  * all File records for that Job.  This is simple enough that no
  * temporary tables are needed. We simply make an in memory list of
@@ -338,12 +340,12 @@ bail_out:
 
 
 /*
- * Purging Jobs is a bit more complicated than purging Files
- * because we delete Job records only if there is a more current
- * backup of the FileSet. Otherwise, we keep the Job record.
- * In other words, we never delete the only Job record that
- * contains a current backup of a FileSet. This prevents the
- * Volume from being recycled and destroying a current backup.
+ * Purge Job records from the database. For any Job which
+ * is older than the retention period, we unconditionally delete
+ * it and all File records for that Job.  This is simple enough that no
+ * temporary tables are needed. We simply make an in memory list of
+ * the JobIds meeting the prune conditions, then delete the Job,
+ * Files, and JobMedia records in that list.
  */
 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
 {
@@ -355,7 +357,7 @@ int purge_jobs_from_client(UAContext *ua, CLIENT *client)
    memset(&cr, 0, sizeof(cr));
    memset(&del, 0, sizeof(del));
 
-   strcpy(cr.Name, client->hdr.name);
+   bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
       return 0;
    }
@@ -427,10 +429,10 @@ void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
 {
    char *query = (char *)get_pool_memory(PM_MESSAGE);
    
-   Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
+   Mmsg(&query, "DELETE FROM File WHERE JobId=%u", jr->JobId);
    db_sql_query(ua->db, query, NULL, (void *)NULL);
 
-   Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
+   Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%u", jr->JobId);
    db_sql_query(ua->db, query, NULL, (void *)NULL);
 
    free_pool_memory(query);
@@ -451,6 +453,18 @@ int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr)
    int i, stat = 0;
    JOB_DBR jr;
 
+   stat = strcmp(mr->VolStatus, "Append") == 0 || 
+          strcmp(mr->VolStatus, "Full")   == 0 ||
+          strcmp(mr->VolStatus, "Used")   == 0 || 
+          strcmp(mr->VolStatus, "Error")  == 0; 
+   if (!stat) {
+      bsendmsg(ua, "\n");
+      bsendmsg(ua, _("Volume \"%s\" has VolStatus \"%s\" and cannot be purged.\n"
+                     "The VolStatus must be: Append, Full, Used, or Error to be purged.\n"),
+                    mr->VolumeName, mr->VolStatus);
+      goto bail_out;
+   }
+   
    memset(&jr, 0, sizeof(jr));
    memset(&del, 0, sizeof(del));
    cnt.count = 0;
@@ -500,7 +514,7 @@ int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr)
    if (del.JobId) {
       free(del.JobId);
    }
-   bsendmsg(ua, _("%d File%s on Volume %s purged from catalog.\n"), del.num_del,
+   bsendmsg(ua, _("%d File%s on Volume \"%s\" purged from catalog.\n"), del.num_del,
       del.num_del==1?"":"s", mr->VolumeName);
 
    /* If purged, mark it so */