~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
41
#if !defined(NO_OPEN_3)
42
  fd = open(FileName, Flags, my_umask);	/* Normal unix */
43
#else
44
  fd = open((char *) FileName, Flags);
45
#endif
46
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
47
  return(my_register_filename(fd, FileName, FILE_BY_OPEN,
1 by brian
clean slate
48
				   EE_FILENOTFOUND, MyFlags));
49
} /* my_open */
50
51
52
/*
53
  Close a file
54
55
  SYNOPSIS
56
    my_close()
57
      fd	File sescriptor
58
      myf	Special Flags
59
60
*/
61
62
int my_close(File fd, myf MyFlags)
63
{
64
  int err;
65
66
  pthread_mutex_lock(&THR_LOCK_open);
67
  do
68
  {
69
    err= close(fd);
70
  } while (err == -1 && errno == EINTR);
71
72
  if (err)
73
  {
74
    my_errno=errno;
75
    if (MyFlags & (MY_FAE | MY_WME))
76
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),my_filename(fd),errno);
77
  }
78
  if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
79
  {
80
    my_free(my_file_info[fd].name, MYF(0));
28.1.35 by Monty Taylor
Removed all references to THREAD.
81
#if !defined(HAVE_PREAD)
1 by brian
clean slate
82
    pthread_mutex_destroy(&my_file_info[fd].mutex);
83
#endif
84
    my_file_info[fd].type = UNOPEN;
85
  }
86
  my_file_opened--;
87
  pthread_mutex_unlock(&THR_LOCK_open);
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
88
  return(err);
1 by brian
clean slate
89
} /* my_close */
90
91
92
/*
93
  Register file in my_file_info[]
94
   
95
  SYNOPSIS
96
    my_register_filename()
97
    fd			   File number opened, -1 if error on open
98
    FileName		   File name
99
    type_file_type	   How file was created
100
    error_message_number   Error message number if caller got error (fd == -1)
101
    MyFlags		   Flags for my_close()
102
103
  RETURN
104
    -1   error
105
     #   Filenumber
106
107
*/
108
109
File my_register_filename(File fd, const char *FileName, enum file_type
110
			  type_of_file, uint error_message_number, myf MyFlags)
111
{
112
  if ((int) fd >= 0)
113
  {
114
    if ((uint) fd >= my_file_limit)
115
    {
28.1.35 by Monty Taylor
Removed all references to THREAD.
116
#if !defined(HAVE_PREAD)
1 by brian
clean slate
117
      my_errno= EMFILE;
118
#else
119
      thread_safe_increment(my_file_opened,&THR_LOCK_open);
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
120
      return(fd);				/* safeguard */
1 by brian
clean slate
121
#endif
122
    }
123
    else
124
    {
125
      pthread_mutex_lock(&THR_LOCK_open);
126
      if ((my_file_info[fd].name = (char*) my_strdup(FileName,MyFlags)))
127
      {
128
        my_file_opened++;
129
        my_file_total_opened++;
130
        my_file_info[fd].type = type_of_file;
28.1.35 by Monty Taylor
Removed all references to THREAD.
131
#if !defined(HAVE_PREAD)
1 by brian
clean slate
132
        pthread_mutex_init(&my_file_info[fd].mutex,MY_MUTEX_INIT_FAST);
133
#endif
134
        pthread_mutex_unlock(&THR_LOCK_open);
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
135
        return(fd);
1 by brian
clean slate
136
      }
137
      pthread_mutex_unlock(&THR_LOCK_open);
138
      my_errno= ENOMEM;
139
    }
140
    (void) my_close(fd, MyFlags);
141
  }
142
  else
143
    my_errno= errno;
144
145
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
146
  {
147
    if (my_errno == EMFILE)
148
      error_message_number= EE_OUT_OF_FILERESOURCES;
149
    my_error(error_message_number, MYF(ME_BELL+ME_WAITTANG),
150
             FileName, my_errno);
151
  }
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
152
  return(-1);
1 by brian
clean slate
153
}
154
155
156
#ifdef EXTRA_DEBUG
157
158
void my_print_open_files(void)
159
{
160
  if (my_file_opened | my_stream_opened)
161
  {
162
    uint i;
163
    for (i= 0 ; i < my_file_limit ; i++)
164
    {
165
      if (my_file_info[i].type != UNOPEN)
166
      {
167
        fprintf(stderr, EE(EE_FILE_NOT_CLOSED), my_file_info[i].name, i);
168
        fputc('\n', stderr);
169
      }
170
    }
171
  }
172
}
173
174
#endif