MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_create.c
1 /* Copyright (c) 2000, 2001, 2005-2008 MySQL AB, 2009 Sun Microsystems, Inc.
2  Use is subject to license terms.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program 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
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 #include "mysys_priv.h"
18 #include <my_dir.h>
19 #include "mysys_err.h"
20 #include <errno.h>
21 #include <my_sys.h>
22 #if defined(_WIN32)
23 #include <share.h>
24 #endif
25 
26  /*
27  ** Create a new file
28  ** Arguments:
29  ** Path-name of file
30  ** Read | write on file (umask value)
31  ** Read & Write on open file
32  ** Special flags
33  */
34 
35 
36 File my_create(const char *FileName, int CreateFlags, int access_flags,
37  myf MyFlags)
38 {
39  int fd, rc;
40  DBUG_ENTER("my_create");
41  DBUG_PRINT("my",("Name: '%s' CreateFlags: %d AccessFlags: %d MyFlags: %d",
42  FileName, CreateFlags, access_flags, MyFlags));
43 #if defined(_WIN32)
44  fd= my_win_open(FileName, access_flags | O_CREAT);
45 #else
46  fd= open((char *) FileName, access_flags | O_CREAT,
47  CreateFlags ? CreateFlags : my_umask);
48 #endif
49 
50  if ((MyFlags & MY_SYNC_DIR) && (fd >=0) &&
51  my_sync_dir_by_file(FileName, MyFlags))
52  {
53  my_close(fd, MyFlags);
54  fd= -1;
55  }
56 
57  rc= my_register_filename(fd, FileName, FILE_BY_CREATE,
58  EE_CANTCREATEFILE, MyFlags);
59  /*
60  my_register_filename() may fail on some platforms even if the call to
61  *open() above succeeds. In this case, don't leave the stale file because
62  callers assume the file to not exist if my_create() fails, so they don't
63  do any cleanups.
64  */
65  if (unlikely(fd >= 0 && rc < 0))
66  {
67  int tmp= my_errno;
68  my_close(fd, MyFlags);
69  my_delete(FileName, MyFlags);
70  my_errno= tmp;
71  }
72 
73  DBUG_RETURN(rc);
74 } /* my_create */