~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_open.cc

  • Committer: Monty Taylor
  • Date: 2008-12-18 07:24:54 UTC
  • mto: This revision was merged to the branch mainline in revision 714.
  • Revision ID: monty@bitters-20081218072454-8pnep622damjgqli
Fixed one more my_time thing.

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
 
 
20
#include <errno.h>
 
21
#include <stdlib.h>
 
22
#include <string.h>
32
23
 
33
24
/*
34
25
  Open a file
40
31
      MyFlags   Special flags
41
32
 
42
33
  RETURN VALUE
43
 
    int descriptor
 
34
    File descriptor
44
35
*/
45
36
 
46
 
int my_open(const char *FileName, int Flags, myf MyFlags)
 
37
File my_open(const char *FileName, int Flags, myf MyFlags)
47
38
                                /* Path-name of file */
48
39
                                /* Read | write .. */
49
40
                                /* Special flags */
50
41
{
51
 
  int fd;
 
42
  File fd;
52
43
 
53
44
#if !defined(NO_OPEN_3)
54
45
  fd = open(FileName, Flags, my_umask); /* Normal unix */
56
47
  fd = open((char *) FileName, Flags);
57
48
#endif
58
49
 
59
 
  return(my_register_filename(fd, FileName, EE_FILENOTFOUND, MyFlags));
 
50
  return(my_register_filename(fd, FileName, FILE_BY_OPEN,
 
51
                                   EE_FILENOTFOUND, MyFlags));
60
52
} /* my_open */
61
53
 
62
54
 
70
62
 
71
63
*/
72
64
 
73
 
int my_close(int fd, myf MyFlags)
 
65
int my_close(File fd, myf MyFlags)
74
66
{
75
67
  int err;
76
68
 
 
69
  pthread_mutex_lock(&THR_LOCK_open);
77
70
  do
78
71
  {
79
72
    err= close(fd);
81
74
 
82
75
  if (err)
83
76
  {
84
 
    errno=errno;
 
77
    my_errno=errno;
85
78
    if (MyFlags & (MY_FAE | MY_WME))
86
 
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG), "unknown", errno);
87
 
  }
88
 
 
 
79
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),my_filename(fd),errno);
 
80
  }
 
81
  if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
 
82
  {
 
83
    free(my_file_info[fd].name);
 
84
#if !defined(HAVE_PREAD)
 
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);
89
91
  return(err);
90
92
} /* my_close */
91
93
 
92
94
 
93
95
/*
94
 
  TODO: Get rid of
 
96
  Register file in my_file_info[]
95
97
 
96
98
  SYNOPSIS
97
99
    my_register_filename()
107
109
 
108
110
*/
109
111
 
110
 
int my_register_filename(int fd, const char *FileName, uint32_t error_message_number, myf MyFlags)
 
112
File my_register_filename(File fd, const char *FileName, enum file_type
 
113
                          type_of_file, uint32_t error_message_number, myf MyFlags)
111
114
{
112
115
  if ((int) fd >= 0)
113
116
  {
114
 
    return fd;
 
117
    if ((uint) fd >= my_file_limit)
 
118
    {
 
119
#if !defined(HAVE_PREAD)
 
120
      my_errno= EMFILE;
 
121
#else
 
122
      thread_safe_increment(my_file_opened,&THR_LOCK_open);
 
123
      return(fd);                               /* safeguard */
 
124
#endif
 
125
    }
 
126
    else
 
127
    {
 
128
      pthread_mutex_lock(&THR_LOCK_open);
 
129
      if ((my_file_info[fd].name = (char*) strdup(FileName)))
 
130
      {
 
131
        my_file_opened++;
 
132
        my_file_total_opened++;
 
133
        my_file_info[fd].type = type_of_file;
 
134
#if !defined(HAVE_PREAD)
 
135
        pthread_mutex_init(&my_file_info[fd].mutex,MY_MUTEX_INIT_FAST);
 
136
#endif
 
137
        pthread_mutex_unlock(&THR_LOCK_open);
 
138
        return(fd);
 
139
      }
 
140
      pthread_mutex_unlock(&THR_LOCK_open);
 
141
      my_errno= ENOMEM;
 
142
    }
 
143
    (void) my_close(fd, MyFlags);
115
144
  }
116
145
  else
117
 
    errno= errno;
 
146
    my_errno= errno;
118
147
 
119
148
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
120
149
  {
121
 
    if (errno == EMFILE)
 
150
    if (my_errno == EMFILE)
122
151
      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 */
 
152
    my_error(error_message_number, MYF(ME_BELL+ME_WAITTANG),
 
153
             FileName, my_errno);
 
154
  }
 
155
  return(-1);
 
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
  {
 
165
    uint32_t i;
 
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