1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _FSYS_HXX 28 #define _FSYS_HXX 29 30 #include <tools/solar.h> 31 #include <tools/datetime.hxx> 32 #include <tools/string.hxx> 33 #include <tools/link.hxx> 34 #include <tools/wldcrd.hxx> 35 #ifndef __ERRCODE_HXX 36 #include <tools/errcode.hxx> 37 #endif 38 #include "tools/toolsdllapi.h" 39 40 #include <cstdarg> 41 42 #define FEAT_FSYS_DOUBLESPEED 43 44 // -------------- 45 // - FSys-Types - 46 // -------------- 47 48 class DirEntry; 49 class DirEntryList; 50 class FSysSortList; 51 class FileStatList; 52 struct FileCopier_Impl; 53 class SvFileStream; 54 class BigInt; 55 56 #define FSYS_BUFSIZE 1024 57 #define FSYS_SHORTNAME_DELIMITER '@' 58 59 // FSysAccess 60 typedef int FSysAccess; 61 #define FSYS_ACCESS_FORCED 1 62 #define FSYS_ACCESS_FLOPPY FSYS_ACCESS_FORCED 63 #define FSYS_ACCESS_CACHED 2 64 65 // DirEntryKind 66 typedef int DirEntryKind; 67 #define FSYS_KIND_NONE ((DirEntryKind) 0) 68 #define FSYS_KIND_UNKNOWN FSYS_KIND_NONE 69 #define FSYS_KIND_FILE ((DirEntryKind) 1) 70 #define FSYS_KIND_DIR ((DirEntryKind) 2) 71 #define FSYS_KIND_DEV ((DirEntryKind) 4) 72 #define FSYS_KIND_BLOCK ((DirEntryKind) 8) 73 #define FSYS_KIND_CHAR ((DirEntryKind) 16) 74 #define FSYS_KIND_WILD ((DirEntryKind) 32) 75 #define FSYS_KIND_BLOCK_REMOTE ((DirEntryKind) 64) //TPF: fuer RFS 76 #define FSYS_KIND_REMOVEABLE ((DirEntryKind) 128) 77 #define FSYS_KIND_FIXED ((DirEntryKind) 256) 78 #define FSYS_KIND_REMOTE ((DirEntryKind) 512) 79 #define FSYS_KIND_RAM ((DirEntryKind) 1024) 80 #define FSYS_KIND_CDROM ((DirEntryKind) 2048) 81 #define FSYS_KIND_ALL ((DirEntryKind) 4095) 82 #define FSYS_KIND_VISIBLE ((DirEntryKind) 4096) 83 84 // FSysSort 85 typedef int FSysSort; 86 #define FSYS_SORT_NONE ((FSysSort) 0) 87 #define FSYS_SORT_SIZE ((FSysSort) 1) 88 #define FSYS_SORT_CREATED ((FSysSort) 2) 89 #define FSYS_SORT_MODIFYED ((FSysSort) 4) 90 #define FSYS_SORT_ACCESSED ((FSysSort) 8) 91 #define FSYS_SORT_ASCENDING ((FSysSort) 16) 92 #define FSYS_SORT_DESCENDING ((FSysSort) 32) 93 #define FSYS_SORT_EXT ((FSysSort) 64) 94 #define FSYS_SORT_NAME ((FSysSort) 128) 95 #define FSYS_SORT_KIND ((FSysSort) 256) 96 #define FSYS_SORT_CREATOR ((FSysSort) 512) 97 #define FSYS_SORT_END ((FSysSort)1024) 98 99 // DirEntryFlag 100 enum DirEntryFlag 101 { 102 FSYS_FLAG_NORMAL, 103 FSYS_FLAG_VOLUME, // Dir( FSYS_FLAG_VOLUME ) und GetDevice() 104 FSYS_FLAG_ABSROOT, // z.B. "a:\" oder "\" 105 FSYS_FLAG_RELROOT, // z.B. "a:", "a:." oder "." 106 FSYS_FLAG_CURRENT = FSYS_FLAG_RELROOT, // Synonym fuer FSYS_FLAG_RELROOT 107 FSYS_FLAG_PARENT, // z.B. ".." 108 FSYS_FLAG_INVALID 109 }; 110 111 // FSysPathStyle 112 enum FSysPathStyle 113 { 114 FSYS_STYLE_HOST, 115 FSYS_STYLE_FAT, 116 FSYS_STYLE_MSDOS = FSYS_STYLE_FAT, 117 FSYS_STYLE_VFAT, 118 FSYS_STYLE_WIN95 = FSYS_STYLE_VFAT, 119 FSYS_STYLE_HPFS, 120 FSYS_STYLE_OS2 = FSYS_STYLE_HPFS, 121 FSYS_STYLE_NTFS, 122 FSYS_STYLE_NWFS, 123 FSYS_STYLE_SYSV, 124 FSYS_STYLE_BSD, 125 FSYS_STYLE_UNX = FSYS_STYLE_BSD, 126 FSYS_STYLE_MAC, 127 FSYS_STYLE_DETECT, 128 FSYS_STYLE_UNKNOWN, 129 FSYS_STYLE_URL 130 }; 131 132 // FSysAction 133 typedef int FSysAction; 134 #define FSYS_ACTION_COPYFILE 0x01 // not only create hardlink 135 #define FSYS_ACTION_RECURSIVE 0x02 // deep through dircetory structure 136 #define FSYS_ACTION_USERECYCLEBIN 0x04 // move to recycle bin 137 #define FSYS_ACTION_MOVE 0x08 // delete after copy (=> move) 138 #define FSYS_ACTION_CONTINUE 0x10 // continue on error 139 #define FSYS_ACTION_KEEP_EXISTING 0x20 // do not overwrite objects in 140 // target folder in case of name 141 // clashes 142 #define FSYS_ACTION_STANDARD 0 143 144 // Fuer RFS 145 #define RFS_IDENTIFIER "-rfs-" 146 #define RFS_LOWER "-rfs-" 147 #define RFS_UPPER "-RFS-" 148 149 typedef sal_uIntPtr FSysError; 150 151 // FSysExact 152 enum FSysExact 153 { 154 FSYS_NOTEXACT, 155 FSYS_EXACT 156 }; 157 158 // ------------ 159 // - Char-Set - 160 // ------------ 161 162 // Was different for WIN, no longer needed... 163 #define GUI2FSYS(s) s 164 #define FSYS2GUI(s) s 165 166 167 // ------------ 168 // - FileStat - 169 // ------------ 170 171 struct dirent; 172 class TOOLS_DLLPUBLIC FileStat 173 { 174 friend class CORmFSys; 175 friend class Dir; 176 friend struct DirReader_Impl; 177 friend void ImpInitFileStat( FileStat&, dirent* ); 178 179 sal_uIntPtr nError; 180 DirEntryKind nKindFlags; 181 sal_uIntPtr nSize; 182 String aCreator; 183 String aType; 184 Date aDateCreated; 185 Time aTimeCreated; 186 Date aDateModified; 187 Time aTimeModified; 188 Date aDateAccessed; 189 Time aTimeAccessed; 190 private: 191 TOOLS_DLLPRIVATE void ImpInit( void* ); 192 193 protected: 194 // Implementation 195 FileStat( const void *pInfo, // CInfoPBRec 196 const void *pVolInfo ); // ParamBlockRec 197 198 public: 199 FileStat(); 200 FileStat( const DirEntry& rDirEntry, 201 FSysAccess nAccess = FSYS_ACCESS_FLOPPY ); 202 sal_Bool Update( const DirEntry& rDirEntry, 203 sal_Bool bForceAccess = sal_True ); 204 205 sal_uIntPtr GetError() const { return ERRCODE_TOERROR(nError); } 206 sal_uIntPtr GetErrorCode() const { return nError; } 207 208 sal_uIntPtr GetSize() const { return nSize; } 209 210 DirEntryKind GetKind() const { return nKindFlags; } 211 sal_Bool IsKind( DirEntryKind nKind ) const; 212 213 String GetType() const { return aType; } 214 String GetCreator() const { return aCreator; } 215 216 Date DateCreated() const { return aDateCreated; } 217 Time TimeCreated() const { return aTimeCreated; } 218 Date DateModified() const { return aDateModified; } 219 Time TimeModified() const { return aTimeModified; } 220 Date DateAccessed() const { return aDateAccessed; } 221 Time TimeAccessed() const { return aTimeAccessed; } 222 sal_Bool IsYounger( const FileStat& rIsOlder ) const; 223 224 #define TF_FSYS_READONLY_FLAG 225 static sal_uIntPtr SetReadOnlyFlag( const DirEntry &rEntry, sal_Bool bRO = sal_True ); 226 static sal_Bool GetReadOnlyFlag( const DirEntry &rEntry ); 227 static sal_Bool HasReadOnlyFlag(); 228 229 static ErrCode QueryDiskSpace( const String &rPath, 230 BigInt &rFreeBytes, BigInt &rTotalBytes ); 231 232 static void SetDateTime( const String& rFileName, 233 const DateTime& rNewDateTime ); 234 }; 235 236 // ------------ 237 // - DirEntry - 238 // ------------ 239 240 class DirEntryStack; 241 242 const char* ImpCheckDirEntry( const void* p ); 243 244 class TOOLS_DLLPUBLIC DirEntry 245 { 246 friend struct DirReader_Impl; 247 friend class FileCopier; 248 249 #ifdef FEAT_FSYS_DOUBLESPEED 250 FileStat* pStat; // optional 251 #endif 252 ByteString aName; 253 DirEntry* pParent; 254 sal_uIntPtr nError; 255 DirEntryFlag eFlag; 256 257 private: 258 TOOLS_DLLPRIVATE DirEntry( const ByteString& rInitName, 259 DirEntryFlag aDirFlag, 260 FSysPathStyle eStyle ); 261 262 //#if 0 // _SOLAR__PRIVATE 263 264 friend class Dir; 265 friend class FileStat; 266 friend const char* ImpCheckDirEntry( const void* p ); 267 268 TOOLS_DLLPRIVATE FSysError ImpParseName( const ByteString& rIntiName, 269 FSysPathStyle eParser ); 270 TOOLS_DLLPRIVATE FSysError ImpParseOs2Name( const ByteString& rPfad, 271 FSysPathStyle eStyle ); 272 TOOLS_DLLPRIVATE FSysError ImpParseUnixName( const ByteString& rPfad, 273 FSysPathStyle eStyle ); 274 TOOLS_DLLPRIVATE sal_uInt16 ImpTryUrl( DirEntryStack& rStack, const String& rPfad, FSysPathStyle eStyle ); 275 TOOLS_DLLPRIVATE const DirEntry* ImpGetTopPtr() const; 276 TOOLS_DLLPRIVATE DirEntry* ImpGetTopPtr(); 277 TOOLS_DLLPRIVATE DirEntry* ImpGetPreTopPtr(); 278 TOOLS_DLLPRIVATE sal_Bool ImpToRel( String aStart ); 279 280 protected: 281 void ImpTrim( FSysPathStyle eStyle ); 282 const ByteString& ImpTheName() const; 283 DirEntryFlag ImpTheFlag() const { return eFlag; }; 284 DirEntry* ImpChangeParent( DirEntry* pNewParent, sal_Bool bNormalize = sal_True ); 285 DirEntry* ImpGetParent() { return pParent; } 286 #ifdef FEAT_FSYS_DOUBLESPEED 287 FileStat* ImpGetStat() const { return pStat; } 288 void ImpSetStat( FileStat *p ) { pStat = p; } 289 #endif 290 291 //#endif 292 293 protected: 294 void SetError( sal_uIntPtr nErr ) { nError = nErr; } 295 DirEntry* GetParent() { return pParent; } 296 public: 297 DirEntry( DirEntryFlag aDirFlag = FSYS_FLAG_CURRENT ); 298 DirEntry( const DirEntry& rEntry ); 299 DirEntry( const ByteString& rInitName, 300 FSysPathStyle eParser = FSYS_STYLE_HOST ); 301 DirEntry( const String& rInitName, 302 FSysPathStyle eParser = FSYS_STYLE_HOST ); 303 ~DirEntry(); 304 305 sal_Bool IsLongNameOnFAT() const; 306 sal_Bool IsCaseSensitive (FSysPathStyle eFormatter = FSYS_STYLE_HOST) const; 307 308 sal_uIntPtr GetError() const { return nError; } 309 sal_Bool IsValid() const; 310 DirEntryFlag GetFlag() const { return eFlag; }; 311 312 void SetExtension( const String& rExt, char cSep = '.' ); 313 String GetExtension( char cSep = '.' ) const; 314 String CutExtension( char cSep = '.' ); 315 void SetName( const String& rName, FSysPathStyle eFormatter = FSYS_STYLE_HOST ); 316 inline const String GetNameDirect() const { return String(aName, osl_getThreadTextEncoding()); } 317 String GetName( FSysPathStyle eFormatter = FSYS_STYLE_HOST ) const; 318 String CutName( FSysPathStyle eFormatter = FSYS_STYLE_HOST ); 319 void SetBase( const String& rBase, char cSep = '.' ); 320 String GetBase(char cSep = '.' ) const; 321 DirEntry GetPath() const; 322 DirEntry GetDevice() const; 323 String GetVolume() const; 324 String GetFull( FSysPathStyle eFormatter = FSYS_STYLE_HOST, 325 sal_Bool bWithDelimiter = sal_False, 326 sal_uInt16 nMaxChars = STRING_MAXLEN ) const; 327 328 DirEntry TempName( DirEntryKind = FSYS_KIND_NONE ) const; 329 static const DirEntry& SetTempNameBase( const String &rBaseName ); 330 sal_Bool MakeShortName( const String& rLongName, 331 DirEntryKind eCreateKind = FSYS_KIND_NONE, 332 sal_Bool bUseTilde = sal_True, 333 FSysPathStyle eStyle = FSYS_STYLE_DETECT ); 334 335 bool IsAbs() const; 336 sal_Bool ToAbs(); 337 sal_Bool Find( const String& rPfad, char cDelim = 0 ); 338 sal_Bool ToRel(); 339 sal_Bool ToRel( const DirEntry& rRefDir ); 340 sal_uInt16 CutRelParents(); 341 342 sal_Bool SetCWD( sal_Bool bSloppy = sal_False ) const; 343 sal_Bool MakeDir( sal_Bool bSloppy = sal_False ) const; 344 sal_Bool Exists( FSysAccess nAccess = FSYS_ACCESS_FLOPPY ) const; 345 sal_Bool First(); 346 347 sal_uInt16 Level() const; 348 const DirEntry& operator []( sal_uInt16 nParentLevel ) const; 349 sal_Bool Contains( const DirEntry &rSubEntry ) const; 350 351 FSysError CopyTo( const DirEntry& rDestDir, 352 FSysAction nActions = FSYS_ACTION_STANDARD ) const; 353 FSysError MoveTo( const DirEntry& rDestDir ) const; 354 FSysError Kill( FSysAction nActions = FSYS_ACTION_STANDARD ) const; 355 356 DirEntry& operator =( const DirEntry& rOrigDir ); 357 DirEntry operator +( const DirEntry& rSubDir ) const; 358 DirEntry& operator +=( const DirEntry& rSubDir ); 359 sal_Bool operator ==( const DirEntry& rAnotherDir ) const; 360 sal_Bool operator !=( const DirEntry& rAnotherDir ) const 361 { return !(DirEntry::operator==( rAnotherDir )); } 362 363 StringCompare NameCompare( const DirEntry &rWith ) const; 364 inline StringCompare NameCompareDirect( const DirEntry &rWith ) const 365 { 366 #ifdef UNX 367 return rWith.aName.CompareTo( aName ); 368 #else 369 return rWith.aName.CompareIgnoreCaseToAscii( aName ); 370 #endif 371 } 372 373 static String GetAccessDelimiter( FSysPathStyle eFormatter = FSYS_STYLE_HOST ); 374 static String GetSearchDelimiter( FSysPathStyle eFormatter = FSYS_STYLE_HOST ); 375 static sal_uInt16 GetMaxNameLen( FSysPathStyle eFormatter = FSYS_STYLE_HOST ); 376 static FSysPathStyle GetPathStyle( const String &rDevice ); 377 static String ConvertNameToSystem( const String & rName ); 378 static String ConvertSystemToName( const String & rName ); 379 static sal_Bool IsRFSAvailable(); 380 }; 381 382 // -------------- 383 // - FileCopier - 384 // -------------- 385 386 class TOOLS_DLLPUBLIC FileCopier 387 { 388 DirEntry aSource; 389 DirEntry aTarget; 390 sal_uIntPtr nBytesTotal; 391 sal_uIntPtr nBytesCopied; 392 Link aProgressLink; 393 sal_uInt16 nBlockSize; 394 FileCopier_Impl* pImp; 395 396 private: 397 TOOLS_DLLPRIVATE FSysError DoCopy_Impl( 398 const DirEntry &rSource, const DirEntry &rTarget ); 399 400 protected: 401 virtual sal_Bool Progress(); 402 virtual ErrCode Error( ErrCode eErr, 403 const DirEntry *pSource, const DirEntry *pTarget ); 404 405 public: 406 FileCopier(); 407 FileCopier( const DirEntry &rSource, 408 const DirEntry &rTarget ); 409 FileCopier( const FileCopier &rCopier ); 410 virtual ~FileCopier(); 411 412 FileCopier& operator = ( const FileCopier &rCopier ); 413 414 void SetBlockSize( sal_uInt16 nBytes ) { nBlockSize = nBytes; } 415 sal_uInt16 GetBlockSize() const { return nBlockSize; } 416 417 sal_uIntPtr GetBytesTotal() const { return nBytesTotal; } 418 sal_uIntPtr GetBytesCopied() const { return nBytesCopied; } 419 420 void SetSource( const DirEntry &rSource ) { aSource = rSource; } 421 void SetTarget( const DirEntry &rTarget ) { aTarget = rTarget; } 422 const DirEntry& GetSource() const { return aSource; } 423 const DirEntry& GetTarget() const { return aTarget; } 424 425 FSysError Execute( FSysAction nActions = FSYS_ACTION_STANDARD ); 426 FSysError ExecuteExact( FSysAction nActions = FSYS_ACTION_STANDARD, 427 FSysExact eExact = FSYS_NOTEXACT); 428 429 const DirEntry* GetErrorSource() const; 430 const DirEntry* GetErrorTarget() const; 431 ErrCode GetError() const; 432 433 void SetProgressHdl( const Link& rLink ) { aProgressLink = rLink; } 434 const Link& GetProgressHdl() const { return aProgressLink; } 435 void SetErrorHdl( const Link& rLink ); 436 const Link& GetErrorHdl() const; 437 }; 438 439 // ------- 440 // - Dir - 441 // ------- 442 443 struct DirReader_Impl; 444 class TOOLS_DLLPUBLIC Dir : public DirEntry 445 { 446 friend struct DirReader_Impl; 447 friend class CORmFSys; 448 449 DirReader_Impl* pReader; // systemabhaengig 450 DirEntryList* pLst; 451 FSysSortList* pSortLst; // NULL, wenn kein Sort gefordert 452 FileStatList* pStatLst; // NULL, wenn keine Stat's benoetigt 453 WildCard aNameMask; 454 DirEntryKind eAttrMask; 455 456 private: 457 TOOLS_DLLPRIVATE Dir( const Dir& ); // not allowed 458 TOOLS_DLLPRIVATE Dir& operator=( const Dir& ); // not allowed 459 460 #ifdef _DIR_CXX 461 TOOLS_DLLPRIVATE FSysError ImpSetSort( std::va_list pArgs, FSysSort nSort ); 462 TOOLS_DLLPRIVATE void Construct( DirEntryKind nKind = FSYS_KIND_DIR|FSYS_KIND_FILE ); 463 #endif 464 465 #ifndef _TOOLS_HXX 466 protected: 467 sal_Bool ImpInsertPointReached( const DirEntry& rIsSmaller, 468 const FileStat& rNewStat, 469 sal_uIntPtr nCurPos, 470 sal_uIntPtr nSortIndex ) const; 471 void ImpSortedInsert( const DirEntry *pNewEntry, 472 const FileStat *pNewStat ); 473 #endif 474 475 public: 476 Dir(); 477 Dir( const DirEntry& rDirEntry, 478 DirEntryKind nKind = FSYS_KIND_ALL ); 479 Dir( const DirEntry& rDirEntry, 480 DirEntryKind nKind, 481 FSysSort nSort, ... ); 482 ~Dir(); 483 484 const WildCard& GetNameMask() const { return aNameMask; } 485 486 FSysError SetSort( FSysSort nSort, ... ); 487 488 void Reset(); 489 sal_uInt16 Scan( sal_uInt16 nCount = 5 ); 490 sal_uInt16 Count( sal_Bool bUpdated = sal_True ) const; 491 sal_Bool Update(); 492 493 Dir& operator +=( const Dir& rDir ); 494 DirEntry& operator []( sal_uInt16 nIndex ) const; 495 }; 496 497 // we don't need this stuff for bootstraping 498 #ifndef BOOTSTRAP 499 500 //======================================================================== 501 502 /** FSysRedirector is an abstract base class for a hook to redirect 503 mirrored directory trees. 504 505 <P>One instance of a subclass can be instanciated and registered 506 using the method FSysRedirector::Register(FSysRedirector*). 507 */ 508 509 class FSysRedirector 510 { 511 static FSysRedirector* _pRedirector; 512 static sal_Bool _bEnabled; 513 514 public: 515 /** This method must called with the one and only instance of the 516 subclass which implements the redirection. 517 518 <P>It must be called with 0 when the instance is destroyed. 519 */ 520 static void Register( FSysRedirector *pRedirector ); 521 522 //----------------------------------------------------------------------- 523 /** This method returns the currently registererd instance of 524 a subclass which implements the redirection. 525 526 <P>If no redirector is registered, it returns 0. 527 */ 528 static FSysRedirector* Redirector(); 529 530 //----------------------------------------------------------------------- 531 /** This method is to be used to redirect a file system path. 532 533 <P>It will not redirect while redirection is disabled. 534 535 <P>It may block while another thread is accessing the redirector 536 or another thread has disabled redirections. 537 538 @param String &rPath<BR> 539 This inout-argument accepts a file:-URL even as a native 540 file system path name to redirect in 'rPath'. It returns the 541 redirected (modified) path too, which can be of both formats 542 too. 543 544 @return sal_Bool<BR> 545 sal_True, if the path is redirected 546 sal_False, if the path is not redirected (unchanged) 547 */ 548 static void DoRedirect( String &rPath ); 549 }; 550 551 #endif // BOOTSTRP 552 553 //======================================================================== 554 555 void FSysEnableSysErrorBox( sal_Bool bEnable ); 556 557 //======================================================================== 558 559 #if defined(DBG_UTIL) 560 void FSysTest(); 561 #endif 562 563 #endif // #ifndef _FSYS_HXX 564 565