1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir /*****************************************************************/ 29*cdf0e10cSrcweir /* Includes */ 30*cdf0e10cSrcweir /*****************************************************************/ 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <stdio.h> 33*cdf0e10cSrcweir #include <stdlib.h> 34*cdf0e10cSrcweir #include <sys/types.h> 35*cdf0e10cSrcweir #include <sys/stat.h> 36*cdf0e10cSrcweir #include <sys/time.h> 37*cdf0e10cSrcweir #include "system.h" 38*cdf0e10cSrcweir #include <osl/file.h> 39*cdf0e10cSrcweir #include <osl/thread.h> 40*cdf0e10cSrcweir #include <rtl/ustrbuf.h> 41*cdf0e10cSrcweir #include <osl/diagnose.h> 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #ifndef _FILE_URL_H_ 44*cdf0e10cSrcweir #include "file_url.h" 45*cdf0e10cSrcweir #endif 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir /*****************************************************************/ 48*cdf0e10cSrcweir /* osl_getTempFirURL */ 49*cdf0e10cSrcweir /*****************************************************************/ 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir oslFileError SAL_CALL osl_getTempDirURL( rtl_uString** pustrTempDir ) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir #ifdef MACOSX 54*cdf0e10cSrcweir const char *pValue = getenv( "TMPDIR" ); 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /* If TMPDIR environment variable is not set, use "/tmp" instead 57*cdf0e10cSrcweir of P_tmpdir because its value is "/var/tmp" and it is not 58*cdf0e10cSrcweir deleted on system start up */ 59*cdf0e10cSrcweir if ( !pValue ) 60*cdf0e10cSrcweir pValue = "/tmp"; 61*cdf0e10cSrcweir #else 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir const char *pValue = getenv( "TEMP" ); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir if ( !pValue ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir pValue = getenv( "TMP" ); 68*cdf0e10cSrcweir #if defined(SOLARIS) || defined (LINUX) || defined (FREEBSD) 69*cdf0e10cSrcweir if ( !pValue ) 70*cdf0e10cSrcweir pValue = P_tmpdir; 71*cdf0e10cSrcweir #endif 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir #endif /* MACOSX */ 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir if ( pValue ) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir oslFileError error; 78*cdf0e10cSrcweir rtl_uString *ustrTempPath = NULL; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS ); 81*cdf0e10cSrcweir OSL_ASSERT(ustrTempPath != NULL); 82*cdf0e10cSrcweir error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir ); 83*cdf0e10cSrcweir rtl_uString_release( ustrTempPath ); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir return error; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir else 88*cdf0e10cSrcweir return osl_File_E_NOENT; 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir /****************************************************************** 92*cdf0e10cSrcweir * Generates a random unique file name. We're using the scheme 93*cdf0e10cSrcweir * from the standard c-lib function mkstemp to generate a more 94*cdf0e10cSrcweir * or less random unique file name 95*cdf0e10cSrcweir * 96*cdf0e10cSrcweir * @param rand_name 97*cdf0e10cSrcweir * receives the random name 98*cdf0e10cSrcweir ******************************************************************/ 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir static const char LETTERS[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 101*cdf0e10cSrcweir static const int COUNT_OF_LETTERS = sizeof(LETTERS)/sizeof(LETTERS[0]) - 1; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir #define RAND_NAME_LENGTH 6 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir static void osl_gen_random_name_impl_(rtl_uString** rand_name) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir static uint64_t value; 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir char buffer[RAND_NAME_LENGTH]; 110*cdf0e10cSrcweir struct timeval tv; 111*cdf0e10cSrcweir uint64_t v; 112*cdf0e10cSrcweir int i; 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir gettimeofday(&tv, NULL); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir value += ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid(); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir v = value; 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir for (i = 0; i < RAND_NAME_LENGTH; i++) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir buffer[i] = LETTERS[v % COUNT_OF_LETTERS]; 123*cdf0e10cSrcweir v /= COUNT_OF_LETTERS; 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir rtl_string2UString( 127*cdf0e10cSrcweir rand_name, 128*cdf0e10cSrcweir buffer, 129*cdf0e10cSrcweir RAND_NAME_LENGTH, 130*cdf0e10cSrcweir RTL_TEXTENCODING_ASCII_US, 131*cdf0e10cSrcweir OSTRING_TO_OUSTRING_CVTFLAGS); 132*cdf0e10cSrcweir OSL_ASSERT(*rand_name != NULL); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /***************************************************************** 136*cdf0e10cSrcweir * Helper function 137*cdf0e10cSrcweir * Either use the directory provided or the result of 138*cdf0e10cSrcweir * osl_getTempDirUrl and return it as system path and file url 139*cdf0e10cSrcweir ****************************************************************/ 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir static oslFileError osl_setup_base_directory_impl_( 142*cdf0e10cSrcweir rtl_uString* pustrDirectoryURL, 143*cdf0e10cSrcweir rtl_uString** ppustr_base_dir) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir rtl_uString* dir_url = 0; 146*cdf0e10cSrcweir rtl_uString* dir = 0; 147*cdf0e10cSrcweir oslFileError error = osl_File_E_None; 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir if (pustrDirectoryURL) 150*cdf0e10cSrcweir rtl_uString_assign(&dir_url, pustrDirectoryURL); 151*cdf0e10cSrcweir else 152*cdf0e10cSrcweir error = osl_getTempDirURL(&dir_url); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir if (osl_File_E_None == error) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir error = osl_getSystemPathFromFileURL_Ex(dir_url, &dir, FURL_DENY_RELATIVE); 157*cdf0e10cSrcweir rtl_uString_release(dir_url); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir if (osl_File_E_None == error) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir rtl_uString_assign(ppustr_base_dir, dir); 163*cdf0e10cSrcweir rtl_uString_release(dir); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir return error; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir /***************************************************************** 170*cdf0e10cSrcweir * osl_setup_createTempFile_impl 171*cdf0e10cSrcweir * validate input parameter, setup variables 172*cdf0e10cSrcweir ****************************************************************/ 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir static oslFileError osl_setup_createTempFile_impl_( 175*cdf0e10cSrcweir rtl_uString* pustrDirectoryURL, 176*cdf0e10cSrcweir oslFileHandle* pHandle, 177*cdf0e10cSrcweir rtl_uString** ppustrTempFileURL, 178*cdf0e10cSrcweir rtl_uString** ppustr_base_dir, 179*cdf0e10cSrcweir sal_Bool* b_delete_on_close) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir oslFileError osl_error; 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir OSL_PRECOND(((0 != pHandle) || (0 != ppustrTempFileURL)), "Invalid parameter!"); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir if ((0 == pHandle) && (0 == ppustrTempFileURL)) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir osl_error = osl_File_E_INVAL; 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir else 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir osl_error = osl_setup_base_directory_impl_( 192*cdf0e10cSrcweir pustrDirectoryURL, ppustr_base_dir); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir *b_delete_on_close = (0 == ppustrTempFileURL); 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir return osl_error; 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir /***************************************************************** 201*cdf0e10cSrcweir * Create a unique file in the specified directory and return 202*cdf0e10cSrcweir * it's name 203*cdf0e10cSrcweir ****************************************************************/ 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir static oslFileError osl_create_temp_file_impl_( 206*cdf0e10cSrcweir const rtl_uString* pustr_base_directory, 207*cdf0e10cSrcweir oslFileHandle* file_handle, 208*cdf0e10cSrcweir rtl_uString** ppustr_temp_file_name) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir rtl_uString* rand_name = 0; 211*cdf0e10cSrcweir sal_uInt32 len_base_dir = 0; 212*cdf0e10cSrcweir rtl_uString* tmp_file_path = 0; 213*cdf0e10cSrcweir rtl_uString* tmp_file_url = 0; 214*cdf0e10cSrcweir sal_Int32 capacity = 0; 215*cdf0e10cSrcweir oslFileError osl_error = osl_File_E_None; 216*cdf0e10cSrcweir sal_Int32 offset_file_name; 217*cdf0e10cSrcweir const sal_Unicode* puchr; 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir OSL_PRECOND(pustr_base_directory, "Invalid Parameter"); 220*cdf0e10cSrcweir OSL_PRECOND(file_handle, "Invalid Parameter"); 221*cdf0e10cSrcweir OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter"); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir len_base_dir = rtl_uString_getLength(pustr_base_directory); 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir rtl_uStringbuffer_newFromStr_WithLength( 226*cdf0e10cSrcweir &tmp_file_path, 227*cdf0e10cSrcweir rtl_uString_getStr((rtl_uString*)pustr_base_directory), 228*cdf0e10cSrcweir len_base_dir); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir rtl_uStringbuffer_ensureCapacity( 231*cdf0e10cSrcweir &tmp_file_path, 232*cdf0e10cSrcweir &capacity, 233*cdf0e10cSrcweir (len_base_dir + 1 + RAND_NAME_LENGTH)); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir offset_file_name = len_base_dir; 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir puchr = rtl_uString_getStr(tmp_file_path); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir /* ensure that the last character is a '/' */ 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir if ((sal_Unicode)'/' != puchr[len_base_dir - 1]) 242*cdf0e10cSrcweir { 243*cdf0e10cSrcweir rtl_uStringbuffer_insert_ascii( 244*cdf0e10cSrcweir &tmp_file_path, 245*cdf0e10cSrcweir &capacity, 246*cdf0e10cSrcweir len_base_dir, 247*cdf0e10cSrcweir "/", 248*cdf0e10cSrcweir 1); 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir offset_file_name++; 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir while(1) /* try until success */ 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir osl_gen_random_name_impl_(&rand_name); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir rtl_uStringbuffer_insert( 258*cdf0e10cSrcweir &tmp_file_path, 259*cdf0e10cSrcweir &capacity, 260*cdf0e10cSrcweir offset_file_name, 261*cdf0e10cSrcweir rtl_uString_getStr(rand_name), 262*cdf0e10cSrcweir rtl_uString_getLength(rand_name)); 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir osl_error = osl_getFileURLFromSystemPath( 265*cdf0e10cSrcweir tmp_file_path, &tmp_file_url); 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir if (osl_File_E_None == osl_error) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir /* RW permission for the user only! */ 270*cdf0e10cSrcweir mode_t old_mode = umask(077); 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir osl_error = osl_openFile( 273*cdf0e10cSrcweir tmp_file_url, 274*cdf0e10cSrcweir file_handle, 275*cdf0e10cSrcweir osl_File_OpenFlag_Read | 276*cdf0e10cSrcweir osl_File_OpenFlag_Write | 277*cdf0e10cSrcweir osl_File_OpenFlag_Create); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir umask(old_mode); 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir /* in case of error osl_File_E_EXIST we simply try again else we give up */ 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST)) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir if (rand_name) 287*cdf0e10cSrcweir rtl_uString_release(rand_name); 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir if (tmp_file_url) 290*cdf0e10cSrcweir rtl_uString_release(tmp_file_url); 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir break; 293*cdf0e10cSrcweir } 294*cdf0e10cSrcweir } /* while(1) */ 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir if (osl_File_E_None == osl_error) 297*cdf0e10cSrcweir rtl_uString_assign(ppustr_temp_file_name, tmp_file_path); 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir if (tmp_file_path) 300*cdf0e10cSrcweir rtl_uString_release(tmp_file_path); 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir return osl_error; 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir /***************************************************************** 306*cdf0e10cSrcweir * osl_createTempFile 307*cdf0e10cSrcweir *****************************************************************/ 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir oslFileError SAL_CALL osl_createTempFile( 310*cdf0e10cSrcweir rtl_uString* pustrDirectoryURL, 311*cdf0e10cSrcweir oslFileHandle* pHandle, 312*cdf0e10cSrcweir rtl_uString** ppustrTempFileURL) 313*cdf0e10cSrcweir { 314*cdf0e10cSrcweir rtl_uString* base_directory = 0; 315*cdf0e10cSrcweir rtl_uString* temp_file_name = 0; 316*cdf0e10cSrcweir oslFileHandle temp_file_handle; 317*cdf0e10cSrcweir sal_Bool b_delete_on_close; 318*cdf0e10cSrcweir oslFileError osl_error; 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir osl_error = osl_setup_createTempFile_impl_( 321*cdf0e10cSrcweir pustrDirectoryURL, 322*cdf0e10cSrcweir pHandle, 323*cdf0e10cSrcweir ppustrTempFileURL, 324*cdf0e10cSrcweir &base_directory, 325*cdf0e10cSrcweir &b_delete_on_close); 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir if (osl_File_E_None != osl_error) 328*cdf0e10cSrcweir return osl_error; 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir osl_error = osl_create_temp_file_impl_( 331*cdf0e10cSrcweir base_directory, &temp_file_handle, &temp_file_name); 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir if (osl_File_E_None == osl_error) 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir rtl_uString* temp_file_url = 0; 336*cdf0e10cSrcweir 337*cdf0e10cSrcweir /* assuming this works */ 338*cdf0e10cSrcweir osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url); 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir if (b_delete_on_close) 341*cdf0e10cSrcweir { 342*cdf0e10cSrcweir osl_error = osl_removeFile(temp_file_url); 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir if (osl_File_E_None == osl_error) 345*cdf0e10cSrcweir *pHandle = temp_file_handle; 346*cdf0e10cSrcweir else 347*cdf0e10cSrcweir osl_closeFile(temp_file_handle); 348*cdf0e10cSrcweir } 349*cdf0e10cSrcweir else 350*cdf0e10cSrcweir { 351*cdf0e10cSrcweir if (pHandle) 352*cdf0e10cSrcweir *pHandle = temp_file_handle; 353*cdf0e10cSrcweir else 354*cdf0e10cSrcweir osl_closeFile(temp_file_handle); 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir rtl_uString_assign(ppustrTempFileURL, temp_file_url); 357*cdf0e10cSrcweir } 358*cdf0e10cSrcweir 359*cdf0e10cSrcweir if (temp_file_url) 360*cdf0e10cSrcweir rtl_uString_release(temp_file_url); 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir if (temp_file_name) 363*cdf0e10cSrcweir rtl_uString_release(temp_file_name); 364*cdf0e10cSrcweir } 365*cdf0e10cSrcweir 366*cdf0e10cSrcweir if (base_directory) 367*cdf0e10cSrcweir rtl_uString_release(base_directory); 368*cdf0e10cSrcweir 369*cdf0e10cSrcweir return osl_error; 370*cdf0e10cSrcweir } 371