Files
mongo/util/logfile.cpp

157 lines
4.6 KiB
C++
Raw Normal View History

2010-11-06 21:29:49 -04:00
// @file logfile.cpp simple file log writing / journaling
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "logfile.h"
#include "text.h"
#include "mongoutils/str.h"
2010-11-08 09:21:33 -05:00
#include "unittest.h"
2010-11-06 21:29:49 -04:00
using namespace mongoutils;
namespace mongo {
2010-11-08 09:21:33 -05:00
struct LogfileTest : public UnitTest {
LogfileTest() { }
void run() {
if( 0 && debug ) {
2010-11-06 21:29:49 -04:00
try {
2010-11-08 09:21:33 -05:00
LogFile f("logfile_test");
2010-12-08 16:23:21 -05:00
void *p = malloc(16384);
2010-11-08 09:21:33 -05:00
char *buf = (char*) p;
2010-12-08 16:23:21 -05:00
buf += 4095;
buf = (char*) (((size_t)buf)&(~0xfff));
2010-11-08 09:21:33 -05:00
memset(buf, 'z', 8192);
buf[8190] = '\n';
buf[8191] = 'B';
2010-11-06 21:29:49 -04:00
buf[0] = 'A';
2010-11-08 09:21:33 -05:00
f.synchronousAppend(buf, 8192);
f.synchronousAppend(buf, 8192);
2010-12-08 16:23:21 -05:00
free(p);
2010-11-06 21:29:49 -04:00
}
catch(DBException& e ) {
2010-12-08 16:23:21 -05:00
log() << "logfile.cpp test failed : " << e.what() << endl;
throw;
2010-11-06 21:29:49 -04:00
}
}
}
} __test;
}
#if defined(_WIN32)
namespace mongo {
LogFile::LogFile(string name) : _name(name) {
2010-11-06 21:29:49 -04:00
_fd = CreateFile(
toNativeString(name.c_str()).c_str(),
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
2010-11-14 22:28:04 -05:00
CREATE_NEW, //OPEN_ALWAYS,
2010-11-10 13:42:08 -05:00
FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
NULL);
if( _fd == INVALID_HANDLE_VALUE ) {
DWORD e = GetLastError();
uasserted(13518, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription(e));
}
SetFilePointer(_fd, 0, 0, FILE_END);
}
2010-11-08 09:21:33 -05:00
LogFile::~LogFile() {
if( _fd != INVALID_HANDLE_VALUE )
CloseHandle(_fd);
2010-11-06 21:29:49 -04:00
}
2010-11-15 22:58:09 -05:00
void LogFile::synchronousAppend(const void *buf, size_t len) {
2010-11-06 21:29:49 -04:00
assert(_fd);
DWORD written;
if( !WriteFile(_fd, buf, len, &written, NULL) ) {
2010-11-08 09:21:33 -05:00
DWORD e = GetLastError();
2010-11-14 22:28:04 -05:00
if( e == 87 )
massert(13519, "error appending to file - misaligned direct write?", false);
else
uasserted(13517, str::stream() << "error appending to file " << errnoWithDescription(e));
2010-11-06 21:29:49 -04:00
}
else {
dassert( written == len );
}
}
}
#else
2010-11-08 09:21:33 -05:00
#include <sys/types.h>
#include <sys/stat.h>
2010-11-06 21:29:49 -04:00
#include <fcntl.h>
namespace mongo {
LogFile::LogFile(string name) {
_fd = open(name.c_str(),
O_APPEND
2010-11-14 22:28:04 -05:00
| O_CREAT | O_EXCL
| O_RDWR
2010-11-06 21:29:49 -04:00
#if defined(O_DIRECT)
| O_DIRECT
#endif
#if defined(O_NOATIME)
| O_NOATIME
#endif
#if defined(O_SYNC)
| O_SYNC
#endif
,
S_IRUSR | S_IWUSR);
2010-11-08 09:21:33 -05:00
log() << "TEMP logfile open " << _fd << endl;
if( _fd < 0 ) {
2010-11-10 13:42:08 -05:00
uasserted(13516, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription());
2010-11-08 09:21:33 -05:00
}
2010-12-08 16:23:21 -05:00
// log() << "\nWRITE TEST: " << write(_fd, "abc", 3) << ' ' << errno << endl;
2010-11-06 21:29:49 -04:00
}
2010-11-08 09:21:33 -05:00
LogFile::~LogFile() {
if( _fd >= 0 )
close(_fd);
2010-12-08 16:23:21 -05:00
_fd = -1;
2010-11-08 09:21:33 -05:00
}
2010-11-15 22:55:19 -05:00
void LogFile::synchronousAppend(const void *b, size_t len) {
const char *buf = (char *) b;
2010-11-06 21:29:49 -04:00
assert(_fd);
2010-12-08 16:23:21 -05:00
assert(((size_t)buf)%4096==0); // aligned
if( len % 4096 != 0 ) {
log() << len << ' ' << len % 4096 << endl;
assert(false);
}
2010-11-06 21:29:49 -04:00
ssize_t written = write(_fd, buf, len);
2010-11-06 21:34:04 -04:00
if( written != (ssize_t) len ) {
2010-12-08 16:23:21 -05:00
log() << "write fails written:" << written << " len:" << len << " errno:" << errno << endl;
uasserted(13515, str::stream() << "error appending to file " << _fd << errnoWithDescription());
2010-11-06 21:29:49 -04:00
}
#if !defined(O_SYNC)
if( fdatasync(_fd) < 0 ) {
2010-12-08 16:23:21 -05:00
uasserted(13514, str::stream() << "error appending to file on fsync " << errnoWithDescription());
2010-11-06 21:29:49 -04:00
}
#endif
}
}
#endif