Commit 24d8bc70 authored by Sergei Golubchik's avatar Sergei Golubchik

cleanup: my_register_filename()

Don't let my_register_filename() fail because strdup() failed. Better to
have NULL for a filename, then to fail the already successful open().

Filenames are only used for error reporting and there was already code
to ignore OOMs (my_fdopen()) and to cope with missing filenames
(my_filename()).
parent 3cba74e0
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
File my_create(const char *FileName, int CreateFlags, int access_flags, File my_create(const char *FileName, int CreateFlags, int access_flags,
myf MyFlags) myf MyFlags)
{ {
int fd, rc; int fd;
DBUG_ENTER("my_create"); DBUG_ENTER("my_create");
DBUG_PRINT("my",("Name: '%s' CreateFlags: %d AccessFlags: %d MyFlags: %d", DBUG_PRINT("my",("Name: '%s' CreateFlags: %d AccessFlags: %d MyFlags: %d",
FileName, CreateFlags, access_flags, MyFlags)); FileName, CreateFlags, access_flags, MyFlags));
...@@ -54,21 +54,7 @@ File my_create(const char *FileName, int CreateFlags, int access_flags, ...@@ -54,21 +54,7 @@ File my_create(const char *FileName, int CreateFlags, int access_flags,
fd= -1; fd= -1;
} }
rc= my_register_filename(fd, FileName, FILE_BY_CREATE, fd= my_register_filename(fd, FileName, FILE_BY_CREATE,
EE_CANTCREATEFILE, MyFlags); EE_CANTCREATEFILE, MyFlags);
/* DBUG_RETURN(fd);
my_register_filename() may fail on some platforms even if the call to
*open() above succeeds. In this case, don't leave the stale file because
callers assume the file to not exist if my_create() fails, so they don't
do any cleanups.
*/
if (unlikely(fd >= 0 && rc < 0))
{
int tmp= my_errno;
my_close(fd, MyFlags);
my_delete(FileName, MyFlags);
my_errno= tmp;
}
DBUG_RETURN(rc);
} /* my_create */ } /* my_create */
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
char * my_filename(File fd) char * my_filename(File fd)
{ {
DBUG_ENTER("my_filename"); DBUG_ENTER("my_filename");
if ((uint) fd >= (uint) my_file_limit) if ((uint) fd >= (uint) my_file_limit || !my_file_info[fd].name)
DBUG_RETURN((char*) "UNKNOWN"); DBUG_RETURN((char*) "UNKNOWN");
if (fd >= 0 && my_file_info[fd].type != UNOPEN) if (fd >= 0 && my_file_info[fd].type != UNOPEN)
{ {
......
...@@ -69,19 +69,13 @@ FILE *my_fopen(const char *filename, int flags, myf MyFlags) ...@@ -69,19 +69,13 @@ FILE *my_fopen(const char *filename, int flags, myf MyFlags)
DBUG_RETURN(fd); /* safeguard */ DBUG_RETURN(fd); /* safeguard */
} }
mysql_mutex_lock(&THR_LOCK_open); mysql_mutex_lock(&THR_LOCK_open);
if ((my_file_info[filedesc].name= (char*) my_file_info[filedesc].name= (char*) my_strdup(filename,MyFlags);
my_strdup(filename,MyFlags))) my_stream_opened++;
{ my_file_total_opened++;
my_stream_opened++; my_file_info[filedesc].type= STREAM_BY_FOPEN;
my_file_total_opened++;
my_file_info[filedesc].type= STREAM_BY_FOPEN;
mysql_mutex_unlock(&THR_LOCK_open);
DBUG_PRINT("exit",("stream: 0x%lx", (long) fd));
DBUG_RETURN(fd);
}
mysql_mutex_unlock(&THR_LOCK_open); mysql_mutex_unlock(&THR_LOCK_open);
(void) my_fclose(fd,MyFlags); DBUG_PRINT("exit",("stream: 0x%lx", (long) fd));
my_errno=ENOMEM; DBUG_RETURN(fd);
} }
else else
my_errno=errno; my_errno=errno;
......
...@@ -131,25 +131,16 @@ File my_register_filename(File fd, const char *FileName, enum file_type ...@@ -131,25 +131,16 @@ File my_register_filename(File fd, const char *FileName, enum file_type
thread_safe_increment(my_file_opened,&THR_LOCK_open); thread_safe_increment(my_file_opened,&THR_LOCK_open);
DBUG_RETURN(fd); /* safeguard */ DBUG_RETURN(fd); /* safeguard */
} }
else mysql_mutex_lock(&THR_LOCK_open);
{ my_file_info[fd].name = (char*) my_strdup(FileName, MyFlags);
mysql_mutex_lock(&THR_LOCK_open); my_file_opened++;
if ((my_file_info[fd].name = (char*) my_strdup(FileName,MyFlags))) my_file_total_opened++;
{ my_file_info[fd].type = type_of_file;
my_file_opened++; mysql_mutex_unlock(&THR_LOCK_open);
my_file_total_opened++; DBUG_PRINT("exit",("fd: %d",fd));
my_file_info[fd].type = type_of_file; DBUG_RETURN(fd);
mysql_mutex_unlock(&THR_LOCK_open);
DBUG_PRINT("exit",("fd: %d",fd));
DBUG_RETURN(fd);
}
mysql_mutex_unlock(&THR_LOCK_open);
my_errno= ENOMEM;
}
(void) my_close(fd, MyFlags);
} }
else my_errno= errno;
my_errno= errno;
DBUG_PRINT("error",("Got error %d on open", my_errno)); DBUG_PRINT("error",("Got error %d on open", my_errno));
if (MyFlags & (MY_FFNF | MY_FAE | MY_WME)) if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment