~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_open.c

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

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