2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/env.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2020-04-30 06:31:41 +08:00
|
|
|
#include <cstdarg>
|
|
|
|
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
// This workaround can be removed when leveldb::Env::DeleteFile is removed.
|
|
|
|
// See env.h for justification.
|
|
|
|
#if defined(_WIN32) && defined(LEVELDB_DELETEFILE_UNDEFINED)
|
|
|
|
#undef DeleteFile
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
namespace leveldb {
|
|
|
|
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
Env::Env() = default;
|
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
Env::~Env() = default;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2014-12-12 00:13:18 +08:00
|
|
|
Status Env::NewAppendableFile(const std::string& fname, WritableFile** result) {
|
|
|
|
return Status::NotSupported("NewAppendableFile", fname);
|
|
|
|
}
|
|
|
|
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
Status Env::RemoveDir(const std::string& dirname) { return DeleteDir(dirname); }
|
|
|
|
Status Env::DeleteDir(const std::string& dirname) { return RemoveDir(dirname); }
|
|
|
|
|
|
|
|
Status Env::RemoveFile(const std::string& fname) { return DeleteFile(fname); }
|
|
|
|
Status Env::DeleteFile(const std::string& fname) { return RemoveFile(fname); }
|
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
SequentialFile::~SequentialFile() = default;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
RandomAccessFile::~RandomAccessFile() = default;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
WritableFile::~WritableFile() = default;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
Logger::~Logger() = default;
|
2011-07-21 10:40:18 +08:00
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
FileLock::~FileLock() = default;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2011-07-21 10:40:18 +08:00
|
|
|
void Log(Logger* info_log, const char* format, ...) {
|
2018-04-11 07:18:06 +08:00
|
|
|
if (info_log != nullptr) {
|
2020-04-30 06:31:41 +08:00
|
|
|
std::va_list ap;
|
2011-07-21 10:40:18 +08:00
|
|
|
va_start(ap, format);
|
|
|
|
info_log->Logv(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2012-01-26 06:56:52 +08:00
|
|
|
static Status DoWriteStringToFile(Env* env, const Slice& data,
|
2019-05-03 02:01:00 +08:00
|
|
|
const std::string& fname, bool should_sync) {
|
2011-03-19 06:37:00 +08:00
|
|
|
WritableFile* file;
|
|
|
|
Status s = env->NewWritableFile(fname, &file);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
s = file->Append(data);
|
2012-01-26 06:56:52 +08:00
|
|
|
if (s.ok() && should_sync) {
|
|
|
|
s = file->Sync();
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
if (s.ok()) {
|
|
|
|
s = file->Close();
|
|
|
|
}
|
|
|
|
delete file; // Will auto-close if we did not close above
|
|
|
|
if (!s.ok()) {
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
env->RemoveFile(fname);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:56:52 +08:00
|
|
|
Status WriteStringToFile(Env* env, const Slice& data,
|
|
|
|
const std::string& fname) {
|
|
|
|
return DoWriteStringToFile(env, data, fname, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WriteStringToFileSync(Env* env, const Slice& data,
|
|
|
|
const std::string& fname) {
|
|
|
|
return DoWriteStringToFile(env, data, fname, true);
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
|
|
|
|
data->clear();
|
|
|
|
SequentialFile* file;
|
|
|
|
Status s = env->NewSequentialFile(fname, &file);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
static const int kBufferSize = 8192;
|
|
|
|
char* space = new char[kBufferSize];
|
|
|
|
while (true) {
|
|
|
|
Slice fragment;
|
|
|
|
s = file->Read(kBufferSize, &fragment, space);
|
|
|
|
if (!s.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data->append(fragment.data(), fragment.size());
|
|
|
|
if (fragment.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] space;
|
|
|
|
delete file;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
EnvWrapper::~EnvWrapper() {}
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|