+ /* Edit PoolId */
+ edit_int64(mr->PoolId, ed1);
+ /*
+ * Get Pool record for Scratch Pool
+ */
+ memset(&spr, 0, sizeof(spr));
+ bstrncpy(spr.Name, "Scratch", sizeof(spr.Name));
+ if (db_get_pool_record(jcr, jcr->db, &spr)) {
+ edit_int64(spr.PoolId, ed2);
+ bstrncat(ed2, ",", sizeof(ed2));
+ } else {
+ ed2[0] = 0;
+ }
+ Dmsg1(050, "Scratch pool=%s\n", ed2);
+ /*
+ * ed2 ends up with scratch poolid and current poolid or
+ * just current poolid if there is no scratch pool
+ */
+ bstrncat(ed2, ed1, sizeof(ed2));
+
+ /*
+ * Get the List of all media ids in the current Pool or whose
+ * RecyclePoolId is the current pool or the scratch pool
+ */
+ const char *select = "SELECT DISTINCT MediaId,LastWritten FROM Media WHERE "
+ "(PoolId=%s OR RecyclePoolId IN (%s)) AND MediaType='%s' %s"
+ "ORDER BY LastWritten ASC,MediaId";
+
+ if (InChanger) {
+ char changer[100];
+ /* Ensure it is in this autochanger */
+ bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
+ edit_int64(mr->StorageId, ed3));
+ Mmsg(query, select, ed1, ed2, mr->MediaType, changer);
+ } else {
+ Mmsg(query, select, ed1, ed2, mr->MediaType, "");
+ }
+
+ Dmsg1(050, "query=%s\n", query.c_str());
+ if (!db_get_query_dbids(ua->jcr, ua->db, query, ids)) {