2013-04-16 23:57:39 -04:00
|
|
|
#include <toku_portability.h>
|
2013-04-16 23:57:30 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
pread(int fildes, void *buf, size_t nbyte, int64_t offset) {
|
2013-04-16 23:57:31 -04:00
|
|
|
HANDLE filehandle;
|
|
|
|
OVERLAPPED win_offset = {0};
|
|
|
|
filehandle = (HANDLE)_get_osfhandle(fildes);
|
|
|
|
int64_t r;
|
|
|
|
if (filehandle==INVALID_HANDLE_VALUE) {
|
|
|
|
r = errno; assert(r!=0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
win_offset.Offset = offset % (1LL<<32LL);
|
|
|
|
win_offset.OffsetHigh = offset / (1LL<<32LL);
|
|
|
|
|
|
|
|
DWORD bytes_read;
|
|
|
|
r = ReadFile(filehandle, buf, nbyte, &bytes_read, &win_offset);
|
|
|
|
if (!r) {
|
|
|
|
r = GetLastError();
|
|
|
|
if (r==ERROR_HANDLE_EOF) r = bytes_read;
|
2013-04-16 23:57:34 -04:00
|
|
|
else {
|
|
|
|
errno = r;
|
|
|
|
r = -1;
|
|
|
|
}
|
2013-04-16 23:57:31 -04:00
|
|
|
}
|
2013-04-16 23:57:31 -04:00
|
|
|
else r = bytes_read;
|
|
|
|
|
2013-04-16 23:57:30 -04:00
|
|
|
// printf("%s: %d %p %u %I64d %I64d\n", __FUNCTION__, fildes, buf, nbyte, offset, r); fflush(stdout);
|
2013-04-16 23:57:31 -04:00
|
|
|
cleanup:
|
2013-04-16 23:57:30 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
pwrite(int fildes, const void *buf, size_t nbyte, int64_t offset) {
|
2013-04-16 23:57:31 -04:00
|
|
|
HANDLE filehandle;
|
|
|
|
OVERLAPPED win_offset = {0};
|
|
|
|
filehandle = (HANDLE)_get_osfhandle(fildes);
|
|
|
|
int64_t r;
|
|
|
|
if (filehandle==INVALID_HANDLE_VALUE) {
|
|
|
|
r = errno; assert(r!=0);
|
|
|
|
goto cleanup;
|
2013-04-16 23:57:31 -04:00
|
|
|
}
|
2013-04-16 23:57:31 -04:00
|
|
|
win_offset.Offset = offset % (1LL<<32LL);
|
|
|
|
win_offset.OffsetHigh = offset / (1LL<<32LL);
|
|
|
|
|
|
|
|
DWORD bytes_written;
|
|
|
|
r = WriteFile(filehandle, buf, nbyte, &bytes_written, &win_offset);
|
2013-04-16 23:57:34 -04:00
|
|
|
if (!r) {
|
|
|
|
errno = GetLastError();
|
2013-04-16 23:58:06 -04:00
|
|
|
if (errno == ERROR_HANDLE_DISK_FULL ||
|
|
|
|
errno == ERROR_DISK_FULL) {
|
|
|
|
char err_msg[sizeof("Failed write of [] bytes to fd=[].") + 20+10]; //64 bit is 20 chars, 32 bit is 10 chars
|
2013-04-16 23:58:06 -04:00
|
|
|
snprintf(err_msg, sizeof(err_msg), "Failed write of [%"PRIu64"] bytes to fd=[%d].", nbyte, fildes);
|
2013-04-16 23:58:06 -04:00
|
|
|
perror(err_msg);
|
|
|
|
fflush(stdout);
|
|
|
|
int out_of_disk_space = 1;
|
|
|
|
assert(!out_of_disk_space); //Give an error message that might be useful if this is the only one that survives.
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:34 -04:00
|
|
|
r = -1;
|
|
|
|
}
|
2013-04-16 23:57:31 -04:00
|
|
|
else r = bytes_written;
|
|
|
|
|
2013-04-16 23:57:30 -04:00
|
|
|
// printf("%s: %d %p %u %I64d %I64d\n", __FUNCTION__, fildes, buf, nbyte, offset, r); fflush(stdout);
|
2013-04-16 23:57:31 -04:00
|
|
|
cleanup:
|
2013-04-16 23:57:30 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fsync(int fd) {
|
|
|
|
int r = _commit(fd);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ftruncate(int fd, int64_t offset) {
|
2013-04-16 23:57:31 -04:00
|
|
|
int r = _chsize_s(fd, offset);
|
|
|
|
return r;
|
2013-04-16 23:57:30 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:42 -04:00
|
|
|
static ssize_t (*t_pwrite)(int, const void *, size_t, toku_off_t) = 0;
|
2013-04-16 23:57:39 -04:00
|
|
|
static ssize_t (*t_write)(int, const void *, size_t) = 0;
|
|
|
|
|
2013-04-16 23:57:42 -04:00
|
|
|
int toku_set_func_pwrite (ssize_t (*pwrite_fun)(int, const void *, size_t, toku_off_t)) {
|
2013-04-16 23:57:39 -04:00
|
|
|
t_pwrite = pwrite_fun;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int toku_set_func_write (ssize_t (*write_fun)(int, const void *, size_t)) {
|
|
|
|
t_write = write_fun;
|
2013-04-16 23:57:39 -04:00
|
|
|
t_write = t_write; //So far unused
|
2013-04-16 23:57:39 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ssize_t
|
2013-04-16 23:57:42 -04:00
|
|
|
toku_os_pwrite (int fd, const void *buf, size_t len, toku_off_t off)
|
2013-04-16 23:57:39 -04:00
|
|
|
{
|
|
|
|
if (t_pwrite) {
|
|
|
|
return t_pwrite(fd, buf, len, off);
|
|
|
|
} else {
|
|
|
|
return pwrite(fd, buf, len, off);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|