49 #include "mysys_priv.h"
54 File my_open_osfhandle(HANDLE handle,
int oflag)
58 DBUG_ENTER(
"my_open_osfhandle");
61 for(i= MY_FILE_MIN; i < my_file_limit;i++)
63 if(my_file_info[i].fhandle == 0)
66 finfo->type= FILE_BY_OPEN;
67 finfo->fhandle= handle;
80 static void invalidate_fd(File fd)
82 DBUG_ENTER(
"invalidate_fd");
83 DBUG_ASSERT(fd >= MY_FILE_MIN && fd < (
int)my_file_limit);
84 my_file_info[fd].fhandle= 0;
90 HANDLE my_get_osfhandle(File fd)
92 DBUG_ENTER(
"my_get_osfhandle");
93 DBUG_ASSERT(fd >= MY_FILE_MIN && fd < (
int)my_file_limit);
94 DBUG_RETURN(my_file_info[fd].fhandle);
98 static int my_get_open_flags(File fd)
100 DBUG_ENTER(
"my_get_open_flags");
101 DBUG_ASSERT(fd >= MY_FILE_MIN && fd < (
int)my_file_limit);
102 DBUG_RETURN(my_file_info[fd].oflag);
122 File my_win_sopen(
const char *path,
int oflag,
int shflag,
int pmode)
131 SECURITY_ATTRIBUTES SecurityAttributes;
133 DBUG_ENTER(
"my_win_sopen");
135 if (check_if_legal_filename(path))
140 SecurityAttributes.nLength=
sizeof(SecurityAttributes);
141 SecurityAttributes.lpSecurityDescriptor= NULL;
142 SecurityAttributes.bInheritHandle= !(oflag & _O_NOINHERIT);
145 switch (oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) {
147 fileaccess= GENERIC_READ;
150 fileaccess= GENERIC_WRITE;
153 fileaccess= GENERIC_READ | GENERIC_WRITE;
163 fileshare= FILE_SHARE_DELETE;
166 fileshare= FILE_SHARE_READ | FILE_SHARE_DELETE;
169 fileshare= FILE_SHARE_WRITE | FILE_SHARE_DELETE;
172 fileshare= FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
178 fileshare= FILE_SHARE_READ;
181 fileshare= FILE_SHARE_WRITE;
184 fileshare= FILE_SHARE_READ | FILE_SHARE_WRITE;
192 switch (oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
195 filecreate= OPEN_EXISTING;
199 filecreate= OPEN_ALWAYS;
202 case _O_CREAT | _O_EXCL:
203 case _O_CREAT | _O_TRUNC | _O_EXCL:
204 filecreate= CREATE_NEW;
208 case _O_TRUNC | _O_EXCL:
209 filecreate= TRUNCATE_EXISTING;
212 case _O_CREAT | _O_TRUNC:
213 filecreate= CREATE_ALWAYS;
223 fileattrib= FILE_ATTRIBUTE_NORMAL;
224 if (oflag & _O_CREAT)
226 _umask((mask= _umask(0)));
228 if (!((pmode & ~mask) & _S_IWRITE))
229 fileattrib= FILE_ATTRIBUTE_READONLY;
233 if (oflag & _O_TEMPORARY)
235 fileattrib|= FILE_FLAG_DELETE_ON_CLOSE;
240 if (oflag & _O_SHORT_LIVED)
241 fileattrib|= FILE_ATTRIBUTE_TEMPORARY;
244 if (oflag & _O_SEQUENTIAL)
245 fileattrib|= FILE_FLAG_SEQUENTIAL_SCAN;
246 else if (oflag & _O_RANDOM)
247 fileattrib|= FILE_FLAG_RANDOM_ACCESS;
250 if ((osfh= CreateFile(path, fileaccess, fileshare, &SecurityAttributes,
251 filecreate, fileattrib, NULL)) == INVALID_HANDLE_VALUE)
258 my_osmaperr(GetLastError());
262 if ((fh= my_open_osfhandle(osfh,
263 oflag & (_O_APPEND | _O_RDONLY | _O_TEXT))) == -1)
272 File my_win_open(
const char *path,
int flags)
274 DBUG_ENTER(
"my_win_open");
275 DBUG_RETURN(my_win_sopen((
char *) path, flags | _O_BINARY, _SH_DENYNO,
276 _S_IREAD | S_IWRITE));
280 int my_win_close(File fd)
282 DBUG_ENTER(
"my_win_close");
283 if(CloseHandle(my_get_osfhandle(fd)))
288 my_osmaperr(GetLastError());
293 size_t my_win_pread(File Filedes, uchar *
Buffer,
size_t Count, my_off_t offset)
300 DBUG_ENTER(
"my_win_pread");
309 hFile= (HANDLE)my_get_osfhandle(Filedes);
311 ov.Offset= li.LowPart;
312 ov.OffsetHigh= li.HighPart;
314 if(!ReadFile(hFile, Buffer, (DWORD)Count, &nBytesRead, &ov))
316 DWORD lastError= GetLastError();
321 if(lastError == ERROR_HANDLE_EOF || lastError == ERROR_BROKEN_PIPE)
323 my_osmaperr(lastError);
324 DBUG_RETURN((
size_t)-1);
326 DBUG_RETURN(nBytesRead);
330 size_t my_win_read(File Filedes, uchar *Buffer,
size_t Count)
335 DBUG_ENTER(
"my_win_read");
343 hFile= (HANDLE)my_get_osfhandle(Filedes);
345 if(!ReadFile(hFile, Buffer, (DWORD)Count, &nBytesRead, NULL))
347 DWORD lastError= GetLastError();
352 if(lastError == ERROR_HANDLE_EOF || lastError == ERROR_BROKEN_PIPE)
354 my_osmaperr(lastError);
355 DBUG_RETURN((
size_t)-1);
357 DBUG_RETURN(nBytesRead);
361 size_t my_win_pwrite(File Filedes,
const uchar *Buffer,
size_t Count,
369 DBUG_ENTER(
"my_win_pwrite");
370 DBUG_PRINT(
"my",(
"Filedes: %d, Buffer: %p, Count: %llu, offset: %llu",
371 Filedes, Buffer, (ulonglong)Count, (ulonglong)offset));
381 hFile= (HANDLE)my_get_osfhandle(Filedes);
383 ov.Offset= li.LowPart;
384 ov.OffsetHigh= li.HighPart;
386 if(!WriteFile(hFile, Buffer, (DWORD)Count, &nBytesWritten, &ov))
388 my_osmaperr(GetLastError());
389 DBUG_RETURN((
size_t)-1);
392 DBUG_RETURN(nBytesWritten);
396 my_off_t my_win_lseek(File fd, my_off_t pos,
int whence)
399 LARGE_INTEGER newpos;
401 DBUG_ENTER(
"my_win_lseek");
404 compile_time_assert(FILE_BEGIN == SEEK_SET && FILE_CURRENT == SEEK_CUR
405 && FILE_END == SEEK_END);
407 offset.QuadPart= pos;
408 if(!SetFilePointerEx(my_get_osfhandle(fd), offset, &newpos, whence))
410 my_osmaperr(GetLastError());
413 DBUG_RETURN(newpos.QuadPart);
417 #ifndef FILE_WRITE_TO_END_OF_FILE
418 #define FILE_WRITE_TO_END_OF_FILE 0xffffffff
420 size_t my_win_write(File fd,
const uchar *Buffer,
size_t Count)
424 OVERLAPPED *pov= NULL;
427 DBUG_ENTER(
"my_win_write");
428 DBUG_PRINT(
"my",(
"Filedes: %d, Buffer: %p, Count %llu", fd, Buffer,
439 if(my_get_open_flags(fd) & _O_APPEND)
445 memset(&ov, 0,
sizeof(ov));
446 ov.Offset= FILE_WRITE_TO_END_OF_FILE;
451 hFile= my_get_osfhandle(fd);
452 if(!WriteFile(hFile, Buffer, (DWORD)Count, &nWritten, pov))
454 my_osmaperr(GetLastError());
455 DBUG_RETURN((
size_t)-1);
457 DBUG_RETURN(nWritten);
461 int my_win_chsize(File fd, my_off_t newlength)
464 LARGE_INTEGER length;
465 DBUG_ENTER(
"my_win_chsize");
467 hFile= (HANDLE) my_get_osfhandle(fd);
468 length.QuadPart= newlength;
469 if (!SetFilePointerEx(hFile, length , NULL , FILE_BEGIN))
471 if (!SetEndOfFile(hFile))
475 my_osmaperr(GetLastError());
482 static File my_get_stdfile_descriptor(FILE *stream)
486 DBUG_ENTER(
"my_get_stdfile_descriptor");
489 nStdHandle= STD_INPUT_HANDLE;
490 else if(stream == stdout)
491 nStdHandle= STD_OUTPUT_HANDLE;
492 else if(stream == stderr)
493 nStdHandle= STD_ERROR_HANDLE;
497 hFile= GetStdHandle(nStdHandle);
498 if(hFile != INVALID_HANDLE_VALUE)
499 DBUG_RETURN(my_open_osfhandle(hFile, 0));
504 File my_win_fileno(FILE *
file)
506 HANDLE hFile= (HANDLE)_get_osfhandle(fileno(file));
510 DBUG_ENTER(
"my_win_fileno");
512 for(i= MY_FILE_MIN; i < my_file_limit; i++)
514 if(my_file_info[i].fhandle == hFile)
522 DBUG_RETURN(my_get_stdfile_descriptor(file));
527 FILE *my_win_fopen(
const char *filename,
const char *
type)
531 DBUG_ENTER(
"my_win_open");
538 if (check_if_legal_filename(filename))
544 file= fopen(filename, type);
548 if(strchr(type,
'a') != NULL)
555 if(my_open_osfhandle((HANDLE)_get_osfhandle(fileno(file)), flags) < 0)
564 FILE * my_win_fdopen(File fd,
const char *type)
570 DBUG_ENTER(
"my_win_fdopen");
572 if(strchr(type,
'a') != NULL)
575 crt_fd= _open_osfhandle((intptr_t)my_get_osfhandle(fd), flags);
579 file= fdopen(crt_fd, type);
584 int my_win_fclose(FILE *file)
588 DBUG_ENTER(
"my_win_close");
606 int my_win_fstat(File fd,
struct _stati64 *
buf)
612 DBUG_ENTER(
"my_win_fstat");
614 hFile= my_get_osfhandle(fd);
615 if(!DuplicateHandle( GetCurrentProcess(), hFile, GetCurrentProcess(),
616 &hDup ,0,FALSE,DUPLICATE_SAME_ACCESS))
618 my_osmaperr(GetLastError());
621 if ((crt_fd= _open_osfhandle((intptr_t)hDup,0)) < 0)
624 retval= _fstati64(crt_fd, buf);
628 GetFileSizeEx(hDup, (PLARGE_INTEGER) (&(buf->st_size)));
636 int my_win_stat(
const char *path,
struct _stati64 *buf)
638 DBUG_ENTER(
"my_win_stat");
639 if(_stati64( path, buf) == 0)
642 WIN32_FILE_ATTRIBUTE_DATA data;
643 if (GetFileAttributesEx(path, GetFileExInfoStandard, &data))
646 li.LowPart= data.nFileSizeLow;
647 li.HighPart= data.nFileSizeHigh;
648 buf->st_size= li.QuadPart;
657 int my_win_fsync(File fd)
659 DBUG_ENTER(
"my_win_fsync");
660 if(FlushFileBuffers(my_get_osfhandle(fd)))
662 my_osmaperr(GetLastError());
668 int my_win_dup(File fd)
671 DBUG_ENTER(
"my_win_dup");
672 if (DuplicateHandle(GetCurrentProcess(), my_get_osfhandle(fd),
673 GetCurrentProcess(), &hDup, 0, FALSE, DUPLICATE_SAME_ACCESS))
675 DBUG_RETURN(my_open_osfhandle(hDup, my_get_open_flags(fd)));
677 my_osmaperr(GetLastError());