diff options
Diffstat (limited to 'net/base/upload_data.cc')
-rw-r--r-- | net/base/upload_data.cc | 72 |
1 files changed, 16 insertions, 56 deletions
diff --git a/net/base/upload_data.cc b/net/base/upload_data.cc index a23b25e..0496873 100644 --- a/net/base/upload_data.cc +++ b/net/base/upload_data.cc @@ -5,7 +5,6 @@ #include "net/base/upload_data.h" #include "base/file_util.h" -#include "base/platform_file.h" #include "base/logging.h" namespace net { @@ -18,67 +17,28 @@ uint64 UploadData::GetContentLength() const { return len; } -void UploadData::CloseFiles() { - std::vector<Element>::iterator it = elements_.begin(); - for (; it != elements_.end(); ++it) { - if (it->type() == TYPE_FILE) - it->Close(); - } -} - -base::PlatformFile UploadData::Element::platform_file() const { - DCHECK(type_ == TYPE_FILE) << "platform_file on non-file Element"; - - return file_; -} - -void UploadData::Element::Close() { - DCHECK(type_ == TYPE_FILE) << "Close on non-file Element"; - - if (file_ != base::kInvalidPlatformFileValue) - base::ClosePlatformFile(file_); - file_ = base::kInvalidPlatformFileValue; -} - -void UploadData::Element::SetToFilePathRange(const FilePath& path, - uint64 offset, - uint64 length) { - type_ = TYPE_FILE; - file_range_offset_ = 0; - file_range_length_ = 0; +uint64 UploadData::Element::GetContentLength() const { + if (override_content_length_) + return content_length_; - Close(); + if (type_ == TYPE_BYTES) + return static_cast<uint64>(bytes_.size()); - if (offset + length < offset) { - LOG(ERROR) << "Upload file offset and length overflow 64-bits. Ignoring."; - return; - } + DCHECK(type_ == TYPE_FILE); - base::PlatformFile file = base::CreatePlatformFile( - path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL); - if (file == base::kInvalidPlatformFileValue) { - // This case occurs when the user selects a file that isn't readable. - file_path_= path; - return; - } + // TODO(darin): This size calculation could be out of sync with the state of + // the file when we get around to reading it. We should probably find a way + // to lock the file or somehow protect against this error condition. - uint64 file_size; - if (!base::GetPlatformFileSize(file, &file_size)) { - base::ClosePlatformFile(file); - return; - } + int64 length = 0; + if (!file_util::GetFileSize(file_path_, &length)) + return 0; - if (offset > file_size) { - base::ClosePlatformFile(file); - return; - } - if (offset + length > file_size) - length = file_size - offset; + if (file_range_offset_ >= static_cast<uint64>(length)) + return 0; // range is beyond eof - file_ = file; - file_path_ = path; - file_range_offset_ = offset; - file_range_length_ = length; + // compensate for the offset and clip file_range_length_ to eof + return std::min(length - file_range_offset_, file_range_length_); } } // namespace net |