~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_fopen.cc

Fix merge issues with 1.0 CC fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "mysys_err.h"
19
 
 
20
 
#include <stdio.h>
21
 
#include <stdlib.h>
22
 
#include <errno.h>
23
 
 
24
 
static void make_ftype(char * to,int flag);
25
 
 
26
 
/*
27
 
  Open a file as stream
28
 
 
29
 
  SYNOPSIS
30
 
    my_fopen()
31
 
    FileName    Path-name of file
32
 
    Flags       Read | write | append | trunc (like for open())
33
 
    MyFlags     Flags for handling errors
34
 
 
35
 
  RETURN
36
 
    0   Error
37
 
    #   File handler
38
 
*/
39
 
 
40
 
FILE *my_fopen(const char *filename, int flags, myf MyFlags)
41
 
{
42
 
  FILE *fd;
43
 
  char type[5];
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
 
      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
 
      return(fd);
75
 
    }
76
 
    pthread_mutex_unlock(&THR_LOCK_open);
77
 
    (void) my_fclose(fd,MyFlags);
78
 
    my_errno=ENOMEM;
79
 
  }
80
 
  else
81
 
    my_errno=errno;
82
 
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
83
 
    my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
84
 
             EE_CANTCREATEFILE,
85
 
             MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
86
 
  return((FILE*) 0);
87
 
} /* my_fopen */
88
 
 
89
 
 
90
 
        /* Close a stream */
91
 
 
92
 
int my_fclose(FILE *fd, myf MyFlags)
93
 
{
94
 
  int err,file;
95
 
 
96
 
  pthread_mutex_lock(&THR_LOCK_open);
97
 
  file=fileno(fd);
98
 
  if ((err = fclose(fd)) < 0)
99
 
  {
100
 
    my_errno=errno;
101
 
    if (MyFlags & (MY_FAE | MY_WME))
102
 
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),
103
 
               my_filename(file),errno);
104
 
  }
105
 
  else
106
 
    my_stream_opened--;
107
 
  if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
108
 
  {
109
 
    my_file_info[file].type = UNOPEN;
110
 
    free(my_file_info[file].name);
111
 
  }
112
 
  pthread_mutex_unlock(&THR_LOCK_open);
113
 
  return(err);
114
 
} /* my_fclose */
115
 
 
116
 
 
117
 
 
118
 
/*   
119
 
  Make a fopen() typestring from a open() type bitmap
120
 
 
121
 
  SYNOPSIS
122
 
    make_ftype()
123
 
    to          String for fopen() is stored here
124
 
    flag        Flag used by open()
125
 
 
126
 
  IMPLEMENTATION
127
 
    This routine attempts to find the best possible match 
128
 
    between  a numeric option and a string option that could be 
129
 
    fed to fopen.  There is not a 1 to 1 mapping between the two.  
130
 
  
131
 
  NOTE
132
 
    On Unix, O_RDONLY is usually 0
133
 
 
134
 
  MAPPING
135
 
    r  == O_RDONLY   
136
 
    w  == O_WRONLY|O_TRUNC|O_CREAT  
137
 
    a  == O_WRONLY|O_APPEND|O_CREAT  
138
 
    r+ == O_RDWR  
139
 
    w+ == O_RDWR|O_TRUNC|O_CREAT  
140
 
    a+ == O_RDWR|O_APPEND|O_CREAT
141
 
*/
142
 
 
143
 
static void make_ftype(register char * to, register int flag)
144
 
{
145
 
  /* check some possible invalid combinations */  
146
 
  assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
147
 
  assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
148
 
 
149
 
  if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)    
150
 
    *to++= (flag & O_APPEND) ? 'a' : 'w';  
151
 
  else if (flag & O_RDWR)          
152
 
  {
153
 
    /* Add '+' after theese */    
154
 
    if (flag & (O_TRUNC | O_CREAT))      
155
 
      *to++= 'w';    
156
 
    else if (flag & O_APPEND)      
157
 
      *to++= 'a';    
158
 
    else      
159
 
      *to++= 'r';
160
 
    *to++= '+';  
161
 
  }  
162
 
  else    
163
 
    *to++= 'r';
164
 
 
165
 
  *to='\0';
166
 
} /* make_ftype */