@@ -61,23 +61,48 @@ file_stream::read_chunk()
6161 return chunk.share_first (chunk.size () - remaining_space.size ());
6262}
6363
64+ void
65+ file_stream::seek (std::size_t offset)
66+ {
67+ seek (offset, /* from_current=*/ false );
68+ }
69+
70+ std::size_t
71+ file_stream::position () const
72+ {
73+ return seek (0 , /* from_current=*/ true );
74+ }
75+
6476void
6577file_stream::reset ()
6678{
67- ::off_t offset = ::lseek (fd_.get (), 0 , SEEK_SET);
68- if (offset == -1 ) {
69- std::error_code err = last_error ();
79+ seek (0 );
7080
71- if (err == std::errc::invalid_seek)
72- throw_<byte_stream_error>( " '{}' is not seekable and cannot be reset. " , path_. string ());
81+ is_eod_ = false ;
82+ }
7383
74- throw_system_error (err,
75- " '{}' cannot be reset" , path_.string ());
76- }
84+ void
85+ file_stream::record_position (tape &t) const
86+ {
87+ std::size_t offset = position ();
88+
89+ t.record (offset);
90+ }
91+
92+ void
93+ file_stream::reload_position (tape &t)
94+ {
95+ seek (t.read <std::size_t >());
7796
7897 is_eod_ = false ;
7998}
8099
100+ bool
101+ file_stream::supports_seek () const noexcept
102+ {
103+ return true ;
104+ }
105+
81106std::size_t
82107file_stream::fill_chunk (writable_memory_span chunk)
83108{
@@ -89,4 +114,22 @@ file_stream::fill_chunk(writable_memory_span chunk)
89114 return static_cast <std::size_t >(num_bytes_read);
90115}
91116
117+ std::size_t
118+ file_stream::seek (std::size_t offset, bool from_current) const
119+ {
120+ ::off_t off = ::lseek (
121+ fd_.get (), static_cast <::off_t >(offset), from_current ? SEEK_CUR : SEEK_SET);
122+
123+ if (off != -1 )
124+ return static_cast <std::size_t >(off);
125+
126+ std::error_code err = last_error ();
127+
128+ if (err == std::errc::invalid_seek)
129+ throw_<byte_stream_error>(" '{}' is not seekable." , path_.string ());
130+
131+ throw_system_error (err,
132+ " '{}' cannot be read" , path_.string ());
133+ }
134+
92135} // namespace fairseq2n::detail
0 commit comments