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