~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/my_open.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
   
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
#define USES_TYPES
 
19
#include "mysys_priv.h"
 
20
#include "mysys_err.h"
 
21
#include <errno.h>
 
22
#if defined(MSDOS) || defined(__WIN__)
 
23
#include <share.h>
 
24
#endif
 
25
 
 
26
        /* Open a file */
 
27
 
 
28
File my_open(const char *FileName, int Flags, myf MyFlags)
 
29
                                /* Path-name of file */
 
30
                                /* Read | write .. */
 
31
                                /* Special flags */
 
32
{
 
33
  File fd;
 
34
  DBUG_ENTER("my_open");
 
35
  DBUG_PRINT("my",("Name: '%s'  Flags: %d  MyFlags: %d",
 
36
                   FileName, Flags, MyFlags));
 
37
#if defined(MSDOS) || defined(__WIN__)
 
38
  if (Flags & O_SHARE)
 
39
    fd = sopen((my_string) FileName, (Flags & ~O_SHARE) | O_BINARY, SH_DENYNO);
 
40
  else
 
41
    fd = open((my_string) FileName, Flags | O_BINARY);
 
42
#elif !defined(NO_OPEN_3)
 
43
  fd = open(FileName, Flags, 0);        /* Normal unix */
 
44
#else
 
45
  fd = open((my_string) FileName, Flags);
 
46
#endif
 
47
 
 
48
  if ((int) fd >= 0)
 
49
  {
 
50
    if ((int) fd >= MY_NFILE)
 
51
      DBUG_RETURN(fd);                          /* safeguard */
 
52
    pthread_mutex_lock(&THR_LOCK_open);
 
53
    if ((my_file_info[fd].name = (char*) my_strdup(FileName,MyFlags)))
 
54
    {
 
55
      my_file_opened++;
 
56
      my_file_info[fd].type = FILE_BY_OPEN;
 
57
      pthread_mutex_unlock(&THR_LOCK_open);
 
58
      DBUG_PRINT("exit",("fd: %d",fd));
 
59
      DBUG_RETURN(fd);
 
60
    }
 
61
    pthread_mutex_unlock(&THR_LOCK_open);
 
62
    (void) my_close(fd,MyFlags);
 
63
    my_errno=ENOMEM;
 
64
  }
 
65
  else
 
66
    my_errno=errno;
 
67
  DBUG_PRINT("error",("Got error %d on open",my_errno));
 
68
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
 
69
    my_error(EE_FILENOTFOUND, MYF(ME_BELL+ME_WAITTANG), FileName,my_errno);
 
70
  DBUG_RETURN(fd);
 
71
} /* my_open */
 
72
 
 
73
 
 
74
        /* Close a file */
 
75
 
 
76
int my_close(File fd, myf MyFlags)
 
77
{
 
78
  int err;
 
79
  DBUG_ENTER("my_close");
 
80
  DBUG_PRINT("my",("fd: %d  MyFlags: %d",fd, MyFlags));
 
81
 
 
82
  pthread_mutex_lock(&THR_LOCK_open);
 
83
  if ((err = close(fd)) != 0)
 
84
  {
 
85
    my_errno=errno;
 
86
    if (MyFlags & (MY_FAE | MY_WME))
 
87
      my_error(EE_BADCLOSE, MYF(ME_BELL+ME_WAITTANG),my_filename(fd),errno);
 
88
  }
 
89
  if ((uint) fd < MY_NFILE && my_file_info[fd].type != UNOPEN)
 
90
  {
 
91
    my_file_opened--;
 
92
    my_free(my_file_info[fd].name, MYF(0));
 
93
    my_file_info[fd].type = UNOPEN;
 
94
  }
 
95
  pthread_mutex_unlock(&THR_LOCK_open);
 
96
  DBUG_RETURN(err);
 
97
} /* my_close */