Stxxl  1.4.0
io/wincall_file.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  io/wincall_file.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2005-2006 Roman Dementiev <dementiev@ira.uka.de>
00007  *  Copyright (C) 2008-2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <stxxl/bits/io/wincall_file.h>
00015 
00016 #if STXXL_HAVE_WINCALL_FILE
00017 
00018 #include <stxxl/bits/io/iostats.h>
00019 #include <stxxl/bits/common/error_handling.h>
00020 
00021 #include <windows.h>
00022 
00023 __STXXL_BEGIN_NAMESPACE
00024 
00025 
00026 void wincall_file::serve(const request * req) throw (io_error)
00027 {
00028     scoped_mutex_lock fd_lock(fd_mutex);
00029     assert(req->get_file() == this);
00030     offset_type offset = req->get_offset();
00031     void * buffer = req->get_buffer();
00032     size_type bytes = req->get_size();
00033     request::request_type type = req->get_type();
00034 
00035     if (bytes > 32 * 1024 * 1024) {
00036         STXXL_ERRMSG("Using a block size larger than 32 MiB may not work with the " << io_type() << " filetype");
00037     }
00038 
00039     HANDLE handle = file_des;
00040     LARGE_INTEGER desired_pos;
00041     desired_pos.QuadPart = offset;
00042     if (!SetFilePointerEx(handle, desired_pos, NULL, FILE_BEGIN))
00043     {
00044         stxxl_win_lasterror_exit("SetFilePointerEx in wincall_request::serve()" <<
00045                                  " offset=" << offset <<
00046                                  " this=" << this <<
00047                                  " buffer=" << buffer <<
00048                                  " bytes=" << bytes <<
00049                                  " type=" << ((type == request::READ) ? "READ" : "WRITE"),
00050                                  io_error);
00051     }
00052     else
00053     {
00054         stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
00055 
00056         if (type == request::READ)
00057         {
00058             DWORD NumberOfBytesRead = 0;
00059             if (!ReadFile(handle, buffer, bytes, &NumberOfBytesRead, NULL))
00060             {
00061                 stxxl_win_lasterror_exit("ReadFile" <<
00062                                          " this=" << this <<
00063                                          " offset=" << offset <<
00064                                          " buffer=" << buffer <<
00065                                          " bytes=" << bytes <<
00066                                          " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
00067                                          " NumberOfBytesRead= " << NumberOfBytesRead,
00068                                          io_error);
00069             } else if (NumberOfBytesRead != bytes) {
00070                 stxxl_win_lasterror_exit(" partial read: missing " << (bytes - NumberOfBytesRead) << " out of " << bytes << " bytes",
00071                                          io_error);
00072             }
00073         }
00074         else
00075         {
00076             DWORD NumberOfBytesWritten = 0;
00077             if (!WriteFile(handle, buffer, bytes, &NumberOfBytesWritten, NULL))
00078             {
00079                 stxxl_win_lasterror_exit("WriteFile" <<
00080                                          " this=" << this <<
00081                                          " offset=" << offset <<
00082                                          " buffer=" << buffer <<
00083                                          " bytes=" << bytes <<
00084                                          " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
00085                                          " NumberOfBytesWritten= " << NumberOfBytesWritten,
00086                                          io_error);
00087             } else if (NumberOfBytesWritten != bytes) {
00088                 stxxl_win_lasterror_exit(" partial write: missing " << (bytes - NumberOfBytesWritten) << " out of " << bytes << " bytes",
00089                                          io_error);
00090             }
00091         }
00092     }
00093 }
00094 
00095 const char * wincall_file::io_type() const
00096 {
00097     return "wincall";
00098 }
00099 
00100 __STXXL_END_NAMESPACE
00101 
00102 #endif  // #if STXXL_HAVE_WINCALL_FILE
00103 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines