static couchstore_error_t couch_open(couchstore_error_info_t *errinfo, couch_file_handle* handle, const char *path, int oflag) { int creationflag = OPEN_EXISTING; if(oflag & O_CREAT) { creationflag = OPEN_ALWAYS; } HANDLE os_handle = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, creationflag, 0, NULL); if(os_handle == INVALID_HANDLE_VALUE) { DWORD last_error = save_windows_error(errinfo); if(last_error == ERROR_FILE_NOT_FOUND || last_error == ERROR_SUCCESS) { return (ssize_t) COUCHSTORE_ERROR_NO_SUCH_FILE; }; return COUCHSTORE_ERROR_OPEN_FILE; } /* Tell the caller about the new handle (file descriptor) */ *handle = win_to_handle(os_handle); return COUCHSTORE_SUCCESS; }
static off_t couch_goto_eof(couch_file_handle handle) { HANDLE file = handle_to_win(handle); LARGE_INTEGER size; if(!GetFileSizeEx(file, &size)) { save_windows_error(); return (off_t) COUCHSTORE_ERROR_READ; } return size.QuadPart; }
static couchstore_error_t couch_sync(couch_file_handle handle) { HANDLE file = handle_to_win(handle); if (!FlushFileBuffers(file)) { save_windows_error(); return COUCHSTORE_ERROR_WRITE; } return COUCHSTORE_SUCCESS; }
static ssize_t couch_pwrite(couch_file_handle handle, const void *buf, size_t nbyte, off_t offset) { #ifdef LOG_IO fprintf(stderr, "PWRITE %8llx -- %8llx (%6.1f kbytes)\n", offset, offset+nbyte, nbyte/1024.0); #endif HANDLE file = handle_to_win(handle); BOOL rv; DWORD byteswritten; OVERLAPPED winoffs; memset(&winoffs, 0, sizeof(winoffs)); winoffs.Offset = offset & 0xFFFFFFFF; winoffs.OffsetHigh = offset >> 32; rv = WriteFile(file, buf, nbyte, &byteswritten, &winoffs); if(!rv) { save_windows_error(); return (ssize_t) COUCHSTORE_ERROR_WRITE; } return byteswritten; }
static ssize_t couch_pread(couchstore_error_info_t *errinfo, couch_file_handle handle, void *buf, size_t nbyte, cs_off_t offset) { #ifdef LOG_IO fprintf(stderr, "PREAD %8llx -- %8llx (%6.1f kbytes)\n", offset, offset+nbyte, nbyte/1024.0); #endif HANDLE file = handle_to_win(handle); BOOL rv; DWORD bytesread; OVERLAPPED winoffs; memset(&winoffs, 0, sizeof(winoffs)); winoffs.Offset = offset & 0xFFFFFFFF; winoffs.OffsetHigh = (offset >> 32) & 0x7FFFFFFF; rv = ReadFile(file, buf, nbyte, &bytesread, &winoffs); if(!rv) { save_windows_error(errinfo); return (ssize_t) COUCHSTORE_ERROR_READ; } return bytesread; }