00001 /* Copyright (C) 2000 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; version 2 of the License. 00006 00007 This program is distributed in the hope that it will be useful, 00008 but WITHOUT ANY WARRANTY; without even the implied warranty of 00009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00010 GNU General Public License for more details. 00011 00012 You should have received a copy of the GNU General Public License 00013 along with this program; if not, write to the Free Software 00014 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 00015 00016 #include <config.h> 00017 00018 #include <drizzled/internal/my_sys.h> 00019 #include <drizzled/internal/m_string.h> 00020 #include "my_static.h" 00021 #include <drizzled/error.h> 00022 #include <stdio.h> 00023 #include <errno.h> 00024 #include <string> 00025 #ifdef HAVE_PATHS_H 00026 #include <paths.h> 00027 #endif 00028 00029 using namespace std; 00030 namespace drizzled 00031 { 00032 namespace internal 00033 { 00034 00035 /* 00036 @brief 00037 Create a temporary file with unique name in a given directory 00038 00039 @details 00040 create_temp_file 00041 to pointer to buffer where temporary filename will be stored 00042 dir directory where to create the file 00043 prefix prefix the filename with this 00044 MyFlags Magic flags 00045 00046 @return 00047 File descriptor of opened file if success 00048 -1 and sets errno if fails. 00049 00050 @note 00051 The behaviour of this function differs a lot between 00052 implementation, it's main use is to generate a file with 00053 a name that does not already exist. 00054 00055 The implementation using mkstemp should be considered the 00056 reference implementation when adding a new or modifying an 00057 existing one 00058 00059 */ 00060 00061 int create_temp_file(char *to, const char *dir, const char *prefix, 00062 myf MyFlags) 00063 { 00064 string prefix_str= prefix ? prefix : "tmp."; 00065 prefix_str.append("XXXXXX"); 00066 00067 if (!dir && ! (dir =getenv("TMPDIR"))) 00068 dir= P_tmpdir; 00069 if (strlen(dir)+prefix_str.length() > FN_REFLEN-2) 00070 { 00071 errno= ENAMETOOLONG; 00072 return -1; 00073 } 00074 strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str()); 00075 int org_file= mkstemp(to); 00076 /* TODO: This was old behavior, but really don't we want to 00077 * unlink files immediately under all circumstances? 00078 * if (mode & O_TEMPORARY) 00079 (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); 00080 */ 00081 int file= my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags); 00082 00083 /* If we didn't manage to register the name, remove the temp file */ 00084 if (org_file >= 0 && file < 0) 00085 { 00086 int tmp= errno; 00087 close(org_file); 00088 (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); 00089 errno= tmp; 00090 } 00091 00092 return file; 00093 } 00094 00095 } /* namespace internal */ 00096 } /* namespace drizzled */