1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 //------------------------------------------------------------------------
17 //------------------------------------------------------------------------
22 #include <OSD_File.hxx>
23 #include <OSD_FromWhere.hxx>
24 #include <OSD_OSDError.hxx>
25 #include <OSD_Path.hxx>
26 #include <OSD_Printer.hxx>
27 #include <OSD_Protection.hxx>
28 #include <OSD_WhoAmI.hxx>
29 #include <Standard_PCharacter.hxx>
30 #include <Standard_ProgramError.hxx>
31 #include <TCollection_AsciiString.hxx>
33 const OSD_WhoAmI Iam = OSD_WFile;
35 #if defined (sun) || defined(SOLARIS)
48 #define NEWLINE '\10';
50 // ---------------------------------------------------------------------
51 // Create an empty file object
52 // ---------------------------------------------------------------------
54 OSD_File::OSD_File():OSD_FileNode()
56 ImperativeFlag = Standard_False;
59 myMode = OSD_ReadWrite;
60 myFILE = (Standard_Address) NULL;
65 // ---------------------------------------------------------------------
66 // Create and initialize a file object
67 // ---------------------------------------------------------------------
69 OSD_File::OSD_File(const OSD_Path& Name):OSD_FileNode(Name)
71 ImperativeFlag = Standard_False;
74 myMode = OSD_ReadWrite;
75 myFILE = (Standard_Address) NULL;
80 // protect against occasional use of myFileHande in Linux code
81 #define myFileHandle myFileHandle_is_only_for_Windows
83 // ---------------------------------------------------------------------
84 // Build a file if it doesn't exist or create again if it already exists
85 // ---------------------------------------------------------------------
87 void OSD_File::Build(const OSD_OpenMode Mode,
88 const OSD_Protection& Protect){
90 Standard_Integer internal_prot;
91 Standard_Integer internal_mode = O_CREAT | O_TRUNC ;
92 TCollection_AsciiString aBuffer;
94 if (myPath.Name().Length()==0)
95 Standard_ProgramError::Raise("OSD_File::Build : no name was given");
97 if (myFileChannel != -1)
98 Standard_ProgramError::Raise("OSD_File::Build : file is already open");
103 internal_prot = Protect.Internal();
105 const char* CMode = "r";
109 internal_mode |= O_RDONLY;
113 internal_mode |= O_WRONLY;
117 internal_mode |= O_RDWR;
122 myPath.SystemName( aBuffer );
123 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
124 if (myFileChannel >= 0) {
125 myFILE = fdopen (myFileChannel, CMode);
128 /* Handle OPEN errors */
130 myError.SetValue (errno, Iam, "Open");
136 // ---------------------------------------------------------------------
137 // Append to an existing file
138 // ---------------------------------------------------------------------
140 void OSD_File::Append(const OSD_OpenMode Mode,
141 const OSD_Protection& Protect){
143 Standard_Integer internal_prot;
144 Standard_Integer internal_mode = O_APPEND;;
145 TCollection_AsciiString aBuffer;
147 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
148 Standard_ProgramError::Raise("OSD_File::Append : it is a directory");
151 if (myPath.Name().Length()==0)
152 Standard_ProgramError::Raise("OSD_File::Append : no name was given");
154 if (myFileChannel != -1)
155 Standard_ProgramError::Raise("OSD_File::Append : file is already open");
157 internal_prot = Protect.Internal();
159 const char* CMode = "r";
163 internal_mode |= O_RDONLY;
167 internal_mode |= O_WRONLY;
171 internal_mode |= O_RDWR;
176 // If file doesn't exist, creates it.
178 if (!Exists()) internal_mode |= O_CREAT;
180 myPath.SystemName ( aBuffer );
181 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
182 if (myFileChannel >= 0)
183 myFILE = fdopen (myFileChannel, CMode);
185 /* Handle OPEN errors */
187 myError.SetValue (errno, Iam, "Open");
190 // ---------------------------------------------------------------------
192 // ---------------------------------------------------------------------
194 void OSD_File::Open(const OSD_OpenMode Mode,
195 const OSD_Protection& Protect){
197 Standard_Integer internal_prot;
198 Standard_Integer internal_mode = 0;
199 TCollection_AsciiString aBuffer;
201 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
202 myError.SetValue (1, Iam, "Could not be open : it is a directory");
205 if (myPath.Name().Length()==0)
206 Standard_ProgramError::Raise("OSD_File::Open : no name was given");
208 if (myFileChannel != -1)
209 Standard_ProgramError::Raise("OSD_File::Open : file is already open");
211 internal_prot = Protect.Internal();
213 const char* CMode = "r";
217 internal_mode |= O_RDONLY;
221 internal_mode |= O_WRONLY;
225 internal_mode |= O_RDWR;
230 myPath.SystemName ( aBuffer );
231 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
232 if (myFileChannel >= 0)
233 myFILE = fdopen (myFileChannel, CMode);
235 /* Handle OPEN errors */
237 myError.SetValue (errno, Iam, "Open");
242 // ---------------------------------------------------------------------
243 // ---------------------------------------------------------------------
244 void OSD_File::BuildTemporary(){
249 #if defined(vax) || defined(__vms) || defined(VAXVMS)
254 dummy = open("dummy", O_RDWR | O_CREAT); // Open a dummy file
255 myFileChannel = dummy - 1; // This is file channel of "fic" +1
256 close(dummy); // Close dummy file
257 unlink("dummy"); // Removes dummy file
260 char name[] = "/tmp/CSFXXXXXX";
261 myFileChannel = mkstemp( name );
263 TCollection_AsciiString aName ( name ) ;
264 OSD_Path aPath( aName ) ;
268 myFILE = fdopen( myFileChannel, "w+" ) ;
272 myMode = OSD_ReadWrite;
277 // ---------------------------------------------------------------------
278 // Read content of a file
279 // ---------------------------------------------------------------------
281 void OSD_File::Read(TCollection_AsciiString& Buffer,
282 const Standard_Integer Nbyte){
283 Standard_PCharacter readbuf;
286 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
287 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
290 if (myFileChannel == -1)
291 Standard_ProgramError::Raise("OSD_File::Read : file is not open");
293 if (Failed()) Perror();
295 if (myMode == OSD_WriteOnly)
296 Standard_ProgramError::Raise("OSD_File::Read : file is Write only");
299 Standard_ProgramError::Raise("OSD_File::Read : Nbyte is null");
301 TCollection_AsciiString transfert(Nbyte,' ');
302 readbuf = (Standard_PCharacter)transfert.ToCString();
304 status = read (myFileChannel, readbuf, Nbyte);
306 Buffer = transfert; // Copy transfert to Buffer
308 if (status == -1) myError.SetValue (errno, Iam, "Read");
310 if ( status < Nbyte ) myIO = EOF;
314 // ---------------------------------------------------------------------
315 // Read a line from a file
316 // ---------------------------------------------------------------------
318 void OSD_File::ReadLine(TCollection_AsciiString& Buffer,
319 const Standard_Integer Nbyte,
320 Standard_Integer& NByteRead)
322 Standard_PCharacter readbuf, abuffer;
324 if (OSD_File::KindOfFile() == OSD_DIRECTORY ) {
325 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
327 if (myFileChannel == -1){
328 Standard_ProgramError::Raise("OSD_File::ReadLine : file is not open");
333 if (myMode == OSD_WriteOnly) {
334 Standard_ProgramError::Raise("OSD_File::ReadLine : file is Write only");
337 Standard_ProgramError::Raise("OSD_File::ReadLine : Nbyte is null");
340 TCollection_AsciiString transfert(Nbyte,' ');
341 readbuf = (Standard_PCharacter)transfert.ToCString();
343 abuffer = fgets(readbuf, Nbyte, (FILE *) myFILE);
345 if (abuffer == NULL) {
346 if (!feof((FILE *) myFILE)) {
347 myError.SetValue (errno, Iam, "ReadLine");
357 NByteRead = (Standard_Integer)strlen(abuffer);
358 Buffer.SetValue(1,abuffer); // Copy transfert to Buffer
359 Buffer.Trunc(NByteRead);
362 // --------------------------------------------------------------------------
363 // OSD::KindOfFile Retourne le type de fichier.
364 // --------------------------------------------------------------------------
365 OSD_KindFile OSD_File::KindOfFile ( ) const{
368 TCollection_AsciiString FullName;
372 aPath.SystemName (FullName);
373 status = stat (FullName.ToCString() , &buffer );
375 if ( S_ISDIR(buffer.st_mode) ) { return OSD_DIRECTORY ; }
376 else if ( S_ISREG(buffer.st_mode) ) { return OSD_FILE ; }
377 else if ( S_ISLNK(buffer.st_mode) ) { return OSD_LINK ; }
378 else if ( S_ISSOCK(buffer.st_mode) ) { return OSD_SOCKET ; }
379 else { return OSD_UNKNOWN ; }
384 // --------------------------------------------------------------------------
385 // Read content of a file
386 // --------------------------------------------------------------------------
387 void OSD_File::Read(const Standard_Address Buffer,
388 const Standard_Integer Nbyte,
389 Standard_Integer& Readbyte)
394 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
395 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
398 if (myFileChannel == -1)
399 Standard_ProgramError::Raise("OSD_File::Read : file is not open");
401 if (Failed()) Perror();
403 if (myMode == OSD_WriteOnly)
404 Standard_ProgramError::Raise("OSD_File::Read : file is Write only");
407 Standard_ProgramError::Raise("OSD_File::Read : Nbyte is null");
410 Standard_ProgramError::Raise("OSD_File::Read : Buffer is null");
412 status = read (myFileChannel, (char*) Buffer, Nbyte);
414 if (status == -1) myError.SetValue (errno, Iam, "Read");
416 if ( status < Nbyte ) myIO = EOF;
421 // Write content of a file
423 void OSD_File::Write(const TCollection_AsciiString &Buffer,
424 const Standard_Integer Nbyte){
426 Standard_CString writebuf;
429 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
430 Standard_ProgramError::Raise("OSD_File::Write : it is a directory");
433 if (myFileChannel == -1)
434 Standard_ProgramError::Raise("OSD_File::Write : file is not open");
436 if (Failed()) Perror();
438 if (myMode == OSD_ReadOnly)
439 Standard_ProgramError::Raise("OSD_File::Write : file is Read only");
442 Standard_ProgramError::Raise("OSD_File::Write : Nbyte is null");
444 writebuf = Buffer.ToCString();
446 status = write (myFileChannel, writebuf, Nbyte);
448 if ( status == -1) myError.SetValue (errno, Iam, "Write");
450 if ( status < Nbyte ) myIO = EOF;
454 void OSD_File::Write(const Standard_Address Buffer,
455 const Standard_Integer Nbyte)
460 if (myFileChannel == -1)
461 Standard_ProgramError::Raise("OSD_File::Write : file is not open");
463 if (Failed()) Perror();
465 if (myMode == OSD_ReadOnly)
466 Standard_ProgramError::Raise("OSD_File::Write : file is Read only");
469 Standard_ProgramError::Raise("OSD_File::Write : Nbyte is null");
471 status = write (myFileChannel, (const char *)Buffer, Nbyte);
473 if ( status == -1) myError.SetValue (errno, Iam, "Write");
475 if ( status < Nbyte ) myIO = EOF;
482 // Move file pointer to a specified position
484 void OSD_File::Seek(const Standard_Integer Offset,
485 const OSD_FromWhere Whence){
488 if (myFileChannel == -1)
489 Standard_ProgramError::Raise("OSD_File::Seek : file is not open");
491 if (Failed()) Perror();
494 case OSD_FromBeginning :
504 myError.SetValue (EINVAL, Iam, "Seek");
507 off_t status = lseek (myFileChannel, Offset, iwhere);
508 if (status == -1) myError.SetValue (errno, Iam, "Seek");
518 void OSD_File::Close(){
521 if (myFileChannel == -1)
522 Standard_ProgramError::Raise("OSD_File::Close : file is not open");
524 if (Failed()) Perror();
526 // note: it probably should be single call to fclose()...
527 status = close (myFileChannel);
529 if (status == -1) myError.SetValue (errno, Iam, "Close");
531 if ( myFILE != NULL ) {
532 status = fclose ( (FILE*) myFILE );
541 // --------------------------------------------------------------------------
542 // Test if at end of file
543 // --------------------------------------------------------------------------
545 Standard_Boolean OSD_File::IsAtEnd(){
546 if (myFileChannel == -1)
547 Standard_ProgramError::Raise("OSD_File::IsAtEnd : file is not open");
549 if (myIO == EOF) return (Standard_True);
550 return (Standard_False);
555 /*void OSD_File::Link(const TCollection_AsciiString& ToFile){
556 if (myFileChannel == -1)
557 Standard_ProgramError::Raise("OSD_File::Link : file is not open");
559 TCollection_AsciiString aBuffer;
560 myPath.SystemName ( aBuffer );
561 link ( aBuffer.ToCString(), ToFile.ToCString() );
567 void OSD_File::SetLock(const OSD_LockType Lock){
570 if (myFileChannel == -1)
571 Standard_ProgramError::Raise("OSD_File::SetLock : file is not open");
579 case OSD_ExclusiveLock :
580 case OSD_WriteLock : lock = F_LOCK;
582 case OSD_ReadLock : return;
584 default : myError.SetValue (EINVAL, Iam, "SetLock");
588 if (fstat (myFileChannel, &buf) == -1) {
589 myError.SetValue (errno, Iam, "SetLock");
593 status = lockf(myFileChannel, lock, buf.st_size);
594 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
603 case OSD_ExclusiveLock :
604 case OSD_WriteLock : key.l_type = F_WRLCK;
606 case OSD_ReadLock : key.l_type = F_RDLCK;
608 case OSD_NoLock : return;
609 // default : myError.SetValue (EINVAL, Iam, "SetLock");
616 status = fcntl (myFileChannel, F_SETLKW, &key);
617 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
620 if (Lock == OSD_ExclusiveLock){
621 fstat (myFileChannel, &buf);
622 TCollection_AsciiString aBuffer;
623 myPath.SystemName ( aBuffer );
624 chmod( aBuffer.ToCString() ,buf.st_mode | S_ISGID);
625 ImperativeFlag = Standard_True;
632 case OSD_ExclusiveLock :
633 case OSD_WriteLock : lock = F_WRLCK;
635 case OSD_ReadLock : lock = F_RDLCK;
637 default : myError.SetValue (EINVAL, Iam, "SetLock");
640 status = flock (myFileChannel, lock);
641 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
650 // Remove a lock from a file
652 void OSD_File::UnLock(){
655 if (myFileChannel == -1)
656 Standard_ProgramError::Raise("OSD_File::UnLock : file is not open");
661 if (fstat(myFileChannel, &buf) == -1) {
662 myError.SetValue(errno, Iam, "UnsetLock");
666 status = lockf(myFileChannel,F_ULOCK, buf.st_size);
667 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
675 fstat (myFileChannel, &buf);
676 TCollection_AsciiString aBuffer;
677 myPath.SystemName ( aBuffer );
678 chmod(aBuffer.ToCString(),buf.st_mode & ~S_ISGID);
679 ImperativeFlag = Standard_False;
681 key.l_type = F_UNLCK;
682 status = fcntl (myFileChannel, F_SETLK, &key);
683 if (status == -1) myError.SetValue (errno, Iam, "UnSetLock");
686 status = flock (myFileChannel, LOCK_UN);
687 if (status == -1) myError.SetValue (errno, Iam, "UnSetLock");
690 else myLock = OSD_NoLock;
697 // Return lock of a file
699 OSD_LockType OSD_File::GetLock(){
706 // --------------------------------------------------------------------------
707 // Return size of a file
708 // --------------------------------------------------------------------------
710 Standard_Size OSD_File::Size(){
714 if (myPath.Name().Length()==0)
715 Standard_ProgramError::Raise("OSD_File::Size : empty file name");
717 TCollection_AsciiString aBuffer;
718 myPath.SystemName ( aBuffer );
719 status = stat( aBuffer.ToCString() ,&buffer );
721 myError.SetValue (errno, Iam, "Size");
725 return (Standard_Size)buffer.st_size;
729 // --------------------------------------------------------------------------
730 // Print contains of a file
731 // --------------------------------------------------------------------------
733 void OSD_File::Print (const OSD_Printer &WhichPrinter ){
735 TCollection_AsciiString PrinterName;
737 if (myPath.Name().Length()==0)
738 Standard_ProgramError::Raise("OSD_File::Print : empty file name");
740 WhichPrinter.Name(PrinterName);
742 TCollection_AsciiString aBuffer;
743 myPath.SystemName ( aBuffer );
745 if (PrinterName.Length()==0)
746 sprintf(buffer,"lp %s",aBuffer.ToCString());
748 sprintf(buffer,"lpr -P%s %s",PrinterName.ToCString(),aBuffer.ToCString());
750 if (system(buffer) != 0)
751 Standard_ProgramError::Raise("OSD_File::Print : No output device was available, or an error occurred");
755 // --------------------------------------------------------------------------
756 // Test if a file is open
757 // --------------------------------------------------------------------------
759 Standard_Boolean OSD_File::IsOpen()const{
760 return (myFileChannel != -1);
764 Standard_Boolean OSD_File::IsLocked(){
765 return(myLock != OSD_NoLock);
768 Standard_Boolean OSD_File::IsReadable()
770 TCollection_AsciiString FileName ;
772 myPath.SystemName(FileName) ;
774 if (access(FileName.ToCString(),F_OK|R_OK))
775 return Standard_False;
777 return Standard_True;
780 Standard_Boolean OSD_File::IsWriteable()
782 TCollection_AsciiString FileName ;
784 myPath.SystemName(FileName) ;
786 if (access(FileName.ToCString(),F_OK|R_OK|W_OK))
787 return Standard_False;
789 return Standard_True;
792 Standard_Boolean OSD_File::IsExecutable()
794 TCollection_AsciiString FileName ;
796 myPath.SystemName(FileName) ;
798 if (access(FileName.ToCString(),F_OK|X_OK))
799 return Standard_False;
801 return Standard_True;
804 int OSD_File::Capture(int theDescr) {
805 // Duplicate an old file descriptor of the given one to be able to restore output to it later.
806 int oldDescr = dup(theDescr);
807 // Redirect the output to this file
808 dup2(myFileChannel, theDescr);
810 // Return the old descriptor
814 void OSD_File::Rewind() {
815 rewind((FILE*)myFILE);
820 //------------------------------------------------------------------------
821 //------------------- Windows NT sources for OSD_File -------------------
822 //------------------------------------------------------------------------
826 #include <OSD_File.hxx>
827 #include <OSD_Protection.hxx>
828 #include <OSD_Printer.hxx>
829 #include <Standard_ProgramError.hxx>
831 #include <OSD_WNT_1.hxx>
835 #include <Standard_PCharacter.hxx>
836 #include <TCollection_ExtendedString.hxx>
842 #if defined(__CYGWIN32__) || defined(__MINGW32__)
846 #if defined(_MSC_VER)
847 #pragma comment( lib, "WSOCK32.LIB" )
848 #pragma comment( lib, "WINSPOOL.LIB" )
851 #define ACE_HEADER_SIZE ( sizeof ( ACCESS_ALLOWED_ACE ) - sizeof ( DWORD ) )
853 #define RAISE( arg ) Standard_ProgramError :: Raise ( ( arg ) )
854 #define TEST_RAISE( arg ) _test_raise ( myFileHandle, ( arg ) )
858 #define OPEN_APPEND 2
860 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
861 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, wchar_t* = NULL );
862 BOOL __fastcall _osd_wnt_sd_to_protection (
863 PSECURITY_DESCRIPTOR, OSD_Protection&, BOOL
865 BOOL __fastcall _osd_print (const Standard_PCharacter, const wchar_t* );
867 static void __fastcall _test_raise ( HANDLE, Standard_CString );
868 static Standard_Integer __fastcall _get_line (Standard_PCharacter& buffer, DWORD dwBuffSize, LONG& theSeekPos);
869 static int __fastcall _get_buffer ( HANDLE, Standard_PCharacter&, DWORD, BOOL, BOOL );
870 static DWORD __fastcall _get_access_mask ( OSD_SingleProtection );
871 static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt );
872 static HANDLE __fastcall _open_file ( Standard_CString, OSD_OpenMode, DWORD, LPBOOL = NULL );
874 static OSD_SingleProtection __fastcall _get_protection ( DWORD );
875 static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD );
877 typedef OSD_SingleProtection ( __fastcall *GET_PROT_FUNC ) ( DWORD );
879 Standard_Integer __fastcall _get_file_type ( Standard_CString, HANDLE );
881 // ---------------------------------------------------------------------
882 // Create an empty file object
883 // ---------------------------------------------------------------------
885 OSD_File :: OSD_File ()
887 ImperativeFlag = Standard_False;
891 myFileHandle = INVALID_HANDLE_VALUE;
892 } // end constructor ( 1 )
894 // ---------------------------------------------------------------------
895 // Create and initialize a file object
896 // ---------------------------------------------------------------------
898 OSD_File :: OSD_File ( const OSD_Path& Name ) : OSD_FileNode ( Name )
900 ImperativeFlag = Standard_False;
905 myFileHandle = INVALID_HANDLE_VALUE;
906 } // end constructor ( 2 )
908 // ---------------------------------------------------------------------
909 // Redirect a standard handle (fileno(stdout), fileno(stdin) or
910 // fileno(stderr) to this OSD_File and return the copy of the original
914 // aTmp.BuildTemporary();
915 // int stdfd = _fileno(stdout);
917 // int oldout = aTmp.Capture(stdfd);
918 // cout << "Some output to the file" << endl;
922 // _dup2(oldout, stdfd); // Restore standard output
924 // ---------------------------------------------------------------------
925 int OSD_File::Capture(int theDescr) {
926 // Get POSIX file descriptor from this file handle
927 int dFile = _open_osfhandle(reinterpret_cast<intptr_t>(myFileHandle), myMode);
931 _osd_wnt_set_error ( myError, OSD_WFile, myFileHandle );
935 // Duplicate an old file descriptor of the given one to be able to restore output to it later.
936 int oldDescr = _dup(theDescr);
937 // Redirect the output to this file
938 _dup2(dFile, theDescr);
940 // Return the old descriptor
944 void OSD_File::Rewind() {
945 SetFilePointer( myFileHandle, 0, NULL, FILE_BEGIN );
948 // protect against occasional use of myFileHande in Windows code
949 #define myFileChannel myFileChannel_is_only_for_Linux
951 // ---------------------------------------------------------------------
952 // Build a file if it doesn't exist or create again if it already exists
953 // ---------------------------------------------------------------------
955 void OSD_File :: Build (
956 const OSD_OpenMode Mode, const OSD_Protection& Protect
959 TCollection_AsciiString fName;
961 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
962 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
965 if (myFileHandle != INVALID_HANDLE_VALUE)
967 RAISE( "OSD_File :: Build (): incorrect call - file already opened" );
970 myPath.SystemName ( fName );
972 if ( fName.IsEmpty () )
974 RAISE( "OSD_File :: Build (): incorrent call - no filename given" );
976 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_NEW );
978 if (myFileHandle == INVALID_HANDLE_VALUE)
980 _osd_wnt_set_error ( myError, OSD_WFile );
984 SetProtection ( Protect );
989 } // end OSD_File :: Build
993 // ---------------------------------------------------------------------
995 // ---------------------------------------------------------------------
997 void OSD_File :: Open (const OSD_OpenMode Mode, const OSD_Protection& /*Protect*/)
1000 TCollection_AsciiString fName;
1003 if (myFileHandle != INVALID_HANDLE_VALUE)
1005 RAISE( "OSD_File :: Open (): incorrect call - file already opened" );
1008 myPath.SystemName ( fName );
1010 if ( fName.IsEmpty () )
1012 RAISE( "OSD_File :: Open (): incorrent call - no filename given" );
1014 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_OLD );
1016 if (myFileHandle == INVALID_HANDLE_VALUE) {
1018 _osd_wnt_set_error ( myError, OSD_WFile );
1022 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
1024 } // end OSD_File :: Open
1026 // ---------------------------------------------------------------------
1027 // Append to an existing file
1028 // ---------------------------------------------------------------------
1030 void OSD_File :: Append (
1031 const OSD_OpenMode Mode, const OSD_Protection& Protect
1035 TCollection_AsciiString fName;
1037 if (myFileHandle != INVALID_HANDLE_VALUE)
1039 RAISE( "OSD_File :: Append (): incorrect call - file already opened" );
1042 myPath.SystemName ( fName );
1044 if ( fName.IsEmpty () )
1046 RAISE( "OSD_File :: Append (): incorrent call - no filename given" );
1048 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_APPEND, &fNew );
1050 if (myFileHandle == INVALID_HANDLE_VALUE)
1052 _osd_wnt_set_error ( myError, OSD_WFile );
1058 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
1059 Seek ( 0, OSD_FromEnd );
1063 SetProtection ( Protect );
1070 } // end OSD_File :: Append
1072 // ---------------------------------------------------------------------
1073 // Read content of a file
1074 // ---------------------------------------------------------------------
1076 void OSD_File :: Read (
1077 TCollection_AsciiString& Buffer, const Standard_Integer Nbyte
1080 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1082 cout << " OSD_File::Read : it is a directory " << endl;
1085 // Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1088 Standard_Integer NbyteRead;
1090 TEST_RAISE( "Read" );
1092 char* buff = new Standard_Character[ Nbyte + 1 ];
1094 Read ( buff, Nbyte, NbyteRead );
1096 buff[ NbyteRead ] = 0;
1098 if ( NbyteRead != 0 )
1108 } // end OSD_File :: Read
1110 // ---------------------------------------------------------------------
1111 // Read a line from a file
1112 // ---------------------------------------------------------------------
1114 // Modified so that we have <nl> at end of line if we have read <nl> or <cr>
1116 // by LD 17 dec 98 for B4.4
1118 void OSD_File :: ReadLine (
1119 TCollection_AsciiString& Buffer,
1120 const Standard_Integer NByte, Standard_Integer& NbyteRead
1125 Standard_Character peekChar;
1126 Standard_PCharacter ppeekChar;
1127 Standard_PCharacter cBuffer;
1130 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1131 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1134 TEST_RAISE( "ReadLine" );
1136 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1138 RAISE( "OSD_File :: ReadLine (): attempt to read from write only pipe" );
1140 // +----> leave space for end-of-string
1141 // | plus <CR><LF> sequence
1144 ppeekChar=&peekChar;
1145 cBuffer = new Standard_Character[ NByte + 3 ];
1147 if ( myIO & FLAG_FILE ) {
1149 if (!ReadFile (myFileHandle, cBuffer, NByte, &dwBytesRead, NULL)) { // an error occured
1151 _osd_wnt_set_error ( myError, OSD_WFile );
1155 } else if ( dwBytesRead == 0 ) { // end-of-file reached
1162 myIO &= ~FLAG_EOF ; // if the file increased since last read (LD)
1163 NbyteRead = _get_line (cBuffer, dwBytesRead, aSeekPos);
1165 if ( NbyteRead == -1 ) // last character in the buffer is <CR> -
1166 { // peek next character to see if it is a <LF>
1167 if (!ReadFile (myFileHandle, ppeekChar, 1, &dwDummy, NULL)) {
1169 _osd_wnt_set_error ( myError, OSD_WFile );
1171 } else if ( dwDummy != 0 ) { // end-of-file reached ?
1173 if ( peekChar != '\n' ) // if we did not get a <CR><LF> sequence
1175 // adjust file position
1177 SetFilePointer (myFileHandle, -1, NULL, FILE_CURRENT);
1182 NbyteRead = dwBytesRead;
1184 } else if ( aSeekPos != 0 )
1186 SetFilePointer (myFileHandle, aSeekPos, NULL, FILE_CURRENT);
1191 } else if ( myIO & FLAG_SOCKET || myIO & FLAG_PIPE || myIO & FLAG_NAMED_PIPE ) {
1193 dwBytesRead = (DWORD)_get_buffer (myFileHandle, cBuffer,
1194 (DWORD)NByte, TRUE, myIO & FLAG_SOCKET);
1196 if ( ( int )dwBytesRead == -1 ) { // an error occured
1198 _osd_wnt_set_error ( myError, OSD_WFile );
1202 } else if ( dwBytesRead == 0 ) { // connection closed - set end-of-file flag
1210 NbyteRead = _get_line (cBuffer, dwBytesRead, aSeekPos);
1212 if (NbyteRead == -1) // last character in the buffer is <CR> -
1213 { // peek next character to see if it is a <LF>
1214 NbyteRead = dwBytesRead; // (LD) always fits this case.
1216 dwDummy = _get_buffer (myFileHandle, ppeekChar, 1, TRUE, myIO & FLAG_SOCKET);
1217 if ( ( int )dwDummy == -1 ) { // an error occured
1219 _osd_wnt_set_error ( myError, OSD_WFile );
1221 } else if ( dwDummy != 0 ) { // connection closed ?
1223 if ( peekChar == '\n' ) // we got a <CR><LF> sequence
1225 dwBytesRead++ ; // (LD) we have to jump <LF>
1230 } else if (aSeekPos != 0)
1232 dwBytesRead = dwBytesRead + aSeekPos;
1235 // Don't rewrite datas in cBuffer.
1237 Standard_PCharacter cDummyBuffer = new Standard_Character[ NByte + 3 ];
1239 // remove pending input
1240 _get_buffer (myFileHandle, cDummyBuffer, dwBytesRead, FALSE, myIO & FLAG_SOCKET);
1241 delete [] cDummyBuffer ;
1247 RAISE( "OSD_File :: ReadLine (): incorrect call - file is a directory" );
1249 if ( !Failed () && !IsAtEnd () )
1253 delete [] (Standard_PCharacter)cBuffer;
1255 } // end OSD_File :: ReadLine
1257 // --------------------------------------------------------------------------
1258 // Read content of a file
1259 // --------------------------------------------------------------------------
1261 void OSD_File :: Read (
1262 const Standard_Address Buffer,
1263 const Standard_Integer Nbyte, Standard_Integer& Readbyte
1268 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1269 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1272 TEST_RAISE( "Read" );
1274 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1276 RAISE( "OSD_File :: Read (): attempt to read from write only pipe" );
1278 if (!ReadFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesRead, NULL)) {
1280 _osd_wnt_set_error ( myError, OSD_WFile );
1283 } else if ( dwBytesRead == 0 )
1291 Readbyte = ( Standard_Integer )dwBytesRead;
1293 } // end OSD_File :: Read
1295 void OSD_File :: Write (
1296 const TCollection_AsciiString& Buffer,
1297 const Standard_Integer Nbyte
1300 Write ( ( Standard_Address )Buffer.ToCString (), Nbyte );
1302 } // end OSD_File :: Write
1304 // --------------------------------------------------------------------------
1305 // Write content of a file
1306 // --------------------------------------------------------------------------
1308 void OSD_File :: Write (
1309 const Standard_Address Buffer,
1310 const Standard_Integer Nbyte
1313 DWORD dwBytesWritten;
1315 TEST_RAISE( "Write" );
1317 if ( myIO & FLAG_PIPE && myIO & FLAG_READ_PIPE )
1319 RAISE( "OSD_File :: Write (): attempt to write to read only pipe" );
1321 if (!WriteFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesWritten, NULL) ||
1322 dwBytesWritten != (DWORD)Nbyte)
1324 _osd_wnt_set_error ( myError, OSD_WFile );
1326 } // end OSD_File :: Write
1328 void OSD_File :: Seek (
1329 const Standard_Integer Offset, const OSD_FromWhere Whence
1332 DWORD dwMoveMethod = 0;
1334 TEST_RAISE( "Seek" );
1336 if ( myIO & FLAG_FILE || myIO & FLAG_DIRECTORY ) {
1340 case OSD_FromBeginning:
1342 dwMoveMethod = FILE_BEGIN;
1348 dwMoveMethod = FILE_CURRENT;
1354 dwMoveMethod = FILE_END;
1360 RAISE( "OSD_File :: Seek (): invalid parameter" );
1364 if (SetFilePointer (myFileHandle, (LONG)Offset, NULL, dwMoveMethod) == 0xFFFFFFFF)
1366 _osd_wnt_set_error ( myError, OSD_WFile );
1372 } // end OSD_File :: Seek
1374 // --------------------------------------------------------------------------
1376 // --------------------------------------------------------------------------
1378 void OSD_File :: Close () {
1380 TEST_RAISE( "Close" );
1382 CloseHandle (myFileHandle);
1384 myFileHandle = INVALID_HANDLE_VALUE;
1387 } // end OSD_File :: Close
1389 // --------------------------------------------------------------------------
1390 // Test if at end of file
1391 // --------------------------------------------------------------------------
1393 Standard_Boolean OSD_File :: IsAtEnd () {
1395 TEST_RAISE( "IsAtEnd" );
1397 if (myIO & FLAG_EOF)
1398 return Standard_True ;
1399 return Standard_False ;
1401 } // end OSD_File :: IsAtEnd
1403 OSD_KindFile OSD_File :: KindOfFile () const {
1405 OSD_KindFile retVal;
1406 Standard_Integer flags;
1408 if (myFileHandle == INVALID_HANDLE_VALUE) {
1410 TCollection_AsciiString fName;
1412 myPath.SystemName ( fName );
1414 if ( fName.IsEmpty () )
1416 RAISE( "OSD_File :: KindOfFile (): incorrent call - no filename given" );
1418 flags = _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE);
1424 switch ( flags & FLAG_TYPE ) {
1432 case FLAG_DIRECTORY:
1434 retVal = OSD_DIRECTORY;
1440 retVal = OSD_SOCKET;
1446 retVal = OSD_UNKNOWN;
1452 } // end OSD_File :: KindOfFile
1454 //-------------------------------------------------debutpri???980424
1456 typedef struct _osd_wnt_key {
1459 const char* keyPath;
1464 void OSD_File::BuildTemporary () {
1468 TCHAR tmpPath[ MAX_PATH ];
1470 OSD_WNT_KEY regKey[ 2 ] = {
1472 { HKEY_LOCAL_MACHINE,
1473 "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
1476 ".DEFAULT\\Environment"
1481 for ( int i = 0; i < 2; ++i ) {
1484 regKey[ i ].hKey, regKey[ i ].keyPath, 0, KEY_QUERY_VALUE, &hKey
1491 if ( RegQueryValueEx (
1492 hKey, "TEMP", NULL, &dwType, NULL, &dwSize
1496 LPTSTR kVal = ( LPTSTR )HeapAlloc (
1497 GetProcessHeap (), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS,
1498 dwSize + sizeof ( TCHAR )
1501 RegQueryValueEx ( hKey, "TEMP", NULL, &dwType, ( LPBYTE )kVal, &dwSize );
1503 if ( dwType == REG_EXPAND_SZ )
1505 ExpandEnvironmentStrings ( kVal, tmpPath, MAX_PATH );
1509 lstrcpy ( tmpPath, kVal );
1511 HeapFree ( GetProcessHeap (), 0, ( LPVOID )kVal );
1516 RegCloseKey ( hKey );
1524 if ( !fOK ) lstrcpy ( tmpPath, "./" );
1526 GetTempFileName ( tmpPath, "CSF", 0, tmpPath );
1531 SetPath ( OSD_Path ( tmpPath ) );
1532 Build ( OSD_ReadWrite, prt );
1533 } // end OSD_File :: BuildTemporary
1535 //-------------------------------------------------finpri???980424
1537 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1540 #define __leave return
1543 void OSD_File :: SetLock ( const OSD_LockType Lock ) {
1548 TEST_RAISE( "SetLock" );
1550 ZeroMemory ( &ovlp, sizeof ( OVERLAPPED ) );
1554 if ( ( myLock = Lock ) == OSD_NoLock ) {
1559 } else if ( myLock == OSD_ReadLock || myLock == OSD_ExclusiveLock ) {
1561 dwFlags = LOCKFILE_EXCLUSIVE_LOCK;
1567 LARGE_INTEGER aSize;
1568 aSize.QuadPart = Size();
1569 if (!LockFileEx (myFileHandle, dwFlags, 0, aSize.LowPart, aSize.HighPart, &ovlp)) {
1571 _osd_wnt_set_error ( myError, OSD_WFile );
1576 ImperativeFlag = Standard_True;
1583 leave: ; // added for VisualAge
1585 } // end OSD_File :: SetLock
1587 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1593 void OSD_File :: UnLock () {
1595 TEST_RAISE( "Unlock" );
1597 if ( ImperativeFlag ) {
1599 LARGE_INTEGER aSize;
1600 aSize.QuadPart = Size();
1601 if (!UnlockFile (myFileHandle, 0, 0, aSize.LowPart, aSize.HighPart))
1603 _osd_wnt_set_error ( myError, OSD_WFile );
1605 ImperativeFlag = Standard_False;
1609 } // end OSD_File :: UnLock
1611 OSD_LockType OSD_File :: GetLock () {
1615 } // end OSD_File :: GetLock
1617 Standard_Boolean OSD_File :: IsLocked () {
1619 TEST_RAISE( "IsLocked" );
1621 return ImperativeFlag;
1623 } // end OSD_File :: IsLocked
1626 // --------------------------------------------------------------------------
1627 // Return size of a file
1628 // --------------------------------------------------------------------------
1630 Standard_Size OSD_File::Size()
1633 #if (_WIN32_WINNT >= 0x0500)
1634 LARGE_INTEGER aSize;
1636 if (GetFileSizeEx (myFileHandle, &aSize) == 0)
1638 _osd_wnt_set_error (myError, OSD_WFile);
1640 return (Standard_Size)aSize.QuadPart;
1642 DWORD aSize = GetFileSize (myFileHandle, NULL);
1643 if (aSize == INVALID_FILE_SIZE)
1645 _osd_wnt_set_error (myError, OSD_WFile);
1651 // --------------------------------------------------------------------------
1652 // Print contains of a file
1653 // --------------------------------------------------------------------------
1655 void OSD_File :: Print ( const OSD_Printer& WhichPrinter ) {
1657 if (myFileHandle != INVALID_HANDLE_VALUE)
1659 RAISE( "OSD_File :: Print (): incorrect call - file opened" );
1661 TCollection_AsciiString pName, fName;
1663 WhichPrinter.Name ( pName );
1664 myPath.SystemName ( fName );
1665 TCollection_ExtendedString fNameW(fName);
1667 if ( !_osd_print ( (Standard_PCharacter)pName.ToCString (),
1668 (const wchar_t*)fNameW.ToExtString () ) )
1670 _osd_wnt_set_error ( myError, OSD_WFile );
1672 } // end OSD_File :: Print
1674 // --------------------------------------------------------------------------
1675 // Test if a file is open
1676 // --------------------------------------------------------------------------
1678 Standard_Boolean OSD_File :: IsOpen () const {
1680 return myFileHandle != INVALID_HANDLE_VALUE;
1682 } // end OSD_File :: IsOpen
1684 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1687 #define __leave return retVal
1690 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd (
1691 const OSD_Protection& prot, BOOL fDir, wchar_t* fName
1697 HANDLE hProcess = NULL;
1702 DWORD dwAccessAdmin;
1703 DWORD dwAccessGroup;
1704 DWORD dwAccessOwner;
1705 DWORD dwAccessWorld;
1706 DWORD dwAccessAdminDir;
1707 // DWORD dwAccessGroupDir;
1708 DWORD dwAccessOwnerDir;
1709 // DWORD dwAccessWorldDir;
1710 DWORD dwACLsize = sizeof ( ACL );
1712 PTOKEN_OWNER pTkOwner = NULL;
1713 PTOKEN_GROUPS pTkGroups = NULL;
1714 PTOKEN_PRIMARY_GROUP pTkPrimaryGroup = NULL;
1715 PSECURITY_DESCRIPTOR retVal = NULL;
1716 PSECURITY_DESCRIPTOR pfSD = NULL;
1724 if ( !OpenProcessToken (
1725 GetCurrentProcess (), TOKEN_QUERY, &hProcess
1729 if ( ( pTkGroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
1730 hProcess, TokenGroups
1735 if ( ( pTkOwner = ( PTOKEN_OWNER )GetTokenInformationEx (
1736 hProcess, TokenOwner
1741 if ( ( pTkPrimaryGroup = ( PTOKEN_PRIMARY_GROUP )GetTokenInformationEx (
1742 hProcess, TokenPrimaryGroup
1749 if ( fName == NULL )
1751 pSIDowner = pTkOwner -> Owner;
1755 pfSD = GetFileSecurityEx ( fName, OWNER_SECURITY_INFORMATION );
1757 if ( pfSD == NULL || !GetSecurityDescriptorOwner ( pfSD, &pSIDowner, &fDummy ) ) {
1766 pSIDadmin = AdminSid ();
1767 pSIDworld = WorldSid ();
1769 dwAccessAdmin = _get_access_mask ( prot.System () );
1770 dwAccessGroup = _get_access_mask ( prot.Group () );
1771 dwAccessOwner = _get_access_mask ( prot.User () );
1772 dwAccessWorld = _get_access_mask ( prot.World () );
1774 dwAccessAdminDir = _get_dir_access_mask ( prot.System () );
1775 // dwAccessGroupDir = _get_dir_access_mask ( prot.Group () );
1776 dwAccessOwnerDir = _get_dir_access_mask ( prot.User () );
1777 // dwAccessWorldDir = _get_dir_access_mask ( prot.World () );
1779 if ( dwAccessGroup != 0 ) {
1781 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1783 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1785 if ( !NtPredefinedSid ( pSIDtemp ) &&
1786 !EqualSid ( pSIDtemp, pSIDworld ) &&
1787 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1788 IsValidSid ( pSIDtemp )
1791 dwACLsize += ( ( GetLengthSid ( pSIDtemp ) + ACE_HEADER_SIZE ) << j );
1797 dwACLsize += ( ( ( GetLengthSid ( pSIDowner ) + ACE_HEADER_SIZE ) << j ) +
1798 ( ( GetLengthSid ( pSIDadmin ) + ACE_HEADER_SIZE ) << j ) +
1799 ( ( GetLengthSid ( pSIDworld ) + ACE_HEADER_SIZE ) << j )
1802 if ( ( pACL = CreateAcl ( dwACLsize ) ) == NULL ) __leave;
1804 if ( dwAccessAdmin != 0 )
1806 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1813 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1817 pFileACE -> dwMask = dwAccessAdminDir;
1818 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1819 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1823 FreeAce ( pFileACE );
1827 if ( dwAccessOwner != 0 )
1829 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1830 dwAccessOwner, 0, pSIDowner
1835 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1839 pFileACE -> dwMask = dwAccessOwnerDir;
1840 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1841 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1845 FreeAce ( pFileACE );
1849 if ( dwAccessWorld != 0 )
1851 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1852 dwAccessWorld, 0, pSIDworld
1857 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1861 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1862 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1866 FreeAce ( pFileACE );
1870 if ( dwAccessGroup != 0 ) {
1872 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1874 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1876 if ( !NtPredefinedSid ( pSIDtemp ) &&
1877 !EqualSid ( pSIDtemp, pSIDworld ) &&
1878 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1879 IsValidSid ( pSIDtemp )
1882 if ( dwAccessGroup != 0 )
1884 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1885 dwAccessGroup, 0, pSIDtemp
1890 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1894 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1895 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1899 FreeAce ( pFileACE );
1909 if ( ( retVal = AllocSD () ) == NULL ) __leave;
1911 if ( !SetSecurityDescriptorDacl ( retVal, TRUE, pACL, TRUE ) ) __leave;
1921 if ( retVal != NULL )
1925 else if ( pACL != NULL )
1933 if ( hProcess != NULL ) CloseHandle ( hProcess );
1934 if ( pTkOwner != NULL ) FreeTokenInformation ( pTkOwner );
1935 if ( pTkGroups != NULL ) FreeTokenInformation ( pTkGroups );
1936 if ( pTkPrimaryGroup != NULL ) FreeTokenInformation ( pTkPrimaryGroup );
1937 if ( pfSD != NULL ) FreeFileSecurity ( pfSD );
1942 leave: ; // added for VisualAge
1947 } // end _osd_wnt_protection_to_sd */
1949 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1955 static void __fastcall _test_raise ( HANDLE hFile, Standard_CString str ) {
1957 Standard_Character buff[ 64 ];
1959 if (hFile == INVALID_HANDLE_VALUE) {
1961 strcpy ( buff, "OSD_File :: " );
1962 strcat ( buff, str );
1963 strcat ( buff, " (): wrong access" );
1965 Standard_ProgramError :: Raise ( buff );
1969 } // end _test_raise
1971 // Returns number of bytes in the string (including end \n, but excluding \r);
1973 static Standard_Integer __fastcall _get_line (Standard_PCharacter& buffer, DWORD dwBuffSize, LONG& theSeekPos)
1976 Standard_PCharacter ptr;
1978 buffer[ dwBuffSize ] = 0;
1981 while ( *ptr != 0 ) {
1985 ptr++ ; // jump newline char.
1987 theSeekPos = (LONG)(ptr - buffer - dwBuffSize);
1988 return (Standard_Integer)(ptr - buffer);
1990 else if ( *ptr == '\r' && ptr[ 1 ] == '\n' )
1992 *(ptr++) = '\n' ; // Substitue carriage return by newline.
1994 theSeekPos = (LONG)(ptr + 1 - buffer - dwBuffSize);
1995 return (Standard_Integer)(ptr - buffer);
1997 else if ( *ptr == '\r' && ptr[ 1 ] == 0 ) {
1998 *ptr = '\n' ; // Substitue carriage return by newline
2009 static int __fastcall _get_buffer (
2011 Standard_PCharacter& buffer,
2013 BOOL fPeek, BOOL fSocket
2023 flags = fPeek ? MSG_PEEK : 0;
2025 retVal = recv ( ( SOCKET )hChannel, buffer, ( int )dwSize, flags );
2027 if ( retVal == SOCKET_ERROR ) retVal = -1;
2033 if ( !PeekNamedPipe (
2034 hChannel, buffer, dwSize, &dwBytesRead, &dwDummy, &dwDummy
2035 ) && GetLastError () != ERROR_BROKEN_PIPE
2042 retVal = ( int )dwBytesRead;
2046 if ( !ReadFile ( hChannel, buffer, dwSize, &dwBytesRead, NULL ) )
2052 retVal = ( int )dwBytesRead;
2060 } // end _get_buffer
2063 static DWORD __fastcall _get_access_mask ( OSD_SingleProtection prt ) {
2077 retVal = FILE_GENERIC_READ;
2083 retVal = FILE_GENERIC_WRITE;
2089 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
2095 retVal = FILE_GENERIC_EXECUTE;
2101 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
2107 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2113 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2125 retVal = FILE_GENERIC_READ | DELETE;
2131 retVal = FILE_GENERIC_WRITE | DELETE;
2137 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE;
2143 retVal = FILE_GENERIC_EXECUTE | DELETE;
2149 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE;
2155 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2161 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2167 RAISE( "_get_access_mask (): incorrect parameter" );
2173 } // end _get_access_mask
2175 static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt ) {
2189 retVal = GENERIC_READ;
2195 retVal = GENERIC_WRITE;
2201 retVal = GENERIC_READ | GENERIC_WRITE;
2207 retVal = GENERIC_EXECUTE;
2213 retVal = GENERIC_READ | GENERIC_EXECUTE;
2219 retVal = GENERIC_WRITE | GENERIC_EXECUTE;
2225 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE;
2237 retVal = GENERIC_READ | DELETE;
2243 retVal = GENERIC_WRITE | DELETE;
2249 retVal = GENERIC_READ | GENERIC_WRITE | DELETE;
2255 retVal = GENERIC_EXECUTE | DELETE;
2261 retVal = GENERIC_READ | GENERIC_EXECUTE | DELETE;
2267 retVal = GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2273 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2279 RAISE( "_get_dir_access_mask (): incorrect parameter" );
2285 } // end _get_dir_access_mask
2287 static HANDLE __fastcall _open_file (
2288 Standard_CString fName,
2290 DWORD dwOptions, LPBOOL fNew
2293 HANDLE retVal = INVALID_HANDLE_VALUE;
2294 DWORD dwDesiredAccess = 0;
2295 DWORD dwCreationDistribution;
2301 dwDesiredAccess = GENERIC_READ;
2307 dwDesiredAccess = GENERIC_WRITE;
2313 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
2319 RAISE( "_open_file (): incorrect parameter" );
2323 dwCreationDistribution = ( dwOptions != OPEN_NEW ) ? OPEN_EXISTING : CREATE_ALWAYS;
2325 // make wide character string from UTF-8
2326 TCollection_ExtendedString fNameW(fName, Standard_True);
2327 retVal = CreateFileW (
2328 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
2329 FILE_SHARE_READ | FILE_SHARE_WRITE,
2330 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2333 if ( retVal == INVALID_HANDLE_VALUE &&
2334 dwOptions == OPEN_APPEND &&
2335 GetLastError () == ERROR_FILE_NOT_FOUND
2339 dwCreationDistribution = CREATE_ALWAYS;
2341 retVal = CreateFileW (
2342 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
2343 FILE_SHARE_READ | FILE_SHARE_WRITE,
2344 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2355 Standard_Integer __fastcall _get_file_type (
2356 Standard_CString fName, HANDLE fileHandle
2359 Standard_Integer retVal = 0;
2363 fileType = (fileHandle == INVALID_HANDLE_VALUE ?
2364 FILE_TYPE_DISK : GetFileType (fileHandle));
2366 switch ( fileType ) {
2368 case FILE_TYPE_UNKNOWN:
2370 retVal = FLAG_SOCKET;
2374 case FILE_TYPE_DISK:
2376 // make wide character string from UTF-8
2377 TCollection_ExtendedString fNameW(fName, Standard_True);
2378 dwType = GetFileAttributesW ( (const wchar_t*) fNameW.ToExtString() );
2379 if ( dwType != 0xFFFFFFFF )
2381 retVal = dwType & FILE_ATTRIBUTE_DIRECTORY ? FLAG_DIRECTORY : FLAG_FILE;
2385 retVal = 0x80000000;
2389 case FILE_TYPE_CHAR:
2391 retVal = FLAG_DEVICE;
2395 case FILE_TYPE_PIPE:
2405 } // end _get_file_type
2407 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2410 #define __leave return retVal
2413 BOOL __fastcall _osd_wnt_sd_to_protection (
2414 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL fDir
2425 DWORD dwAccessOwner = 0;
2426 DWORD dwAccessGroup = 0;
2427 DWORD dwAccessAdmin = 0;
2428 DWORD dwAccessWorld = 0;
2429 BOOL retVal = FALSE;
2430 GET_PROT_FUNC _get_prot_func = fDir ? &_get_protection_dir : &_get_protection;
2434 if ( !GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) ) __leave;
2436 if ( !GetSecurityDescriptorDacl ( pSD, &fPresent, &pACL, &fDefaulted ) ||
2440 if ( pSIDowner == NULL || pACL == NULL ) {
2442 SetLastError ( ERROR_NO_SECURITY_ON_OBJECT );
2447 pSIDadmin = AdminSid ();
2448 pSIDworld = WorldSid ();
2450 for ( i = 0; i < ( int )pACL -> AceCount; ++i ) {
2452 if ( GetAce ( pACL, i, &pACE ) ) {
2454 if ( EqualSid ( pSIDowner, GET_SID( pACE ) ) )
2456 dwAccessOwner = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2457 0 : *GET_MSK( pACE );
2459 else if ( EqualSid ( pSIDadmin, GET_SID( pACE ) ) )
2461 dwAccessAdmin = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2462 0 : *GET_MSK( pACE );
2464 else if ( EqualSid ( pSIDworld, GET_SID( pACE ) ) )
2466 dwAccessWorld = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2467 0 : *GET_MSK( pACE );
2471 dwAccessGroup = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2472 0 : *GET_MSK( pACE );
2479 ( *_get_prot_func ) ( dwAccessAdmin ),
2480 ( *_get_prot_func ) ( dwAccessOwner ),
2481 ( *_get_prot_func ) ( dwAccessGroup ),
2482 ( *_get_prot_func ) ( dwAccessWorld )
2492 leave: ; // added for VisualAge
2497 } // end _osd_wnt_sd_to_protection
2499 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2505 static OSD_SingleProtection __fastcall _get_protection ( DWORD mask ) {
2507 OSD_SingleProtection retVal;
2511 case FILE_GENERIC_READ:
2517 case FILE_GENERIC_WRITE:
2523 case FILE_GENERIC_READ | FILE_GENERIC_WRITE:
2529 case FILE_GENERIC_EXECUTE:
2535 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE:
2541 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2547 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2559 case FILE_GENERIC_READ | DELETE:
2565 case FILE_GENERIC_WRITE | DELETE:
2571 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE:
2577 case FILE_GENERIC_EXECUTE | DELETE:
2583 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE:
2589 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2595 case FILE_ALL_ACCESS:
2596 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2611 } // end _get_protection
2613 static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD mask ) {
2615 OSD_SingleProtection retVal;
2631 case GENERIC_READ | GENERIC_WRITE:
2637 case GENERIC_EXECUTE:
2643 case GENERIC_READ | GENERIC_EXECUTE:
2649 case GENERIC_WRITE | GENERIC_EXECUTE:
2655 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE:
2667 case GENERIC_READ | DELETE:
2673 case GENERIC_WRITE | DELETE:
2679 case GENERIC_READ | GENERIC_WRITE | DELETE:
2685 case GENERIC_EXECUTE | DELETE:
2691 case GENERIC_READ | GENERIC_EXECUTE | DELETE:
2697 case GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2703 case FILE_ALL_ACCESS:
2704 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2719 } // end _get_protection_dir
2721 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2724 #define __leave return fOK
2727 BOOL __fastcall _osd_print (const Standard_PCharacter pName, const wchar_t* fName ) {
2730 HANDLE hPrinter = NULL;
2731 BYTE jobInfo[ MAX_PATH + sizeof ( DWORD ) ];
2732 DWORD dwNeeded, dwCode = 0;
2738 if ( !OpenPrinter ( Standard_PCharacter(pName), &hPrinter, NULL ) ) {
2746 hPrinter, 1, jobInfo, MAX_PATH + sizeof ( DWORD ), &dwNeeded
2753 fName, (LPWSTR) ( ( ADDJOB_INFO_1* )jobInfo ) -> Path, FALSE
2758 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId
2771 DWORD dwBytesNeeded;
2773 dwCode = GetLastError ();
2775 if ( fJob && hPrinter != NULL ) {
2778 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId, 1,
2779 info, 1024, &dwBytesNeeded
2782 if ( fJob ) SetJob (
2784 ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId,
2785 1, info, JOB_CONTROL_CANCEL
2792 if ( hPrinter != NULL ) ClosePrinter ( hPrinter );
2797 leave: ; // added for VisualAge
2800 if ( !fOK ) SetLastError ( dwCode );
2806 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2812 Standard_Boolean OSD_File::IsReadable()
2814 TCollection_AsciiString FileName ;
2817 myPath.SystemName(FileName) ;
2818 Channel = _open_file(FileName.ToCString(), OSD_ReadOnly, OPEN_OLD) ;
2819 if (Channel == INVALID_HANDLE_VALUE)
2820 return Standard_False ;
2822 CloseHandle (Channel) ;
2823 return Standard_True ;
2828 Standard_Boolean OSD_File::IsWriteable()
2830 TCollection_AsciiString FileName ;
2833 myPath.SystemName(FileName) ;
2834 Channel = _open_file(FileName.ToCString(), OSD_ReadWrite, OPEN_OLD) ;
2835 if (Channel == INVALID_HANDLE_VALUE)
2836 return Standard_False ;
2838 CloseHandle (Channel) ;
2839 return Standard_True ;
2843 Standard_Boolean OSD_File::IsExecutable()
2845 return IsReadable() ;
2847 // if (_access(FileName.ToCString(),0))
2852 // ---------------------------------------------------------------------
2853 // Destructs a file object (unlocks and closes file if it is open)
2854 // ---------------------------------------------------------------------
2856 OSD_File::~OSD_File()
2866 // ---------------------------------------------------------------------
2867 // Read lines in a file while it is increasing.
2868 // Each line is terminated with a <nl>.
2869 // ---------------------------------------------------------------------
2873 Standard_Boolean OSD_File::ReadLastLine(TCollection_AsciiString& aLine,const Standard_Integer aDelay,const Standard_Integer aNbTries)
2875 static Standard_Integer MaxLength = 1000 ;
2876 Standard_Integer Len ;
2877 Standard_Integer Count = aNbTries ;
2880 return Standard_False ;
2882 ReadLine(aLine, MaxLength, Len) ;
2883 if (!aLine.IsEmpty())
2884 return Standard_True ;
2886 return Standard_False ;
2887 OSD::SecSleep(aDelay) ;
2892 Standard_Boolean OSD_File::Edit()
2894 cout << "Function OSD_File::Edit() not yet implemented." << endl;
2895 return Standard_False ;