~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_open.cc

  • Committer: Brian Aker
  • Date: 2008-12-15 19:32:58 UTC
  • mfrom: (677.1.2 devel)
  • Revision ID: brian@tangent.org-20081215193258-fsvc1sh9h7a9sb1t
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include "config.h"
17
 
 
18
 
#include "drizzled/internal/my_sys.h"
19
 
#include "drizzled/error.h"
20
 
 
21
 
#include <fcntl.h>
22
 
 
23
 
#include <cerrno>
24
 
#include <cstdlib>
25
 
#include <cstring>
26
 
 
27
 
 
28
 
namespace drizzled
29
 
{
30
 
namespace internal
31
 
{
 
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
#include <stdlib.h>
 
21
#include <string.h>
32
22
 
33
23
/*
34
24
  Open a file
40
30
      MyFlags   Special flags
41
31
 
42
32
  RETURN VALUE
43
 
    int descriptor
 
33
    File descriptor
44
34
*/
45
35
 
46
 
int my_open(const char *FileName, int Flags, myf MyFlags)
 
36
File my_open(const char *FileName, int Flags, myf MyFlags)
47
37
                                /* Path-name of file */
48
38
                                /* Read | write .. */
49
39
                                /* Special flags */
50
40
{
51
 
  int fd;
 
41
  File fd;
52
42
 
53
43
#if !defined(NO_OPEN_3)
54
44
  fd = open(FileName, Flags, my_umask); /* Normal unix */
56
46
  fd = open((char *) FileName, Flags);
57
47
#endif
58
48
 
59
 
  return(my_register_filename(fd, FileName, EE_FILENOTFOUND, MyFlags));
 
49
  return(my_register_filename(fd, FileName, FILE_BY_OPEN,
 
50
                                   EE_FILENOTFOUND, MyFlags));
60
51
} /* my_open */
61
52
 
62
53
 
70
61
 
71
62
*/
72
63
 
73
 
int my_close(int fd, myf MyFlags)
 
64
int my_close(File fd, myf MyFlags)
74
65
{
75
66
  int err;
76
67
 
 
68
  pthread_mutex_lock(&THR_LOCK_open);
77
69
  do
78
70
  {
79
71
    err= close(fd);
81
73
 
82
74
  if (err)
83
75
  {
84
 
    errno=errno;
 
76
    my_errno=errno;
85
77
    if (MyFlags & (MY_FAE | MY_WME))
86
 
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
87
 
  }
88
 
 
 
78
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),my_filename(fd),errno);
 
79
  }
 
80
  if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
 
81
  {
 
82
    free(my_file_info[fd].name);
 
83
#if !defined(HAVE_PREAD)
 
84
    pthread_mutex_destroy(&my_file_info[fd].mutex);
 
85
#endif
 
86
    my_file_info[fd].type = UNOPEN;
 
87
  }
 
88
  my_file_opened--;
 
89
  pthread_mutex_unlock(&THR_LOCK_open);
89
90
  return(err);
90
91
} /* my_close */
91
92
 
92
93
 
93
94
/*
94
 
  TODO: Get rid of
 
95
  Register file in my_file_info[]
95
96
 
96
97
  SYNOPSIS
97
98
    my_register_filename()
107
108
 
108
109
*/
109
110
 
110
 
int my_register_filename(int fd, const char *FileName, uint32_t error_message_number, myf MyFlags)
 
111
File my_register_filename(File fd, const char *FileName, enum file_type
 
112
                          type_of_file, uint32_t error_message_number, myf MyFlags)
111
113
{
112
114
  if ((int) fd >= 0)
113
115
  {
114
 
    return fd;
 
116
    if ((uint) fd >= my_file_limit)
 
117
    {
 
118
#if !defined(HAVE_PREAD)
 
119
      my_errno= EMFILE;
 
120
#else
 
121
      thread_safe_increment(my_file_opened,&THR_LOCK_open);
 
122
      return(fd);                               /* safeguard */
 
123
#endif
 
124
    }
 
125
    else
 
126
    {
 
127
      pthread_mutex_lock(&THR_LOCK_open);
 
128
      if ((my_file_info[fd].name = (char*) strdup(FileName)))
 
129
      {
 
130
        my_file_opened++;
 
131
        my_file_total_opened++;
 
132
        my_file_info[fd].type = type_of_file;
 
133
#if !defined(HAVE_PREAD)
 
134
        pthread_mutex_init(&my_file_info[fd].mutex,MY_MUTEX_INIT_FAST);
 
135
#endif
 
136
        pthread_mutex_unlock(&THR_LOCK_open);
 
137
        return(fd);
 
138
      }
 
139
      pthread_mutex_unlock(&THR_LOCK_open);
 
140
      my_errno= ENOMEM;
 
141
    }
 
142
    (void) my_close(fd, MyFlags);
115
143
  }
116
144
  else
117
 
    errno= errno;
 
145
    my_errno= errno;
118
146
 
119
147
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
120
148
  {
121
 
    if (errno == EMFILE)
 
149
    if (my_errno == EMFILE)
122
150
      error_message_number= EE_OUT_OF_FILERESOURCES;
123
 
    my_error(static_cast<drizzled::error_t>(error_message_number), MYF(ME_BELL+ME_WAITTANG),
124
 
             FileName, errno);
125
 
  }
126
 
  return -1;
127
 
}
128
 
 
129
 
} /* namespace internal */
130
 
} /* namespace drizzled */
 
151
    my_error(error_message_number, MYF(ME_BELL+ME_WAITTANG),
 
152
             FileName, my_errno);
 
153
  }
 
154
  return(-1);
 
155
}
 
156
 
 
157
 
 
158
#ifdef EXTRA_DEBUG
 
159
 
 
160
void my_print_open_files(void)
 
161
{
 
162
  if (my_file_opened | my_stream_opened)
 
163
  {
 
164
    uint32_t i;
 
165
    for (i= 0 ; i < my_file_limit ; i++)
 
166
    {
 
167
      if (my_file_info[i].type != UNOPEN)
 
168
      {
 
169
        fprintf(stderr, EE(EE_FILE_NOT_CLOSED), my_file_info[i].name, i);
 
170
        fputc('\n', stderr);
 
171
      }
 
172
    }
 
173
  }
 
174
}
 
175
 
 
176
#endif