]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/bvfs.c
Fix #5346 .bvfs_lsfiles and .bvfs_restore to handle deleted files
[bacula/bacula] / bacula / src / cats / bvfs.c
index 9b73c992f5330f5eb71336d5c3bf5ae70e70e1ef..27f76d54b7c0c764c0a54de47faec267d2a98847 100644 (file)
@@ -270,7 +270,10 @@ static void build_path_hierarchy(JCR *jcr, B_DB *mdb,
               "SELECT PPathId FROM PathHierarchy WHERE PathId = %s",
               pathid);
 
-         QUERY_DB(jcr, mdb, mdb->cmd);
+         if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
+            goto bail_out;      /* Query failed, just leave */
+         }
+
          /* Do we have a result ? */
          if (sql_num_rows(mdb) > 0) {
             ppathid_cache.insert(pathid);
@@ -293,7 +296,9 @@ static void build_path_hierarchy(JCR *jcr, B_DB *mdb,
                  "VALUES (%s,%lld)",
                  pathid, (uint64_t) parent.PathId);
             
-            INSERT_DB(jcr, mdb, mdb->cmd);
+            if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
+               goto bail_out;   /* Can't insert the record, just leave */
+            }
 
             edit_uint64(parent.PathId, pathid);
             path = mdb->path;   /* already done */
@@ -313,14 +318,16 @@ bail_out:
 
 /* 
  * Internal function to update path_hierarchy cache with a shared pathid cache
+ * return Error 0
+ *        OK    1
  */
-static void update_path_hierarchy_cache(JCR *jcr,
+static int update_path_hierarchy_cache(JCR *jcr,
                                         B_DB *mdb,
                                         pathid_cache &ppathid_cache,
                                         JobId_t JobId)
 {
    Dmsg0(dbglevel, "update_path_hierarchy_cache()\n");
-
+   int ret=0;
    uint32_t num;
    char jobid[50];
    edit_uint64(JobId, jobid);
@@ -332,6 +339,7 @@ static void update_path_hierarchy_cache(JCR *jcr,
    
    if (!QUERY_DB(jcr, mdb, mdb->cmd) || sql_num_rows(mdb) > 0) {
       Dmsg1(dbglevel, "already computed %d\n", (uint32_t)JobId );
+      ret = 1;
       goto bail_out;
    }
 
@@ -344,7 +352,11 @@ static void update_path_hierarchy_cache(JCR *jcr,
                              "FROM BaseFiles JOIN File AS F USING (FileId) "
                             "WHERE BaseFiles.JobId = %s) AS B",
         jobid, jobid);
-   QUERY_DB(jcr, mdb, mdb->cmd);
+
+   if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
+      Dmsg1(dbglevel, "Can't fill PathVisibility %d\n", (uint32_t)JobId );
+      goto bail_out;
+   }
 
    /* Now we have to do the directory recursion stuff to determine missing
     * visibility We try to avoid recursion, to be as fast as possible We also
@@ -360,7 +372,11 @@ static void update_path_hierarchy_cache(JCR *jcr,
         "AND PathHierarchy.PathId IS NULL "
       "ORDER BY Path", jobid);
    Dmsg1(dbglevel_sql, "q=%s\n", mdb->cmd);
-   QUERY_DB(jcr, mdb, mdb->cmd);
+
+   if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
+      Dmsg1(dbglevel, "Can't get new Path %d\n", (uint32_t)JobId );
+      goto bail_out;
+   }
 
    /* TODO: I need to reuse the DB connection without emptying the result 
     * So, now i'm copying the result in memory to be able to query the
@@ -412,8 +428,8 @@ static void update_path_hierarchy_cache(JCR *jcr,
    }
 
    do {
-      QUERY_DB(jcr, mdb, mdb->cmd);
-   } while (sql_affected_rows(mdb) > 0);
+      ret = QUERY_DB(jcr, mdb, mdb->cmd);
+   } while (ret && sql_affected_rows(mdb) > 0);
    
    Mmsg(mdb->cmd, "UPDATE Job SET HasCache=1 WHERE JobId=%s", jobid);
    UPDATE_DB(jcr, mdb, mdb->cmd);
@@ -421,6 +437,7 @@ static void update_path_hierarchy_cache(JCR *jcr,
 bail_out:
    db_end_transaction(jcr, mdb);
    db_unlock(mdb);
+   return ret;
 }
 
 /* 
@@ -511,24 +528,28 @@ void bvfs_update_cache(JCR *jcr, B_DB *mdb)
 /*
  * Update the bvfs cache for given jobids (1,2,3,4)
  */
-void
+int
 bvfs_update_path_hierarchy_cache(JCR *jcr, B_DB *mdb, char *jobids)
 {
    pathid_cache ppathid_cache;
    JobId_t JobId;
    char *p;
+   int ret=1;
 
    for (p=jobids; ; ) {
       int stat = get_next_jobid_from_list(&p, &JobId);
       if (stat < 0) {
-         return;
+         return 0;
       }
       if (stat == 0) {
          break;
       }
       Dmsg1(dbglevel, "Updating cache for %lld\n", (uint64_t)JobId);
-      update_path_hierarchy_cache(jcr, mdb, ppathid_cache, JobId);
+      if (!update_path_hierarchy_cache(jcr, mdb, ppathid_cache, JobId)) {
+         ret = 0;
+      }
    }
+   return ret;
 }
 
 /* 
@@ -544,7 +565,9 @@ bool Bvfs::ch_dir(const char *path)
 {
    pm_strcpy(db->path, path);
    db->pnl = strlen(db->path);
+   db_lock(db);
    ch_dir(db_get_path_record(jcr, db)); 
+   db_unlock(db);
    return pwd_id != 0;
 }
 
@@ -591,8 +614,12 @@ void Bvfs::get_all_file_versions(DBId_t pathid, DBId_t fnid, const char *client)
 
 DBId_t Bvfs::get_root()
 {
+   int p;
    *db->path = 0;
-   return db_get_path_record(jcr, db);
+   db_lock(db);
+   p = db_get_path_record(jcr, db);
+   db_unlock(db);
+   return p;
 }
 
 static int path_handler(void *ctx, int fields, char **row)
@@ -859,7 +886,7 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
 {
    POOL_MEM query;
    POOL_MEM tmp, tmp2;
-   int64_t id, jobid;
+   int64_t id, jobid, prev_jobid;
    bool init=false;
    bool ret=false;
    /* check args */
@@ -874,12 +901,22 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
       return false;
    }
 
+   db_lock(db);
+
+   /* Cleanup old tables first */
+   Mmsg(query, "DROP TABLE btemp%s", output_table);
+   db_sql_query(db, query.c_str());
+
+   Mmsg(query, "DROP TABLE %s", output_table);
+   db_sql_query(db, query.c_str());
+
    Mmsg(query, "CREATE TABLE btemp%s AS ", output_table);
 
    if (*fileid) {               /* Select files with their direct id */
       init=true;
-      Mmsg(tmp,"SELECT JobId, JobTDate, FileIndex, FilenameId, PathId, FileId "
-                  "FROM File JOIN Job USING (JobId) WHERE FileId IN (%s)",
+      Mmsg(tmp,"SELECT Job.JobId, JobTDate, FileIndex, FilenameId, "
+                      "PathId, FileId "
+                 "FROM File JOIN Job USING (JobId) WHERE FileId IN (%s)",
            fileid);
       pm_strcat(query, tmp.c_str());
    }
@@ -891,7 +928,7 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
       if (!db_sql_query(db, tmp.c_str(), get_path_handler, (void *)&tmp2)) {
          Dmsg0(dbglevel, "Can't search for path\n");
          /* print error */
-         return false;
+         goto bail_out;
       }
       if (!strcmp(tmp2.c_str(), "")) { /* path not found */
          Dmsg3(dbglevel, "Path not found %lld q=%s s=%s\n",
@@ -920,7 +957,7 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
          query.strcat(" UNION ");
       }
 
-      Mmsg(tmp, "SELECT JobId, JobTDate, File.FileIndex, File.FilenameId, "
+      Mmsg(tmp, "SELECT Job.JobId, JobTDate, File.FileIndex, File.FilenameId, "
                         "File.PathId, FileId "
                    "FROM Path JOIN File USING (PathId) JOIN Job USING (JobId) "
                   "WHERE Path.Path LIKE '%s' AND File.JobId IN (%s) ", 
@@ -943,11 +980,11 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
    }
 
    /* expect jobid,fileindex */
-   int64_t prev_jobid=0;
+   prev_jobid=0;
    while (get_next_id_from_list(&hardlink, &jobid) == 1) {
       if (get_next_id_from_list(&hardlink, &id) != 1) {
          Dmsg0(dbglevel, "hardlink should be two by two\n");
-         return false;
+         goto bail_out;
       }
       if (jobid != prev_jobid) { /* new job */
          if (prev_jobid == 0) {  /* first jobid */
@@ -958,7 +995,7 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
             tmp.strcat(") UNION ");
             query.strcat(tmp.c_str());
          }
-         Mmsg(tmp,   "SELECT JobId, JobTDate, FileIndex, FilenameId, "
+         Mmsg(tmp,   "SELECT Job.JobId, JobTDate, FileIndex, FilenameId, "
                             "PathId, FileId "
                        "FROM File JOIN Job USING (JobId) WHERE JobId = %lld " 
                         "AND FileIndex IN (%lld", jobid, id);
@@ -983,8 +1020,8 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
       goto bail_out;
    }
 
-   /* TODO: handle basejob and SQLite3 */
-   Mmsg(query, sql_bvfs_select[db_get_type_index(db)], output_table, output_table);
+   Mmsg(query, sql_bvfs_select[db_get_type_index(db)], 
+        output_table, output_table, output_table);
 
    /* TODO: handle jobid filter */
    Dmsg1(dbglevel_sql, "q=%s\n", query.c_str());
@@ -1009,6 +1046,7 @@ bool Bvfs::compute_restore_list(char *fileid, char *dirid, char *hardlink,
 bail_out:
    Mmsg(query, "DROP TABLE btemp%s", output_table);
    db_sql_query(db, query.c_str(), NULL, NULL);
+   db_unlock(db);
    return ret;
 }