From 5d56f13f1a66645240ed91ef65950a89902543fb Mon Sep 17 00:00:00 2001 From: David Rose Date: Sat, 19 Oct 2002 02:28:45 +0000 Subject: [PATCH] download_to_file() with offset --- panda/src/downloader/httpChannel.cxx | 28 +++++++++++++++++++++++++++- panda/src/downloader/httpChannel.h | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/panda/src/downloader/httpChannel.cxx b/panda/src/downloader/httpChannel.cxx index e1f3afe862..b3079e5828 100644 --- a/panda/src/downloader/httpChannel.cxx +++ b/panda/src/downloader/httpChannel.cxx @@ -415,18 +415,44 @@ read_body() { // At this time, it is possible that a communications // error will have left a partial file, so // is_download_complete() may be called to test this. +// +// If first_byte is nonzero, it specifies the first byte +// within the file (zero-based) at which to start +// writing the downloaded data. This can work well in +// conjunction with get_subdocument() to restart a +// previously-interrupted download. //////////////////////////////////////////////////////////////////// bool HTTPChannel:: -download_to_file(const Filename &filename) { +download_to_file(const Filename &filename, size_t first_byte) { reset_download_to(); _download_to_filename = filename; _download_to_filename.set_binary(); + _download_to_file.close(); + _download_to_file.clear(); + if (!_download_to_filename.open_write(_download_to_file)) { downloader_cat.info() << "Could not open " << filename << " for writing.\n"; return false; } + if (first_byte != 0) { + // Windows doesn't complain if you try to seek past the end of + // file--it happily appends enough zero bytes to make the + // difference. Blecch. That means we need to get the file size + // first to check it ourselves. + _download_to_file.seekp(0, ios::end); + if (first_byte > (size_t)_download_to_file.tellp()) { + downloader_cat.info() + << "Invalid starting position of byte " << first_byte << " within " + << _download_to_filename << " (which has " + << _download_to_file.tellp() << " bytes)\n"; + _download_to_file.close(); + return false; + } + _download_to_file.seekp(first_byte); + } + _download_dest = DD_file; if (_nonblocking) { diff --git a/panda/src/downloader/httpChannel.h b/panda/src/downloader/httpChannel.h index 058edbb5e1..ddcc39930d 100644 --- a/panda/src/downloader/httpChannel.h +++ b/panda/src/downloader/httpChannel.h @@ -115,7 +115,7 @@ PUBLISHED: bool run(); ISocketStream *read_body(); - bool download_to_file(const Filename &filename); + bool download_to_file(const Filename &filename, size_t first_byte = 0); bool download_to_ram(Ramfile *ramfile); INLINE size_t get_bytes_downloaded() const;