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 |
/*
|
|
42 |
if we are not creating, then we need to use my_access to make sure
|
|
43 |
the file exists since Windows doesn't handle files like "com1.sym"
|
|
44 |
very well
|
|
45 |
*/
|
|
46 |
{
|
|
47 |
make_ftype(type,flags); |
|
48 |
fd = fopen(filename, type); |
|
49 |
}
|
|
50 |
||
51 |
if (fd != 0) |
|
52 |
{
|
|
53 |
/*
|
|
54 |
The test works if MY_NFILE < 128. The problem is that fileno() is char
|
|
55 |
on some OS (SUNOS). Actually the filename save isn't that important
|
|
56 |
so we can ignore if this doesn't work.
|
|
57 |
*/
|
|
58 |
if ((uint) fileno(fd) >= my_file_limit) |
|
59 |
{
|
|
60 |
thread_safe_increment(my_stream_opened,&THR_LOCK_open); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
61 |
return(fd); /* safeguard */ |
1
by brian
clean slate |
62 |
}
|
63 |
pthread_mutex_lock(&THR_LOCK_open); |
|
64 |
if ((my_file_info[fileno(fd)].name = (char*) |
|
65 |
my_strdup(filename,MyFlags))) |
|
66 |
{
|
|
67 |
my_stream_opened++; |
|
68 |
my_file_total_opened++; |
|
69 |
my_file_info[fileno(fd)].type = STREAM_BY_FOPEN; |
|
70 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
71 |
return(fd); |
1
by brian
clean slate |
72 |
}
|
73 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
74 |
(void) my_fclose(fd,MyFlags); |
|
75 |
my_errno=ENOMEM; |
|
76 |
}
|
|
77 |
else
|
|
78 |
my_errno=errno; |
|
79 |
if (MyFlags & (MY_FFNF | MY_FAE | MY_WME)) |
|
80 |
my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND : |
|
81 |
EE_CANTCREATEFILE, |
|
82 |
MYF(ME_BELL+ME_WAITTANG), filename, my_errno); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
83 |
return((FILE*) 0); |
1
by brian
clean slate |
84 |
} /* my_fopen */ |
85 |
||
86 |
||
87 |
/* Close a stream */
|
|
88 |
||
89 |
int my_fclose(FILE *fd, myf MyFlags) |
|
90 |
{
|
|
91 |
int err,file; |
|
92 |
||
93 |
pthread_mutex_lock(&THR_LOCK_open); |
|
94 |
file=fileno(fd); |
|
95 |
if ((err = fclose(fd)) < 0) |
|
96 |
{
|
|
97 |
my_errno=errno; |
|
98 |
if (MyFlags & (MY_FAE | MY_WME)) |
|
99 |
my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG), |
|
100 |
my_filename(file),errno); |
|
101 |
}
|
|
102 |
else
|
|
103 |
my_stream_opened--; |
|
104 |
if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN) |
|
105 |
{
|
|
106 |
my_file_info[file].type = UNOPEN; |
|
107 |
my_free(my_file_info[file].name, MYF(MY_ALLOW_ZERO_PTR)); |
|
108 |
}
|
|
109 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
110 |
return(err); |
1
by brian
clean slate |
111 |
} /* my_fclose */ |
112 |
||
113 |
||
114 |
/* Make a stream out of a file handle */
|
|
115 |
/* Name may be 0 */
|
|
116 |
||
117 |
FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags) |
|
118 |
{
|
|
119 |
FILE *fd; |
|
120 |
char type[5]; |
|
121 |
||
122 |
make_ftype(type,Flags); |
|
123 |
if ((fd = fdopen(Filedes, type)) == 0) |
|
124 |
{
|
|
125 |
my_errno=errno; |
|
126 |
if (MyFlags & (MY_FAE | MY_WME)) |
|
127 |
my_error(EE_CANT_OPEN_STREAM, MYF(ME_BELL+ME_WAITTANG),errno); |
|
128 |
}
|
|
129 |
else
|
|
130 |
{
|
|
131 |
pthread_mutex_lock(&THR_LOCK_open); |
|
132 |
my_stream_opened++; |
|
133 |
if ((uint) Filedes < (uint) my_file_limit) |
|
134 |
{
|
|
135 |
if (my_file_info[Filedes].type != UNOPEN) |
|
136 |
{
|
|
137 |
my_file_opened--; /* File is opened with my_open ! */ |
|
138 |
}
|
|
139 |
else
|
|
140 |
{
|
|
141 |
my_file_info[Filedes].name= my_strdup(name,MyFlags); |
|
142 |
}
|
|
143 |
my_file_info[Filedes].type = STREAM_BY_FDOPEN; |
|
144 |
}
|
|
145 |
pthread_mutex_unlock(&THR_LOCK_open); |
|
146 |
}
|
|
147 |
||
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
148 |
return(fd); |
1
by brian
clean slate |
149 |
} /* my_fdopen */ |
150 |
||
151 |
||
152 |
/*
|
|
153 |
Make a fopen() typestring from a open() type bitmap
|
|
154 |
||
155 |
SYNOPSIS
|
|
156 |
make_ftype()
|
|
157 |
to String for fopen() is stored here
|
|
158 |
flag Flag used by open()
|
|
159 |
||
160 |
IMPLEMENTATION
|
|
161 |
This routine attempts to find the best possible match
|
|
162 |
between a numeric option and a string option that could be
|
|
163 |
fed to fopen. There is not a 1 to 1 mapping between the two.
|
|
164 |
|
|
165 |
NOTE
|
|
166 |
On Unix, O_RDONLY is usually 0
|
|
167 |
||
168 |
MAPPING
|
|
169 |
r == O_RDONLY
|
|
170 |
w == O_WRONLY|O_TRUNC|O_CREAT
|
|
171 |
a == O_WRONLY|O_APPEND|O_CREAT
|
|
172 |
r+ == O_RDWR
|
|
173 |
w+ == O_RDWR|O_TRUNC|O_CREAT
|
|
174 |
a+ == O_RDWR|O_APPEND|O_CREAT
|
|
175 |
*/
|
|
176 |
||
177 |
static void make_ftype(register char * to, register int flag) |
|
178 |
{
|
|
179 |
/* check some possible invalid combinations */
|
|
51.3.19
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
180 |
assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND)); |
181 |
assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR)); |
|
1
by brian
clean slate |
182 |
|
183 |
if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY) |
|
184 |
*to++= (flag & O_APPEND) ? 'a' : 'w'; |
|
185 |
else if (flag & O_RDWR) |
|
186 |
{
|
|
187 |
/* Add '+' after theese */
|
|
188 |
if (flag & (O_TRUNC | O_CREAT)) |
|
189 |
*to++= 'w'; |
|
190 |
else if (flag & O_APPEND) |
|
191 |
*to++= 'a'; |
|
192 |
else
|
|
193 |
*to++= 'r'; |
|
194 |
*to++= '+'; |
|
195 |
}
|
|
196 |
else
|
|
197 |
*to++= 'r'; |
|
198 |
||
199 |
#if FILE_BINARY /* If we have binary-files */ |
|
200 |
if (flag & FILE_BINARY) |
|
201 |
*to++='b'; |
|
202 |
#endif
|
|
203 |
*to='\0'; |
|
204 |
} /* make_ftype */ |