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 #include "osl/file.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include "osl/diagnose.h" 31*cdf0e10cSrcweir #include "osl/thread.h" 32*cdf0e10cSrcweir #include <osl/signal.h> 33*cdf0e10cSrcweir #include "rtl/alloc.h" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include "system.h" 36*cdf0e10cSrcweir #include "file_impl.hxx" 37*cdf0e10cSrcweir #include "file_error_transl.h" 38*cdf0e10cSrcweir #include "file_path_helper.hxx" 39*cdf0e10cSrcweir #include "file_url.h" 40*cdf0e10cSrcweir #include "uunxapi.hxx" 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir #include <sys/types.h> 43*cdf0e10cSrcweir #include <errno.h> 44*cdf0e10cSrcweir #include <dirent.h> 45*cdf0e10cSrcweir #include <limits.h> 46*cdf0e10cSrcweir #include <stdio.h> 47*cdf0e10cSrcweir #include <string.h> 48*cdf0e10cSrcweir #include <unistd.h> 49*cdf0e10cSrcweir #include <sys/stat.h> 50*cdf0e10cSrcweir #include <sys/mman.h> 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir #include <algorithm> 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir /************************************************************************ 55*cdf0e10cSrcweir * ToDo 56*cdf0e10cSrcweir * 57*cdf0e10cSrcweir * - Fix: check for corresponding struct sizes in exported functions 58*cdf0e10cSrcweir * - check size/use of oslDirectory 59*cdf0e10cSrcweir * - check size/use of oslDirectoryItem 60*cdf0e10cSrcweir ***********************************************************************/ 61*cdf0e10cSrcweir /****************************************************************************** 62*cdf0e10cSrcweir * 63*cdf0e10cSrcweir * Data Type Definition 64*cdf0e10cSrcweir * 65*cdf0e10cSrcweir ******************************************************************************/ 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir typedef struct 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir rtl_uString* ustrPath; /* holds native directory path */ 70*cdf0e10cSrcweir DIR* pDirStruct; 71*cdf0e10cSrcweir } oslDirectoryImpl; 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir #if 0 74*cdf0e10cSrcweir /* FIXME: reintroducing this may save some extra bytes per Item */ 75*cdf0e10cSrcweir typedef struct 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir rtl_uString* ustrFileName; /* holds native file name */ 78*cdf0e10cSrcweir rtl_uString* ustrDirPath; /* holds native dir path */ 79*cdf0e10cSrcweir sal_uInt32 RefCount; 80*cdf0e10cSrcweir } oslDirectoryItemImpl; 81*cdf0e10cSrcweir #endif 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir DirectoryItem_Impl::DirectoryItem_Impl( 84*cdf0e10cSrcweir rtl_uString * ustrFilePath, unsigned char DType) 85*cdf0e10cSrcweir : m_RefCount (1), 86*cdf0e10cSrcweir m_ustrFilePath (ustrFilePath), 87*cdf0e10cSrcweir m_DType (DType) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir if (m_ustrFilePath != 0) 90*cdf0e10cSrcweir rtl_uString_acquire(m_ustrFilePath); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir DirectoryItem_Impl::~DirectoryItem_Impl() 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir if (m_ustrFilePath != 0) 95*cdf0e10cSrcweir rtl_uString_release(m_ustrFilePath); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir void * DirectoryItem_Impl::operator new(size_t n) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir return rtl_allocateMemory(n); 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir void DirectoryItem_Impl::operator delete(void * p, size_t) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir rtl_freeMemory(p); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir void DirectoryItem_Impl::acquire() 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir ++m_RefCount; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir void DirectoryItem_Impl::release() 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir if (0 == --m_RefCount) 114*cdf0e10cSrcweir delete this; 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir oslFileType DirectoryItem_Impl::getFileType() const 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir switch (m_DType) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir #ifdef _DIRENT_HAVE_D_TYPE 122*cdf0e10cSrcweir case DT_LNK: 123*cdf0e10cSrcweir return osl_File_Type_Link; 124*cdf0e10cSrcweir case DT_DIR: 125*cdf0e10cSrcweir return osl_File_Type_Directory; 126*cdf0e10cSrcweir case DT_REG: 127*cdf0e10cSrcweir return osl_File_Type_Regular; 128*cdf0e10cSrcweir case DT_FIFO: 129*cdf0e10cSrcweir return osl_File_Type_Fifo; 130*cdf0e10cSrcweir case DT_SOCK: 131*cdf0e10cSrcweir return osl_File_Type_Socket; 132*cdf0e10cSrcweir case DT_CHR: 133*cdf0e10cSrcweir case DT_BLK: 134*cdf0e10cSrcweir return osl_File_Type_Special; 135*cdf0e10cSrcweir #endif /* _DIRENT_HAVE_D_TYPE */ 136*cdf0e10cSrcweir default: 137*cdf0e10cSrcweir break; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir return osl_File_Type_Unknown; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir /****************************************************************************** 143*cdf0e10cSrcweir * 144*cdf0e10cSrcweir * C-String Function Declarations 145*cdf0e10cSrcweir * 146*cdf0e10cSrcweir *****************************************************************************/ 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir static oslFileError osl_psz_createDirectory(const sal_Char* pszPath); 149*cdf0e10cSrcweir static oslFileError osl_psz_removeDirectory(const sal_Char* pszPath); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir /******************************************************************* 152*cdf0e10cSrcweir * osl_openDirectory 153*cdf0e10cSrcweir ******************************************************************/ 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir oslFileError SAL_CALL osl_openDirectory(rtl_uString* ustrDirectoryURL, oslDirectory* pDirectory) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir rtl_uString* ustrSystemPath = NULL; 158*cdf0e10cSrcweir oslFileError eRet; 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir char path[PATH_MAX]; 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir if ((0 == ustrDirectoryURL) || (0 == ustrDirectoryURL->length) || (0 == pDirectory)) 163*cdf0e10cSrcweir return osl_File_E_INVAL; 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir /* convert file URL to system path */ 166*cdf0e10cSrcweir eRet = osl_getSystemPathFromFileURL_Ex(ustrDirectoryURL, &ustrSystemPath, sal_False); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir if( osl_File_E_None != eRet ) 169*cdf0e10cSrcweir return eRet; 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir osl_systemPathRemoveSeparator(ustrSystemPath); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir /* convert unicode path to text */ 174*cdf0e10cSrcweir if ( UnicodeToText( path, PATH_MAX, ustrSystemPath->buffer, ustrSystemPath->length ) 175*cdf0e10cSrcweir #ifdef MACOSX 176*cdf0e10cSrcweir && macxp_resolveAlias( path, PATH_MAX ) == 0 177*cdf0e10cSrcweir #endif /* MACOSX */ 178*cdf0e10cSrcweir ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir /* open directory */ 181*cdf0e10cSrcweir DIR *pdir = opendir( path ); 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir if( pdir ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir /* create and initialize impl structure */ 186*cdf0e10cSrcweir oslDirectoryImpl* pDirImpl = (oslDirectoryImpl*) rtl_allocateMemory( sizeof(oslDirectoryImpl) ); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir if( pDirImpl ) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir pDirImpl->pDirStruct = pdir; 191*cdf0e10cSrcweir pDirImpl->ustrPath = ustrSystemPath; 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir *pDirectory = (oslDirectory) pDirImpl; 194*cdf0e10cSrcweir return osl_File_E_None; 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir else 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir errno = ENOMEM; 199*cdf0e10cSrcweir closedir( pdir ); 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir else 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir #ifdef DEBUG_OSL_FILE 205*cdf0e10cSrcweir perror ("osl_openDirectory"); fprintf (stderr, path); 206*cdf0e10cSrcweir #endif 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir rtl_uString_release( ustrSystemPath ); 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, errno); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir /****************************************************************************/ 216*cdf0e10cSrcweir /* osl_closeDirectory */ 217*cdf0e10cSrcweir /****************************************************************************/ 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir oslFileError SAL_CALL osl_closeDirectory( oslDirectory Directory ) 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir oslDirectoryImpl* pDirImpl = (oslDirectoryImpl*) Directory; 222*cdf0e10cSrcweir oslFileError err = osl_File_E_None; 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir OSL_ASSERT( Directory ); 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir if( NULL == pDirImpl ) 227*cdf0e10cSrcweir return osl_File_E_INVAL; 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir /* close directory */ 230*cdf0e10cSrcweir if( closedir( pDirImpl->pDirStruct ) ) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir err = oslTranslateFileError(OSL_FET_ERROR, errno); 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir /* cleanup members */ 236*cdf0e10cSrcweir rtl_uString_release( pDirImpl->ustrPath ); 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir rtl_freeMemory( pDirImpl ); 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir return err; 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir /********************************************** 244*cdf0e10cSrcweir * osl_readdir_impl_ 245*cdf0e10cSrcweir * 246*cdf0e10cSrcweir * readdir wrapper, filters out "." and ".." 247*cdf0e10cSrcweir * on request 248*cdf0e10cSrcweir *********************************************/ 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir static struct dirent* osl_readdir_impl_(DIR* pdir, sal_Bool bFilterLocalAndParentDir) 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir struct dirent* pdirent; 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir while ((pdirent = readdir(pdir)) != NULL) 255*cdf0e10cSrcweir { 256*cdf0e10cSrcweir if (bFilterLocalAndParentDir && 257*cdf0e10cSrcweir ((0 == strcmp(pdirent->d_name, ".")) || (0 == strcmp(pdirent->d_name, "..")))) 258*cdf0e10cSrcweir continue; 259*cdf0e10cSrcweir else 260*cdf0e10cSrcweir break; 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir return pdirent; 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir /**************************************************************************** 267*cdf0e10cSrcweir * osl_getNextDirectoryItem 268*cdf0e10cSrcweir ***************************************************************************/ 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir oslFileError SAL_CALL osl_getNextDirectoryItem(oslDirectory Directory, oslDirectoryItem* pItem, sal_uInt32 /*uHint*/) 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir oslDirectoryImpl* pDirImpl = (oslDirectoryImpl*)Directory; 273*cdf0e10cSrcweir rtl_uString* ustrFileName = NULL; 274*cdf0e10cSrcweir rtl_uString* ustrFilePath = NULL; 275*cdf0e10cSrcweir struct dirent* pEntry; 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir OSL_ASSERT(Directory); 278*cdf0e10cSrcweir OSL_ASSERT(pItem); 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir if ((NULL == Directory) || (NULL == pItem)) 281*cdf0e10cSrcweir return osl_File_E_INVAL; 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir pEntry = osl_readdir_impl_(pDirImpl->pDirStruct, sal_True); 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir if (NULL == pEntry) 286*cdf0e10cSrcweir return osl_File_E_NOENT; 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir #if defined(MACOSX) 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir // convert decomposed filename to precomposed unicode 292*cdf0e10cSrcweir char composed_name[BUFSIZ]; 293*cdf0e10cSrcweir CFMutableStringRef strRef = CFStringCreateMutable (NULL, 0 ); 294*cdf0e10cSrcweir CFStringAppendCString( strRef, pEntry->d_name, kCFStringEncodingUTF8 ); //UTF8 is default on Mac OSX 295*cdf0e10cSrcweir CFStringNormalize( strRef, kCFStringNormalizationFormC ); 296*cdf0e10cSrcweir CFStringGetCString( strRef, composed_name, BUFSIZ, kCFStringEncodingUTF8 ); 297*cdf0e10cSrcweir CFRelease( strRef ); 298*cdf0e10cSrcweir rtl_string2UString( &ustrFileName, composed_name, strlen( composed_name), 299*cdf0e10cSrcweir osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS ); 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir #else // not MACOSX 302*cdf0e10cSrcweir /* convert file name to unicode */ 303*cdf0e10cSrcweir rtl_string2UString( &ustrFileName, pEntry->d_name, strlen( pEntry->d_name ), 304*cdf0e10cSrcweir osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS ); 305*cdf0e10cSrcweir OSL_ASSERT(ustrFileName != 0); 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir #endif 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir osl_systemPathMakeAbsolutePath(pDirImpl->ustrPath, ustrFileName, &ustrFilePath); 310*cdf0e10cSrcweir rtl_uString_release( ustrFileName ); 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir DirectoryItem_Impl * pImpl = static_cast< DirectoryItem_Impl* >(*pItem); 313*cdf0e10cSrcweir if (0 != pImpl) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir pImpl->release(), pImpl = 0; 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir #ifdef _DIRENT_HAVE_D_TYPE 318*cdf0e10cSrcweir pImpl = new DirectoryItem_Impl(ustrFilePath, pEntry->d_type); 319*cdf0e10cSrcweir #else 320*cdf0e10cSrcweir pImpl = new DirectoryItem_Impl(ustrFilePath); 321*cdf0e10cSrcweir #endif /* _DIRENT_HAVE_D_TYPE */ 322*cdf0e10cSrcweir *pItem = pImpl; 323*cdf0e10cSrcweir rtl_uString_release( ustrFilePath ); 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir return osl_File_E_None; 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir /****************************************************************************/ 329*cdf0e10cSrcweir /* osl_getDirectoryItem */ 330*cdf0e10cSrcweir /****************************************************************************/ 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir oslFileError SAL_CALL osl_getDirectoryItem( rtl_uString* ustrFileURL, oslDirectoryItem* pItem ) 333*cdf0e10cSrcweir { 334*cdf0e10cSrcweir rtl_uString* ustrSystemPath = NULL; 335*cdf0e10cSrcweir oslFileError osl_error = osl_File_E_INVAL; 336*cdf0e10cSrcweir 337*cdf0e10cSrcweir OSL_ASSERT((0 != ustrFileURL) && (0 != pItem)); 338*cdf0e10cSrcweir if ((0 == ustrFileURL) || (0 == ustrFileURL->length) || (0 == pItem)) 339*cdf0e10cSrcweir return osl_File_E_INVAL; 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir osl_error = osl_getSystemPathFromFileURL_Ex(ustrFileURL, &ustrSystemPath, sal_False); 342*cdf0e10cSrcweir if (osl_File_E_None != osl_error) 343*cdf0e10cSrcweir return osl_error; 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir osl_systemPathRemoveSeparator(ustrSystemPath); 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir if (-1 == access_u(ustrSystemPath, F_OK)) 348*cdf0e10cSrcweir { 349*cdf0e10cSrcweir osl_error = oslTranslateFileError(OSL_FET_ERROR, errno); 350*cdf0e10cSrcweir } 351*cdf0e10cSrcweir else 352*cdf0e10cSrcweir { 353*cdf0e10cSrcweir *pItem = new DirectoryItem_Impl(ustrSystemPath); 354*cdf0e10cSrcweir } 355*cdf0e10cSrcweir rtl_uString_release(ustrSystemPath); 356*cdf0e10cSrcweir 357*cdf0e10cSrcweir return osl_error; 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir /****************************************************************************/ 362*cdf0e10cSrcweir /* osl_acquireDirectoryItem */ 363*cdf0e10cSrcweir /****************************************************************************/ 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir oslFileError SAL_CALL osl_acquireDirectoryItem( oslDirectoryItem Item ) 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir DirectoryItem_Impl * pImpl = static_cast< DirectoryItem_Impl* >(Item); 368*cdf0e10cSrcweir if (0 == pImpl) 369*cdf0e10cSrcweir return osl_File_E_INVAL; 370*cdf0e10cSrcweir 371*cdf0e10cSrcweir pImpl->acquire(); 372*cdf0e10cSrcweir return osl_File_E_None; 373*cdf0e10cSrcweir } 374*cdf0e10cSrcweir 375*cdf0e10cSrcweir /****************************************************************************/ 376*cdf0e10cSrcweir /* osl_releaseDirectoryItem */ 377*cdf0e10cSrcweir /****************************************************************************/ 378*cdf0e10cSrcweir 379*cdf0e10cSrcweir oslFileError SAL_CALL osl_releaseDirectoryItem( oslDirectoryItem Item ) 380*cdf0e10cSrcweir { 381*cdf0e10cSrcweir DirectoryItem_Impl * pImpl = static_cast< DirectoryItem_Impl* >(Item); 382*cdf0e10cSrcweir if (0 == pImpl) 383*cdf0e10cSrcweir return osl_File_E_INVAL; 384*cdf0e10cSrcweir 385*cdf0e10cSrcweir pImpl->release(); 386*cdf0e10cSrcweir return osl_File_E_None; 387*cdf0e10cSrcweir } 388*cdf0e10cSrcweir 389*cdf0e10cSrcweir /****************************************************************************/ 390*cdf0e10cSrcweir /* osl_createDirectory */ 391*cdf0e10cSrcweir /****************************************************************************/ 392*cdf0e10cSrcweir 393*cdf0e10cSrcweir oslFileError SAL_CALL osl_createDirectory( rtl_uString* ustrDirectoryURL ) 394*cdf0e10cSrcweir { 395*cdf0e10cSrcweir char path[PATH_MAX]; 396*cdf0e10cSrcweir oslFileError eRet; 397*cdf0e10cSrcweir 398*cdf0e10cSrcweir OSL_ASSERT( ustrDirectoryURL ); 399*cdf0e10cSrcweir 400*cdf0e10cSrcweir /* convert directory url to system path */ 401*cdf0e10cSrcweir eRet = FileURLToPath( path, PATH_MAX, ustrDirectoryURL ); 402*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 403*cdf0e10cSrcweir return eRet; 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir #ifdef MACOSX 406*cdf0e10cSrcweir if ( macxp_resolveAlias( path, PATH_MAX ) != 0 ) 407*cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno ); 408*cdf0e10cSrcweir #endif/* MACOSX */ 409*cdf0e10cSrcweir 410*cdf0e10cSrcweir return osl_psz_createDirectory( path ); 411*cdf0e10cSrcweir } 412*cdf0e10cSrcweir 413*cdf0e10cSrcweir /****************************************************************************/ 414*cdf0e10cSrcweir /* osl_removeDirectory */ 415*cdf0e10cSrcweir /****************************************************************************/ 416*cdf0e10cSrcweir 417*cdf0e10cSrcweir oslFileError SAL_CALL osl_removeDirectory( rtl_uString* ustrDirectoryURL ) 418*cdf0e10cSrcweir { 419*cdf0e10cSrcweir char path[PATH_MAX]; 420*cdf0e10cSrcweir oslFileError eRet; 421*cdf0e10cSrcweir 422*cdf0e10cSrcweir OSL_ASSERT( ustrDirectoryURL ); 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir /* convert directory url to system path */ 425*cdf0e10cSrcweir eRet = FileURLToPath( path, PATH_MAX, ustrDirectoryURL ); 426*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 427*cdf0e10cSrcweir return eRet; 428*cdf0e10cSrcweir 429*cdf0e10cSrcweir #ifdef MACOSX 430*cdf0e10cSrcweir if ( macxp_resolveAlias( path, PATH_MAX ) != 0 ) 431*cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno ); 432*cdf0e10cSrcweir #endif/* MACOSX */ 433*cdf0e10cSrcweir 434*cdf0e10cSrcweir return osl_psz_removeDirectory( path ); 435*cdf0e10cSrcweir } 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir /***************************************** 438*cdf0e10cSrcweir * osl_psz_createDirectory 439*cdf0e10cSrcweir ****************************************/ 440*cdf0e10cSrcweir 441*cdf0e10cSrcweir static oslFileError osl_psz_createDirectory( const sal_Char* pszPath ) 442*cdf0e10cSrcweir { 443*cdf0e10cSrcweir int nRet=0; 444*cdf0e10cSrcweir int mode = S_IRWXU | S_IRWXG | S_IRWXO; 445*cdf0e10cSrcweir 446*cdf0e10cSrcweir nRet = mkdir(pszPath,mode); 447*cdf0e10cSrcweir 448*cdf0e10cSrcweir if ( nRet < 0 ) 449*cdf0e10cSrcweir { 450*cdf0e10cSrcweir nRet=errno; 451*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 452*cdf0e10cSrcweir } 453*cdf0e10cSrcweir 454*cdf0e10cSrcweir return osl_File_E_None; 455*cdf0e10cSrcweir } 456*cdf0e10cSrcweir 457*cdf0e10cSrcweir /***************************************** 458*cdf0e10cSrcweir * osl_psz_removeDirectory 459*cdf0e10cSrcweir ****************************************/ 460*cdf0e10cSrcweir 461*cdf0e10cSrcweir static oslFileError osl_psz_removeDirectory( const sal_Char* pszPath ) 462*cdf0e10cSrcweir { 463*cdf0e10cSrcweir int nRet=0; 464*cdf0e10cSrcweir 465*cdf0e10cSrcweir nRet = rmdir(pszPath); 466*cdf0e10cSrcweir 467*cdf0e10cSrcweir if ( nRet < 0 ) 468*cdf0e10cSrcweir { 469*cdf0e10cSrcweir nRet=errno; 470*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 471*cdf0e10cSrcweir } 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir return osl_File_E_None; 474*cdf0e10cSrcweir } 475*cdf0e10cSrcweir 476*cdf0e10cSrcweir /****************************************************************************/ 477*cdf0e10cSrcweir /* osl_createDirectoryPath */ 478*cdf0e10cSrcweir /****************************************************************************/ 479*cdf0e10cSrcweir 480*cdf0e10cSrcweir static int path_make_parent(sal_Unicode* path) 481*cdf0e10cSrcweir { 482*cdf0e10cSrcweir int i = rtl_ustr_lastIndexOfChar(path, '/'); 483*cdf0e10cSrcweir 484*cdf0e10cSrcweir if (i > 0) 485*cdf0e10cSrcweir { 486*cdf0e10cSrcweir *(path + i) = 0; 487*cdf0e10cSrcweir return i; 488*cdf0e10cSrcweir } 489*cdf0e10cSrcweir else 490*cdf0e10cSrcweir return 0; 491*cdf0e10cSrcweir } 492*cdf0e10cSrcweir 493*cdf0e10cSrcweir static int create_dir_with_callback( 494*cdf0e10cSrcweir sal_Unicode* directory_path, 495*cdf0e10cSrcweir oslDirectoryCreationCallbackFunc aDirectoryCreationCallbackFunc, 496*cdf0e10cSrcweir void* pData) 497*cdf0e10cSrcweir { 498*cdf0e10cSrcweir int mode = S_IRWXU | S_IRWXG | S_IRWXO; 499*cdf0e10cSrcweir 500*cdf0e10cSrcweir if (osl::mkdir(directory_path, mode) == 0) 501*cdf0e10cSrcweir { 502*cdf0e10cSrcweir if (aDirectoryCreationCallbackFunc) 503*cdf0e10cSrcweir { 504*cdf0e10cSrcweir rtl::OUString url; 505*cdf0e10cSrcweir osl::FileBase::getFileURLFromSystemPath(directory_path, url); 506*cdf0e10cSrcweir aDirectoryCreationCallbackFunc(pData, url.pData); 507*cdf0e10cSrcweir } 508*cdf0e10cSrcweir return 0; 509*cdf0e10cSrcweir } 510*cdf0e10cSrcweir return errno; 511*cdf0e10cSrcweir } 512*cdf0e10cSrcweir 513*cdf0e10cSrcweir static oslFileError create_dir_recursively_( 514*cdf0e10cSrcweir sal_Unicode* dir_path, 515*cdf0e10cSrcweir oslDirectoryCreationCallbackFunc aDirectoryCreationCallbackFunc, 516*cdf0e10cSrcweir void* pData) 517*cdf0e10cSrcweir { 518*cdf0e10cSrcweir OSL_PRECOND((rtl_ustr_getLength(dir_path) > 0) && ((dir_path + (rtl_ustr_getLength(dir_path) - 1)) != (dir_path + rtl_ustr_lastIndexOfChar(dir_path, '/'))), \ 519*cdf0e10cSrcweir "Path must not end with a slash"); 520*cdf0e10cSrcweir 521*cdf0e10cSrcweir int native_err = create_dir_with_callback( 522*cdf0e10cSrcweir dir_path, aDirectoryCreationCallbackFunc, pData); 523*cdf0e10cSrcweir 524*cdf0e10cSrcweir if (native_err == 0) 525*cdf0e10cSrcweir return osl_File_E_None; 526*cdf0e10cSrcweir 527*cdf0e10cSrcweir if (native_err != ENOENT) 528*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, native_err); 529*cdf0e10cSrcweir 530*cdf0e10cSrcweir // we step back until '/a_dir' at maximum because 531*cdf0e10cSrcweir // we should get an error unequal ENOENT when 532*cdf0e10cSrcweir // we try to create 'a_dir' at '/' and would so 533*cdf0e10cSrcweir // return before 534*cdf0e10cSrcweir int pos = path_make_parent(dir_path); 535*cdf0e10cSrcweir 536*cdf0e10cSrcweir oslFileError osl_error = create_dir_recursively_( 537*cdf0e10cSrcweir dir_path, aDirectoryCreationCallbackFunc, pData); 538*cdf0e10cSrcweir 539*cdf0e10cSrcweir if (osl_File_E_None != osl_error) 540*cdf0e10cSrcweir return osl_error; 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir dir_path[pos] = '/'; 543*cdf0e10cSrcweir 544*cdf0e10cSrcweir return create_dir_recursively_(dir_path, aDirectoryCreationCallbackFunc, pData); 545*cdf0e10cSrcweir } 546*cdf0e10cSrcweir 547*cdf0e10cSrcweir oslFileError SAL_CALL osl_createDirectoryPath( 548*cdf0e10cSrcweir rtl_uString* aDirectoryUrl, 549*cdf0e10cSrcweir oslDirectoryCreationCallbackFunc aDirectoryCreationCallbackFunc, 550*cdf0e10cSrcweir void* pData) 551*cdf0e10cSrcweir { 552*cdf0e10cSrcweir if (aDirectoryUrl == NULL) 553*cdf0e10cSrcweir return osl_File_E_INVAL; 554*cdf0e10cSrcweir 555*cdf0e10cSrcweir rtl::OUString sys_path; 556*cdf0e10cSrcweir oslFileError osl_error = osl_getSystemPathFromFileURL_Ex( 557*cdf0e10cSrcweir aDirectoryUrl, &sys_path.pData, sal_False); 558*cdf0e10cSrcweir 559*cdf0e10cSrcweir if (osl_error != osl_File_E_None) 560*cdf0e10cSrcweir return osl_error; 561*cdf0e10cSrcweir 562*cdf0e10cSrcweir osl::systemPathRemoveSeparator(sys_path); 563*cdf0e10cSrcweir 564*cdf0e10cSrcweir // const_cast because sys_path is a local copy which we want to modify inplace instead of 565*cdf0e10cSrcweir // coyp it into another buffer on the heap again 566*cdf0e10cSrcweir return create_dir_recursively_(sys_path.pData->buffer, aDirectoryCreationCallbackFunc, pData); 567*cdf0e10cSrcweir } 568*cdf0e10cSrcweir 569*cdf0e10cSrcweir /****************************************************************************** 570*cdf0e10cSrcweir * 571*cdf0e10cSrcweir * C-String Function Declarations 572*cdf0e10cSrcweir * 573*cdf0e10cSrcweir *****************************************************************************/ 574*cdf0e10cSrcweir 575*cdf0e10cSrcweir static oslFileError osl_psz_removeFile(const sal_Char* pszPath); 576*cdf0e10cSrcweir static oslFileError osl_psz_copyFile(const sal_Char* pszPath, const sal_Char* pszDestPath); 577*cdf0e10cSrcweir static oslFileError osl_psz_moveFile(const sal_Char* pszPath, const sal_Char* pszDestPath); 578*cdf0e10cSrcweir 579*cdf0e10cSrcweir 580*cdf0e10cSrcweir /****************************************************************************** 581*cdf0e10cSrcweir * 582*cdf0e10cSrcweir * Static Module Utility Function Declarations 583*cdf0e10cSrcweir * 584*cdf0e10cSrcweir *****************************************************************************/ 585*cdf0e10cSrcweir 586*cdf0e10cSrcweir static oslFileError oslDoCopy(const sal_Char* pszSourceFileName, const sal_Char* pszDestFileName, mode_t nMode, size_t nSourceSize, int DestFileExists); 587*cdf0e10cSrcweir static oslFileError oslChangeFileModes(const sal_Char* pszFileName, mode_t nMode, time_t nAcTime, time_t nModTime, uid_t nUID, gid_t nGID); 588*cdf0e10cSrcweir static int oslDoCopyLink(const sal_Char* pszSourceFileName, const sal_Char* pszDestFileName); 589*cdf0e10cSrcweir static int oslDoCopyFile(const sal_Char* pszSourceFileName, const sal_Char* pszDestFileName, size_t nSourceSize, mode_t mode); 590*cdf0e10cSrcweir static oslFileError oslDoMoveFile(const sal_Char* pszPath, const sal_Char* pszDestPath); 591*cdf0e10cSrcweir 592*cdf0e10cSrcweir /****************************************************************************/ 593*cdf0e10cSrcweir /* osl_moveFile */ 594*cdf0e10cSrcweir /****************************************************************************/ 595*cdf0e10cSrcweir 596*cdf0e10cSrcweir oslFileError SAL_CALL osl_moveFile( rtl_uString* ustrFileURL, rtl_uString* ustrDestURL ) 597*cdf0e10cSrcweir { 598*cdf0e10cSrcweir char srcPath[PATH_MAX]; 599*cdf0e10cSrcweir char destPath[PATH_MAX]; 600*cdf0e10cSrcweir oslFileError eRet; 601*cdf0e10cSrcweir 602*cdf0e10cSrcweir OSL_ASSERT( ustrFileURL ); 603*cdf0e10cSrcweir OSL_ASSERT( ustrDestURL ); 604*cdf0e10cSrcweir 605*cdf0e10cSrcweir /* convert source url to system path */ 606*cdf0e10cSrcweir eRet = FileURLToPath( srcPath, PATH_MAX, ustrFileURL ); 607*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 608*cdf0e10cSrcweir return eRet; 609*cdf0e10cSrcweir 610*cdf0e10cSrcweir /* convert destination url to system path */ 611*cdf0e10cSrcweir eRet = FileURLToPath( destPath, PATH_MAX, ustrDestURL ); 612*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 613*cdf0e10cSrcweir return eRet; 614*cdf0e10cSrcweir 615*cdf0e10cSrcweir #ifdef MACOSX 616*cdf0e10cSrcweir if ( macxp_resolveAlias( srcPath, PATH_MAX ) != 0 || macxp_resolveAlias( destPath, PATH_MAX ) != 0 ) 617*cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno ); 618*cdf0e10cSrcweir #endif/* MACOSX */ 619*cdf0e10cSrcweir 620*cdf0e10cSrcweir return oslDoMoveFile( srcPath, destPath ); 621*cdf0e10cSrcweir } 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir /****************************************************************************/ 624*cdf0e10cSrcweir /* osl_copyFile */ 625*cdf0e10cSrcweir /****************************************************************************/ 626*cdf0e10cSrcweir 627*cdf0e10cSrcweir oslFileError SAL_CALL osl_copyFile( rtl_uString* ustrFileURL, rtl_uString* ustrDestURL ) 628*cdf0e10cSrcweir { 629*cdf0e10cSrcweir char srcPath[PATH_MAX]; 630*cdf0e10cSrcweir char destPath[PATH_MAX]; 631*cdf0e10cSrcweir oslFileError eRet; 632*cdf0e10cSrcweir 633*cdf0e10cSrcweir OSL_ASSERT( ustrFileURL ); 634*cdf0e10cSrcweir OSL_ASSERT( ustrDestURL ); 635*cdf0e10cSrcweir 636*cdf0e10cSrcweir /* convert source url to system path */ 637*cdf0e10cSrcweir eRet = FileURLToPath( srcPath, PATH_MAX, ustrFileURL ); 638*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 639*cdf0e10cSrcweir return eRet; 640*cdf0e10cSrcweir 641*cdf0e10cSrcweir /* convert destination url to system path */ 642*cdf0e10cSrcweir eRet = FileURLToPath( destPath, PATH_MAX, ustrDestURL ); 643*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 644*cdf0e10cSrcweir return eRet; 645*cdf0e10cSrcweir 646*cdf0e10cSrcweir #ifdef MACOSX 647*cdf0e10cSrcweir if ( macxp_resolveAlias( srcPath, PATH_MAX ) != 0 || macxp_resolveAlias( destPath, PATH_MAX ) != 0 ) 648*cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno ); 649*cdf0e10cSrcweir #endif/* MACOSX */ 650*cdf0e10cSrcweir 651*cdf0e10cSrcweir return osl_psz_copyFile( srcPath, destPath ); 652*cdf0e10cSrcweir } 653*cdf0e10cSrcweir 654*cdf0e10cSrcweir /****************************************************************************/ 655*cdf0e10cSrcweir /* osl_removeFile */ 656*cdf0e10cSrcweir /****************************************************************************/ 657*cdf0e10cSrcweir 658*cdf0e10cSrcweir oslFileError SAL_CALL osl_removeFile( rtl_uString* ustrFileURL ) 659*cdf0e10cSrcweir { 660*cdf0e10cSrcweir char path[PATH_MAX]; 661*cdf0e10cSrcweir oslFileError eRet; 662*cdf0e10cSrcweir 663*cdf0e10cSrcweir OSL_ASSERT( ustrFileURL ); 664*cdf0e10cSrcweir 665*cdf0e10cSrcweir /* convert file url to system path */ 666*cdf0e10cSrcweir eRet = FileURLToPath( path, PATH_MAX, ustrFileURL ); 667*cdf0e10cSrcweir if( eRet != osl_File_E_None ) 668*cdf0e10cSrcweir return eRet; 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir #ifdef MACOSX 671*cdf0e10cSrcweir if ( macxp_resolveAlias( path, PATH_MAX ) != 0 ) 672*cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno ); 673*cdf0e10cSrcweir #endif/* MACOSX */ 674*cdf0e10cSrcweir 675*cdf0e10cSrcweir return osl_psz_removeFile( path ); 676*cdf0e10cSrcweir } 677*cdf0e10cSrcweir 678*cdf0e10cSrcweir /****************************************************************************** 679*cdf0e10cSrcweir * 680*cdf0e10cSrcweir * Utility Functions 681*cdf0e10cSrcweir * 682*cdf0e10cSrcweir *****************************************************************************/ 683*cdf0e10cSrcweir 684*cdf0e10cSrcweir /***************************************** 685*cdf0e10cSrcweir * oslDoMoveFile 686*cdf0e10cSrcweir ****************************************/ 687*cdf0e10cSrcweir 688*cdf0e10cSrcweir static oslFileError oslDoMoveFile( const sal_Char* pszPath, const sal_Char* pszDestPath) 689*cdf0e10cSrcweir { 690*cdf0e10cSrcweir oslFileError tErr=osl_File_E_invalidError; 691*cdf0e10cSrcweir 692*cdf0e10cSrcweir tErr = osl_psz_moveFile(pszPath,pszDestPath); 693*cdf0e10cSrcweir if ( tErr == osl_File_E_None ) 694*cdf0e10cSrcweir { 695*cdf0e10cSrcweir return tErr; 696*cdf0e10cSrcweir } 697*cdf0e10cSrcweir 698*cdf0e10cSrcweir if ( tErr != osl_File_E_XDEV ) 699*cdf0e10cSrcweir { 700*cdf0e10cSrcweir return tErr; 701*cdf0e10cSrcweir } 702*cdf0e10cSrcweir 703*cdf0e10cSrcweir tErr=osl_psz_copyFile(pszPath,pszDestPath); 704*cdf0e10cSrcweir 705*cdf0e10cSrcweir if ( tErr != osl_File_E_None ) 706*cdf0e10cSrcweir { 707*cdf0e10cSrcweir oslFileError tErrRemove; 708*cdf0e10cSrcweir tErrRemove=osl_psz_removeFile(pszDestPath); 709*cdf0e10cSrcweir return tErr; 710*cdf0e10cSrcweir } 711*cdf0e10cSrcweir 712*cdf0e10cSrcweir tErr=osl_psz_removeFile(pszPath); 713*cdf0e10cSrcweir 714*cdf0e10cSrcweir return tErr; 715*cdf0e10cSrcweir } 716*cdf0e10cSrcweir 717*cdf0e10cSrcweir /***************************************** 718*cdf0e10cSrcweir * osl_psz_removeFile 719*cdf0e10cSrcweir ****************************************/ 720*cdf0e10cSrcweir static oslFileError osl_psz_removeFile( const sal_Char* pszPath ) 721*cdf0e10cSrcweir { 722*cdf0e10cSrcweir int nRet=0; 723*cdf0e10cSrcweir struct stat aStat; 724*cdf0e10cSrcweir 725*cdf0e10cSrcweir nRet = lstat(pszPath,&aStat); 726*cdf0e10cSrcweir if ( nRet < 0 ) 727*cdf0e10cSrcweir { 728*cdf0e10cSrcweir nRet=errno; 729*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 730*cdf0e10cSrcweir } 731*cdf0e10cSrcweir 732*cdf0e10cSrcweir if ( S_ISDIR(aStat.st_mode) ) 733*cdf0e10cSrcweir { 734*cdf0e10cSrcweir return osl_File_E_ISDIR; 735*cdf0e10cSrcweir } 736*cdf0e10cSrcweir 737*cdf0e10cSrcweir nRet = unlink(pszPath); 738*cdf0e10cSrcweir if ( nRet < 0 ) 739*cdf0e10cSrcweir { 740*cdf0e10cSrcweir nRet=errno; 741*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 742*cdf0e10cSrcweir } 743*cdf0e10cSrcweir 744*cdf0e10cSrcweir return osl_File_E_None; 745*cdf0e10cSrcweir } 746*cdf0e10cSrcweir 747*cdf0e10cSrcweir /***************************************** 748*cdf0e10cSrcweir * osl_psz_moveFile 749*cdf0e10cSrcweir ****************************************/ 750*cdf0e10cSrcweir 751*cdf0e10cSrcweir static oslFileError osl_psz_moveFile(const sal_Char* pszPath, const sal_Char* pszDestPath) 752*cdf0e10cSrcweir { 753*cdf0e10cSrcweir 754*cdf0e10cSrcweir int nRet = 0; 755*cdf0e10cSrcweir 756*cdf0e10cSrcweir nRet = rename(pszPath,pszDestPath); 757*cdf0e10cSrcweir 758*cdf0e10cSrcweir if ( nRet < 0 ) 759*cdf0e10cSrcweir { 760*cdf0e10cSrcweir nRet=errno; 761*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 762*cdf0e10cSrcweir } 763*cdf0e10cSrcweir 764*cdf0e10cSrcweir return osl_File_E_None; 765*cdf0e10cSrcweir } 766*cdf0e10cSrcweir 767*cdf0e10cSrcweir /***************************************** 768*cdf0e10cSrcweir * osl_psz_copyFile 769*cdf0e10cSrcweir ****************************************/ 770*cdf0e10cSrcweir 771*cdf0e10cSrcweir static oslFileError osl_psz_copyFile( const sal_Char* pszPath, const sal_Char* pszDestPath ) 772*cdf0e10cSrcweir { 773*cdf0e10cSrcweir time_t nAcTime=0; 774*cdf0e10cSrcweir time_t nModTime=0; 775*cdf0e10cSrcweir uid_t nUID=0; 776*cdf0e10cSrcweir gid_t nGID=0; 777*cdf0e10cSrcweir int nRet=0; 778*cdf0e10cSrcweir mode_t nMode=0; 779*cdf0e10cSrcweir struct stat aFileStat; 780*cdf0e10cSrcweir oslFileError tErr=osl_File_E_invalidError; 781*cdf0e10cSrcweir size_t nSourceSize=0; 782*cdf0e10cSrcweir int DestFileExists=1; 783*cdf0e10cSrcweir 784*cdf0e10cSrcweir /* mfe: does the source file really exists? */ 785*cdf0e10cSrcweir nRet = lstat(pszPath,&aFileStat); 786*cdf0e10cSrcweir 787*cdf0e10cSrcweir if ( nRet < 0 ) 788*cdf0e10cSrcweir { 789*cdf0e10cSrcweir nRet=errno; 790*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 791*cdf0e10cSrcweir } 792*cdf0e10cSrcweir 793*cdf0e10cSrcweir /* mfe: we do only copy files here! */ 794*cdf0e10cSrcweir if ( S_ISDIR(aFileStat.st_mode) ) 795*cdf0e10cSrcweir { 796*cdf0e10cSrcweir return osl_File_E_ISDIR; 797*cdf0e10cSrcweir } 798*cdf0e10cSrcweir 799*cdf0e10cSrcweir nSourceSize=(size_t)aFileStat.st_size; 800*cdf0e10cSrcweir nMode=aFileStat.st_mode; 801*cdf0e10cSrcweir nAcTime=aFileStat.st_atime; 802*cdf0e10cSrcweir nModTime=aFileStat.st_mtime; 803*cdf0e10cSrcweir nUID=aFileStat.st_uid; 804*cdf0e10cSrcweir nGID=aFileStat.st_gid; 805*cdf0e10cSrcweir 806*cdf0e10cSrcweir nRet = stat(pszDestPath,&aFileStat); 807*cdf0e10cSrcweir if ( nRet < 0 ) 808*cdf0e10cSrcweir { 809*cdf0e10cSrcweir nRet=errno; 810*cdf0e10cSrcweir 811*cdf0e10cSrcweir if ( nRet == ENOENT ) 812*cdf0e10cSrcweir { 813*cdf0e10cSrcweir DestFileExists=0; 814*cdf0e10cSrcweir } 815*cdf0e10cSrcweir /* return oslTranslateFileError(nRet);*/ 816*cdf0e10cSrcweir } 817*cdf0e10cSrcweir 818*cdf0e10cSrcweir /* mfe: the destination file must not be a directory! */ 819*cdf0e10cSrcweir if ( nRet == 0 && S_ISDIR(aFileStat.st_mode) ) 820*cdf0e10cSrcweir { 821*cdf0e10cSrcweir return osl_File_E_ISDIR; 822*cdf0e10cSrcweir } 823*cdf0e10cSrcweir else 824*cdf0e10cSrcweir { 825*cdf0e10cSrcweir /* mfe: file does not exists or is no dir */ 826*cdf0e10cSrcweir } 827*cdf0e10cSrcweir 828*cdf0e10cSrcweir tErr = oslDoCopy(pszPath,pszDestPath,nMode,nSourceSize,DestFileExists); 829*cdf0e10cSrcweir 830*cdf0e10cSrcweir if ( tErr != osl_File_E_None ) 831*cdf0e10cSrcweir { 832*cdf0e10cSrcweir return tErr; 833*cdf0e10cSrcweir } 834*cdf0e10cSrcweir 835*cdf0e10cSrcweir /* 836*cdf0e10cSrcweir * mfe: ignore return code 837*cdf0e10cSrcweir * since only the success of the copy is 838*cdf0e10cSrcweir * important 839*cdf0e10cSrcweir */ 840*cdf0e10cSrcweir oslChangeFileModes(pszDestPath,nMode,nAcTime,nModTime,nUID,nGID); 841*cdf0e10cSrcweir 842*cdf0e10cSrcweir return tErr; 843*cdf0e10cSrcweir } 844*cdf0e10cSrcweir 845*cdf0e10cSrcweir 846*cdf0e10cSrcweir /****************************************************************************** 847*cdf0e10cSrcweir * 848*cdf0e10cSrcweir * Utility Functions 849*cdf0e10cSrcweir * 850*cdf0e10cSrcweir *****************************************************************************/ 851*cdf0e10cSrcweir 852*cdf0e10cSrcweir /***************************************** 853*cdf0e10cSrcweir * oslDoCopy 854*cdf0e10cSrcweir ****************************************/ 855*cdf0e10cSrcweir 856*cdf0e10cSrcweir #define TMP_DEST_FILE_EXTENSION ".osl-tmp" 857*cdf0e10cSrcweir 858*cdf0e10cSrcweir static oslFileError oslDoCopy(const sal_Char* pszSourceFileName, const sal_Char* pszDestFileName, mode_t nMode, size_t nSourceSize, int DestFileExists) 859*cdf0e10cSrcweir { 860*cdf0e10cSrcweir int nRet=0; 861*cdf0e10cSrcweir sal_Char pszTmpDestFile[PATH_MAX]; 862*cdf0e10cSrcweir size_t size_tmp_dest_buff = sizeof(pszTmpDestFile); 863*cdf0e10cSrcweir 864*cdf0e10cSrcweir /* Quick fix for #106048, the whole copy file function seems 865*cdf0e10cSrcweir to be erroneous anyway and needs to be rewritten. 866*cdf0e10cSrcweir Besides osl_copyFile is currently not used from OO/SO code. 867*cdf0e10cSrcweir */ 868*cdf0e10cSrcweir memset(pszTmpDestFile, 0, size_tmp_dest_buff); 869*cdf0e10cSrcweir 870*cdf0e10cSrcweir if ( DestFileExists ) 871*cdf0e10cSrcweir { 872*cdf0e10cSrcweir strncpy(pszTmpDestFile, pszDestFileName, size_tmp_dest_buff - 1); 873*cdf0e10cSrcweir 874*cdf0e10cSrcweir if ((strlen(pszTmpDestFile) + strlen(TMP_DEST_FILE_EXTENSION)) >= size_tmp_dest_buff) 875*cdf0e10cSrcweir return osl_File_E_NAMETOOLONG; 876*cdf0e10cSrcweir 877*cdf0e10cSrcweir strncat(pszTmpDestFile, TMP_DEST_FILE_EXTENSION, strlen(TMP_DEST_FILE_EXTENSION)); 878*cdf0e10cSrcweir 879*cdf0e10cSrcweir /* FIXME: what if pszTmpDestFile already exists? */ 880*cdf0e10cSrcweir /* with getcanonical??? */ 881*cdf0e10cSrcweir nRet=rename(pszDestFileName,pszTmpDestFile); 882*cdf0e10cSrcweir } 883*cdf0e10cSrcweir 884*cdf0e10cSrcweir /* mfe: should be S_ISREG */ 885*cdf0e10cSrcweir if ( !S_ISLNK(nMode) ) 886*cdf0e10cSrcweir { 887*cdf0e10cSrcweir /* copy SourceFile to DestFile */ 888*cdf0e10cSrcweir nRet = oslDoCopyFile(pszSourceFileName,pszDestFileName,nSourceSize, nMode); 889*cdf0e10cSrcweir } 890*cdf0e10cSrcweir /* mfe: OK redundant at the moment */ 891*cdf0e10cSrcweir else if ( S_ISLNK(nMode) ) 892*cdf0e10cSrcweir { 893*cdf0e10cSrcweir nRet = oslDoCopyLink(pszSourceFileName,pszDestFileName); 894*cdf0e10cSrcweir } 895*cdf0e10cSrcweir else 896*cdf0e10cSrcweir { 897*cdf0e10cSrcweir /* mfe: what to do here? */ 898*cdf0e10cSrcweir nRet=ENOSYS; 899*cdf0e10cSrcweir } 900*cdf0e10cSrcweir 901*cdf0e10cSrcweir if ( nRet > 0 && DestFileExists == 1 ) 902*cdf0e10cSrcweir { 903*cdf0e10cSrcweir unlink(pszDestFileName); 904*cdf0e10cSrcweir rename(pszTmpDestFile,pszDestFileName); 905*cdf0e10cSrcweir } 906*cdf0e10cSrcweir 907*cdf0e10cSrcweir if ( nRet > 0 ) 908*cdf0e10cSrcweir { 909*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 910*cdf0e10cSrcweir } 911*cdf0e10cSrcweir 912*cdf0e10cSrcweir if ( DestFileExists == 1 ) 913*cdf0e10cSrcweir { 914*cdf0e10cSrcweir unlink(pszTmpDestFile); 915*cdf0e10cSrcweir } 916*cdf0e10cSrcweir 917*cdf0e10cSrcweir return osl_File_E_None; 918*cdf0e10cSrcweir } 919*cdf0e10cSrcweir 920*cdf0e10cSrcweir /***************************************** 921*cdf0e10cSrcweir * oslChangeFileModes 922*cdf0e10cSrcweir ****************************************/ 923*cdf0e10cSrcweir 924*cdf0e10cSrcweir static oslFileError oslChangeFileModes( const sal_Char* pszFileName, mode_t nMode, time_t nAcTime, time_t nModTime, uid_t nUID, gid_t nGID) 925*cdf0e10cSrcweir { 926*cdf0e10cSrcweir int nRet=0; 927*cdf0e10cSrcweir struct utimbuf aTimeBuffer; 928*cdf0e10cSrcweir 929*cdf0e10cSrcweir nRet = chmod(pszFileName,nMode); 930*cdf0e10cSrcweir if ( nRet < 0 ) 931*cdf0e10cSrcweir { 932*cdf0e10cSrcweir nRet=errno; 933*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 934*cdf0e10cSrcweir } 935*cdf0e10cSrcweir 936*cdf0e10cSrcweir aTimeBuffer.actime=nAcTime; 937*cdf0e10cSrcweir aTimeBuffer.modtime=nModTime; 938*cdf0e10cSrcweir nRet=utime(pszFileName,&aTimeBuffer); 939*cdf0e10cSrcweir if ( nRet < 0 ) 940*cdf0e10cSrcweir { 941*cdf0e10cSrcweir nRet=errno; 942*cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet); 943*cdf0e10cSrcweir } 944*cdf0e10cSrcweir 945*cdf0e10cSrcweir if ( nUID != getuid() ) 946*cdf0e10cSrcweir { 947*cdf0e10cSrcweir nUID=getuid(); 948*cdf0e10cSrcweir } 949*cdf0e10cSrcweir 950*cdf0e10cSrcweir nRet=chown(pszFileName,nUID,nGID); 951*cdf0e10cSrcweir if ( nRet < 0 ) 952*cdf0e10cSrcweir { 953*cdf0e10cSrcweir nRet=errno; 954*cdf0e10cSrcweir 955*cdf0e10cSrcweir /* mfe: do not return an error here! */ 956*cdf0e10cSrcweir /* return oslTranslateFileError(nRet);*/ 957*cdf0e10cSrcweir } 958*cdf0e10cSrcweir 959*cdf0e10cSrcweir return osl_File_E_None; 960*cdf0e10cSrcweir } 961*cdf0e10cSrcweir 962*cdf0e10cSrcweir /***************************************** 963*cdf0e10cSrcweir * oslDoCopyLink 964*cdf0e10cSrcweir ****************************************/ 965*cdf0e10cSrcweir 966*cdf0e10cSrcweir static int oslDoCopyLink(const sal_Char* pszSourceFileName, const sal_Char* pszDestFileName) 967*cdf0e10cSrcweir { 968*cdf0e10cSrcweir int nRet=0; 969*cdf0e10cSrcweir 970*cdf0e10cSrcweir /* mfe: if dest file is symbolic link remove the link and place the file instead (hro says so) */ 971*cdf0e10cSrcweir /* mfe: if source is a link copy the link and not the file it points to (hro says so) */ 972*cdf0e10cSrcweir sal_Char pszLinkContent[PATH_MAX]; 973*cdf0e10cSrcweir 974*cdf0e10cSrcweir pszLinkContent[0] = '\0'; 975*cdf0e10cSrcweir 976*cdf0e10cSrcweir nRet = readlink(pszSourceFileName,pszLinkContent,PATH_MAX); 977*cdf0e10cSrcweir 978*cdf0e10cSrcweir if ( nRet < 0 ) 979*cdf0e10cSrcweir { 980*cdf0e10cSrcweir nRet=errno; 981*cdf0e10cSrcweir return nRet; 982*cdf0e10cSrcweir } 983*cdf0e10cSrcweir else 984*cdf0e10cSrcweir pszLinkContent[ nRet ] = 0; 985*cdf0e10cSrcweir 986*cdf0e10cSrcweir nRet = symlink(pszLinkContent,pszDestFileName); 987*cdf0e10cSrcweir 988*cdf0e10cSrcweir if ( nRet < 0 ) 989*cdf0e10cSrcweir { 990*cdf0e10cSrcweir nRet=errno; 991*cdf0e10cSrcweir return nRet; 992*cdf0e10cSrcweir } 993*cdf0e10cSrcweir 994*cdf0e10cSrcweir return 0; 995*cdf0e10cSrcweir } 996*cdf0e10cSrcweir 997*cdf0e10cSrcweir /***************************************** 998*cdf0e10cSrcweir * oslDoCopyFile 999*cdf0e10cSrcweir ****************************************/ 1000*cdf0e10cSrcweir 1001*cdf0e10cSrcweir static int oslDoCopyFile(const sal_Char* pszSourceFileName, const sal_Char* pszDestFileName, size_t nSourceSize, mode_t mode) 1002*cdf0e10cSrcweir { 1003*cdf0e10cSrcweir int SourceFileFD=0; 1004*cdf0e10cSrcweir int DestFileFD=0; 1005*cdf0e10cSrcweir int nRet=0; 1006*cdf0e10cSrcweir 1007*cdf0e10cSrcweir SourceFileFD=open(pszSourceFileName,O_RDONLY); 1008*cdf0e10cSrcweir if ( SourceFileFD < 0 ) 1009*cdf0e10cSrcweir { 1010*cdf0e10cSrcweir nRet=errno; 1011*cdf0e10cSrcweir return nRet; 1012*cdf0e10cSrcweir } 1013*cdf0e10cSrcweir 1014*cdf0e10cSrcweir DestFileFD=open(pszDestFileName, O_WRONLY | O_CREAT, mode); 1015*cdf0e10cSrcweir 1016*cdf0e10cSrcweir if ( DestFileFD < 0 ) 1017*cdf0e10cSrcweir { 1018*cdf0e10cSrcweir nRet=errno; 1019*cdf0e10cSrcweir close(SourceFileFD); 1020*cdf0e10cSrcweir return nRet; 1021*cdf0e10cSrcweir } 1022*cdf0e10cSrcweir 1023*cdf0e10cSrcweir size_t nWritten = 0; 1024*cdf0e10cSrcweir size_t nRemains = nSourceSize; 1025*cdf0e10cSrcweir 1026*cdf0e10cSrcweir if ( nRemains ) 1027*cdf0e10cSrcweir { 1028*cdf0e10cSrcweir /* mmap has problems, try the direct streaming */ 1029*cdf0e10cSrcweir char pBuffer[0x8000]; 1030*cdf0e10cSrcweir size_t nRead = 0; 1031*cdf0e10cSrcweir 1032*cdf0e10cSrcweir nRemains = nSourceSize; 1033*cdf0e10cSrcweir 1034*cdf0e10cSrcweir do 1035*cdf0e10cSrcweir { 1036*cdf0e10cSrcweir nRead = 0; 1037*cdf0e10cSrcweir nWritten = 0; 1038*cdf0e10cSrcweir 1039*cdf0e10cSrcweir size_t nToRead = std::min( (size_t)0x8000, nRemains ); 1040*cdf0e10cSrcweir nRead = read( SourceFileFD, pBuffer, nToRead ); 1041*cdf0e10cSrcweir if ( (size_t)-1 != nRead ) 1042*cdf0e10cSrcweir nWritten = write( DestFileFD, pBuffer, nRead ); 1043*cdf0e10cSrcweir 1044*cdf0e10cSrcweir if ( (size_t)-1 != nWritten ) 1045*cdf0e10cSrcweir nRemains -= nWritten; 1046*cdf0e10cSrcweir } 1047*cdf0e10cSrcweir while( nRemains && (size_t)-1 != nRead && nRead == nWritten ); 1048*cdf0e10cSrcweir } 1049*cdf0e10cSrcweir 1050*cdf0e10cSrcweir if ( nRemains ) 1051*cdf0e10cSrcweir { 1052*cdf0e10cSrcweir if ( errno ) 1053*cdf0e10cSrcweir nRet = errno; 1054*cdf0e10cSrcweir else 1055*cdf0e10cSrcweir nRet = ENOSPC; 1056*cdf0e10cSrcweir } 1057*cdf0e10cSrcweir 1058*cdf0e10cSrcweir close( SourceFileFD ); 1059*cdf0e10cSrcweir if ( close( DestFileFD ) == -1 && nRet == 0 ) 1060*cdf0e10cSrcweir nRet = errno; 1061*cdf0e10cSrcweir 1062*cdf0e10cSrcweir return nRet; 1063*cdf0e10cSrcweir } 1064*cdf0e10cSrcweir 1065