+ return ok;
+}
+
+/*
+ * Find the last "accurate" backup state (that can take deleted files in account)
+ * 1) Get all files with jobid in list (F subquery)
+ * 2) Take only the last version of each file (Temp subquery) => accurate list is ok
+ * 3) Join the result to file table to get fileindex, jobid and lstat information
+ *
+ * TODO: See if we can do the SORT only if needed (as an argument)
+ */
+bool db_get_file_list(JCR *jcr, B_DB *mdb, char *jobids,
+ DB_RESULT_HANDLER *result_handler, void *ctx)
+{
+ if (!*jobids) {
+ db_lock(mdb);
+ Mmsg(mdb->errmsg, _("ERR=JobIds are empty\n"));
+ db_unlock(mdb);
+ return false;
+ }
+ POOL_MEM buf(PM_MESSAGE);
+
+#define new_db_get_file_list
+#ifdef new_db_get_file_list
+ /* This is broken, at least if called from ua_restore.c */
+ Mmsg(buf,
+ "SELECT Path.Path, Filename.Name, File.FileIndex, File.JobId, File.LStat "
+ "FROM ( "
+ "SELECT max(FileId) as FileId, PathId, FilenameId "
+ "FROM (SELECT FileId, PathId, FilenameId FROM File WHERE JobId IN (%s)) AS F "
+ "GROUP BY PathId, FilenameId "
+ ") AS Temp "
+ "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
+ "JOIN Path ON (Path.PathId = Temp.PathId) "
+ "JOIN File ON (File.FileId = Temp.FileId) "
+"WHERE File.FileIndex > 0 ORDER BY JobId, FileIndex ASC", /* Return sorted by JobId, */
+ /* FileIndex for restore code */
+ jobids);
+#else
+ /*
+ * I am not sure that this works the same as the code in ua_restore.c
+ * but it is very similar. The accurate-test fails in a restore. Bad file count.
+ */
+ Mmsg(buf, uar_sel_files, jobids);
+#endif
+
+ return db_sql_query(mdb, buf.c_str(), result_handler, ctx);
+}
+
+/* The decision do change an incr/diff was done before
+ * Full : do nothing
+ * Differential : get the last full id
+ * Incremental : get the last full + last diff + last incr(s) ids
+ *
+ * TODO: look and merge from ua_restore.c
+ */
+bool db_accurate_get_jobids(JCR *jcr, B_DB *mdb,
+ JOB_DBR *jr, POOLMEM *jobids)
+{
+ bool ret=false;
+ char clientid[50], jobid[50], filesetid[50];
+ char date[MAX_TIME_LENGTH];
+ POOL_MEM query(PM_FNAME);
+ bstrutime(date, sizeof(date), time(NULL) + 1);
+ jobids[0]='\0';
+
+ /* First, find the last good Full backup for this job/client/fileset */
+ Mmsg(query,
+"CREATE TABLE btemp3%s AS "
+ "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
+ "FROM Job JOIN FileSet USING (FileSetId) "
+ "WHERE ClientId = %s "
+ "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
+ "AND StartTime<'%s' "
+ "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
+ "ORDER BY Job.JobTDate DESC LIMIT 1",
+ edit_uint64(jcr->JobId, jobid),
+ edit_uint64(jr->ClientId, clientid),
+ date,
+ edit_uint64(jr->FileSetId, filesetid));
+
+ if (!db_sql_query(mdb, query.c_str(), NULL, NULL)) {
+ goto bail_out;
+ }
+
+ if (jr->JobLevel == L_INCREMENTAL || jr->JobLevel == L_VIRTUAL_FULL) {
+ /* Now, find the last differential backup after the last full */
+ Mmsg(query,
+"INSERT INTO btemp3%s (JobId, StartTime, EndTime, JobTDate, PurgedFiles) "
+ "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
+ "FROM Job JOIN FileSet USING (FileSetId) "
+ "WHERE ClientId = %s "
+ "AND Level='D' AND JobStatus IN ('T','W') AND Type='B' "
+ "AND StartTime > (SELECT EndTime FROM btemp3%s ORDER BY EndTime DESC LIMIT 1) "
+ "AND FileSet.FileSet= (SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
+ "ORDER BY Job.JobTDate DESC LIMIT 1 ",
+ jobid,
+ clientid,
+ jobid,
+ filesetid);
+
+ if (!db_sql_query(mdb, query.c_str(), NULL, NULL)) {
+ goto bail_out;
+ }
+
+ /* We just have to take all incremental after the last Full/Diff */
+ Mmsg(query,
+"INSERT INTO btemp3%s (JobId, StartTime, EndTime, JobTDate, PurgedFiles) "
+ "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
+ "FROM Job JOIN FileSet USING (FileSetId) "
+ "WHERE ClientId = %s "
+ "AND Level='I' AND JobStatus IN ('T','W') AND Type='B' "
+ "AND StartTime > (SELECT EndTime FROM btemp3%s ORDER BY EndTime DESC LIMIT 1) "
+ "AND FileSet.FileSet= (SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
+ "ORDER BY Job.JobTDate DESC ",
+ jobid,
+ clientid,
+ jobid,
+ filesetid);
+ if (!db_sql_query(mdb, query.c_str(), NULL, NULL)) {
+ goto bail_out;
+ }
+ }
+
+ /* build a jobid list ie: 1,2,3,4 */
+ Mmsg(query, "SELECT JobId FROM btemp3%s ORDER by JobTDate", jobid);
+ db_sql_query(mdb, query.c_str(), db_get_int_handler, jobids);
+ Dmsg1(1, "db_accurate_get_jobids=%s\n", jobids);
+ ret = true;
+
+bail_out:
+ Mmsg(query, "DROP TABLE btemp3%s", jobid);
+ db_sql_query(mdb, query.c_str(), NULL, NULL);
+
+ return ret;