2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* AsyncFileWinASIO.actor.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
#define Net2AsyncFile AsyncFileWinASIO
|
|
|
|
|
|
|
|
// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version.
|
|
|
|
#if defined(NO_INTELLISENSE) && !defined(FLOW_ASYNCFILEWINASIO_ACTOR_G_H)
|
|
|
|
#define FLOW_ASYNCFILEWINASIO_ACTOR_G_H
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbrpc/AsyncFileWinASIO.actor.g.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#elif !defined(FLOW_ASYNCFILEWINASIO_ACTOR_H)
|
|
|
|
#define FLOW_ASYNCFILEWINASIO_ACTOR_H
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
|
|
|
class AsyncFileWinASIO : public IAsyncFile, public ReferenceCounted<AsyncFileWinASIO> {
|
|
|
|
public:
|
|
|
|
static void init() {}
|
|
|
|
|
2020-04-19 10:40:55 +08:00
|
|
|
static void stop() {}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
static bool should_poll() { return false; }
|
|
|
|
// FIXME: This implementation isn't actually asynchronous - it just does operations synchronously!
|
|
|
|
|
|
|
|
static Future<Reference<IAsyncFile>> open( std::string filename, int flags, int mode, boost::asio::io_service* ios ) {
|
2019-03-05 07:31:45 +08:00
|
|
|
ASSERT( flags & OPEN_UNBUFFERED );
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
std::string open_filename = filename;
|
|
|
|
if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) {
|
|
|
|
ASSERT( (flags & OPEN_CREATE) && (flags & OPEN_READWRITE) && !(flags & OPEN_EXCLUSIVE) );
|
|
|
|
open_filename = filename + ".part";
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE h = CreateFile( open_filename.c_str(),
|
|
|
|
GENERIC_READ | ((flags&OPEN_READWRITE) ? GENERIC_WRITE : 0),
|
2020-08-19 05:43:17 +08:00
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, nullptr,
|
2017-05-26 04:48:44 +08:00
|
|
|
(flags&OPEN_EXCLUSIVE) ? CREATE_NEW :
|
|
|
|
(flags&OPEN_CREATE) ? OPEN_ALWAYS :
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
|
2020-08-19 05:43:17 +08:00
|
|
|
nullptr );
|
2017-05-26 04:48:44 +08:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
bool notFound = GetLastError() == ERROR_FILE_NOT_FOUND;
|
|
|
|
Error e = notFound ? file_not_found() : io_error();
|
|
|
|
TraceEvent(notFound ? SevWarn : SevWarnAlways, "FileOpenError").error(e).GetLastError()
|
|
|
|
.detail("File", filename).detail("Flags", flags).detail("Mode", mode);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return Reference<IAsyncFile>( new AsyncFileWinASIO( *ios, h, flags, filename ) );
|
|
|
|
}
|
|
|
|
static Future<Void> deleteFile( std::string filename, bool mustBeDurable ) {
|
|
|
|
::deleteFile(filename);
|
|
|
|
// SOMEDAY: What is necessary to implement mustBeDurable on Windows? Does DeleteFile take care of it? DeleteFileTransacted?
|
|
|
|
return Void();
|
|
|
|
}
|
2018-07-07 09:51:36 +08:00
|
|
|
static Future<std::time_t> lastWriteTime( std::string filename ) {
|
|
|
|
// TODO(alexmiller): I have no idea about windows
|
|
|
|
struct _stat buf;
|
|
|
|
if (_stat( filename.c_str(), &buf ) != 0) {
|
|
|
|
throw io_error();
|
|
|
|
}
|
2018-07-31 08:13:37 +08:00
|
|
|
return buf.st_mtime;
|
2018-07-07 09:51:36 +08:00
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
virtual void addref() { ReferenceCounted<AsyncFileWinASIO>::addref(); }
|
|
|
|
virtual void delref() { ReferenceCounted<AsyncFileWinASIO>::delref(); }
|
|
|
|
|
2020-08-20 09:21:31 +08:00
|
|
|
int64_t debugFD() const override { return (int64_t)(const_cast<decltype(file)&>(file).native_handle()); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
static void onReadReady( Promise<int> onReady, const boost::system::error_code& error, size_t bytesRead ) {
|
|
|
|
if (error) {
|
|
|
|
Error e = io_error();
|
2018-08-02 05:30:57 +08:00
|
|
|
TraceEvent("AsyncReadError").error(e).GetLastError()
|
2017-05-26 04:48:44 +08:00
|
|
|
.detail("ASIOCode", error.value())
|
|
|
|
.detail("ASIOMessage", error.message());
|
|
|
|
onReady.sendError(e);
|
|
|
|
} else {
|
|
|
|
onReady.send( bytesRead );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void onWriteReady( Promise<Void> onReady, size_t bytesExpected, const boost::system::error_code& error, size_t bytesWritten ) {
|
|
|
|
if (error) {
|
|
|
|
Error e = io_error();
|
2018-08-02 05:30:57 +08:00
|
|
|
TraceEvent("AsyncWriteError").error(e).GetLastError()
|
2017-05-26 04:48:44 +08:00
|
|
|
.detail("ASIOCode", error.value())
|
|
|
|
.detail("ASIOMessage", error.message());
|
|
|
|
onReady.sendError(e);
|
|
|
|
} else if (bytesWritten != bytesExpected) {
|
|
|
|
Error e = io_error();
|
|
|
|
TraceEvent("AsyncWriteError").detail("BytesExpected", bytesExpected).detail("BytesWritten", bytesWritten);
|
|
|
|
onReady.sendError(io_error());
|
|
|
|
} else {
|
|
|
|
onReady.send( Void() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:21:31 +08:00
|
|
|
Future<int> read(void* data, int length, int64_t offset) override {
|
2017-05-26 04:48:44 +08:00
|
|
|
// the size call is set inline
|
|
|
|
auto end = this->size().get();
|
|
|
|
//TraceEvent("WinAsyncRead").detail("Offset", offset).detail("Length", length).detail("FileSize", end).detail("FileName", filename);
|
|
|
|
if(offset >= end)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Promise<int> result;
|
|
|
|
file.async_read_some_at(offset, boost::asio::mutable_buffers_1(data, length), boost::bind(&onReadReady, result, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
|
|
|
|
|
|
|
|
return result.getFuture();
|
|
|
|
}
|
2020-08-20 09:21:31 +08:00
|
|
|
Future<Void> write(void const* data, int length, int64_t offset) override {
|
2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
FIXME
|
|
|
|
if ( length + offset >= fileValidData ) {
|
|
|
|
SetFileValidData( length+offset );
|
|
|
|
fileValidData = length+offset;
|
|
|
|
}*/
|
|
|
|
Promise<Void> result;
|
|
|
|
boost::asio::async_write_at( file, offset, boost::asio::const_buffers_1( data, length ), boost::bind( &onWriteReady, result, length, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) );
|
|
|
|
return result.getFuture();
|
|
|
|
}
|
2020-08-20 09:21:31 +08:00
|
|
|
Future<Void> truncate(int64_t size) override {
|
2017-05-26 04:48:44 +08:00
|
|
|
// FIXME: Possibly use SetFileInformationByHandle( file.native_handle(), FileEndOfFileInfo, ... ) instead
|
2020-08-19 05:43:17 +08:00
|
|
|
if (!SetFilePointerEx( file.native_handle(), *(LARGE_INTEGER*)&size, nullptr, FILE_BEGIN ))
|
2017-05-26 04:48:44 +08:00
|
|
|
throw io_error();
|
|
|
|
if (!SetEndOfFile(file.native_handle()))
|
|
|
|
throw io_error();
|
|
|
|
return Void();
|
|
|
|
}
|
2020-08-20 09:21:31 +08:00
|
|
|
Future<Void> sync() override {
|
2017-05-26 04:48:44 +08:00
|
|
|
// FIXME: Do FlushFileBuffers in a worker thread (using g_network->createThreadPool)?
|
|
|
|
if (!FlushFileBuffers( file.native_handle() )) throw io_error();
|
|
|
|
|
|
|
|
if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) {
|
|
|
|
flags &= ~OPEN_ATOMIC_WRITE_AND_CREATE;
|
|
|
|
// FIXME: MoveFileEx(..., MOVEFILE_WRITE_THROUGH) in thread?
|
|
|
|
MoveFile( (filename+".part").c_str(), filename.c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
2020-08-20 08:32:11 +08:00
|
|
|
Future<int64_t> size() const override {
|
2017-05-26 04:48:44 +08:00
|
|
|
LARGE_INTEGER s;
|
2020-08-20 09:21:31 +08:00
|
|
|
if (!GetFileSizeEx(const_cast<decltype(file)&>(file).native_handle(), &s)) throw io_error();
|
2017-05-26 04:48:44 +08:00
|
|
|
return *(int64_t*)&s;
|
|
|
|
}
|
2020-08-20 08:32:11 +08:00
|
|
|
std::string getFilename() const override { return filename; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
~AsyncFileWinASIO() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
boost::asio::windows::random_access_handle file;
|
|
|
|
int flags;
|
|
|
|
std::string filename;
|
|
|
|
|
|
|
|
AsyncFileWinASIO(boost::asio::io_service& ios, HANDLE h, int flags, std::string filename)
|
|
|
|
: file(ios, h), flags(flags), filename(filename) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
2018-07-07 09:51:36 +08:00
|
|
|
#endif
|