my_fopen.c 5.59 KB
Newer Older
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1 2 3 4 5 6 7 8
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10 11 12 13 14 15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include "mysys_priv.h"
#include "my_static.h"
#include <errno.h>
#include "mysys_err.h"

static void	make_ftype(my_string to,int flag);

	/* Open a file as stream */

FILE *my_fopen(const char *FileName, int Flags, myf MyFlags)
					/* Path-name of file */
					/* Read | write .. */
					/* Special flags */
{
  FILE *fd;
  char type[5];
  DBUG_ENTER("my_fopen");
  DBUG_PRINT("my",("Name: '%s'  Flags: %d  MyFlags: %d",
		   FileName, Flags, MyFlags));
36
  /* 
37 38 39
    if we are not creating, then we need to use my_access to make sure  
    the file exists since Windows doesn't handle files like "com1.sym" 
    very  well 
40 41
  */
#ifdef __WIN__
monty@mysql.com's avatar
monty@mysql.com committed
42 43 44 45 46
  if (check_if_legal_filename(FileName))
  {
    errno= EACCES;
    fd= 0;
  }
47 48 49
  else
#endif
  {
monty@mysql.com's avatar
monty@mysql.com committed
50 51
    make_ftype(type,Flags);
    fd = fopen(FileName, type);
52 53 54
  }
  
  if (fd != 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
55 56 57 58 59 60
  {
    /*
      The test works if MY_NFILE < 128. The problem is that fileno() is char
      on some OS (SUNOS). Actually the filename save isn't that important
      so we can ignore if this doesn't work.
    */
61
    if ((uint) fileno(fd) >= my_file_limit)
62 63
    {
      thread_safe_increment(my_stream_opened,&THR_LOCK_open);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
64
      DBUG_RETURN(fd);				/* safeguard */
65
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
66 67 68 69 70 71 72
    pthread_mutex_lock(&THR_LOCK_open);
    if ((my_file_info[fileno(fd)].name = (char*)
	 my_strdup(FileName,MyFlags)))
    {
      my_stream_opened++;
      my_file_info[fileno(fd)].type = STREAM_BY_FOPEN;
      pthread_mutex_unlock(&THR_LOCK_open);
73
      DBUG_PRINT("exit",("stream: 0x%lx",fd));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      DBUG_RETURN(fd);
    }
    pthread_mutex_unlock(&THR_LOCK_open);
    (void) my_fclose(fd,MyFlags);
    my_errno=ENOMEM;
  }
  else
    my_errno=errno;
  DBUG_PRINT("error",("Got error %d on open",my_errno));
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
    my_error((Flags & O_RDONLY) || (Flags == O_RDONLY ) ? EE_FILENOTFOUND :
	     EE_CANTCREATEFILE,
	     MYF(ME_BELL+ME_WAITTANG), FileName,my_errno);
  DBUG_RETURN((FILE*) 0);
} /* my_fopen */


	/* Close a stream */

int my_fclose(FILE *fd, myf MyFlags)
{
  int err,file;
  DBUG_ENTER("my_fclose");
97
  DBUG_PRINT("my",("stream: 0x%lx  MyFlags: %d",fd, MyFlags));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
98 99 100 101 102 103 104 105 106 107

  pthread_mutex_lock(&THR_LOCK_open);
  file=fileno(fd);
  if ((err = fclose(fd)) < 0)
  {
    my_errno=errno;
    if (MyFlags & (MY_FAE | MY_WME))
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
	       my_filename(file),errno);
  }
108 109
  else
    my_stream_opened--;
110
  if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
111 112
  {
    my_file_info[file].type = UNOPEN;
113
    my_free(my_file_info[file].name, MYF(MY_ALLOW_ZERO_PTR));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
114 115 116 117 118 119 120
  }
  pthread_mutex_unlock(&THR_LOCK_open);
  DBUG_RETURN(err);
} /* my_fclose */


	/* Make a stream out of a file handle */
121
	/* Name may be 0 */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
122

123
FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
  FILE *fd;
  char type[5];
  DBUG_ENTER("my_fdopen");
  DBUG_PRINT("my",("Fd: %d  Flags: %d  MyFlags: %d",
		   Filedes, Flags, MyFlags));

  make_ftype(type,Flags);
  if ((fd = fdopen(Filedes, type)) == 0)
  {
    my_errno=errno;
    if (MyFlags & (MY_FAE | MY_WME))
      my_error(EE_CANT_OPEN_STREAM, MYF(ME_BELL+ME_WAITTANG),errno);
  }
  else
  {
    pthread_mutex_lock(&THR_LOCK_open);
141
    my_stream_opened++;
142
    if ((uint) Filedes < (uint) my_file_limit)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
143
    {
144 145
      if (my_file_info[Filedes].type != UNOPEN)
      {
146
        my_file_opened--;		/* File is opened with my_open ! */
147 148 149 150 151
      }
      else
      {
        my_file_info[Filedes].name=  my_strdup(name,MyFlags);
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
152 153 154 155 156
      my_file_info[Filedes].type = STREAM_BY_FDOPEN;
    }
    pthread_mutex_unlock(&THR_LOCK_open);
  }

157
  DBUG_PRINT("exit",("stream: 0x%lx",fd));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
158 159 160
  DBUG_RETURN(fd);
} /* my_fdopen */

161 162 163
/*   
  make_ftype    
  Make a filehandler-open-typestring from ordinary inputflags  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
164

165 166 167 168 169 170 171 172 173 174 175
  Note:  This routine attempts to find the best possible match 
  between  a numeric option and a string option that could be 
  fed to fopen.  There is not a 1 to 1 mapping between the two.  
  
  r == O_RDONLY   
  w == O_WRONLY|O_TRUNC|O_CREAT  
  a == O_WRONLY|O_APPEND|O_CREAT  
  r+ == O_RDWR  
  w+ == O_RDWR|O_TRUNC|O_CREAT  
  a+ == O_RDWR|O_APPEND|O_CREAT
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
176 177
static void make_ftype(register my_string to, register int flag)
{
178 179
#if FILE_BINARY
  /* If we have binary-files */  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
180
  reg3 int org_flag=flag;
181 182 183 184 185 186 187
#endif  
  flag&= ~FILE_BINARY;             /* remove binary bit */  
  
  /* check some possible invalid combinations */  
  DBUG_ASSERT(flag & (O_TRUNC|O_APPEND) != O_TRUNC|O_APPEND);  

  if (flag & (O_RDONLY|O_WRONLY) == O_WRONLY)    
188
    *to++= (flag & O_APPEND) ? 'a' : 'w';  
189 190 191
  else if (flag & O_RDWR)          
  {
    /* Add '+' after theese */    
192
    if (flag & (O_TRUNC | O_CREAT))      
193 194 195 196
      *to++= 'w';    
    else if (flag & O_APPEND)      
      *to++= 'a';    
    else      
bk@work.mysql.com's avatar
bk@work.mysql.com committed
197
      *to++= 'r';
198 199 200 201 202 203 204
    *to++= '+';  
  }  
  else    
    *to++= 'r';

#if FILE_BINARY            /* If we have binary-files */  
  if (org_flag & FILE_BINARY)    
bk@work.mysql.com's avatar
bk@work.mysql.com committed
205
    *to++='b';
206
#endif  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
207 208
  *to='\0';
} /* make_ftype */
209