~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include "mysys_priv.h"
17
#include "my_static.h"
18
#include <errno.h>
19
#include "mysys_err.h"
20
21
static void make_ftype(char * to,int flag);
22
23
/*
24
  Open a file as stream
25
26
  SYNOPSIS
27
    my_fopen()
28
    FileName	Path-name of file
29
    Flags	Read | write | append | trunc (like for open())
30
    MyFlags	Flags for handling errors
31
32
  RETURN
33
    0	Error
34
    #	File handler
35
*/
36
37
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
38
{
39
  FILE *fd;
40
  char type[5];
41
  DBUG_ENTER("my_fopen");
42
  DBUG_PRINT("my",("Name: '%s'  flags: %d  MyFlags: %d",
43
		   filename, flags, MyFlags));
44
  /* 
45
    if we are not creating, then we need to use my_access to make sure  
46
    the file exists since Windows doesn't handle files like "com1.sym" 
47
    very  well 
48
  */
49
  {
50
    make_ftype(type,flags);
51
    fd = fopen(filename, type);
52
  }
53
  
54
  if (fd != 0)
55
  {
56
    /*
57
      The test works if MY_NFILE < 128. The problem is that fileno() is char
58
      on some OS (SUNOS). Actually the filename save isn't that important
59
      so we can ignore if this doesn't work.
60
    */
61
    if ((uint) fileno(fd) >= my_file_limit)
62
    {
63
      thread_safe_increment(my_stream_opened,&THR_LOCK_open);
64
      DBUG_RETURN(fd);				/* safeguard */
65
    }
66
    pthread_mutex_lock(&THR_LOCK_open);
67
    if ((my_file_info[fileno(fd)].name = (char*)
68
	 my_strdup(filename,MyFlags)))
69
    {
70
      my_stream_opened++;
71
      my_file_total_opened++;
72
      my_file_info[fileno(fd)].type = STREAM_BY_FOPEN;
73
      pthread_mutex_unlock(&THR_LOCK_open);
74
      DBUG_PRINT("exit",("stream: 0x%lx", (long) fd));
75
      DBUG_RETURN(fd);
76
    }
77
    pthread_mutex_unlock(&THR_LOCK_open);
78
    (void) my_fclose(fd,MyFlags);
79
    my_errno=ENOMEM;
80
  }
81
  else
82
    my_errno=errno;
83
  DBUG_PRINT("error",("Got error %d on open",my_errno));
84
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
85
    my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
86
	     EE_CANTCREATEFILE,
87
	     MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
88
  DBUG_RETURN((FILE*) 0);
89
} /* my_fopen */
90
91
92
	/* Close a stream */
93
94
int my_fclose(FILE *fd, myf MyFlags)
95
{
96
  int err,file;
97
  DBUG_ENTER("my_fclose");
98
  DBUG_PRINT("my",("stream: 0x%lx  MyFlags: %d", (long) fd, MyFlags));
99
100
  pthread_mutex_lock(&THR_LOCK_open);
101
  file=fileno(fd);
102
  if ((err = fclose(fd)) < 0)
103
  {
104
    my_errno=errno;
105
    if (MyFlags & (MY_FAE | MY_WME))
106
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
107
	       my_filename(file),errno);
108
  }
109
  else
110
    my_stream_opened--;
111
  if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
112
  {
113
    my_file_info[file].type = UNOPEN;
114
    my_free(my_file_info[file].name, MYF(MY_ALLOW_ZERO_PTR));
115
  }
116
  pthread_mutex_unlock(&THR_LOCK_open);
117
  DBUG_RETURN(err);
118
} /* my_fclose */
119
120
121
	/* Make a stream out of a file handle */
122
	/* Name may be 0 */
123
124
FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags)
125
{
126
  FILE *fd;
127
  char type[5];
128
  DBUG_ENTER("my_fdopen");
129
  DBUG_PRINT("my",("Fd: %d  Flags: %d  MyFlags: %d",
130
		   Filedes, Flags, MyFlags));
131
132
  make_ftype(type,Flags);
133
  if ((fd = fdopen(Filedes, type)) == 0)
134
  {
135
    my_errno=errno;
136
    if (MyFlags & (MY_FAE | MY_WME))
137
      my_error(EE_CANT_OPEN_STREAM, MYF(ME_BELL+ME_WAITTANG),errno);
138
  }
139
  else
140
  {
141
    pthread_mutex_lock(&THR_LOCK_open);
142
    my_stream_opened++;
143
    if ((uint) Filedes < (uint) my_file_limit)
144
    {
145
      if (my_file_info[Filedes].type != UNOPEN)
146
      {
147
        my_file_opened--;		/* File is opened with my_open ! */
148
      }
149
      else
150
      {
151
        my_file_info[Filedes].name=  my_strdup(name,MyFlags);
152
      }
153
      my_file_info[Filedes].type = STREAM_BY_FDOPEN;
154
    }
155
    pthread_mutex_unlock(&THR_LOCK_open);
156
  }
157
158
  DBUG_PRINT("exit",("stream: 0x%lx", (long) fd));
159
  DBUG_RETURN(fd);
160
} /* my_fdopen */
161
162
163
/*   
164
  Make a fopen() typestring from a open() type bitmap
165
166
  SYNOPSIS
167
    make_ftype()
168
    to		String for fopen() is stored here
169
    flag	Flag used by open()
170
171
  IMPLEMENTATION
172
    This routine attempts to find the best possible match 
173
    between  a numeric option and a string option that could be 
174
    fed to fopen.  There is not a 1 to 1 mapping between the two.  
175
  
176
  NOTE
177
    On Unix, O_RDONLY is usually 0
178
179
  MAPPING
180
    r  == O_RDONLY   
181
    w  == O_WRONLY|O_TRUNC|O_CREAT  
182
    a  == O_WRONLY|O_APPEND|O_CREAT  
183
    r+ == O_RDWR  
184
    w+ == O_RDWR|O_TRUNC|O_CREAT  
185
    a+ == O_RDWR|O_APPEND|O_CREAT
186
*/
187
188
static void make_ftype(register char * to, register int flag)
189
{
190
  /* check some possible invalid combinations */  
191
  DBUG_ASSERT((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
192
  DBUG_ASSERT((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
193
194
  if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)    
195
    *to++= (flag & O_APPEND) ? 'a' : 'w';  
196
  else if (flag & O_RDWR)          
197
  {
198
    /* Add '+' after theese */    
199
    if (flag & (O_TRUNC | O_CREAT))      
200
      *to++= 'w';    
201
    else if (flag & O_APPEND)      
202
      *to++= 'a';    
203
    else      
204
      *to++= 'r';
205
    *to++= '+';  
206
  }  
207
  else    
208
    *to++= 'r';
209
210
#if FILE_BINARY            /* If we have binary-files */  
211
  if (flag & FILE_BINARY)    
212
    *to++='b';
213
#endif  
214
  *to='\0';
215
} /* make_ftype */