~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"
685.1.3 by Monty Taylor
Turned off stdinc - and then fixed the carnage.
18
#include "my_dir.h"
19
1 by brian
clean slate
20
#include <errno.h>
612.2.6 by Monty Taylor
OpenSolaris builds.
21
#include <stdlib.h>
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
22
#include <string.h>
1 by brian
clean slate
23
24
/*
25
  Open a file
26
27
  SYNOPSIS
28
    my_open()
29
      FileName	Fully qualified file name
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
30
      Flags	Read | write
1 by brian
clean slate
31
      MyFlags	Special flags
32
33
  RETURN VALUE
34
    File descriptor
35
*/
36
37
File my_open(const char *FileName, int Flags, myf MyFlags)
38
				/* Path-name of file */
39
				/* Read | write .. */
40
				/* Special flags */
41
{
42
  File fd;
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
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
50
  return(my_register_filename(fd, FileName, FILE_BY_OPEN,
1 by brian
clean slate
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
69
  pthread_mutex_lock(&THR_LOCK_open);
70
  do
71
  {
72
    err= close(fd);
73
  } while (err == -1 && errno == EINTR);
74
75
  if (err)
76
  {
77
    my_errno=errno;
78
    if (MyFlags & (MY_FAE | MY_WME))
79
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),my_filename(fd),errno);
80
  }
895 by Brian Aker
Completion (?) of uint conversion.
81
  if ((uint32_t) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
1 by brian
clean slate
82
  {
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.
83
    free(my_file_info[fd].name);
28.1.35 by Monty Taylor
Removed all references to THREAD.
84
#if !defined(HAVE_PREAD)
1 by brian
clean slate
85
    pthread_mutex_destroy(&my_file_info[fd].mutex);
86
#endif
87
    my_file_info[fd].type = UNOPEN;
88
  }
89
  my_file_opened--;
90
  pthread_mutex_unlock(&THR_LOCK_open);
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
91
  return(err);
1 by brian
clean slate
92
} /* my_close */
93
94
95
/*
96
  Register file in my_file_info[]
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
97
1 by brian
clean slate
98
  SYNOPSIS
99
    my_register_filename()
100
    fd			   File number opened, -1 if error on open
101
    FileName		   File name
102
    type_file_type	   How file was created
103
    error_message_number   Error message number if caller got error (fd == -1)
104
    MyFlags		   Flags for my_close()
105
106
  RETURN
107
    -1   error
108
     #   Filenumber
109
110
*/
111
112
File my_register_filename(File fd, const char *FileName, enum file_type
482 by Brian Aker
Remove uint.
113
			  type_of_file, uint32_t error_message_number, myf MyFlags)
1 by brian
clean slate
114
{
115
  if ((int) fd >= 0)
116
  {
895 by Brian Aker
Completion (?) of uint conversion.
117
    if ((uint32_t) fd >= my_file_limit)
1 by brian
clean slate
118
    {
28.1.35 by Monty Taylor
Removed all references to THREAD.
119
#if !defined(HAVE_PREAD)
1 by brian
clean slate
120
      my_errno= EMFILE;
121
#else
122
      thread_safe_increment(my_file_opened,&THR_LOCK_open);
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
123
      return(fd);				/* safeguard */
1 by brian
clean slate
124
#endif
125
    }
126
    else
127
    {
128
      pthread_mutex_lock(&THR_LOCK_open);
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
129
      if ((my_file_info[fd].name = (char*) strdup(FileName)))
1 by brian
clean slate
130
      {
131
        my_file_opened++;
132
        my_file_total_opened++;
133
        my_file_info[fd].type = type_of_file;
28.1.35 by Monty Taylor
Removed all references to THREAD.
134
#if !defined(HAVE_PREAD)
1 by brian
clean slate
135
        pthread_mutex_init(&my_file_info[fd].mutex,MY_MUTEX_INIT_FAST);
136
#endif
137
        pthread_mutex_unlock(&THR_LOCK_open);
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
138
        return(fd);
1 by brian
clean slate
139
      }
140
      pthread_mutex_unlock(&THR_LOCK_open);
141
      my_errno= ENOMEM;
142
    }
143
    (void) my_close(fd, MyFlags);
144
  }
145
  else
146
    my_errno= errno;
147
148
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
149
  {
150
    if (my_errno == EMFILE)
151
      error_message_number= EE_OUT_OF_FILERESOURCES;
152
    my_error(error_message_number, MYF(ME_BELL+ME_WAITTANG),
153
             FileName, my_errno);
154
  }
51.3.18 by Jay Pipes
Phase 5 - Remove DBUG from mysys
155
  return(-1);
1 by brian
clean slate
156
}
157
158
159
#ifdef EXTRA_DEBUG
160
161
void my_print_open_files(void)
162
{
163
  if (my_file_opened | my_stream_opened)
164
  {
482 by Brian Aker
Remove uint.
165
    uint32_t i;
1 by brian
clean slate
166
    for (i= 0 ; i < my_file_limit ; i++)
167
    {
168
      if (my_file_info[i].type != UNOPEN)
169
      {
170
        fprintf(stderr, EE(EE_FILE_NOT_CLOSED), my_file_info[i].name, i);
171
        fputc('\n', stderr);
172
      }
173
    }
174
  }
175
}
176
177
#endif