~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 "mysys_err.h"
18
#include <my_dir.h>
19
#include <errno.h>
20
21
/*
22
  Open a file
23
24
  SYNOPSIS
25
    my_open()
26
      FileName	Fully qualified file name
27
      Flags	Read | write 
28
      MyFlags	Special flags
29
30
  RETURN VALUE
31
    File descriptor
32
*/
33
34
File my_open(const char *FileName, int Flags, myf MyFlags)
35
				/* Path-name of file */
36
				/* Read | write .. */
37
				/* Special flags */
38
{
39
  File fd;
40
  DBUG_ENTER("my_open");
41
  DBUG_PRINT("my",("Name: '%s'  Flags: %d  MyFlags: %d",
42
		   FileName, Flags, MyFlags));
43
44
#if !defined(NO_OPEN_3)
45
  fd = open(FileName, Flags, my_umask);	/* Normal unix */
46
#else
47
  fd = open((char *) FileName, Flags);
48
#endif
49
50
  DBUG_RETURN(my_register_filename(fd, FileName, FILE_BY_OPEN,
51
				   EE_FILENOTFOUND, MyFlags));
52
} /* my_open */
53
54
55
/*
56
  Close a file
57
58
  SYNOPSIS
59
    my_close()
60
      fd	File sescriptor
61
      myf	Special Flags
62
63
*/
64
65
int my_close(File fd, myf MyFlags)
66
{
67
  int err;
68
  DBUG_ENTER("my_close");
69
  DBUG_PRINT("my",("fd: %d  MyFlags: %d",fd, MyFlags));
70
71
  pthread_mutex_lock(&THR_LOCK_open);
72
  do
73
  {
74
    err= close(fd);
75
  } while (err == -1 && errno == EINTR);
76
77
  if (err)
78
  {
79
    DBUG_PRINT("error",("Got error %d on close",err));
80
    my_errno=errno;
81
    if (MyFlags & (MY_FAE | MY_WME))
82
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),my_filename(fd),errno);
83
  }
84
  if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
85
  {
86
    my_free(my_file_info[fd].name, MYF(0));
87
#if defined(THREAD) && !defined(HAVE_PREAD)
88
    pthread_mutex_destroy(&my_file_info[fd].mutex);
89
#endif
90
    my_file_info[fd].type = UNOPEN;
91
  }
92
  my_file_opened--;
93
  pthread_mutex_unlock(&THR_LOCK_open);
94
  DBUG_RETURN(err);
95
} /* my_close */
96
97
98
/*
99
  Register file in my_file_info[]
100
   
101
  SYNOPSIS
102
    my_register_filename()
103
    fd			   File number opened, -1 if error on open
104
    FileName		   File name
105
    type_file_type	   How file was created
106
    error_message_number   Error message number if caller got error (fd == -1)
107
    MyFlags		   Flags for my_close()
108
109
  RETURN
110
    -1   error
111
     #   Filenumber
112
113
*/
114
115
File my_register_filename(File fd, const char *FileName, enum file_type
116
			  type_of_file, uint error_message_number, myf MyFlags)
117
{
118
  DBUG_ENTER("my_register_filename");
119
  if ((int) fd >= 0)
120
  {
121
    if ((uint) fd >= my_file_limit)
122
    {
123
#if defined(THREAD) && !defined(HAVE_PREAD)
124
      my_errno= EMFILE;
125
#else
126
      thread_safe_increment(my_file_opened,&THR_LOCK_open);
127
      DBUG_RETURN(fd);				/* safeguard */
128
#endif
129
    }
130
    else
131
    {
132
      pthread_mutex_lock(&THR_LOCK_open);
133
      if ((my_file_info[fd].name = (char*) my_strdup(FileName,MyFlags)))
134
      {
135
        my_file_opened++;
136
        my_file_total_opened++;
137
        my_file_info[fd].type = type_of_file;
138
#if defined(THREAD) && !defined(HAVE_PREAD)
139
        pthread_mutex_init(&my_file_info[fd].mutex,MY_MUTEX_INIT_FAST);
140
#endif
141
        pthread_mutex_unlock(&THR_LOCK_open);
142
        DBUG_PRINT("exit",("fd: %d",fd));
143
        DBUG_RETURN(fd);
144
      }
145
      pthread_mutex_unlock(&THR_LOCK_open);
146
      my_errno= ENOMEM;
147
    }
148
    (void) my_close(fd, MyFlags);
149
  }
150
  else
151
    my_errno= errno;
152
153
  DBUG_PRINT("error",("Got error %d on open", my_errno));
154
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
155
  {
156
    if (my_errno == EMFILE)
157
      error_message_number= EE_OUT_OF_FILERESOURCES;
158
    DBUG_PRINT("error",("print err: %d",error_message_number));
159
    my_error(error_message_number, MYF(ME_BELL+ME_WAITTANG),
160
             FileName, my_errno);
161
  }
162
  DBUG_RETURN(-1);
163
}
164
165
166
#ifdef EXTRA_DEBUG
167
168
void my_print_open_files(void)
169
{
170
  if (my_file_opened | my_stream_opened)
171
  {
172
    uint i;
173
    for (i= 0 ; i < my_file_limit ; i++)
174
    {
175
      if (my_file_info[i].type != UNOPEN)
176
      {
177
        fprintf(stderr, EE(EE_FILE_NOT_CLOSED), my_file_info[i].name, i);
178
        fputc('\n', stderr);
179
      }
180
    }
181
  }
182
}
183
184
#endif