Commits

Gfy  committed 3c85fb0

function added to sort the stored files

  • Participants
  • Parent commits 78942bc

Comments (0)

Files changed (1)

  * LGPLv3 with Affero clause (LAGPL)
  * See http://mo.morsi.org/blog/node/270
  * rescene.php written on 2011-07-27
- * Last version: 2012-11-25
+ * Last version: 2012-12-14
  *
  * Features:
  *  - process a SRR file which returns:
  *          -> see what is missing
  *      - other files
  *          -> quick: by hash
+ *  - compare SRS files
  *  - Output flag added to indicate if the RARs used compression.
  *  - Support to read SRS files. (AVI/MKV/MP4/WMV)
+ *  - Sort stored files inside the SRR.
  *
  *  - nfo compare: strip line endings + new line?
  *      Indiana.Jones.And.The.Last.Crusade.1989.PAL.DVDR-DNA
  * List of possible features/todo list:
  *  - process in memory SRR 'file' + other API functions (very low priority)
  *      => can be done using temp files in memory
- *  - compare SRS files
  *  - refactor compare SRR
  *  - merge SRRs (Python script exists)
  *  - encryption sanity check
             } else {
                 echo 'failure';
             }
-
-
+            
+//             $sf = array_keys($result['storedFiles']);
+//             sort($sf);
+//             if (sortStoredFiles($srr, $sf)) {
+//                 echo 'success';
+//             } else {
+//                 echo 'failure';
+//             }
         }
     } else {
         $result = processSrr($srr);
             // sample name and crc32 must be the same to be the same sample
             if ($ovalue['fileData']->name === $tvalue['fileData']->name &&
                     $ovalue['fileData']->crc32 === $tvalue['fileData']->crc32) {
-                // checked agains main AVI/MKV file
+                // checked against main movie file
                 if ($ovalue['trackData'][1]->matchOffset === $tvalue['trackData'][1]->matchOffset) {
                     // equal enough
                     array_push($same, array($okey, $tkey));
     return processSrsHandle($fp, $fileAttributes['size']);
 }
 
+/**
+ * Parses an SRS file.
+ * @param $fileHandle
+ * @param int $srsSize
+ * @return info array
+ */
 function processSrsHandle($fileHandle, $srsSize) {
     switch(detectFileFormat($fileHandle)) {
         case FileType::AVI:
 }
 
 /**
- * Returns the list of stored files in a sorted way.
- * Directories first, then the files.
- * @param $storedFiles result from processSrr().
+ * Sorts the stored files in $srr according to $sortedFileNameList.
+ * @param string $srr: path to the SRR file
+ * @param array $sortedFileNameList: simple array with file names
+ * @return bool success status
  */
-function sortStoredFiles($storedFiles) {
-    //TODO: nfo at the top
-    // the keys are the unique file name
-    $all = array_keys($storedFiles);
-
-    // folders on top, files under that
-    $dirs = array_filter($all, "isFolder");
-    $files = array_diff($all, $dirs);
-
-    sort($dirs);
-    sort($files);
-
-    return array_merge($dirs, $files);
+function sortStoredFiles($srr, $sortedFileNameList) {
+    $srrInfo = processSrr($srr);
+    if (count($srrInfo['storedFiles']) != count($sortedFileNameList)) {
+        // not the same amount of elements: bad input
+        return FALSE;
+    }
+    
+    $before = $srrInfo['srrSize'];
+    $after = 0;
+    // check if each name is the same in both lists
+    foreach ($srrInfo['storedFiles'] as $key => $value) {
+        if (array_search($key, $sortedFileNameList) === FALSE) {
+            return FALSE;
+        }
+        
+        // offsets where the stored files start and end
+        if ($value['blockOffset'] < $before) {
+            $before = $value['blockOffset'];
+        }
+        $offset = $value['fileOffset'] + $value['fileSize'];
+        if ($offset > $after) {
+            $after = $offset;
+        }
+    }
+    
+    $fh = fopen($srr, 'rb');
+    $beforeData = fread($fh, $before);
+    fseek($fh, $after, SEEK_SET);
+    $afterData = fread($fh, $srrInfo['srrSize']); // srrSize: the (max) amount to read
+    
+    // sort the files and grab their blocks
+    $between = ''; 
+    foreach ($sortedFileNameList as $key) {
+        $bo = $srrInfo['storedFiles'][$key]['blockOffset'];
+        $fo = $srrInfo['storedFiles'][$key]['fileOffset'];
+        $fs = $srrInfo['storedFiles'][$key]['fileSize'];
+        $blockSize = ($fo + $fs) - $bo;
+        fseek($fh, $bo, SEEK_SET);
+        $between .= fread($fh, $blockSize);
+    }
+    fclose($fh);
+    $amount = file_put_contents($srr, $beforeData . $between . $afterData, LOCK_EX);
+    assert($amount === $srrInfo['srrSize']);
+    
+    return TRUE;
 }
 
 // Private helper functions -------------------------------------------------------------------------------------------
     }
 }
 
-
 class MovReader {
     public function __construct($fileHandle, $srsSize) {
         $this->fh = $fileHandle;