00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #if !defined(_WIN32)
00021 #include "errno.h"
00022 #include <sys/mman.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 #else
00027 #include <windows.h>
00028 #endif
00029
00030 #include "text.h"
00031
00032 namespace mongo {
00033
00034 #ifndef __sunos__
00035 typedef uint64_t fileofs;
00036 #else
00037 typedef boost::uint64_t fileofs;
00038 #endif
00039
00040 class FileInterface {
00041 public:
00042 void open(const char *fn) {}
00043 void write(fileofs o, const char *data, unsigned len) {}
00044 void read(fileofs o, char *data, unsigned len) {}
00045 bool bad() {return false;}
00046 bool is_open() {return false;}
00047 fileofs len() { return 0; }
00048 void fsync() { assert(false); }
00049 };
00050
00051 #if defined(_WIN32)
00052 #include <io.h>
00053
00054 class File : public FileInterface {
00055 HANDLE fd;
00056 bool _bad;
00057 void err(BOOL b=false) {
00058 if( !b && !_bad ) {
00059 _bad = true;
00060 log() << "File I/O error " << GetLastError() << '\n';
00061 }
00062 }
00063 public:
00064 File() {
00065 fd = INVALID_HANDLE_VALUE;
00066 _bad = true;
00067 }
00068 ~File() {
00069 if( is_open() ) CloseHandle(fd);
00070 fd = INVALID_HANDLE_VALUE;
00071 }
00072 void open(const char *filename, bool readOnly=false ) {
00073 fd = CreateFile(
00074 toNativeString(filename).c_str(),
00075 ( readOnly ? 0 : GENERIC_WRITE ) | GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,
00076 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00077 if( !is_open() ) {
00078 DWORD e = GetLastError();
00079 log() << "Create/Open File failed " << filename << ' ' << errnoWithDescription(e) << endl;
00080 }
00081 else
00082 _bad = false;
00083 }
00084 void write(fileofs o, const char *data, unsigned len) {
00085 LARGE_INTEGER li;
00086 li.QuadPart = o;
00087 SetFilePointerEx(fd, li, NULL, FILE_BEGIN);
00088 DWORD written;
00089 err( WriteFile(fd, data, len, &written, NULL) );
00090 }
00091 void read(fileofs o, char *data, unsigned len) {
00092 DWORD read;
00093 LARGE_INTEGER li;
00094 li.QuadPart = o;
00095 SetFilePointerEx(fd, li, NULL, FILE_BEGIN);
00096 int ok = ReadFile(fd, data, len, &read, 0);
00097 if( !ok )
00098 err(ok);
00099 else
00100 massert( 10438 , "ReadFile error - truncated file?", read == len);
00101 }
00102 bool bad() { return _bad; }
00103 bool is_open() { return fd != INVALID_HANDLE_VALUE; }
00104 fileofs len() {
00105 LARGE_INTEGER li;
00106 li.LowPart = GetFileSize(fd, (DWORD *) &li.HighPart);
00107 if( li.HighPart == 0 && li.LowPart == INVALID_FILE_SIZE ) {
00108 err( false );
00109 return 0;
00110 }
00111 return li.QuadPart;
00112 }
00113 void fsync() { FlushFileBuffers(fd); }
00114 };
00115
00116 #else
00117
00118 class File : public FileInterface {
00119 public:
00120 int fd;
00121 private:
00122 bool _bad;
00123 void err(bool ok) {
00124 if( !ok && !_bad ) {
00125 _bad = true;
00126 log() << "File I/O " << errnoWithDescription() << '\n';
00127 }
00128 }
00129 public:
00130 File() {
00131 fd = -1;
00132 _bad = true;
00133 }
00134 ~File() {
00135 if( is_open() ) ::close(fd);
00136 fd = -1;
00137 }
00138
00139 #ifndef O_NOATIME
00140 #define O_NOATIME 0
00141 #endif
00142
00143 void open(const char *filename, bool readOnly=false ) {
00144 fd = ::open(filename,
00145 O_CREAT | ( readOnly ? 0 : ( O_RDWR | O_NOATIME ) ) ,
00146 S_IRUSR | S_IWUSR);
00147 if ( fd <= 0 ) {
00148 out() << "couldn't open " << filename << ' ' << errnoWithDescription() << endl;
00149 return;
00150 }
00151 _bad = false;
00152 }
00153 void write(fileofs o, const char *data, unsigned len) {
00154 err( ::pwrite(fd, data, len, o) == (int) len );
00155 }
00156 void read(fileofs o, char *data, unsigned len) {
00157 err( ::pread(fd, data, len, o) == (int) len );
00158 }
00159 bool bad() { return _bad; }
00160 bool is_open() { return fd > 0; }
00161 fileofs len() {
00162 return lseek(fd, 0, SEEK_END);
00163 }
00164 void fsync() { ::fsync(fd); }
00165 };
00166
00167
00168 #endif
00169
00170
00171 }
00172