common: Avoid duplicated recordings shown in Logger::list_recordings().
[gps-watch.git] / src / common / logger.rs
index 95f4a4342a1f0320f76a98b26d5909f217442663..2ca2e22980f48b7867abddae91b2e09e1506c7d2 100644 (file)
@@ -144,10 +144,22 @@ fn cmp_sector_header_indices(a: u16, b: u16,
     let header_a = &sector_header[a as usize];
     let header_b = &sector_header[b as usize];
 
-    // Latest entries come first.
-    if header_a.start_time > header_b.start_time {
+    if header_a.starts_recording() && header_b.starts_recording() {
+        // Latest entries come first.
+        if header_a.start_time > header_b.start_time {
+            -1
+        } else if header_a.start_time < header_b.start_time {
+            1
+        } else {
+            0
+        }
+    } else if header_a.starts_recording() {
+        -1
+    } else if header_b.starts_recording() {
+        1
+    } else if a < b {
         -1
-    } else if header_a.start_time < header_b.start_time {
+    } else if a > b {
         1
     } else {
         0
@@ -187,56 +199,11 @@ impl<'a> SectorHeaderIter<'a> {
             indices: [0; NUM_SECTORS]
         };
 
-        let mut num_used = 0;
-
-        // Put the indices of the used directory entries at the beginning
-        // of the array. Ignore the unused ones since we are not going
-        // to sort them anyway.
         for i in 0..NUM_SECTORS {
-            let sector_header = &iter.sector_header[i];
-
-            if sector_header.starts_recording() {
-                iter.indices[num_used] = i as u16;
-                num_used += 1;
-            }
+            iter.indices[i] = i as u16;
         }
 
-        let num_elts_to_sort = num_used;
-
-        if num_elts_to_sort != 0 {
-            // Sort the used directory entries.
-            iter.sort(num_elts_to_sort);
-        }
-
-        // Now put the indices of the unused directory entries in the array.
-        if num_used == 0 {
-            for i in 0..NUM_SECTORS {
-                iter.indices[i] = i as u16;
-            }
-        } else {
-            let latest_used = iter.indices[0] as usize;
-            let mut offset_unused = num_used;
-
-            // First put the entries that come after the latest one in use...
-            for i in (latest_used + 1)..NUM_SECTORS {
-                let sector_header = &iter.sector_header[i];
-
-                if !sector_header.is_in_use() {
-                    iter.indices[offset_unused] = i as u16;
-                    offset_unused += 1;
-                }
-            }
-
-            // ... then wrap around if necessary.
-            for i in 0..latest_used {
-                let sector_header = &iter.sector_header[i];
-
-                if !sector_header.is_in_use() {
-                    iter.indices[offset_unused] = i as u16;
-                    offset_unused += 1;
-                }
-            }
-        }
+        iter.sort(NUM_SECTORS);
 
         // XXX:
         // Need to handle those sectors that don't start recordings