/* Copies the data from ORIGINAL_STREAM to a temporary file, returning both the original and compressed size. */ static svn_error_t * create_compressed(apr_file_t **result, svn_filesize_t *full_size, svn_filesize_t *compressed_size, svn_stream_t *original_stream, svn_cancel_func_t cancel_func, void *cancel_baton, apr_pool_t *result_pool, apr_pool_t *scratch_pool) { svn_stream_t *compressed; svn_filesize_t bytes_read = 0; apr_size_t rd; SVN_ERR(svn_io_open_uniquely_named(result, NULL, NULL, "diffgz", NULL, svn_io_file_del_on_pool_cleanup, result_pool, scratch_pool)); compressed = svn_stream_compressed( svn_stream_from_aprfile2(*result, TRUE, scratch_pool), scratch_pool); if (original_stream) do { char buffer[SVN__STREAM_CHUNK_SIZE]; rd = sizeof(buffer); if (cancel_func) SVN_ERR(cancel_func(cancel_baton)); SVN_ERR(svn_stream_read_full(original_stream, buffer, &rd)); bytes_read += rd; SVN_ERR(svn_stream_write(compressed, buffer, &rd)); } while(rd == SVN__STREAM_CHUNK_SIZE); else { apr_size_t zero = 0; SVN_ERR(svn_stream_write(compressed, NULL, &zero)); } SVN_ERR(svn_stream_close(compressed)); /* Flush compression */ *full_size = bytes_read; SVN_ERR(svn_io_file_size_get(compressed_size, *result, scratch_pool)); return SVN_NO_ERROR; }
static svn_error_t * test_stream_compressed_empty_file(apr_pool_t *pool) { svn_stream_t *stream, *empty_file_stream; char buf[1]; apr_size_t len; /* Reading an empty file with a compressed stream should not error. */ SVN_ERR(svn_stream_open_unique(&empty_file_stream, NULL, NULL, svn_io_file_del_on_pool_cleanup, pool, pool)); stream = svn_stream_compressed(empty_file_stream, pool); len = sizeof(buf); SVN_ERR(svn_stream_read_full(stream, buf, &len)); if (len > 0) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Got unexpected result."); SVN_ERR(svn_stream_close(stream)); return SVN_NO_ERROR; }
static svn_error_t * test_stream_compressed_read_full(apr_pool_t *pool) { svn_stream_t *stream, *empty_stream; char buf[1]; apr_size_t len; /* Reading an empty stream with read_full only support should not error. */ empty_stream = svn_stream_create(NULL, pool); /* Create stream with only full read support. */ svn_stream_set_read2(empty_stream, NULL, empty_read_full_fn); stream = svn_stream_compressed(empty_stream, pool); len = sizeof(buf); SVN_ERR(svn_stream_read_full(stream, buf, &len)); if (len > 0) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Got unexpected result."); SVN_ERR(svn_stream_close(stream)); return SVN_NO_ERROR; }
static svn_error_t * test_stream_compressed(apr_pool_t *pool) { #define NUM_TEST_STRINGS 5 #define TEST_BUF_SIZE 10 #define GENERATED_SIZE 20000 int i; svn_stringbuf_t *bufs[NUM_TEST_STRINGS]; apr_pool_t *subpool = svn_pool_create(pool); static const char * const strings[NUM_TEST_STRINGS - 1] = { /* 0 */ "", /* 1 */ "This is a string.", /* 2 */ "This is, by comparison to the previous string, a much longer string.", /* 3 */ "And if you thought that last string was long, you just wait until " "I'm finished here. I mean, how can a string really claim to be long " "when it fits on a single line of 80-columns? Give me a break. " "Now, I'm not saying that I'm the longest string out there--far from " "it--but I feel that it is safe to assume that I'm far longer than my " "peers. And that demands some amount of respect, wouldn't you say?" }; for (i = 0; i < (NUM_TEST_STRINGS - 1); i++) bufs[i] = svn_stringbuf_create(strings[i], pool); /* the last buffer is for the generated data */ bufs[NUM_TEST_STRINGS - 1] = generate_test_bytes(GENERATED_SIZE, pool); for (i = 0; i < NUM_TEST_STRINGS; i++) { svn_stream_t *stream; svn_stringbuf_t *origbuf, *inbuf, *outbuf; char buf[TEST_BUF_SIZE]; apr_size_t len; origbuf = bufs[i]; inbuf = svn_stringbuf_create_empty(subpool); outbuf = svn_stringbuf_create_empty(subpool); stream = svn_stream_compressed(svn_stream_from_stringbuf(outbuf, subpool), subpool); len = origbuf->len; SVN_ERR(svn_stream_write(stream, origbuf->data, &len)); SVN_ERR(svn_stream_close(stream)); stream = svn_stream_compressed(svn_stream_from_stringbuf(outbuf, subpool), subpool); len = TEST_BUF_SIZE; while (len >= TEST_BUF_SIZE) { len = TEST_BUF_SIZE; SVN_ERR(svn_stream_read_full(stream, buf, &len)); if (len > 0) svn_stringbuf_appendbytes(inbuf, buf, len); } if (! svn_stringbuf_compare(inbuf, origbuf)) return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "Got unexpected result."); SVN_ERR(svn_stream_close(stream)); svn_pool_clear(subpool); } #undef NUM_TEST_STRINGS #undef TEST_BUF_SIZE #undef GENERATED_SIZE svn_pool_destroy(subpool); return SVN_NO_ERROR; }