Skip to content

Commit

Permalink
"Fix" file size at the moment of adding it to ConcatStream (fixes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif committed Nov 11, 2015
1 parent afbd9d7 commit 41bf5ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion libgrive/src/drive2/Syncer2.cc
Expand Up @@ -172,10 +172,11 @@ bool Syncer2::Upload( Resource *res )
else
{
File file( res->Path() ) ;
uint64_t size = file.Size() ;
ConcatStream multipart ;
StringStream p1(
"--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"
);
StringStream p2("\r\n--file_contents--\r\n");
Expand Down
32 changes: 22 additions & 10 deletions libgrive/src/util/ConcatStream.cc
Expand Up @@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <string.h>
#include "ConcatStream.hh"

namespace gr {
Expand All @@ -31,15 +32,22 @@ std::size_t ConcatStream::Read( char *data, std::size_t size )
std::size_t done = 0, l;
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 )
{
// 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++;
if ( m_cur < m_streams.size() )
m_streams[m_cur]->Seek( 0, 0 );
}
done += l;
m_pos += l;
}
return done ;
}
Expand All @@ -61,12 +69,9 @@ off_t ConcatStream::Seek( off_t offset, int whence )
m_pos = offset;
if ( m_streams.size() )
{
while ( offset > m_streams[m_cur]->Size() )
{
offset -= m_streams[m_cur]->Size();
while ( offset > m_sizes[m_cur] )
m_cur++;
}
m_streams[m_cur]->Seek( offset, 0 );
m_streams[m_cur]->Seek( offset - ( m_cur > 0 ? m_sizes[m_cur-1] : 0 ), 0 );
}
return m_pos ;
}
Expand All @@ -85,8 +90,15 @@ void ConcatStream::Append( SeekStream *stream )
{
if ( stream )
{
m_streams.push_back( stream );
m_size += stream->Size();
off_t 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 );
}
}
}

Expand Down
1 change: 1 addition & 0 deletions libgrive/src/util/ConcatStream.hh
Expand Up @@ -41,6 +41,7 @@ public :

private :
std::vector<SeekStream*> m_streams ;
std::vector<off_t> m_sizes ;
off_t m_size, m_pos ;
int m_cur ;
} ;
Expand Down

0 comments on commit 41bf5ba

Please sign in to comment.