"Fix" file size at the moment of adding it to ConcatStream (fixes #39)

pull/115/head
Vitaliy Filippov 2015-11-11 13:07:36 +03:00
parent afbd9d7234
commit 41bf5ba845
3 changed files with 25 additions and 11 deletions

View File

@ -172,10 +172,11 @@ bool Syncer2::Upload( Resource *res )
else else
{ {
File file( res->Path() ) ; File file( res->Path() ) ;
uint64_t size = file.Size() ;
ConcatStream multipart ; ConcatStream multipart ;
StringStream p1( StringStream p1(
"--file_contents\r\nContent-Type: application/json; charset=utf-8\r\n\r\n" + json_meta + "--file_contents\r\nContent-Type: application/json; charset=utf-8\r\n\r\n" + json_meta +
"\r\n--file_contents\r\nContent-Type: application/octet-stream\r\nContent-Length: " + to_string( file.Size() ) + "\r\n--file_contents\r\nContent-Type: application/octet-stream\r\nContent-Length: " + to_string( size ) +
"\r\n\r\n" "\r\n\r\n"
); );
StringStream p2("\r\n--file_contents--\r\n"); StringStream p2("\r\n--file_contents--\r\n");

View File

@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include <string.h>
#include "ConcatStream.hh" #include "ConcatStream.hh"
namespace gr { namespace gr {
@ -31,15 +32,22 @@ std::size_t ConcatStream::Read( char *data, std::size_t size )
std::size_t done = 0, l; std::size_t done = 0, l;
while ( done < size && m_cur < m_streams.size() ) while ( done < size && m_cur < m_streams.size() )
{ {
l = m_streams[m_cur]->Read( data+done, size-done ); l = m_streams[m_cur]->Read( data+done, ( size-done < m_sizes[m_cur]-m_pos ? size-done : m_sizes[m_cur]-m_pos ) );
done += l;
m_pos += l;
if ( !l ) if ( !l )
{
// current stream was truncated in the meantime, pad output with zeros
memset( data+done, 0, m_sizes[m_cur]-m_pos );
done += m_sizes[m_cur]-m_pos;
m_pos = m_sizes[m_cur];
}
if ( m_pos >= m_sizes[m_cur] )
{ {
m_cur++; m_cur++;
if ( m_cur < m_streams.size() ) if ( m_cur < m_streams.size() )
m_streams[m_cur]->Seek( 0, 0 ); m_streams[m_cur]->Seek( 0, 0 );
} }
done += l;
m_pos += l;
} }
return done ; return done ;
} }
@ -61,12 +69,9 @@ off_t ConcatStream::Seek( off_t offset, int whence )
m_pos = offset; m_pos = offset;
if ( m_streams.size() ) if ( m_streams.size() )
{ {
while ( offset > m_streams[m_cur]->Size() ) while ( offset > m_sizes[m_cur] )
{
offset -= m_streams[m_cur]->Size();
m_cur++; m_cur++;
} m_streams[m_cur]->Seek( offset - ( m_cur > 0 ? m_sizes[m_cur-1] : 0 ), 0 );
m_streams[m_cur]->Seek( offset, 0 );
} }
return m_pos ; return m_pos ;
} }
@ -85,8 +90,15 @@ void ConcatStream::Append( SeekStream *stream )
{ {
if ( stream ) if ( stream )
{ {
m_streams.push_back( stream ); off_t size = stream->Size();
m_size += stream->Size(); if ( size > 0 )
{
// "fix" stream size at the moment of adding so further changes of underlying files
// don't affect the total size of resulting stream...
m_size += size;
m_streams.push_back( stream );
m_sizes.push_back( m_size );
}
} }
} }

View File

@ -41,6 +41,7 @@ public :
private : private :
std::vector<SeekStream*> m_streams ; std::vector<SeekStream*> m_streams ;
std::vector<off_t> m_sizes ;
off_t m_size, m_pos ; off_t m_size, m_pos ;
int m_cur ; int m_cur ;
} ; } ;