Fix the behaviour of FileGroup when seeking too far.
[dcpomatic.git] / src / lib / file_group.cc
index 942eb435d87f081e38ef5e48bf53ea0e4dd3cdea..f06ca300718acd727c74b39fa09981d1185ffb55 100644 (file)
@@ -93,7 +93,7 @@ FileGroup::ensure_open_path (size_t p) const
        _current_path = p;
        _current_file = fopen_boost (_paths[_current_path], "rb");
        if (_current_file == 0) {
-               throw OpenFileError (_paths[_current_path], errno, true);
+               throw OpenFileError (_paths[_current_path], errno, OpenFileError::READ);
        }
 }
 
@@ -132,12 +132,18 @@ FileGroup::seek (int64_t pos, int whence) const
                if (sub_pos < int64_t (len)) {
                        break;
                }
-               sub_pos -= len;
                ++i;
+               if (i < _paths.size()) {
+                       /* If we've run out of files we need to seek off the end of the last file */
+                       sub_pos -= len;
+               }
        }
 
        if (i == _paths.size ()) {
-               return -1;
+               /* Seeking too far isn't an error; we'll seek too far in the last file which
+                * will "pass on" fseek()'s behaviour to our caller.
+                */
+               i--;
        }
 
        ensure_open_path (i);