Re-fixes issue 300.

BUG=300

Review URL: https://webrtc-codereview.appspot.com/839007

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2835 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrike@webrtc.org 2012-09-27 22:20:34 +00:00
parent 2338131274
commit 9a6dac4193
2 changed files with 51 additions and 20 deletions

View File

@ -8,7 +8,7 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include "file_impl.h" #include "system_wrappers/source/file_impl.h"
#include <assert.h> #include <assert.h>
@ -19,6 +19,8 @@
#include <string.h> #include <string.h>
#endif #endif
#include "system_wrappers/interface/rw_lock_wrapper.h"
namespace webrtc { namespace webrtc {
FileWrapper* FileWrapper::Create() FileWrapper* FileWrapper::Create()
@ -27,7 +29,8 @@ FileWrapper* FileWrapper::Create()
} }
FileWrapperImpl::FileWrapperImpl() FileWrapperImpl::FileWrapperImpl()
: _id(NULL), : _rwLock(RWLockWrapper::CreateRWLock()),
_id(NULL),
_open(false), _open(false),
_looping(false), _looping(false),
_readOnly(false), _readOnly(false),
@ -47,18 +50,13 @@ FileWrapperImpl::~FileWrapperImpl()
int FileWrapperImpl::CloseFile() int FileWrapperImpl::CloseFile()
{ {
if (_id != NULL) WriteLockScoped write(*_rwLock);
{ return CloseFileImpl();
fclose(_id);
_id = NULL;
}
memset(_fileNameUTF8, 0, kMaxFileNameSize);
_open = false;
return 0;
} }
int FileWrapperImpl::Rewind() int FileWrapperImpl::Rewind()
{ {
WriteLockScoped write(*_rwLock);
if(_looping || !_readOnly) if(_looping || !_readOnly)
{ {
if (_id != NULL) if (_id != NULL)
@ -72,22 +70,21 @@ int FileWrapperImpl::Rewind()
int FileWrapperImpl::SetMaxFileSize(size_t bytes) int FileWrapperImpl::SetMaxFileSize(size_t bytes)
{ {
WriteLockScoped write(*_rwLock);
_maxSizeInBytes = bytes; _maxSizeInBytes = bytes;
return 0; return 0;
} }
int FileWrapperImpl::Flush() int FileWrapperImpl::Flush()
{ {
if (_id != NULL) WriteLockScoped write(*_rwLock);
{ return FlushImpl();
return fflush(_id);
}
return -1;
} }
int FileWrapperImpl::FileName(char* fileNameUTF8, int FileWrapperImpl::FileName(char* fileNameUTF8,
size_t size) const size_t size) const
{ {
ReadLockScoped read(*_rwLock);
size_t length = strlen(_fileNameUTF8); size_t length = strlen(_fileNameUTF8);
if(length > kMaxFileNameSize) if(length > kMaxFileNameSize)
{ {
@ -111,12 +108,14 @@ int FileWrapperImpl::FileName(char* fileNameUTF8,
bool FileWrapperImpl::Open() const bool FileWrapperImpl::Open() const
{ {
ReadLockScoped read(*_rwLock);
return _open; return _open;
} }
int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly, int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly,
bool loop, bool text) bool loop, bool text)
{ {
WriteLockScoped write(*_rwLock);
size_t length = strlen(fileNameUTF8); size_t length = strlen(fileNameUTF8);
if (length > kMaxFileNameSize - 1) if (length > kMaxFileNameSize - 1)
{ {
@ -189,6 +188,7 @@ int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly,
int FileWrapperImpl::Read(void* buf, int length) int FileWrapperImpl::Read(void* buf, int length)
{ {
WriteLockScoped write(*_rwLock);
if (length < 0) if (length < 0)
return -1; return -1;
@ -198,13 +198,14 @@ int FileWrapperImpl::Read(void* buf, int length)
int bytes_read = static_cast<int>(fread(buf, 1, length, _id)); int bytes_read = static_cast<int>(fread(buf, 1, length, _id));
if (bytes_read != length && !_looping) if (bytes_read != length && !_looping)
{ {
CloseFile(); CloseFileImpl();
} }
return bytes_read; return bytes_read;
} }
int FileWrapperImpl::WriteText(const char* format, ...) int FileWrapperImpl::WriteText(const char* format, ...)
{ {
WriteLockScoped write(*_rwLock);
if (format == NULL) if (format == NULL)
return -1; return -1;
@ -225,13 +226,14 @@ int FileWrapperImpl::WriteText(const char* format, ...)
} }
else else
{ {
CloseFile(); CloseFileImpl();
return -1; return -1;
} }
} }
bool FileWrapperImpl::Write(const void* buf, int length) bool FileWrapperImpl::Write(const void* buf, int length)
{ {
WriteLockScoped write(*_rwLock);
if (buf == NULL) if (buf == NULL)
return false; return false;
@ -247,7 +249,7 @@ bool FileWrapperImpl::Write(const void* buf, int length)
// Check if it's time to stop writing. // Check if it's time to stop writing.
if (_maxSizeInBytes > 0 && (_sizeInBytes + length) > _maxSizeInBytes) if (_maxSizeInBytes > 0 && (_sizeInBytes + length) > _maxSizeInBytes)
{ {
Flush(); FlushImpl();
return false; return false;
} }
@ -258,8 +260,28 @@ bool FileWrapperImpl::Write(const void* buf, int length)
return true; return true;
} }
CloseFile(); CloseFileImpl();
return false; return false;
} }
int FileWrapperImpl::CloseFileImpl() {
if (_id != NULL)
{
fclose(_id);
_id = NULL;
}
memset(_fileNameUTF8, 0, kMaxFileNameSize);
_open = false;
return 0;
}
int FileWrapperImpl::FlushImpl() {
if (_id != NULL)
{
return fflush(_id);
}
return -1;
}
} // namespace webrtc } // namespace webrtc

View File

@ -11,12 +11,16 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_ #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_ #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_
#include "file_wrapper.h" #include "system_wrappers/interface/file_wrapper.h"
#include <stdio.h> #include <stdio.h>
#include "system_wrappers/interface/scoped_ptr.h"
namespace webrtc { namespace webrtc {
class RWLockWrapper;
class FileWrapperImpl : public FileWrapper class FileWrapperImpl : public FileWrapper
{ {
public: public:
@ -43,6 +47,11 @@ public:
virtual int Rewind(); virtual int Rewind();
private: private:
int CloseFileImpl();
int FlushImpl();
scoped_ptr<RWLockWrapper> _rwLock;
FILE* _id; FILE* _id;
bool _open; bool _open;
bool _looping; bool _looping;