Removing, opening and creating files in platform_file and file
BUG= R=perkj@webrtc.org, sprang@google.com Review URL: https://codereview.webrtc.org/2347473003 . Cr-Commit-Position: refs/heads/master@{#14250}
This commit is contained in:
@ -18,6 +18,14 @@ File::~File() {
|
||||
Close();
|
||||
}
|
||||
|
||||
File File::Open(const std::string& path) {
|
||||
return File(OpenPlatformFile(path));
|
||||
}
|
||||
|
||||
File File::Create(const std::string& path) {
|
||||
return File(CreatePlatformFile(path));
|
||||
}
|
||||
|
||||
File::File(File&& other) : file_(other.file_) {
|
||||
other.file_ = kInvalidPlatformFileValue;
|
||||
}
|
||||
|
||||
@ -35,7 +35,10 @@ class File {
|
||||
File(File&& other);
|
||||
File& operator=(File&& other);
|
||||
|
||||
// Open and Create give files with both reading and writing enabled.
|
||||
static File Open(const std::string& path);
|
||||
// If the file already exists it will be overwritten.
|
||||
static File Create(const std::string& path);
|
||||
|
||||
size_t Write(const uint8_t* data, size_t length);
|
||||
size_t Read(uint8_t* buffer, size_t length);
|
||||
|
||||
@ -22,10 +22,6 @@
|
||||
|
||||
namespace rtc {
|
||||
|
||||
File File::Open(const std::string& path) {
|
||||
return File(::open(path.c_str(), O_RDWR));
|
||||
}
|
||||
|
||||
size_t File::Write(const uint8_t* data, size_t length) {
|
||||
size_t total_written = 0;
|
||||
do {
|
||||
|
||||
@ -8,10 +8,6 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@ -32,14 +28,6 @@
|
||||
|
||||
namespace rtc {
|
||||
|
||||
void RemoveFile(const std::string& path) {
|
||||
#if defined(WEBRTC_WIN)
|
||||
::DeleteFile(ToUtf16(path).c_str());
|
||||
#else
|
||||
::unlink(path.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
int LastError() {
|
||||
#if defined(WEBRTC_WIN)
|
||||
return ::GetLastError();
|
||||
@ -69,7 +57,7 @@ class FileTest : public ::testing::Test {
|
||||
ASSERT_FALSE(path_.empty());
|
||||
}
|
||||
rtc::File OpenTempFile() { return rtc::File::Open(path_); }
|
||||
void TearDown() { RemoveFile(path_); }
|
||||
void TearDown() { rtc::RemoveFile(path_); }
|
||||
};
|
||||
|
||||
TEST_F(FileTest, DoubleClose) {
|
||||
|
||||
@ -19,13 +19,6 @@
|
||||
|
||||
namespace rtc {
|
||||
|
||||
File File::Open(const std::string& path) {
|
||||
HANDLE handle =
|
||||
::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
return File(handle);
|
||||
}
|
||||
|
||||
size_t File::Write(const uint8_t* data, size_t length) {
|
||||
RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
|
||||
size_t total_written = 0;
|
||||
|
||||
@ -13,6 +13,9 @@
|
||||
#if defined(WEBRTC_WIN)
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
@ -34,7 +37,23 @@ FILE* FdopenPlatformFileForWriting(PlatformFile file) {
|
||||
bool ClosePlatformFile(PlatformFile file) {
|
||||
return CloseHandle(file) != 0;
|
||||
}
|
||||
#else
|
||||
|
||||
bool RemoveFile(const std::string& path) {
|
||||
return ::DeleteFile(ToUtf16(path).c_str()) != 0;
|
||||
}
|
||||
|
||||
PlatformFile OpenPlatformFile(const std::string& path) {
|
||||
return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
}
|
||||
|
||||
PlatformFile CreatePlatformFile(const std::string& path) {
|
||||
return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
|
||||
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
}
|
||||
|
||||
#else // defined(WEBRTC_WIN)
|
||||
|
||||
const PlatformFile kInvalidPlatformFileValue = -1;
|
||||
|
||||
FILE* FdopenPlatformFileForWriting(PlatformFile file) {
|
||||
@ -44,6 +63,19 @@ FILE* FdopenPlatformFileForWriting(PlatformFile file) {
|
||||
bool ClosePlatformFile(PlatformFile file) {
|
||||
return close(file);
|
||||
}
|
||||
|
||||
bool RemoveFile(const std::string& path) {
|
||||
return ::unlink(path.c_str()) == 0;
|
||||
}
|
||||
|
||||
PlatformFile OpenPlatformFile(const std::string& path) {
|
||||
return ::open(path.c_str(), O_RDWR);
|
||||
}
|
||||
|
||||
PlatformFile CreatePlatformFile(const std::string& path) {
|
||||
return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
@ -12,9 +12,10 @@
|
||||
#define WEBRTC_BASE_PLATFORM_FILE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
#if defined(WEBRTC_WIN)
|
||||
#include <windows.h>
|
||||
#include "webrtc/base/win32.h"
|
||||
#endif
|
||||
|
||||
namespace rtc {
|
||||
@ -39,6 +40,17 @@ FILE* FdopenPlatformFileForWriting(PlatformFile file);
|
||||
// Use fclose instead.
|
||||
bool ClosePlatformFile(PlatformFile file);
|
||||
|
||||
// Removes a file in the filesystem.
|
||||
bool RemoveFile(const std::string& path);
|
||||
|
||||
// Opens a file for reading and writing. You might want to use base/file.h
|
||||
// instead.
|
||||
PlatformFile OpenPlatformFile(const std::string& path);
|
||||
|
||||
// Creates a new file for reading and writing. If the file already exists it
|
||||
// will be overwritten. You might want to use base/file.h instead.
|
||||
PlatformFile CreatePlatformFile(const std::string& path);
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // WEBRTC_BASE_PLATFORM_FILE_H_
|
||||
|
||||
Reference in New Issue
Block a user