MySQL 8.4.0
Source Code Documentation
my_winfile.cc File Reference

The purpose of this file is to provide implementation of file IO routines on Windows that can be thought as drop-in replacement for corresponding C runtime functionality. More...

#include <algorithm>
#include <iostream>
#include <vector>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <share.h>
#include <sys/stat.h>
#include "mutex_lock.h"
#include "my_dbug.h"
#include "my_io.h"
#include "my_sys.h"
#include "my_thread_local.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysys_priv.h"
#include "scope_guard.h"
#include "sql/malloc_allocator.h"

Classes

class  anonymous_namespace{my_winfile.cc}::WindowsErrorGuard
 RAII guard which ensures that: More...
 
struct  anonymous_namespace{my_winfile.cc}::HandleInfo
 

Namespaces

namespace  anonymous_namespace{my_winfile.cc}
 

Typedefs

using anonymous_namespace{my_winfile.cc}::HandleInfoAllocator = Malloc_allocator< HandleInfo >
 
using anonymous_namespace{my_winfile.cc}::HandleInfoVector = std::vector< HandleInfo, HandleInfoAllocator >
 

Functions

size_t anonymous_namespace{my_winfile.cc}::ToIndex (File fd)
 
int anonymous_namespace{my_winfile.cc}::ToDescr (size_t hi)
 
bool anonymous_namespace{my_winfile.cc}::IsValidIndex (size_t hi)
 
HandleInfo anonymous_namespace{my_winfile.cc}::GetHandleInfo (File fd)
 
File anonymous_namespace{my_winfile.cc}::RegisterHandle (HANDLE handle, int oflag)
 
HandleInfo anonymous_namespace{my_winfile.cc}::UnregisterHandle (File fd)
 
File anonymous_namespace{my_winfile.cc}::FileIndex (HANDLE handle)
 
LARGE_INTEGER anonymous_namespace{my_winfile.cc}::MakeLargeInteger (int64_t src)
 
OVERLAPPED anonymous_namespace{my_winfile.cc}::MakeOverlapped (DWORD l, DWORD h)
 
OVERLAPPED anonymous_namespace{my_winfile.cc}::MakeOverlapped (int64_t src)
 
File anonymous_namespace{my_winfile.cc}::my_win_sopen (const char *path, int oflag, int shflag, int pmode)
 Open a file with sharing. More...
 
File anonymous_namespace{my_winfile.cc}::my_get_stdfile_descriptor (FILE *stream)
 
HANDLE my_get_osfhandle (File fd)
 Return the Windows HANDLE for a file descriptor obtained from RegisterHandle(). More...
 
File my_win_open (const char *path, int mode)
 Homegrown posix emulation for Windows. More...
 
int my_win_close (File fd)
 Homegrown posix emulation for Windows. More...
 
int64_t my_win_pread (File fd, uchar *buffer, size_t count, int64_t offset)
 Homegrown posix emulation for Windows. More...
 
int64_t my_win_pwrite (File fd, const uchar *buffer, size_t count, int64_t offset)
 Homegrown posix emulation for Windows. More...
 
int64_t my_win_lseek (File fd, int64_t pos, int whence)
 Homegrown posix emulation for Windows. More...
 
int64_t my_win_write (File fd, const uchar *Buffer, size_t Count)
 Homegrown posix emulation for Windows. More...
 
int my_win_chsize (File fd, int64_t newlength)
 Homegrown posix emulation for Windows. More...
 
File my_win_fileno (FILE *stream)
 Homegrown posix emulation for Windows. More...
 
FILE * my_win_fopen (const char *filename, const char *mode)
 Homegrown posix emulation for Windows. More...
 
FILE * my_win_fdopen (File fd, const char *mode)
 Homegrown posix emulation for Windows. More...
 
int my_win_fclose (FILE *stream)
 Homegrown posix emulation for Windows. More...
 
FILE * my_win_freopen (const char *path, const char *mode, FILE *stream)
 Homegrown posix emulation for Windows. More...
 
int my_win_fstat (File fd, struct _stati64 *buf)
 Homegrown posix emulation for Windows. More...
 
int my_win_stat (const char *path, struct _stati64 *buf)
 Homegrown posix emulation for Windows. More...
 
int my_win_fsync (File fd)
 Homegrown posix emulation for Windows. More...
 
void MyWinfileInit ()
 Constructs static objects. More...
 
void MyWinfileEnd ()
 Destroys static objects. More...
 

Variables

HandleInfoVector * anonymous_namespace{my_winfile.cc}::hivp = nullptr
 

Detailed Description

The purpose of this file is to provide implementation of file IO routines on Windows that can be thought as drop-in replacement for corresponding C runtime functionality.

Compared to Windows CRT, this one

  • does not have the same file descriptor limitation (default is 16384 and can be increased further, whereas CRT poses a hard limit of 2048 file descriptors)
  • the file operations are not serialized
  • positional IO pread/pwrite is ported here.
  • no text mode for files, all IO is "binary"

Naming convention: All routines are prefixed with my_win_, e.g Posix open() is implemented with my_win_open()

Implemented are

  • POSIX routines(e.g open, read, lseek ...)
  • Some ANSI C stream routines (fopen, fdopen, fileno, fclose)
  • Windows CRT equivalents (my_get_osfhandle, open_osfhandle)

Worth to note:

  • File descriptors used here are located in a range that is not compatible with CRT on purpose. Attempt to use a file descriptor from Windows CRT library range in my_win_* function will be punished with assert()
  • File streams (FILE *) are actually from the C runtime. The routines provided here are useful only in scenarios that use low-level IO with my_win_fileno()

Function Documentation

◆ my_get_osfhandle()

HANDLE my_get_osfhandle ( File  fd)

Return the Windows HANDLE for a file descriptor obtained from RegisterHandle().

Parameters
fd"file descriptor" index into HandleInfoVector.
Return values
handlecorresponding to fd, if found
INVALID_HANDLE_VALUE,iffd is not valid (illegal index into HandleInfoVector). In this case SetLastError(ERROR_INVALID_HANDLE) will have been called.

◆ my_win_chsize()

int my_win_chsize ( File  fd,
int64_t  newlength 
)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
newlengthnew desired length.
Return values
0if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_close()

int my_win_close ( File  fd)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
Return values
0if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_fclose()

int my_win_fclose ( FILE *  stream)

Homegrown posix emulation for Windows.

Parameters
streamFILE stream to close.
Note
Calls to my_fileno() and UnregsterHandle() would not be necessary if pseudo fds were not allocated for streams.
Return values
0if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_fdopen()

FILE * my_win_fdopen ( File  fd,
const char *  mode 
)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
modeFile access mode string.
Note
Currently only used in ibd2sdi to bind the fd for a temporary file to a file stream. Could possibly be replaced with tmpfile_s.
Return values
FILEstream associated with given file descriptor, if successful.
nullptrin case of errors. errno and/or LastError set to indicate the error.

◆ my_win_fileno()

File my_win_fileno ( FILE *  stream)

Homegrown posix emulation for Windows.

Parameters
streamFILE stream to get fd for.
Note
Returned fd can either be a CRT standard fd (stdin stdout or stderr), or a pseudo fd which is an index into the mapping vector).
Return values
Valid"file descriptor" if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_fopen()

FILE * my_win_fopen ( const char *  filename,
const char *  mode 
)

Homegrown posix emulation for Windows.

Parameters
filenameFile name.
modeFile access mode string.
Return values
FILEstream associated with given file name, if successful.
nullptrin case of errors. errno and/or LastError set to indicate the error.

◆ my_win_freopen()

FILE * my_win_freopen ( const char *  path,
const char *  mode,
FILE *  stream 
)

Homegrown posix emulation for Windows.

Parameters
pathFile name.
modeFile access mode string
streamExisting stream to reopen
Note
C11 includes freopen_s. If pseudo fds did not have to be created for streams it would probably be sufficient to just forward to freopen_s.
Return values
validFILE stream if successful.
nullptrin case of errors. errno and/or LastError set to indicate the error.

◆ my_win_fstat()

int my_win_fstat ( File  fd,
struct _stati64 *  buf 
)

Homegrown posix emulation for Windows.

Quick and dirty implementation for Windows. Use CRT fstat on temporarily allocated file descriptor. Patch file size, because size that fstat returns is not reliable (may be outdated).

Parameters
fd"File descriptor" (index into mapping vector).
[out]bufArea to store stat information in.
Return values
0if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_fsync()

int my_win_fsync ( File  fd)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
Return values
0if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_lseek()

int64_t my_win_lseek ( File  fd,
int64_t  pos,
int  whence 
)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
posOffset to seek.
whenceWhere to seek from.
Return values
Newoffset into file, if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_open()

File my_win_open ( const char *  path,
int  mode 
)

Homegrown posix emulation for Windows.

Parameters
pathFile name.
modeFile access mode string.
Return values
valid"file descriptor" (actually index into mapping vector) if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_pread()

int64_t my_win_pread ( File  fd,
uchar buffer,
size_t  count,
int64_t  offset 
)

Homegrown posix emulation for Windows.

Implements BOTH posix read() and posix pread(). To read from the current file position (like posix read() supply an offset argument that is -1.

Parameters
fd"File descriptor" (index into mapping vector).
bufferDestination buffer.
countNumber of bytes to read.
offsetOffset where read starts. -1 to read from the current file position.
Note
ERROR_HANDLE_EOF and ERROR_BROKEN_PIPE are treated as success (returns 0, but with LastError set).
Return values
Numberof bytes read, if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_pwrite()

int64_t my_win_pwrite ( File  fd,
const uchar buffer,
size_t  count,
int64_t  offset 
)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
bufferSource buffer.
countNumber of bytes to write.
offsetWhere to start the write.
Return values
Numberof bytes written, if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_stat()

int my_win_stat ( const char *  path,
struct _stati64 *  buf 
)

Homegrown posix emulation for Windows.

Parameters
pathFile name to obtain information about.
[out]bufArea to store stat information in.
Return values
Valid"file descriptor" (actually index into mapping vector) if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ my_win_write()

int64_t my_win_write ( File  fd,
const uchar Buffer,
size_t  Count 
)

Homegrown posix emulation for Windows.

Parameters
fd"File descriptor" (index into mapping vector).
BufferSource Bytes.
CountNumber of bytes to write.
Return values
Numberof bytes written, if successful.
-1in case of errors. errno and/or LastError set to indicate the error.

◆ MyWinfileEnd()

void MyWinfileEnd ( )

Destroys static objects.

◆ MyWinfileInit()

void MyWinfileInit ( )

Constructs static objects.