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 int file= -1; 00065 00066 int org_file; 00067 string prefix_str; 00068 00069 prefix_str= prefix ? prefix : "tmp."; 00070 prefix_str.append("XXXXXX"); 00071 00072 if (!dir && ! (dir =getenv("TMPDIR"))) 00073 dir= P_tmpdir; 00074 if (strlen(dir)+prefix_str.length() > FN_REFLEN-2) 00075 { 00076 errno= ENAMETOOLONG; 00077 return(file); 00078 } 00079 strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str()); 00080 org_file=mkstemp(to); 00081 /* TODO: This was old behavior, but really don't we want to 00082 * unlink files immediately under all circumstances? 00083 * if (mode & O_TEMPORARY) 00084 (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); 00085 */ 00086 file=my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags); 00087 00088 /* If we didn't manage to register the name, remove the temp file */ 00089 if (org_file >= 0 && file < 0) 00090 { 00091 int tmp=errno; 00092 close(org_file); 00093 (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); 00094 errno=tmp; 00095 } 00096 00097 return(file); 00098 } 00099 00100 } /* namespace internal */ 00101 } /* namespace drizzled */