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 //------------------------------------------------------------------------
21 #include <Standard_ProgramError.hxx>
22 #include <OSD_OSDError.hxx>
23 #include <OSD_WhoAmI.hxx>
24 #include <OSD_FromWhere.hxx>
25 #include <OSD_File.ixx>
27 #include <Standard_PCharacter.hxx>
29 const OSD_WhoAmI Iam = OSD_WFile;
31 #if defined (sun) || defined(SOLARIS)
44 #define NEWLINE '\10';
46 // ---------------------------------------------------------------------
47 // Create an empty file object
48 // ---------------------------------------------------------------------
50 OSD_File::OSD_File():OSD_FileNode()
52 ImperativeFlag = Standard_False;
55 myMode = OSD_ReadWrite;
56 myFILE = (Standard_Address) NULL;
61 // ---------------------------------------------------------------------
62 // Create and initialize a file object
63 // ---------------------------------------------------------------------
65 OSD_File::OSD_File(const OSD_Path& Name):OSD_FileNode(Name)
67 ImperativeFlag = Standard_False;
70 myMode = OSD_ReadWrite;
71 myFILE = (Standard_Address) NULL;
76 // protect against occasional use of myFileHande in Linux code
77 #define myFileHandle myFileHandle_is_only_for_Windows
79 // ---------------------------------------------------------------------
80 // Build a file if it doesn't exist or create again if it already exists
81 // ---------------------------------------------------------------------
83 void OSD_File::Build(const OSD_OpenMode Mode,
84 const OSD_Protection& Protect){
86 Standard_Integer internal_prot;
87 Standard_Integer internal_mode = O_CREAT | O_TRUNC ;
88 TCollection_AsciiString aBuffer;
90 if (myPath.Name().Length()==0)
91 Standard_ProgramError::Raise("OSD_File::Build : no name was given");
93 if (myFileChannel != -1)
94 Standard_ProgramError::Raise("OSD_File::Build : file is already open");
99 internal_prot = Protect.Internal();
101 const char* CMode = "r";
105 internal_mode |= O_RDONLY;
109 internal_mode |= O_WRONLY;
113 internal_mode |= O_RDWR;
118 myPath.SystemName( aBuffer );
119 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
120 if (myFileChannel >= 0) {
121 myFILE = fdopen (myFileChannel, CMode);
124 /* Handle OPEN errors */
126 myError.SetValue (errno, Iam, "Open");
132 // ---------------------------------------------------------------------
133 // Append to an existing file
134 // ---------------------------------------------------------------------
136 void OSD_File::Append(const OSD_OpenMode Mode,
137 const OSD_Protection& Protect){
139 Standard_Integer internal_prot;
140 Standard_Integer internal_mode = O_APPEND;;
141 TCollection_AsciiString aBuffer;
143 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
144 Standard_ProgramError::Raise("OSD_File::Append : it is a directory");
147 if (myPath.Name().Length()==0)
148 Standard_ProgramError::Raise("OSD_File::Append : no name was given");
150 if (myFileChannel != -1)
151 Standard_ProgramError::Raise("OSD_File::Append : file is already open");
153 internal_prot = Protect.Internal();
155 const char* CMode = "r";
159 internal_mode |= O_RDONLY;
163 internal_mode |= O_WRONLY;
167 internal_mode |= O_RDWR;
172 // If file doesn't exist, creates it.
174 if (!Exists()) internal_mode |= O_CREAT;
176 myPath.SystemName ( aBuffer );
177 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
178 if (myFileChannel >= 0)
179 myFILE = fdopen (myFileChannel, CMode);
181 /* Handle OPEN errors */
183 myError.SetValue (errno, Iam, "Open");
186 // ---------------------------------------------------------------------
188 // ---------------------------------------------------------------------
190 void OSD_File::Open(const OSD_OpenMode Mode,
191 const OSD_Protection& Protect){
193 Standard_Integer internal_prot;
194 Standard_Integer internal_mode = 0;
195 TCollection_AsciiString aBuffer;
197 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
198 myError.SetValue (1, Iam, "Could not be open : it is a directory");
201 if (myPath.Name().Length()==0)
202 Standard_ProgramError::Raise("OSD_File::Open : no name was given");
204 if (myFileChannel != -1)
205 Standard_ProgramError::Raise("OSD_File::Open : file is already open");
207 internal_prot = Protect.Internal();
209 const char* CMode = "r";
213 internal_mode |= O_RDONLY;
217 internal_mode |= O_WRONLY;
221 internal_mode |= O_RDWR;
226 myPath.SystemName ( aBuffer );
227 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
228 if (myFileChannel >= 0)
229 myFILE = fdopen (myFileChannel, CMode);
231 /* Handle OPEN errors */
233 myError.SetValue (errno, Iam, "Open");
238 // ---------------------------------------------------------------------
239 // ---------------------------------------------------------------------
240 OSD_File OSD_File::BuildTemporary(){
242 #if defined(vax) || defined(__vms) || defined(VAXVMS)
248 dummy = open("dummy", O_RDWR | O_CREAT); // Open a dummy file
249 result.myFileChannel = dummy - 1; // This is file channel of "fic" +1
250 close(dummy); // Close dummy file
251 unlink("dummy"); // Removes dummy file
255 char *name = tmpnam((char*) 0) ;
258 TCollection_AsciiString aName ( name ) ;
259 OSD_Path aPath( aName ) ;
261 result.SetPath( aPath ) ;
263 result.myFILE = fopen( name, "w+" ) ;
265 result.myFileChannel = fileno( (FILE*)result.myFILE );
269 result.myMode = OSD_ReadWrite;
276 // ---------------------------------------------------------------------
277 // Read content of a file
278 // ---------------------------------------------------------------------
280 void OSD_File::Read(TCollection_AsciiString& Buffer,
281 const Standard_Integer Nbyte){
282 Standard_PCharacter readbuf;
285 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
286 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
289 if (myFileChannel == -1)
290 Standard_ProgramError::Raise("OSD_File::Read : file is not open");
292 if (Failed()) Perror();
294 if (myMode == OSD_WriteOnly)
295 Standard_ProgramError::Raise("OSD_File::Read : file is Write only");
298 Standard_ProgramError::Raise("OSD_File::Read : Nbyte is null");
300 TCollection_AsciiString transfert(Nbyte,' ');
301 readbuf = (Standard_PCharacter)transfert.ToCString();
303 status = read (myFileChannel, readbuf, Nbyte);
305 Buffer = transfert; // Copy transfert to Buffer
307 if (status == -1) myError.SetValue (errno, Iam, "Read");
309 if ( status < Nbyte ) myIO = EOF;
313 // ---------------------------------------------------------------------
314 // Read a line from a file
315 // ---------------------------------------------------------------------
317 void OSD_File::ReadLine(TCollection_AsciiString& Buffer,
318 const Standard_Integer Nbyte,
319 Standard_Integer& NByteRead)
321 Standard_PCharacter readbuf, abuffer;
323 if (OSD_File::KindOfFile() == OSD_DIRECTORY ) {
324 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
326 if (myFileChannel == -1){
327 Standard_ProgramError::Raise("OSD_File::ReadLine : file is not open");
332 if (myMode == OSD_WriteOnly) {
333 Standard_ProgramError::Raise("OSD_File::ReadLine : file is Write only");
336 Standard_ProgramError::Raise("OSD_File::ReadLine : Nbyte is null");
339 TCollection_AsciiString transfert(Nbyte,' ');
340 readbuf = (Standard_PCharacter)transfert.ToCString();
342 abuffer = fgets(readbuf, Nbyte, (FILE *) myFILE);
344 if (abuffer == NULL) {
345 if (!feof((FILE *) myFILE)) {
346 myError.SetValue (errno, Iam, "ReadLine");
356 NByteRead = (Standard_Integer)strlen(abuffer);
357 Buffer.SetValue(1,abuffer); // Copy transfert to Buffer
358 Buffer.Trunc(NByteRead);
361 // --------------------------------------------------------------------------
362 // OSD::KindOfFile Retourne le type de fichier.
363 // --------------------------------------------------------------------------
364 OSD_KindFile OSD_File::KindOfFile ( ) const{
367 TCollection_AsciiString FullName;
371 aPath.SystemName (FullName);
372 status = stat (FullName.ToCString() , &buffer );
374 if ( S_ISDIR(buffer.st_mode) ) { return OSD_DIRECTORY ; }
375 else if ( S_ISREG(buffer.st_mode) ) { return OSD_FILE ; }
376 else if ( S_ISLNK(buffer.st_mode) ) { return OSD_LINK ; }
377 else if ( S_ISSOCK(buffer.st_mode) ) { return OSD_SOCKET ; }
378 else { return OSD_UNKNOWN ; }
383 // --------------------------------------------------------------------------
384 // Read content of a file
385 // --------------------------------------------------------------------------
386 void OSD_File::Read( Standard_Address& Buffer,
387 const Standard_Integer Nbyte,
388 Standard_Integer& Readbyte)
393 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
394 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
397 if (myFileChannel == -1)
398 Standard_ProgramError::Raise("OSD_File::Read : file is not open");
400 if (Failed()) Perror();
402 if (myMode == OSD_WriteOnly)
403 Standard_ProgramError::Raise("OSD_File::Read : file is Write only");
406 Standard_ProgramError::Raise("OSD_File::Read : Nbyte is null");
409 Standard_ProgramError::Raise("OSD_File::Read : Buffer is null");
411 status = read (myFileChannel, (char*) Buffer, Nbyte);
413 if (status == -1) myError.SetValue (errno, Iam, "Read");
415 if ( status < Nbyte ) myIO = EOF;
420 // Write content of a file
422 void OSD_File::Write(const TCollection_AsciiString &Buffer,
423 const Standard_Integer Nbyte){
425 Standard_CString writebuf;
428 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
429 Standard_ProgramError::Raise("OSD_File::Write : it is a directory");
432 if (myFileChannel == -1)
433 Standard_ProgramError::Raise("OSD_File::Write : file is not open");
435 if (Failed()) Perror();
437 if (myMode == OSD_ReadOnly)
438 Standard_ProgramError::Raise("OSD_File::Write : file is Read only");
441 Standard_ProgramError::Raise("OSD_File::Write : Nbyte is null");
443 writebuf = Buffer.ToCString();
445 status = write (myFileChannel, writebuf, Nbyte);
447 if ( status == -1) myError.SetValue (errno, Iam, "Write");
449 if ( status < Nbyte ) myIO = EOF;
453 void OSD_File::Write(const Standard_Address Buffer,
454 const Standard_Integer Nbyte)
459 if (myFileChannel == -1)
460 Standard_ProgramError::Raise("OSD_File::Write : file is not open");
462 if (Failed()) Perror();
464 if (myMode == OSD_ReadOnly)
465 Standard_ProgramError::Raise("OSD_File::Write : file is Read only");
468 Standard_ProgramError::Raise("OSD_File::Write : Nbyte is null");
470 status = write (myFileChannel, (const char *)Buffer, Nbyte);
472 if ( status == -1) myError.SetValue (errno, Iam, "Write");
474 if ( status < Nbyte ) myIO = EOF;
481 // Move file pointer to a specified position
483 void OSD_File::Seek(const Standard_Integer Offset,
484 const OSD_FromWhere Whence){
487 if (myFileChannel == -1)
488 Standard_ProgramError::Raise("OSD_File::Seek : file is not open");
490 if (Failed()) Perror();
493 case OSD_FromBeginning :
503 myError.SetValue (EINVAL, Iam, "Seek");
506 off_t status = lseek (myFileChannel, Offset, iwhere);
507 if (status == -1) myError.SetValue (errno, Iam, "Seek");
517 void OSD_File::Close(){
520 if (myFileChannel == -1)
521 Standard_ProgramError::Raise("OSD_File::Close : file is not open");
523 if (Failed()) Perror();
525 // note: it probably should be single call to fclose()...
526 status = close (myFileChannel);
528 if (status == -1) myError.SetValue (errno, Iam, "Close");
530 if ( myFILE != NULL ) {
531 status = fclose ( (FILE*) myFILE );
540 // --------------------------------------------------------------------------
541 // Test if at end of file
542 // --------------------------------------------------------------------------
544 Standard_Boolean OSD_File::IsAtEnd(){
545 if (myFileChannel == -1)
546 Standard_ProgramError::Raise("OSD_File::IsAtEnd : file is not open");
548 if (myIO == EOF) return (Standard_True);
549 return (Standard_False);
554 /*void OSD_File::Link(const TCollection_AsciiString& ToFile){
555 if (myFileChannel == -1)
556 Standard_ProgramError::Raise("OSD_File::Link : file is not open");
558 TCollection_AsciiString aBuffer;
559 myPath.SystemName ( aBuffer );
560 link ( aBuffer.ToCString(), ToFile.ToCString() );
566 void OSD_File::SetLock(const OSD_LockType Lock){
569 if (myFileChannel == -1)
570 Standard_ProgramError::Raise("OSD_File::SetLock : file is not open");
578 case OSD_ExclusiveLock :
579 case OSD_WriteLock : lock = F_LOCK;
581 case OSD_ReadLock : return;
583 default : myError.SetValue (EINVAL, Iam, "SetLock");
587 if (fstat (myFileChannel, &buf) == -1) {
588 myError.SetValue (errno, Iam, "SetLock");
592 status = lockf(myFileChannel, lock, buf.st_size);
593 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
602 case OSD_ExclusiveLock :
603 case OSD_WriteLock : key.l_type = F_WRLCK;
605 case OSD_ReadLock : key.l_type = F_RDLCK;
607 case OSD_NoLock : return;
608 // default : myError.SetValue (EINVAL, Iam, "SetLock");
615 status = fcntl (myFileChannel, F_SETLKW, &key);
616 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
619 if (Lock == OSD_ExclusiveLock){
620 fstat (myFileChannel, &buf);
621 TCollection_AsciiString aBuffer;
622 myPath.SystemName ( aBuffer );
623 chmod( aBuffer.ToCString() ,buf.st_mode | S_ISGID);
624 ImperativeFlag = Standard_True;
631 case OSD_ExclusiveLock :
632 case OSD_WriteLock : lock = F_WRLCK;
634 case OSD_ReadLock : lock = F_RDLCK;
636 default : myError.SetValue (EINVAL, Iam, "SetLock");
639 status = flock (myFileChannel, lock);
640 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
649 // Remove a lock from a file
651 void OSD_File::UnLock(){
654 if (myFileChannel == -1)
655 Standard_ProgramError::Raise("OSD_File::UnLock : file is not open");
660 if (fstat(myFileChannel, &buf) == -1) {
661 myError.SetValue(errno, Iam, "UnsetLock");
665 status = lockf(myFileChannel,F_ULOCK, buf.st_size);
666 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
674 fstat (myFileChannel, &buf);
675 TCollection_AsciiString aBuffer;
676 myPath.SystemName ( aBuffer );
677 chmod(aBuffer.ToCString(),buf.st_mode & ~S_ISGID);
678 ImperativeFlag = Standard_False;
680 key.l_type = F_UNLCK;
681 status = fcntl (myFileChannel, F_SETLK, &key);
682 if (status == -1) myError.SetValue (errno, Iam, "UnSetLock");
685 status = flock (myFileChannel, LOCK_UN);
686 if (status == -1) myError.SetValue (errno, Iam, "UnSetLock");
689 else myLock = OSD_NoLock;
696 // Return lock of a file
698 OSD_LockType OSD_File::GetLock(){
699 if (myFileChannel == -1)
700 Standard_ProgramError::Raise("OSD_File::GetLock : file is not open");
708 // --------------------------------------------------------------------------
709 // Return size of a file
710 // --------------------------------------------------------------------------
712 Standard_Size OSD_File::Size(){
716 if (myPath.Name().Length()==0)
717 Standard_ProgramError::Raise("OSD_File::Size : empty file name");
719 TCollection_AsciiString aBuffer;
720 myPath.SystemName ( aBuffer );
721 status = stat( aBuffer.ToCString() ,&buffer );
723 myError.SetValue (errno, Iam, "Size");
727 return (Standard_Size)buffer.st_size;
731 // --------------------------------------------------------------------------
732 // Print contains of a file
733 // --------------------------------------------------------------------------
735 void OSD_File::Print (const OSD_Printer &WhichPrinter ){
737 TCollection_AsciiString PrinterName;
739 if (myPath.Name().Length()==0)
740 Standard_ProgramError::Raise("OSD_File::Print : empty file name");
742 WhichPrinter.Name(PrinterName);
744 TCollection_AsciiString aBuffer;
745 myPath.SystemName ( aBuffer );
747 if (PrinterName.Length()==0)
748 sprintf(buffer,"lp %s",aBuffer.ToCString());
750 sprintf(buffer,"lpr -P%s %s",PrinterName.ToCString(),aBuffer.ToCString());
756 // --------------------------------------------------------------------------
757 // Test if a file is open
758 // --------------------------------------------------------------------------
760 Standard_Boolean OSD_File::IsOpen()const{
762 if (myPath.Name().Length()==0)
763 Standard_ProgramError::Raise("OSD_File::IsOpen : empty file name");
765 return (myFileChannel != -1);
769 Standard_Boolean OSD_File::IsLocked(){
771 if (myPath.Name().Length()==0)
772 Standard_ProgramError::Raise("OSD_File::IsLocked : empty file name");
774 return(myLock != OSD_NoLock);
777 Standard_Boolean OSD_File::IsReadable()
779 TCollection_AsciiString FileName ;
781 myPath.SystemName(FileName) ;
783 if (access(FileName.ToCString(),F_OK|R_OK))
784 return Standard_False;
786 return Standard_True;
789 Standard_Boolean OSD_File::IsWriteable()
791 TCollection_AsciiString FileName ;
793 myPath.SystemName(FileName) ;
795 if (access(FileName.ToCString(),F_OK|R_OK|W_OK))
796 return Standard_False;
798 return Standard_True;
801 Standard_Boolean OSD_File::IsExecutable()
803 TCollection_AsciiString FileName ;
805 myPath.SystemName(FileName) ;
807 if (access(FileName.ToCString(),F_OK|X_OK))
808 return Standard_False;
810 return Standard_True;
815 //------------------------------------------------------------------------
816 //------------------- Windows NT sources for OSD_File -------------------
817 //------------------------------------------------------------------------
821 #include <OSD_File.hxx>
822 #include <OSD_Protection.hxx>
823 #include <OSD_Printer.hxx>
824 #include <Standard_ProgramError.hxx>
826 #include <OSD_WNT_1.hxx>
830 #include <Standard_PCharacter.hxx>
836 #if defined(__CYGWIN32__) || defined(__MINGW32__)
841 #pragma comment( lib, "WSOCK32.LIB" )
842 #pragma comment( lib, "WINSPOOL.LIB" )
844 #define ACE_HEADER_SIZE ( sizeof ( ACCESS_ALLOWED_ACE ) - sizeof ( DWORD ) )
846 #define RAISE( arg ) Standard_ProgramError :: Raise ( ( arg ) )
847 #define TEST_RAISE( arg ) _test_raise ( myFileHandle, ( arg ) )
851 #define OPEN_APPEND 2
853 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
854 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, char* = NULL );
855 BOOL __fastcall _osd_wnt_sd_to_protection (
856 PSECURITY_DESCRIPTOR, OSD_Protection&, BOOL
858 BOOL __fastcall _osd_print (const Standard_PCharacter, Standard_CString );
860 static void __fastcall _test_raise ( HANDLE, Standard_CString );
861 static DWORDLONG __fastcall _get_line ( Standard_PCharacter&, DWORD );
862 static int __fastcall _get_buffer ( HANDLE, Standard_PCharacter&, DWORD, BOOL, BOOL );
863 static DWORD __fastcall _get_access_mask ( OSD_SingleProtection );
864 static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt );
865 static HANDLE __fastcall _open_file ( Standard_CString, OSD_OpenMode, DWORD, LPBOOL = NULL );
867 static OSD_SingleProtection __fastcall _get_protection ( DWORD );
868 static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD );
870 typedef OSD_SingleProtection ( __fastcall *GET_PROT_FUNC ) ( DWORD );
872 Standard_Integer __fastcall _get_file_type ( Standard_CString, HANDLE );
874 // ---------------------------------------------------------------------
875 // Create an empty file object
876 // ---------------------------------------------------------------------
878 OSD_File :: OSD_File ()
880 ImperativeFlag = Standard_False;
884 myFileHandle = INVALID_HANDLE_VALUE;
885 } // end constructor ( 1 )
887 // ---------------------------------------------------------------------
888 // Create and initialize a file object
889 // ---------------------------------------------------------------------
891 OSD_File :: OSD_File ( const OSD_Path& Name ) : OSD_FileNode ( Name )
893 ImperativeFlag = Standard_False;
898 myFileHandle = INVALID_HANDLE_VALUE;
899 } // end constructor ( 2 )
901 // protect against occasional use of myFileHande in Windows code
902 #define myFileChannel myFileChannel_is_only_for_Linux
904 // ---------------------------------------------------------------------
905 // Build a file if it doesn't exist or create again if it already exists
906 // ---------------------------------------------------------------------
908 void OSD_File :: Build (
909 const OSD_OpenMode Mode, const OSD_Protection& Protect
912 TCollection_AsciiString fName;
914 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
915 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
918 if (myFileHandle != INVALID_HANDLE_VALUE)
920 RAISE( TEXT( "OSD_File :: Build (): incorrect call - file already opened" ) );
923 myPath.SystemName ( fName );
925 if ( fName.IsEmpty () )
927 RAISE( TEXT( "OSD_File :: Build (): incorrent call - no filename given" ) );
929 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_NEW );
931 if (myFileHandle == INVALID_HANDLE_VALUE)
933 _osd_wnt_set_error ( myError, OSD_WFile );
937 SetProtection ( Protect );
942 } // end OSD_File :: Build
946 // ---------------------------------------------------------------------
948 // ---------------------------------------------------------------------
950 void OSD_File :: Open (const OSD_OpenMode Mode, const OSD_Protection& /*Protect*/)
953 TCollection_AsciiString fName;
956 if (myFileHandle != INVALID_HANDLE_VALUE)
958 RAISE( TEXT( "OSD_File :: Open (): incorrect call - file already opened" ) );
961 myPath.SystemName ( fName );
963 if ( fName.IsEmpty () )
965 RAISE( TEXT( "OSD_File :: Open (): incorrent call - no filename given" ) );
967 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_OLD );
969 if (myFileHandle == INVALID_HANDLE_VALUE) {
971 _osd_wnt_set_error ( myError, OSD_WFile );
975 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
977 } // end OSD_File :: Open
979 // ---------------------------------------------------------------------
980 // Append to an existing file
981 // ---------------------------------------------------------------------
983 void OSD_File :: Append (
984 const OSD_OpenMode Mode, const OSD_Protection& Protect
988 TCollection_AsciiString fName;
990 if (myFileHandle != INVALID_HANDLE_VALUE)
992 RAISE( TEXT( "OSD_File :: Append (): incorrect call - file already opened" ) );
995 myPath.SystemName ( fName );
997 if ( fName.IsEmpty () )
999 RAISE( TEXT( "OSD_File :: Append (): incorrent call - no filename given" ) );
1001 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_APPEND, &fNew );
1003 if (myFileHandle == INVALID_HANDLE_VALUE)
1005 _osd_wnt_set_error ( myError, OSD_WFile );
1011 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
1012 Seek ( 0, OSD_FromEnd );
1016 SetProtection ( Protect );
1023 } // end OSD_File :: Append
1025 // ---------------------------------------------------------------------
1026 // Read content of a file
1027 // ---------------------------------------------------------------------
1029 void OSD_File :: Read (
1030 TCollection_AsciiString& Buffer, const Standard_Integer Nbyte
1033 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1034 cout << " OSD_File::Read : it is a directory " << endl;
1036 // Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1039 Standard_Integer NbyteRead;
1040 Standard_Address buff;
1042 TEST_RAISE( TEXT( "Read" ) );
1044 buff = ( Standard_Address )new Standard_Character[ Nbyte + 1 ];
1046 Read ( buff, Nbyte, NbyteRead );
1048 ( ( Standard_PCharacter )buff )[ NbyteRead ] = 0;
1050 if ( NbyteRead != 0 )
1052 Buffer = ( Standard_PCharacter )buff;
1060 } // end OSD_File :: Read
1062 // ---------------------------------------------------------------------
1063 // Read a line from a file
1064 // ---------------------------------------------------------------------
1066 // Modified so that we have <nl> at end of line if we have read <nl> or <cr>
1068 // by LD 17 dec 98 for B4.4
1070 void OSD_File :: ReadLine (
1071 TCollection_AsciiString& Buffer,
1072 const Standard_Integer NByte, Standard_Integer& NbyteRead
1078 Standard_Character peekChar;
1079 Standard_PCharacter ppeekChar;
1080 Standard_PCharacter cBuffer;
1081 Standard_CString eos;
1084 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1085 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1088 TEST_RAISE( TEXT( "ReadLine" ) );
1090 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1092 RAISE( TEXT( "OSD_File :: ReadLine (): attempt to read from write only pipe" ) );
1094 // +----> leave space for end-of-string
1095 // | plus <CR><LF> sequence
1098 ppeekChar=&peekChar;
1099 cBuffer = new Standard_Character[ NByte + 3 ];
1101 if ( myIO & FLAG_FILE ) {
1103 if (!ReadFile (myFileHandle, cBuffer, (DWORD)NByte, &dwBytesRead, NULL)) { // an error occured
1105 _osd_wnt_set_error ( myError, OSD_WFile );
1109 } else if ( dwBytesRead == 0 ) { // end-of-file reached
1116 myIO &= ~FLAG_EOF ; // if the file increased since last read (LD)
1117 status = _get_line ( cBuffer, dwBytesRead );
1119 dwSeekPos = LODWORD( status );
1120 eos = ( Standard_CString )HIDWORD( status );
1122 if ( (__int64) status == (__int64) -1 ) { // last character in the buffer is <CR> -
1124 if ( status == 0xFFFFFFFFFFFFFFFF ) { // last character in the buffer is <CR> -
1125 // peek next character to see if it is a <LF>
1127 if (!ReadFile (myFileHandle, ppeekChar, 1, &dwDummy, NULL)) {
1129 _osd_wnt_set_error ( myError, OSD_WFile );
1131 } else if ( dwDummy != 0 ) { // end-of-file reached ?
1133 if ( peekChar != '\n' ) // if we did not get a <CR><LF> sequence
1135 // adjust file position
1137 SetFilePointer (myFileHandle, -1, NULL, FILE_CURRENT);
1142 NbyteRead = dwBytesRead;
1146 if ( dwSeekPos != 0 )
1147 SetFilePointer (myFileHandle, (LONG)dwSeekPos, NULL, FILE_CURRENT);
1149 NbyteRead = ( Standard_Integer )( eos - cBuffer );
1155 } else if ( myIO & FLAG_SOCKET || myIO & FLAG_PIPE || myIO & FLAG_NAMED_PIPE ) {
1157 dwBytesRead = (DWORD)_get_buffer (myFileHandle, cBuffer,
1158 (DWORD)NByte, TRUE, myIO & FLAG_SOCKET);
1160 if ( ( int )dwBytesRead == -1 ) { // an error occured
1162 _osd_wnt_set_error ( myError, OSD_WFile );
1166 } else if ( dwBytesRead == 0 ) { // connection closed - set end-of-file flag
1174 status = _get_line ( cBuffer, dwBytesRead );
1176 dwSeekPos = LODWORD( status );
1177 eos = ( Standard_CString )HIDWORD( status );
1180 if ( (__int64) status == (__int64) -1 ) { // last character in the buffer is <CR> -
1182 if ( status == 0xFFFFFFFFFFFFFFFF ) { // last character in the buffer is <CR> -
1183 // peek next character to see if it is a <LF>
1186 NbyteRead = dwBytesRead; // (LD) always fits this case.
1188 dwDummy = _get_buffer (myFileHandle, ppeekChar, 1, TRUE, myIO & FLAG_SOCKET);
1189 if ( ( int )dwDummy == -1 ) { // an error occured
1191 _osd_wnt_set_error ( myError, OSD_WFile );
1193 } else if ( dwDummy != 0 ) { // connection closed ?
1195 if ( peekChar == '\n' ) // we got a <CR><LF> sequence
1197 dwBytesRead++ ; // (LD) we have to jump <LF>
1204 if ( dwSeekPos != 0 )
1205 dwBytesRead = dwBytesRead + dwSeekPos;
1207 NbyteRead = ( Standard_Integer )( eos - cBuffer );
1211 // Don't rewrite datas in cBuffer.
1213 Standard_PCharacter cDummyBuffer = new Standard_Character[ NByte + 3 ];
1215 // remove pending input
1216 _get_buffer (myFileHandle, cDummyBuffer, dwBytesRead, FALSE, myIO & FLAG_SOCKET);
1217 delete [] cDummyBuffer ;
1223 RAISE( TEXT( "OSD_File :: ReadLine (): incorrect call - file is a directory" ) );
1225 if ( !Failed () && !IsAtEnd () )
1229 delete [] (Standard_PCharacter)cBuffer;
1231 } // end OSD_File :: ReadLine
1233 // --------------------------------------------------------------------------
1234 // Read content of a file
1235 // --------------------------------------------------------------------------
1237 void OSD_File :: Read (
1238 Standard_Address& Buffer,
1239 const Standard_Integer Nbyte, Standard_Integer& Readbyte
1244 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1245 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1248 TEST_RAISE( TEXT( "Read" ) );
1250 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1252 RAISE( TEXT( "OSD_File :: Read (): attempt to read from write only pipe" ) );
1254 if (!ReadFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesRead, NULL)) {
1256 _osd_wnt_set_error ( myError, OSD_WFile );
1259 } else if ( dwBytesRead == 0 )
1267 Readbyte = ( Standard_Integer )dwBytesRead;
1269 } // end OSD_File :: Read
1271 void OSD_File :: Write (
1272 const TCollection_AsciiString& Buffer,
1273 const Standard_Integer Nbyte
1276 Write ( ( Standard_Address )Buffer.ToCString (), Nbyte );
1278 } // end OSD_File :: Write
1280 // --------------------------------------------------------------------------
1281 // Write content of a file
1282 // --------------------------------------------------------------------------
1284 void OSD_File :: Write (
1285 const Standard_Address Buffer,
1286 const Standard_Integer Nbyte
1289 DWORD dwBytesWritten;
1291 TEST_RAISE( TEXT( "Write" ) );
1293 if ( myIO & FLAG_PIPE && myIO & FLAG_READ_PIPE )
1295 RAISE( TEXT( "OSD_File :: Write (): attempt to write to read only pipe" ) );
1297 if (!WriteFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesWritten, NULL) ||
1298 dwBytesWritten != (DWORD)Nbyte)
1300 _osd_wnt_set_error ( myError, OSD_WFile );
1302 } // end OSD_File :: Write
1304 void OSD_File :: Seek (
1305 const Standard_Integer Offset, const OSD_FromWhere Whence
1308 DWORD dwMoveMethod = 0;
1310 TEST_RAISE( TEXT( "Seek" ) );
1312 if ( myIO & FLAG_FILE || myIO & FLAG_DIRECTORY ) {
1316 case OSD_FromBeginning:
1318 dwMoveMethod = FILE_BEGIN;
1324 dwMoveMethod = FILE_CURRENT;
1330 dwMoveMethod = FILE_END;
1336 RAISE( TEXT( "OSD_File :: Seek (): invalid parameter" ) );
1340 if (SetFilePointer (myFileHandle, (LONG)Offset, NULL, dwMoveMethod) == 0xFFFFFFFF)
1342 _osd_wnt_set_error ( myError, OSD_WFile );
1348 } // end OSD_File :: Seek
1350 // --------------------------------------------------------------------------
1352 // --------------------------------------------------------------------------
1354 void OSD_File :: Close () {
1356 TEST_RAISE( TEXT( "Close" ) );
1358 CloseHandle (myFileHandle);
1360 myFileHandle = INVALID_HANDLE_VALUE;
1363 } // end OSD_File :: Close
1365 // --------------------------------------------------------------------------
1366 // Test if at end of file
1367 // --------------------------------------------------------------------------
1369 Standard_Boolean OSD_File :: IsAtEnd () {
1371 TEST_RAISE( TEXT( "IsAtEnd" ) );
1373 if (myIO & FLAG_EOF)
1374 return Standard_True ;
1375 return Standard_False ;
1377 } // end OSD_File :: IsAtEnd
1379 OSD_KindFile OSD_File :: KindOfFile () const {
1381 OSD_KindFile retVal;
1382 Standard_Integer flags;
1384 if (myFileHandle == INVALID_HANDLE_VALUE) {
1386 TCollection_AsciiString fName;
1388 myPath.SystemName ( fName );
1390 if ( fName.IsEmpty () )
1392 RAISE( TEXT( "OSD_File :: KindOfFile (): incorrent call - no filename given" ) );
1394 flags = _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE);
1400 switch ( flags & FLAG_TYPE ) {
1408 case FLAG_DIRECTORY:
1410 retVal = OSD_DIRECTORY;
1416 retVal = OSD_SOCKET;
1422 retVal = OSD_UNKNOWN;
1428 } // end OSD_File :: KindOfFile
1432 OSD_File OSD_File :: BuildTemporary () {
1438 char tmpbuf [MAX_PATH];
1439 if (GetTempPath (MAX_PATH, tmpbuf) == 0)
1441 perror ("ERROR in GetTempPath");
1444 char tmpbuf2 [MAX_PATH];
1445 if (GetTempFileName (tmpbuf, NULL, 0, tmpbuf2) == 0)
1447 perror ("ERROR in GetTempFileName");
1450 TCollection_AsciiString fileName ( tmpbuf2 );
1452 TCollection_AsciiString fileName ( _ttmpnam ( NULL ) );
1455 OSD_Path filePath ( fileName );
1457 retVal.SetPath ( filePath );
1458 retVal.Build ( OSD_ReadWrite, prt );
1462 } // end OSD_File :: BuildTemporary
1466 //-------------------------------------------------debutpri???980424
1468 typedef struct _osd_wnt_key {
1476 OSD_File OSD_File :: BuildTemporary () {
1481 TCHAR tmpPath[ MAX_PATH ];
1483 OSD_WNT_KEY regKey[ 2 ] = {
1485 { HKEY_LOCAL_MACHINE,
1486 TEXT( "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" )
1489 TEXT( ".DEFAULT\\Environment" )
1494 for ( int i = 0; i < 2; ++i ) {
1497 regKey[ i ].hKey, regKey[ i ].keyPath, 0, KEY_QUERY_VALUE, &hKey
1504 if ( RegQueryValueEx (
1505 hKey, TEXT( "TEMP" ), NULL, &dwType, NULL, &dwSize
1509 LPTSTR kVal = ( LPTSTR )HeapAlloc (
1510 GetProcessHeap (), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS,
1511 dwSize + sizeof ( TCHAR )
1514 RegQueryValueEx ( hKey, TEXT( "TEMP" ), NULL, &dwType, ( LPBYTE )kVal, &dwSize );
1516 if ( dwType == REG_EXPAND_SZ )
1518 ExpandEnvironmentStrings ( kVal, tmpPath, MAX_PATH );
1522 lstrcpy ( tmpPath, kVal );
1524 HeapFree ( GetProcessHeap (), 0, ( LPVOID )kVal );
1529 RegCloseKey ( hKey );
1537 if ( !fOK ) lstrcpy ( tmpPath, TEXT( "./" ) );
1539 GetTempFileName ( tmpPath, "CSF", 0, tmpPath );
1541 retVal.SetPath ( OSD_Path ( tmpPath ) );
1542 retVal.Build ( OSD_ReadWrite, prt );
1546 } // end OSD_File :: BuildTemporary
1550 //-------------------------------------------------finpri???980424
1552 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1555 #define __leave return
1558 void OSD_File :: SetLock ( const OSD_LockType Lock ) {
1563 TEST_RAISE( TEXT( "SetLock" ) );
1565 ZeroMemory ( &ovlp, sizeof ( OVERLAPPED ) );
1569 if ( ( myLock = Lock ) == OSD_NoLock ) {
1574 } else if ( myLock == OSD_ReadLock || myLock == OSD_ExclusiveLock ) {
1576 dwFlags = LOCKFILE_EXCLUSIVE_LOCK;
1582 LARGE_INTEGER aSize;
1583 aSize.QuadPart = Size();
1584 if (!LockFileEx (myFileHandle, dwFlags, 0, aSize.LowPart, aSize.HighPart, &ovlp)) {
1586 _osd_wnt_set_error ( myError, OSD_WFile );
1591 ImperativeFlag = Standard_True;
1598 leave: ; // added for VisualAge
1600 } // end OSD_File :: SetLock
1602 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1608 void OSD_File :: UnLock () {
1610 TEST_RAISE( TEXT( "Unlock" ) );
1612 if ( ImperativeFlag ) {
1614 LARGE_INTEGER aSize;
1615 aSize.QuadPart = Size();
1616 if (!UnlockFile (myFileHandle, 0, 0, aSize.LowPart, aSize.HighPart))
1618 _osd_wnt_set_error ( myError, OSD_WFile );
1620 ImperativeFlag = Standard_False;
1624 } // end OSD_File :: UnLock
1626 OSD_LockType OSD_File :: GetLock () {
1630 } // end OSD_File :: GetLock
1632 Standard_Boolean OSD_File :: IsLocked () {
1634 TEST_RAISE( TEXT( "IsLocked" ) );
1636 return ImperativeFlag;
1638 } // end OSD_File :: IsLocked
1641 // --------------------------------------------------------------------------
1642 // Return size of a file
1643 // --------------------------------------------------------------------------
1645 Standard_Size OSD_File :: Size () {
1647 Standard_Integer retVal;
1649 TEST_RAISE( TEXT( "Size" ) );
1651 LARGE_INTEGER aSize;
1653 retVal = GetFileSizeEx (myFileHandle, &aSize);
1657 _osd_wnt_set_error ( myError, OSD_WFile );
1659 return (Standard_Size)aSize.QuadPart;
1661 } // end OSD_File :: Size
1663 // --------------------------------------------------------------------------
1664 // Print contains of a file
1665 // --------------------------------------------------------------------------
1667 void OSD_File :: Print ( const OSD_Printer& WhichPrinter ) {
1669 if (myFileHandle != INVALID_HANDLE_VALUE)
1671 RAISE( TEXT( "OSD_File :: Print (): incorrect call - file opened" ) );
1673 TCollection_AsciiString pName, fName;
1675 WhichPrinter.Name ( pName );
1676 myPath.SystemName ( fName );
1678 if ( !_osd_print ( (Standard_PCharacter)pName.ToCString (), fName.ToCString () ) )
1680 _osd_wnt_set_error ( myError, OSD_WFile );
1682 } // end OSD_File :: Print
1684 // --------------------------------------------------------------------------
1685 // Test if a file is open
1686 // --------------------------------------------------------------------------
1688 Standard_Boolean OSD_File :: IsOpen () const {
1690 return myFileHandle != INVALID_HANDLE_VALUE;
1692 } // end OSD_File :: IsOpen
1694 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1697 #define __leave return retVal
1700 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd (
1701 const OSD_Protection& prot, BOOL fDir, char* fName
1707 HANDLE hProcess = NULL;
1712 DWORD dwAccessAdmin;
1713 DWORD dwAccessGroup;
1714 DWORD dwAccessOwner;
1715 DWORD dwAccessWorld;
1716 DWORD dwAccessAdminDir;
1717 DWORD dwAccessGroupDir;
1718 DWORD dwAccessOwnerDir;
1719 DWORD dwAccessWorldDir;
1720 DWORD dwACLsize = sizeof ( ACL );
1722 PTOKEN_OWNER pTkOwner = NULL;
1723 PTOKEN_GROUPS pTkGroups = NULL;
1724 PTOKEN_PRIMARY_GROUP pTkPrimaryGroup = NULL;
1725 PSECURITY_DESCRIPTOR retVal = NULL;
1726 PSECURITY_DESCRIPTOR pfSD = NULL;
1734 if ( !OpenProcessToken (
1735 GetCurrentProcess (), TOKEN_QUERY, &hProcess
1739 if ( ( pTkGroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
1740 hProcess, TokenGroups
1745 if ( ( pTkOwner = ( PTOKEN_OWNER )GetTokenInformationEx (
1746 hProcess, TokenOwner
1751 if ( ( pTkPrimaryGroup = ( PTOKEN_PRIMARY_GROUP )GetTokenInformationEx (
1752 hProcess, TokenPrimaryGroup
1759 if ( fName == NULL )
1761 pSIDowner = pTkOwner -> Owner;
1765 pfSD = GetFileSecurityEx ( fName, OWNER_SECURITY_INFORMATION );
1767 if ( pfSD == NULL || !GetSecurityDescriptorOwner ( pfSD, &pSIDowner, &fDummy ) ) {
1776 pSIDadmin = AdminSid ();
1777 pSIDworld = WorldSid ();
1779 dwAccessAdmin = _get_access_mask ( prot.System () );
1780 dwAccessGroup = _get_access_mask ( prot.Group () );
1781 dwAccessOwner = _get_access_mask ( prot.User () );
1782 dwAccessWorld = _get_access_mask ( prot.World () );
1784 dwAccessAdminDir = _get_dir_access_mask ( prot.System () );
1785 dwAccessGroupDir = _get_dir_access_mask ( prot.Group () );
1786 dwAccessOwnerDir = _get_dir_access_mask ( prot.User () );
1787 dwAccessWorldDir = _get_dir_access_mask ( prot.World () );
1789 if ( dwAccessGroup != 0 ) {
1791 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1793 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1795 if ( !NtPredefinedSid ( pSIDtemp ) &&
1796 !EqualSid ( pSIDtemp, pSIDworld ) &&
1797 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1798 IsValidSid ( pSIDtemp )
1801 dwACLsize += ( ( GetLengthSid ( pSIDtemp ) + ACE_HEADER_SIZE ) << j );
1807 dwACLsize += ( ( ( GetLengthSid ( pSIDowner ) + ACE_HEADER_SIZE ) << j ) +
1808 ( ( GetLengthSid ( pSIDadmin ) + ACE_HEADER_SIZE ) << j ) +
1809 ( ( GetLengthSid ( pSIDworld ) + ACE_HEADER_SIZE ) << j )
1812 if ( ( pACL = CreateAcl ( dwACLsize ) ) == NULL ) __leave;
1814 if ( dwAccessAdmin != 0 )
1816 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1823 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1827 pFileACE -> dwMask = dwAccessAdminDir;
1828 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1829 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1833 FreeAce ( pFileACE );
1837 if ( dwAccessOwner != 0 )
1839 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1840 dwAccessOwner, 0, pSIDowner
1845 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1849 pFileACE -> dwMask = dwAccessOwnerDir;
1850 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1851 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1855 FreeAce ( pFileACE );
1859 if ( dwAccessWorld != 0 )
1861 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1862 dwAccessWorld, 0, pSIDworld
1867 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1871 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1872 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1876 FreeAce ( pFileACE );
1880 if ( dwAccessGroup != 0 ) {
1882 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1884 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1886 if ( !NtPredefinedSid ( pSIDtemp ) &&
1887 !EqualSid ( pSIDtemp, pSIDworld ) &&
1888 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1889 IsValidSid ( pSIDtemp )
1892 if ( dwAccessGroup != 0 )
1894 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1895 dwAccessGroup, 0, pSIDtemp
1900 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1904 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1905 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1909 FreeAce ( pFileACE );
1919 if ( ( retVal = AllocSD () ) == NULL ) __leave;
1921 if ( !SetSecurityDescriptorDacl ( retVal, TRUE, pACL, TRUE ) ) __leave;
1931 if ( retVal != NULL )
1935 else if ( pACL != NULL )
1943 if ( hProcess != NULL ) CloseHandle ( hProcess );
1944 if ( pTkOwner != NULL ) FreeTokenInformation ( pTkOwner );
1945 if ( pTkGroups != NULL ) FreeTokenInformation ( pTkGroups );
1946 if ( pTkPrimaryGroup != NULL ) FreeTokenInformation ( pTkPrimaryGroup );
1947 if ( pfSD != NULL ) FreeFileSecurity ( pfSD );
1952 leave: ; // added for VisualAge
1957 } // end _osd_wnt_protection_to_sd */
1959 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1965 static void __fastcall _test_raise ( HANDLE hFile, Standard_CString str ) {
1967 Standard_Character buff[ 64 ];
1969 if (hFile == INVALID_HANDLE_VALUE) {
1971 _tcscpy ( buff, TEXT( "OSD_File :: " ) );
1972 _tcscat ( buff, str );
1973 _tcscat ( buff, TEXT( " (): wrong access" ) );
1975 Standard_ProgramError :: Raise ( buff );
1979 } // end _test_raise
1981 // Modified so that we have <nl> at end of line if we have read <nl> or <cr>
1982 // by LD 17 dec 98 for B4.4
1984 static DWORDLONG __fastcall _get_line ( Standard_PCharacter& buffer, DWORD dwBuffSize ) {
1987 Standard_PCharacter ptr;
1989 buffer[ dwBuffSize ] = 0;
1992 while ( *ptr != 0 ) {
1994 if ( *ptr == TEXT( '\n' ) ) {
1996 ptr++ ; // jump newline char.
1998 retVal = ptr - buffer - dwBuffSize;
1999 retVal &= 0x0000000FFFFFFFF;// import 32-bit to 64-bit
2001 retVal = (DWORDLONG) ( (unsigned __int64) retVal | (((unsigned __int64) ptr) << 32) );
2003 retVal |= ( ( ( DWORDLONG )( DWORD )ptr ) << 32 );
2007 } else if ( *ptr == TEXT( '\r' ) && ptr[ 1 ] == TEXT( '\n' ) ) {
2009 *(ptr++) = '\n' ; // Substitue carriage return by newline.
2011 retVal = ptr + 1 - buffer - dwBuffSize;
2012 retVal &= 0x0000000FFFFFFFF;// import 32-bit to 64-bit
2014 retVal = (DWORDLONG) ( (unsigned __int64) retVal | (((unsigned __int64) ptr) << 32) );
2016 retVal |= ( ( ( DWORDLONG )( DWORD )ptr ) << 32 );
2020 } else if ( *ptr == TEXT( '\r' ) && ptr[ 1 ] == 0 ) {
2021 *ptr = '\n' ; // Substitue carriage return by newline
2024 return (DWORDLONG) (__int64) (-1);
2026 return 0xFFFFFFFFFFFFFFFF;
2034 retVal = (DWORDLONG) ( ( (unsigned __int64) ((DWORD) buffer + dwBuffSize) ) << 32 );
2035 retVal = (DWORDLONG) ( (unsigned __int64) retVal & (((unsigned __int64) 0xFFFFFFFF) << 32) );
2037 retVal = ( ( ( DWORDLONG )( ( DWORD )buffer + dwBuffSize ) ) << 32 );
2038 retVal &= 0xFFFFFFFF00000000;
2045 static int __fastcall _get_buffer (
2047 Standard_PCharacter& buffer,
2049 BOOL fPeek, BOOL fSocket
2059 flags = fPeek ? MSG_PEEK : 0;
2061 retVal = recv ( ( SOCKET )hChannel, buffer, ( int )dwSize, flags );
2063 if ( retVal == SOCKET_ERROR ) retVal = -1;
2069 if ( !PeekNamedPipe (
2070 hChannel, buffer, dwSize, &dwBytesRead, &dwDummy, &dwDummy
2071 ) && GetLastError () != ERROR_BROKEN_PIPE
2078 retVal = ( int )dwBytesRead;
2082 if ( !ReadFile ( hChannel, buffer, dwSize, &dwBytesRead, NULL ) )
2088 retVal = ( int )dwBytesRead;
2096 } // end _get_buffer
2099 static DWORD __fastcall _get_access_mask ( OSD_SingleProtection prt ) {
2113 retVal = FILE_GENERIC_READ;
2119 retVal = FILE_GENERIC_WRITE;
2125 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
2131 retVal = FILE_GENERIC_EXECUTE;
2137 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
2143 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2149 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2161 retVal = FILE_GENERIC_READ | DELETE;
2167 retVal = FILE_GENERIC_WRITE | DELETE;
2173 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE;
2179 retVal = FILE_GENERIC_EXECUTE | DELETE;
2185 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE;
2191 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2197 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2203 RAISE( TEXT( "_get_access_mask (): incorrect parameter" ) );
2209 } // end _get_access_mask
2211 static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt ) {
2225 retVal = GENERIC_READ;
2231 retVal = GENERIC_WRITE;
2237 retVal = GENERIC_READ | GENERIC_WRITE;
2243 retVal = GENERIC_EXECUTE;
2249 retVal = GENERIC_READ | GENERIC_EXECUTE;
2255 retVal = GENERIC_WRITE | GENERIC_EXECUTE;
2261 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE;
2273 retVal = GENERIC_READ | DELETE;
2279 retVal = GENERIC_WRITE | DELETE;
2285 retVal = GENERIC_READ | GENERIC_WRITE | DELETE;
2291 retVal = GENERIC_EXECUTE | DELETE;
2297 retVal = GENERIC_READ | GENERIC_EXECUTE | DELETE;
2303 retVal = GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2309 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2315 RAISE( TEXT( "_get_dir_access_mask (): incorrect parameter" ) );
2321 } // end _get_dir_access_mask
2323 static HANDLE __fastcall _open_file (
2324 Standard_CString fName,
2326 DWORD dwOptions, LPBOOL fNew
2329 HANDLE retVal = INVALID_HANDLE_VALUE;
2330 DWORD dwDesiredAccess = 0;
2331 DWORD dwCreationDistribution;
2337 dwDesiredAccess = GENERIC_READ;
2343 dwDesiredAccess = GENERIC_WRITE;
2349 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
2355 RAISE( TEXT( "_open_file (): incorrect parameter" ) );
2359 dwCreationDistribution = ( dwOptions != OPEN_NEW ) ? OPEN_EXISTING : CREATE_ALWAYS;
2361 retVal = CreateFile (
2362 fName, dwDesiredAccess,
2363 FILE_SHARE_READ | FILE_SHARE_WRITE,
2364 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2367 if ( retVal == INVALID_HANDLE_VALUE &&
2368 dwOptions == OPEN_APPEND &&
2369 GetLastError () == ERROR_FILE_NOT_FOUND
2373 dwCreationDistribution = CREATE_ALWAYS;
2375 retVal = CreateFile (
2376 fName, dwDesiredAccess,
2377 FILE_SHARE_READ | FILE_SHARE_WRITE,
2378 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2389 Standard_Integer __fastcall _get_file_type (
2390 Standard_CString fName, HANDLE fileHandle
2393 Standard_Integer retVal = 0;
2397 fileType = (fileHandle == INVALID_HANDLE_VALUE ?
2398 FILE_TYPE_DISK : GetFileType (fileHandle));
2400 switch ( fileType ) {
2402 case FILE_TYPE_UNKNOWN:
2404 retVal = FLAG_SOCKET;
2408 case FILE_TYPE_DISK:
2410 if ( ( dwType = GetFileAttributes ( fName ) ) != 0xFFFFFFFF )
2412 retVal = dwType & FILE_ATTRIBUTE_DIRECTORY ? FLAG_DIRECTORY : FLAG_FILE;
2416 retVal = 0x80000000;
2420 case FILE_TYPE_CHAR:
2422 retVal = FLAG_DEVICE;
2426 case FILE_TYPE_PIPE:
2436 } // end _get_file_type
2438 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2441 #define __leave return retVal
2444 BOOL __fastcall _osd_wnt_sd_to_protection (
2445 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL fDir
2456 DWORD dwAccessOwner = 0;
2457 DWORD dwAccessGroup = 0;
2458 DWORD dwAccessAdmin = 0;
2459 DWORD dwAccessWorld = 0;
2460 BOOL retVal = FALSE;
2461 GET_PROT_FUNC _get_prot_func = fDir ? &_get_protection_dir : &_get_protection;
2465 if ( !GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) ) __leave;
2467 if ( !GetSecurityDescriptorDacl ( pSD, &fPresent, &pACL, &fDefaulted ) ||
2471 if ( pSIDowner == NULL || pACL == NULL ) {
2473 SetLastError ( ERROR_NO_SECURITY_ON_OBJECT );
2478 pSIDadmin = AdminSid ();
2479 pSIDworld = WorldSid ();
2481 for ( i = 0; i < ( int )pACL -> AceCount; ++i ) {
2483 if ( GetAce ( pACL, i, &pACE ) ) {
2485 if ( EqualSid ( pSIDowner, GET_SID( pACE ) ) )
2487 dwAccessOwner = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2488 0 : *GET_MSK( pACE );
2490 else if ( EqualSid ( pSIDadmin, GET_SID( pACE ) ) )
2492 dwAccessAdmin = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2493 0 : *GET_MSK( pACE );
2495 else if ( EqualSid ( pSIDworld, GET_SID( pACE ) ) )
2497 dwAccessWorld = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2498 0 : *GET_MSK( pACE );
2502 dwAccessGroup = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2503 0 : *GET_MSK( pACE );
2510 ( *_get_prot_func ) ( dwAccessAdmin ),
2511 ( *_get_prot_func ) ( dwAccessOwner ),
2512 ( *_get_prot_func ) ( dwAccessGroup ),
2513 ( *_get_prot_func ) ( dwAccessWorld )
2523 leave: ; // added for VisualAge
2528 } // end _osd_wnt_sd_to_protection
2530 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2536 static OSD_SingleProtection __fastcall _get_protection ( DWORD mask ) {
2538 OSD_SingleProtection retVal;
2542 case FILE_GENERIC_READ:
2548 case FILE_GENERIC_WRITE:
2554 case FILE_GENERIC_READ | FILE_GENERIC_WRITE:
2560 case FILE_GENERIC_EXECUTE:
2566 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE:
2572 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2578 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2590 case FILE_GENERIC_READ | DELETE:
2596 case FILE_GENERIC_WRITE | DELETE:
2602 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE:
2608 case FILE_GENERIC_EXECUTE | DELETE:
2614 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE:
2620 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2626 case FILE_ALL_ACCESS:
2627 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2642 } // end _get_protection
2644 static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD mask ) {
2646 OSD_SingleProtection retVal;
2662 case GENERIC_READ | GENERIC_WRITE:
2668 case GENERIC_EXECUTE:
2674 case GENERIC_READ | GENERIC_EXECUTE:
2680 case GENERIC_WRITE | GENERIC_EXECUTE:
2686 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE:
2698 case GENERIC_READ | DELETE:
2704 case GENERIC_WRITE | DELETE:
2710 case GENERIC_READ | GENERIC_WRITE | DELETE:
2716 case GENERIC_EXECUTE | DELETE:
2722 case GENERIC_READ | GENERIC_EXECUTE | DELETE:
2728 case GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2734 case FILE_ALL_ACCESS:
2735 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2750 } // end _get_protection_dir
2752 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2755 #define __leave return fOK
2758 BOOL __fastcall _osd_print (const Standard_PCharacter pName, Standard_CString fName ) {
2761 HANDLE hPrinter = NULL;
2762 BYTE jobInfo[ MAX_PATH + sizeof ( DWORD ) ];
2763 DWORD dwNeeded, dwCode = 0;
2769 if ( !OpenPrinter ( Standard_PCharacter(pName), &hPrinter, NULL ) ) {
2777 hPrinter, 1, jobInfo, MAX_PATH + sizeof ( DWORD ), &dwNeeded
2784 fName, ( ( ADDJOB_INFO_1* )jobInfo ) -> Path, FALSE
2789 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId
2802 DWORD dwBytesNeeded;
2804 dwCode = GetLastError ();
2806 if ( fJob && hPrinter != NULL ) {
2809 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId, 1,
2810 info, 1024, &dwBytesNeeded
2813 if ( fJob ) SetJob (
2815 ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId,
2816 1, info, JOB_CONTROL_CANCEL
2823 if ( hPrinter != NULL ) ClosePrinter ( hPrinter );
2828 leave: ; // added for VisualAge
2831 if ( !fOK ) SetLastError ( dwCode );
2837 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2843 Standard_Boolean OSD_File::IsReadable()
2845 TCollection_AsciiString FileName ;
2848 myPath.SystemName(FileName) ;
2849 Channel = _open_file(FileName.ToCString(), OSD_ReadOnly, OPEN_OLD) ;
2850 if (Channel == INVALID_HANDLE_VALUE)
2851 return Standard_False ;
2853 CloseHandle (Channel) ;
2854 return Standard_True ;
2859 Standard_Boolean OSD_File::IsWriteable()
2861 TCollection_AsciiString FileName ;
2864 myPath.SystemName(FileName) ;
2865 Channel = _open_file(FileName.ToCString(), OSD_ReadWrite, OPEN_OLD) ;
2866 if (Channel == INVALID_HANDLE_VALUE)
2867 return Standard_False ;
2869 CloseHandle (Channel) ;
2870 return Standard_True ;
2874 Standard_Boolean OSD_File::IsExecutable()
2876 return IsReadable() ;
2878 // if (_access(FileName.ToCString(),0))
2884 // ---------------------------------------------------------------------
2885 // Read lines in a file while it is increasing.
2886 // Each line is terminated with a <nl>.
2887 // ---------------------------------------------------------------------
2891 Standard_Boolean OSD_File::ReadLastLine(TCollection_AsciiString& aLine,const Standard_Integer aDelay,const Standard_Integer aNbTries)
2893 static Standard_Integer MaxLength = 1000 ;
2894 Standard_Integer Len ;
2895 Standard_Integer Count = aNbTries ;
2898 return Standard_False ;
2900 ReadLine(aLine, MaxLength, Len) ;
2901 if (!aLine.IsEmpty())
2902 return Standard_True ;
2904 return Standard_False ;
2905 OSD::SecSleep(aDelay) ;
2910 Standard_Boolean OSD_File::Edit()
2912 cout << "Function OSD_File::Edit() not yet implemented." << endl;
2913 return Standard_False ;