]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
xz: Add io_seek_src().
authorLasse Collin <lasse.collin@tukaani.org>
Wed, 5 Apr 2017 15:47:22 +0000 (18:47 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Wed, 5 Apr 2017 15:47:22 +0000 (18:47 +0300)
src/xz/file_io.c
src/xz/file_io.h

index 041bed88e0067a81c3cf5ca56a2f0f45ccab5ac3..48ef8223ca8bdae23bbd7a11ac7ecd0828c67b62 100644 (file)
@@ -1169,16 +1169,30 @@ io_read(file_pair *pair, io_buf *buf_union, size_t size)
 
 
 extern bool
-io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos)
+io_seek_src(file_pair *pair, off_t pos)
 {
-       // Using lseek() and read() is more portable than pread() and
-       // for us it is as good as real pread().
+       assert(pos >= 0);
+
        if (lseek(pair->src_fd, pos, SEEK_SET) != pos) {
                message_error(_("%s: Error seeking the file: %s"),
                                pair->src_name, strerror(errno));
                return true;
        }
 
+       pair->src_eof = false;
+
+       return false;
+}
+
+
+extern bool
+io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos)
+{
+       // Using lseek() and read() is more portable than pread() and
+       // for us it is as good as real pread().
+       if (io_seek_src(pair, pos))
+               return true;
+
        const size_t amount = io_read(pair, buf, size);
        if (amount == SIZE_MAX)
                return true;
index 6722aef8409231a478fa3124e4afb9ed165e00ce..84d5b44d0e8d9c71499c18383dfb4824d2372632 100644 (file)
@@ -129,6 +129,19 @@ extern size_t io_read(file_pair *pair, io_buf *buf, size_t size);
 extern void io_fix_src_pos(file_pair *pair, size_t rewind_size);
 
 
+/// \brief      Seek to the given absolute position in the source file
+///
+/// This calls lseek() and also clears pair->src_eof.
+///
+/// \param      pair    Seekable source file
+/// \param      pos     Offset relative to the beginning of the file,
+///                     from which the data should be read.
+///
+/// \return     On success, false is returned. On error, error message
+///             is printed and true is returned.
+extern bool io_seek_src(file_pair *pair, off_t pos);
+
+
 /// \brief      Read from source file from given offset to a buffer
 ///
 /// This is remotely similar to standard pread(). This uses lseek() though,