diff options
-rw-r--r-- | base/platform_file.h | 37 | ||||
-rw-r--r-- | base/platform_file_posix.cc | 8 | ||||
-rw-r--r-- | base/platform_file_win.cc | 8 | ||||
-rw-r--r-- | net/base/file_stream.h | 10 | ||||
-rw-r--r-- | net/base/file_stream_posix.cc | 32 | ||||
-rw-r--r-- | net/base/file_stream_win.cc | 41 | ||||
-rw-r--r-- | net/base/upload_data.cc | 77 | ||||
-rw-r--r-- | net/base/upload_data.h | 47 | ||||
-rw-r--r-- | net/base/upload_data_stream.cc | 47 | ||||
-rw-r--r-- | net/base/upload_data_stream.h | 4 |
10 files changed, 67 insertions, 244 deletions
diff --git a/base/platform_file.h b/base/platform_file.h index 4ab1ace..0dbf4e4 100644 --- a/base/platform_file.h +++ b/base/platform_file.h @@ -12,8 +12,6 @@ #include <string> -#include "base/ref_counted.h" - class FilePath; namespace base { @@ -55,41 +53,6 @@ PlatformFile CreatePlatformFile(const std::wstring& name, // Closes a file handle bool ClosePlatformFile(PlatformFile file); -// Get the length of an underlying file. Returns false on error. Otherwise -// *size is set to the length of the file, in bytes. -bool GetPlatformFileSize(PlatformFile file, uint64* size); - -// This is a reference counted PlatformFile. When the ref count drops to zero, -// the file handle is closed. See the comments in base/ref_counted.h for -// details on how to use it. -class RefCountedPlatformFile : - public base::RefCountedThreadSafe<RefCountedPlatformFile> { - public: - RefCountedPlatformFile(PlatformFile f) : file_(f) { } - - ~RefCountedPlatformFile() { - if (file_ != kInvalidPlatformFileValue) { - ClosePlatformFile(file_); - file_ = kInvalidPlatformFileValue; - } - } - - PlatformFile get() const { - return file_; - } - - PlatformFile release() { - PlatformFile f = file_; - file_ = kInvalidPlatformFileValue; - return f; - } - - private: - PlatformFile file_; - - DISALLOW_COPY_AND_ASSIGN(RefCountedPlatformFile); -}; - } // namespace base #endif // BASE_PLATFORM_FILE_H_ diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc index bfd40e9..46039b9 100644 --- a/base/platform_file_posix.cc +++ b/base/platform_file_posix.cc @@ -77,12 +77,4 @@ bool ClosePlatformFile(PlatformFile file) { return close(file); } -bool GetPlatformFileSize(PlatformFile file, uint64* out_size) { - struct stat st; - if (fstat(file, &st)) - return false; - *out_size = st.st_size; - return true; -} - } // namespace base diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc index ccaee1e..1143487 100644 --- a/base/platform_file_win.cc +++ b/base/platform_file_win.cc @@ -75,12 +75,4 @@ bool ClosePlatformFile(PlatformFile file) { return (CloseHandle(file) == 0); } -bool GetPlatformFileSize(PlatformFile file, uint64* out_size) { - LARGE_INTEGER size; - if (!GetFileSizeEx(file, &size)) - return false; - *out_size = size.QuadPart; - return true; -} - } // namespace disk_cache diff --git a/net/base/file_stream.h b/net/base/file_stream.h index 9fedea2..6b7f3dc 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -43,22 +43,12 @@ class FileStream { // Note that if there are any pending async operations, they'll be aborted. void Close(); - // Release performs the same actions as Close, but doesn't actually close the - // underlying PlatformFile. - void Release(); - // Call this method to open the FileStream. The remaining methods // cannot be used unless this method returns OK. If the file cannot be // opened then an error code is returned. // open_flags is a bitfield of base::PlatformFileFlags int Open(const FilePath& path, int open_flags); - // Calling this method is functionally the same as constructing the object - // with the non-default constructor. This method can only be used if the - // FileSteam isn't currently open (i.e. was constructed with the default - // constructor). - int Open(base::PlatformFile file, int open_flags); - // Returns true if Open succeeded and Close has not been called. bool IsOpen() const; diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index e947a6e..a4c5b3c 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -298,8 +298,13 @@ FileStream::FileStream() } FileStream::FileStream(base::PlatformFile file, int flags) - : file_(base::kInvalidPlatformFileValue) { - Open(file, flags); + : file_(file), + open_flags_(flags) { + // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to + // make sure we will perform asynchronous File IO to it. + if (flags & base::PLATFORM_FILE_ASYNC) { + async_context_.reset(new AsyncContext()); + } } FileStream::~FileStream() { @@ -318,12 +323,6 @@ void FileStream::Close() { } } -void FileStream::Release() { - // Abort any existing asynchronous operations. - async_context_.reset(); - file_ = base::kInvalidPlatformFileValue; -} - int FileStream::Open(const FilePath& path, int open_flags) { if (IsOpen()) { DLOG(FATAL) << "File is already open!"; @@ -345,21 +344,6 @@ int FileStream::Open(const FilePath& path, int open_flags) { return OK; } -int FileStream::Open(base::PlatformFile file, int open_flags) { - if (IsOpen()) { - DLOG(FATAL) << "File is already open!"; - return ERR_UNEXPECTED; - } - - open_flags_ = open_flags; - file_ = file; - - if (open_flags & base::PLATFORM_FILE_ASYNC) - async_context_.reset(new AsyncContext()); - - return OK; -} - bool FileStream::IsOpen() const { return file_ != base::kInvalidPlatformFileValue; } @@ -461,7 +445,7 @@ int64 FileStream::Truncate(int64 bytes) { if (!IsOpen()) return ERR_UNEXPECTED; - // We better be open for writing. + // We better be open for reading. DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); // Seek to the position to truncate from. diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index e1928a7..cec6a9d 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -120,10 +120,16 @@ FileStream::FileStream() open_flags_(0) { } -FileStream::FileStream(base::PlatformFile file, int open_flags) - : file_(INVALID_HANDLE_VALUE), - open_flags_(0) { - Open(file, open_flags); +FileStream::FileStream(base::PlatformFile file, int flags) + : file_(file), + open_flags_(flags) { + // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to + // make sure we will perform asynchronous File IO to it. + if (flags & base::PLATFORM_FILE_ASYNC) { + async_context_.reset(new AsyncContext(this)); + MessageLoopForIO::current()->RegisterIOHandler(file_, + async_context_.get()); + } } FileStream::~FileStream() { @@ -141,13 +147,6 @@ void FileStream::Close() { } } -void FileStream::Release() { - if (file_ != INVALID_HANDLE_VALUE) - CancelIo(file_); - async_context_.reset(); - file_ = INVALID_HANDLE_VALUE; -} - int FileStream::Open(const FilePath& path, int open_flags) { if (IsOpen()) { DLOG(FATAL) << "File is already open!"; @@ -171,26 +170,6 @@ int FileStream::Open(const FilePath& path, int open_flags) { return OK; } -int FileStream::Open(base::PlatformFile file, int open_flags) { - if (IsOpen()) { - DLOG(FATAL) << "File is already open!"; - return ERR_UNEXPECTED; - } - - open_flags_ = open_flags; - file_ = file; - - // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to - // make sure we will perform asynchronous File IO to it. - if (open_flags_ & base::PLATFORM_FILE_ASYNC) { - async_context_.reset(new AsyncContext(this)); - MessageLoopForIO::current()->RegisterIOHandler(file_, - async_context_.get()); - } - - return OK; -} - bool FileStream::IsOpen() const { return file_ != INVALID_HANDLE_VALUE; } diff --git a/net/base/upload_data.cc b/net/base/upload_data.cc index d030b3d..0496873 100644 --- a/net/base/upload_data.cc +++ b/net/base/upload_data.cc @@ -5,81 +5,40 @@ #include "net/base/upload_data.h" #include "base/file_util.h" -#include "base/platform_file.h" #include "base/logging.h" namespace net { -uint64 UploadData::GetContentLength() { +uint64 UploadData::GetContentLength() const { uint64 len = 0; - std::vector<Element>::iterator it = elements_.begin(); + std::vector<Element>::const_iterator it = elements_.begin(); for (; it != elements_.end(); ++it) len += (*it).GetContentLength(); 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"; - - if (file_ != NULL) { - return file_->get(); - } else { - return base::kInvalidPlatformFileValue; - } -} - -void UploadData::Element::Close() { - DCHECK(type_ == TYPE_FILE) << "Close on non-file Element"; - - if (file_ != NULL) - file_->release(); -} +uint64 UploadData::Element::GetContentLength() const { + if (override_content_length_) + return content_length_; -void UploadData::Element::SetToFilePathRange(const FilePath& path, - uint64 offset, - uint64 length) { - type_ = TYPE_FILE; - file_range_offset_ = 0; - file_range_length_ = 0; + 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_ = new base::RefCountedPlatformFile(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 diff --git a/net/base/upload_data.h b/net/base/upload_data.h index dfee0c4..3aab835 100644 --- a/net/base/upload_data.h +++ b/net/base/upload_data.h @@ -9,7 +9,6 @@ #include "base/basictypes.h" #include "base/file_path.h" -#include "base/platform_file.h" #include "base/ref_counted.h" #include "testing/gtest/include/gtest/gtest_prod.h" @@ -27,7 +26,7 @@ class UploadData : public base::RefCounted<UploadData> { class Element { public: Element() : type_(TYPE_BYTES), file_range_offset_(0), - file_range_length_(0), + file_range_length_(kuint64max), override_content_length_(false) { } @@ -46,41 +45,19 @@ class UploadData : public base::RefCounted<UploadData> { SetToFilePathRange(path, 0, kuint64max); } - void SetToFilePathRange(const FilePath& path, uint64 offset, uint64 length); + void SetToFilePathRange(const FilePath& path, + uint64 offset, uint64 length) { + type_ = TYPE_FILE; + file_path_ = path; + file_range_offset_ = offset; + file_range_length_ = length; + } // Returns the byte-length of the element. For files that do not exist, 0 // is returned. This is done for consistency with Mozilla. - uint64 GetContentLength() const { - if (override_content_length_) - return content_length_; - - if (type_ == TYPE_BYTES) { - return bytes_.size(); - } else { - return file_range_length_; - } - } - - // For a TYPE_FILE, return a handle to the file. The caller does not take - // ownership and should not close the file handle. - base::PlatformFile platform_file() const; - - // For a TYPE_FILE, this closes the file handle. It's a fatal error to call - // platform_file() after this. - void Close(); + uint64 GetContentLength() const; private: - // type_ == TYPE_BYTES: - // bytes_ is valid - // type_ == TYPE_FILE: - // file_path_ may be empty, in which case file_range_* are 0 and file_ is - // invalid. This occurs when we cannot open the requested file. - // - // Else, if file_path_ is non-empty, then file_range_* are within range - // of the length of the file that we found when opening the file. Also, - // the sum of offset and length will not overflow a uint64. file_ will be - // handle to the file. - // Allows tests to override the result of GetContentLength. void SetContentLength(uint64 content_length) { override_content_length_ = true; @@ -92,7 +69,6 @@ class UploadData : public base::RefCounted<UploadData> { FilePath file_path_; uint64 file_range_offset_; uint64 file_range_length_; - scoped_refptr<base::RefCountedPlatformFile> file_; bool override_content_length_; uint64 content_length_; @@ -119,7 +95,7 @@ class UploadData : public base::RefCounted<UploadData> { } // Returns the total size in bytes of the data to upload. - uint64 GetContentLength(); + uint64 GetContentLength() const; const std::vector<Element>& elements() const { return elements_; @@ -133,9 +109,6 @@ class UploadData : public base::RefCounted<UploadData> { elements_.swap(*elements); } - // CloseFiles closes the file handles of all Elements of type TYPE_FILE. - void CloseFiles(); - // Identifies a particular upload instance, which is used by the cache to // formulate a cache key. This value should be unique across browser // sessions. A value of 0 is used to indicate an unspecified identifier. diff --git a/net/base/upload_data_stream.cc b/net/base/upload_data_stream.cc index 0a0ee73..221ef28 100644 --- a/net/base/upload_data_stream.cc +++ b/net/base/upload_data_stream.cc @@ -10,7 +10,7 @@ namespace net { -UploadDataStream::UploadDataStream(UploadData* data) +UploadDataStream::UploadDataStream(const UploadData* data) : data_(data), buf_(new IOBuffer(kBufSize)), buf_len_(0), @@ -24,7 +24,6 @@ UploadDataStream::UploadDataStream(UploadData* data) } UploadDataStream::~UploadDataStream() { - data_->CloseFiles(); } void UploadDataStream::DidConsume(size_t num_bytes) { @@ -68,19 +67,18 @@ void UploadDataStream::FillBuf() { } else { DCHECK(element.type() == UploadData::TYPE_FILE); - if (element.file_range_length() == 0) { - // If we failed to open the file, then the length is set to zero. The - // length used when calculating the POST size was also zero. This - // matches the behaviour of Mozilla. - next_element_remaining_ = 0; - } else { - if (!next_element_stream_.IsOpen()) { - // We ignore the return value of Open becuase we've already checked - // !IsOpen, above. - int flags = base::PLATFORM_FILE_READ | - base::PLATFORM_FILE_WRITE; - next_element_stream_.Open(element.platform_file(), flags); - + if (!next_element_stream_.IsOpen()) { + int flags = base::PLATFORM_FILE_OPEN | + base::PLATFORM_FILE_READ; + int rv = next_element_stream_.Open(element.file_path(), flags); + // If the file does not exist, that's technically okay.. we'll just + // upload an empty file. This is for consistency with Mozilla. + DLOG_IF(WARNING, rv != OK) << "Failed to open \"" + << element.file_path().value() + << "\" for reading: " << rv; + + next_element_remaining_ = 0; // Default to reading nothing. + if (rv == OK) { uint64 offset = element.file_range_offset(); if (offset && next_element_stream_.Seek(FROM_BEGIN, offset) < 0) { DLOG(WARNING) << "Failed to seek \"" << element.file_path().value() @@ -92,18 +90,11 @@ void UploadDataStream::FillBuf() { } int rv = 0; - if (next_element_remaining_ > 0) { - int count = - static_cast<int>(std::min(next_element_remaining_, - static_cast<uint64>(size_remaining))); - rv = next_element_stream_.Read(buf_->data() + buf_len_, count, NULL); - if (rv < 1) { - // If the file was truncated between the time that we opened it and - // now, or if we got an error on reading, then we pad with NULs. - memset(buf_->data() + buf_len_, 0, count); - rv = count; - } - + int count = static_cast<int>(std::min( + static_cast<uint64>(size_remaining), next_element_remaining_)); + if (count > 0 && + (rv = next_element_stream_.Read(buf_->data() + buf_len_, + count, NULL)) > 0) { buf_len_ += rv; next_element_remaining_ -= rv; } else { @@ -114,7 +105,7 @@ void UploadDataStream::FillBuf() { if (advance_to_next_element) { ++next_element_; next_element_offset_ = 0; - next_element_stream_.Release(); + next_element_stream_.Close(); } } diff --git a/net/base/upload_data_stream.h b/net/base/upload_data_stream.h index 9df5db0..0dd7dd3 100644 --- a/net/base/upload_data_stream.h +++ b/net/base/upload_data_stream.h @@ -14,7 +14,7 @@ class IOBuffer; class UploadDataStream { public: - explicit UploadDataStream(UploadData* data); + explicit UploadDataStream(const UploadData* data); ~UploadDataStream(); // Returns the stream's buffer and buffer length. @@ -42,7 +42,7 @@ class UploadDataStream { // left to fill the buffer with. void FillBuf(); - UploadData* data_; + const UploadData* data_; // This buffer is filled with data to be uploaded. The data to be sent is // always at the front of the buffer. If we cannot send all of the buffer at |