- Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
-"VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
-"VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
-"FirstWritten,LastWritten,VolStatus "
-"FROM Media WHERE PoolId=%u AND MediaType='%s' AND VolStatus='%s' "
-"ORDER BY MediaId", mr->PoolId, mr->MediaType, mr->VolStatus);
+ if (InChanger) {
+ Mmsg(changer, "AND InChanger=1 AND StorageId=%s",
+ edit_int64(mr->StorageId, ed1));
+ }
+ if (strcmp(mr->VolStatus, "Recycle") == 0 ||
+ strcmp(mr->VolStatus, "Purged") == 0) {
+ order = "AND Recycle=1 ORDER BY LastWritten ASC,MediaId"; /* take oldest that can be recycled */
+ } else {
+ order = "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId"; /* take most recently written */
+ }
+ Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
+ "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
+ "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
+ "MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger,"
+ "EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId,"
+ "Enabled,LocationId,RecycleCount,InitialWrite,"
+ "ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime "
+ "FROM Media WHERE PoolId=%s AND MediaType='%s' AND Enabled=1 "
+ "AND VolStatus='%s' "
+ "%s "
+ "%s LIMIT %d",
+ edit_int64(mr->PoolId, ed1), mr->MediaType,
+ mr->VolStatus, changer.c_str(), order, item);