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>
831 #include <TCollection_ExtendedString.hxx>
837 #if defined(__CYGWIN32__) || defined(__MINGW32__)
842 #pragma comment( lib, "WSOCK32.LIB" )
843 #pragma comment( lib, "WINSPOOL.LIB" )
845 #define ACE_HEADER_SIZE ( sizeof ( ACCESS_ALLOWED_ACE ) - sizeof ( DWORD ) )
847 #define RAISE( arg ) Standard_ProgramError :: Raise ( ( arg ) )
848 #define TEST_RAISE( arg ) _test_raise ( myFileHandle, ( arg ) )
852 #define OPEN_APPEND 2
854 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
855 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, wchar_t* = NULL );
856 BOOL __fastcall _osd_wnt_sd_to_protection (
857 PSECURITY_DESCRIPTOR, OSD_Protection&, BOOL
859 BOOL __fastcall _osd_print (const Standard_PCharacter, const wchar_t* );
861 static void __fastcall _test_raise ( HANDLE, Standard_CString );
862 static DWORDLONG __fastcall _get_line ( Standard_PCharacter&, DWORD );
863 static int __fastcall _get_buffer ( HANDLE, Standard_PCharacter&, DWORD, BOOL, BOOL );
864 static DWORD __fastcall _get_access_mask ( OSD_SingleProtection );
865 static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt );
866 static HANDLE __fastcall _open_file ( Standard_CString, OSD_OpenMode, DWORD, LPBOOL = NULL );
868 static OSD_SingleProtection __fastcall _get_protection ( DWORD );
869 static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD );
871 typedef OSD_SingleProtection ( __fastcall *GET_PROT_FUNC ) ( DWORD );
873 Standard_Integer __fastcall _get_file_type ( Standard_CString, HANDLE );
875 // ---------------------------------------------------------------------
876 // Create an empty file object
877 // ---------------------------------------------------------------------
879 OSD_File :: OSD_File ()
881 ImperativeFlag = Standard_False;
885 myFileHandle = INVALID_HANDLE_VALUE;
886 } // end constructor ( 1 )
888 // ---------------------------------------------------------------------
889 // Create and initialize a file object
890 // ---------------------------------------------------------------------
892 OSD_File :: OSD_File ( const OSD_Path& Name ) : OSD_FileNode ( Name )
894 ImperativeFlag = Standard_False;
899 myFileHandle = INVALID_HANDLE_VALUE;
900 } // end constructor ( 2 )
902 // protect against occasional use of myFileHande in Windows code
903 #define myFileChannel myFileChannel_is_only_for_Linux
905 // ---------------------------------------------------------------------
906 // Build a file if it doesn't exist or create again if it already exists
907 // ---------------------------------------------------------------------
909 void OSD_File :: Build (
910 const OSD_OpenMode Mode, const OSD_Protection& Protect
913 TCollection_AsciiString fName;
915 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
916 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
919 if (myFileHandle != INVALID_HANDLE_VALUE)
921 RAISE( "OSD_File :: Build (): incorrect call - file already opened" );
924 myPath.SystemName ( fName );
926 if ( fName.IsEmpty () )
928 RAISE( "OSD_File :: Build (): incorrent call - no filename given" );
930 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_NEW );
932 if (myFileHandle == INVALID_HANDLE_VALUE)
934 _osd_wnt_set_error ( myError, OSD_WFile );
938 SetProtection ( Protect );
943 } // end OSD_File :: Build
947 // ---------------------------------------------------------------------
949 // ---------------------------------------------------------------------
951 void OSD_File :: Open (const OSD_OpenMode Mode, const OSD_Protection& /*Protect*/)
954 TCollection_AsciiString fName;
957 if (myFileHandle != INVALID_HANDLE_VALUE)
959 RAISE( "OSD_File :: Open (): incorrect call - file already opened" );
962 myPath.SystemName ( fName );
964 if ( fName.IsEmpty () )
966 RAISE( "OSD_File :: Open (): incorrent call - no filename given" );
968 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_OLD );
970 if (myFileHandle == INVALID_HANDLE_VALUE) {
972 _osd_wnt_set_error ( myError, OSD_WFile );
976 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
978 } // end OSD_File :: Open
980 // ---------------------------------------------------------------------
981 // Append to an existing file
982 // ---------------------------------------------------------------------
984 void OSD_File :: Append (
985 const OSD_OpenMode Mode, const OSD_Protection& Protect
989 TCollection_AsciiString fName;
991 if (myFileHandle != INVALID_HANDLE_VALUE)
993 RAISE( "OSD_File :: Append (): incorrect call - file already opened" );
996 myPath.SystemName ( fName );
998 if ( fName.IsEmpty () )
1000 RAISE( "OSD_File :: Append (): incorrent call - no filename given" );
1002 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_APPEND, &fNew );
1004 if (myFileHandle == INVALID_HANDLE_VALUE)
1006 _osd_wnt_set_error ( myError, OSD_WFile );
1012 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
1013 Seek ( 0, OSD_FromEnd );
1017 SetProtection ( Protect );
1024 } // end OSD_File :: Append
1026 // ---------------------------------------------------------------------
1027 // Read content of a file
1028 // ---------------------------------------------------------------------
1030 void OSD_File :: Read (
1031 TCollection_AsciiString& Buffer, const Standard_Integer Nbyte
1034 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1036 cout << " OSD_File::Read : it is a directory " << endl;
1039 // Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1042 Standard_Integer NbyteRead;
1043 Standard_Address buff;
1045 TEST_RAISE( "Read" );
1047 buff = ( Standard_Address )new Standard_Character[ Nbyte + 1 ];
1049 Read ( buff, Nbyte, NbyteRead );
1051 ( ( Standard_PCharacter )buff )[ NbyteRead ] = 0;
1053 if ( NbyteRead != 0 )
1055 Buffer = ( Standard_PCharacter )buff;
1063 } // end OSD_File :: Read
1065 // ---------------------------------------------------------------------
1066 // Read a line from a file
1067 // ---------------------------------------------------------------------
1069 // Modified so that we have <nl> at end of line if we have read <nl> or <cr>
1071 // by LD 17 dec 98 for B4.4
1073 void OSD_File :: ReadLine (
1074 TCollection_AsciiString& Buffer,
1075 const Standard_Integer NByte, Standard_Integer& NbyteRead
1081 Standard_Character peekChar;
1082 Standard_PCharacter ppeekChar;
1083 Standard_PCharacter cBuffer;
1084 Standard_CString eos;
1087 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1088 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1091 TEST_RAISE( "ReadLine" );
1093 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1095 RAISE( "OSD_File :: ReadLine (): attempt to read from write only pipe" );
1097 // +----> leave space for end-of-string
1098 // | plus <CR><LF> sequence
1101 ppeekChar=&peekChar;
1102 cBuffer = new Standard_Character[ NByte + 3 ];
1104 if ( myIO & FLAG_FILE ) {
1106 if (!ReadFile (myFileHandle, cBuffer, (DWORD)NByte, &dwBytesRead, NULL)) { // an error occured
1108 _osd_wnt_set_error ( myError, OSD_WFile );
1112 } else if ( dwBytesRead == 0 ) { // end-of-file reached
1119 myIO &= ~FLAG_EOF ; // if the file increased since last read (LD)
1120 status = _get_line ( cBuffer, dwBytesRead );
1122 dwSeekPos = LODWORD( status );
1123 eos = ( Standard_CString )HIDWORD( status );
1125 if ( (__int64) status == (__int64) -1 ) { // last character in the buffer is <CR> -
1127 if ( status == 0xFFFFFFFFFFFFFFFF ) { // last character in the buffer is <CR> -
1128 // peek next character to see if it is a <LF>
1130 if (!ReadFile (myFileHandle, ppeekChar, 1, &dwDummy, NULL)) {
1132 _osd_wnt_set_error ( myError, OSD_WFile );
1134 } else if ( dwDummy != 0 ) { // end-of-file reached ?
1136 if ( peekChar != '\n' ) // if we did not get a <CR><LF> sequence
1138 // adjust file position
1140 SetFilePointer (myFileHandle, -1, NULL, FILE_CURRENT);
1145 NbyteRead = dwBytesRead;
1149 if ( dwSeekPos != 0 )
1150 SetFilePointer (myFileHandle, (LONG)dwSeekPos, NULL, FILE_CURRENT);
1152 NbyteRead = ( Standard_Integer )( eos - cBuffer );
1158 } else if ( myIO & FLAG_SOCKET || myIO & FLAG_PIPE || myIO & FLAG_NAMED_PIPE ) {
1160 dwBytesRead = (DWORD)_get_buffer (myFileHandle, cBuffer,
1161 (DWORD)NByte, TRUE, myIO & FLAG_SOCKET);
1163 if ( ( int )dwBytesRead == -1 ) { // an error occured
1165 _osd_wnt_set_error ( myError, OSD_WFile );
1169 } else if ( dwBytesRead == 0 ) { // connection closed - set end-of-file flag
1177 status = _get_line ( cBuffer, dwBytesRead );
1179 dwSeekPos = LODWORD( status );
1180 eos = ( Standard_CString )HIDWORD( status );
1183 if ( (__int64) status == (__int64) -1 ) { // last character in the buffer is <CR> -
1185 if ( status == 0xFFFFFFFFFFFFFFFF ) { // last character in the buffer is <CR> -
1186 // peek next character to see if it is a <LF>
1189 NbyteRead = dwBytesRead; // (LD) always fits this case.
1191 dwDummy = _get_buffer (myFileHandle, ppeekChar, 1, TRUE, myIO & FLAG_SOCKET);
1192 if ( ( int )dwDummy == -1 ) { // an error occured
1194 _osd_wnt_set_error ( myError, OSD_WFile );
1196 } else if ( dwDummy != 0 ) { // connection closed ?
1198 if ( peekChar == '\n' ) // we got a <CR><LF> sequence
1200 dwBytesRead++ ; // (LD) we have to jump <LF>
1207 if ( dwSeekPos != 0 )
1208 dwBytesRead = dwBytesRead + dwSeekPos;
1210 NbyteRead = ( Standard_Integer )( eos - cBuffer );
1214 // Don't rewrite datas in cBuffer.
1216 Standard_PCharacter cDummyBuffer = new Standard_Character[ NByte + 3 ];
1218 // remove pending input
1219 _get_buffer (myFileHandle, cDummyBuffer, dwBytesRead, FALSE, myIO & FLAG_SOCKET);
1220 delete [] cDummyBuffer ;
1226 RAISE( "OSD_File :: ReadLine (): incorrect call - file is a directory" );
1228 if ( !Failed () && !IsAtEnd () )
1232 delete [] (Standard_PCharacter)cBuffer;
1234 } // end OSD_File :: ReadLine
1236 // --------------------------------------------------------------------------
1237 // Read content of a file
1238 // --------------------------------------------------------------------------
1240 void OSD_File :: Read (
1241 Standard_Address& Buffer,
1242 const Standard_Integer Nbyte, Standard_Integer& Readbyte
1247 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1248 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1251 TEST_RAISE( "Read" );
1253 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1255 RAISE( "OSD_File :: Read (): attempt to read from write only pipe" );
1257 if (!ReadFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesRead, NULL)) {
1259 _osd_wnt_set_error ( myError, OSD_WFile );
1262 } else if ( dwBytesRead == 0 )
1270 Readbyte = ( Standard_Integer )dwBytesRead;
1272 } // end OSD_File :: Read
1274 void OSD_File :: Write (
1275 const TCollection_AsciiString& Buffer,
1276 const Standard_Integer Nbyte
1279 Write ( ( Standard_Address )Buffer.ToCString (), Nbyte );
1281 } // end OSD_File :: Write
1283 // --------------------------------------------------------------------------
1284 // Write content of a file
1285 // --------------------------------------------------------------------------
1287 void OSD_File :: Write (
1288 const Standard_Address Buffer,
1289 const Standard_Integer Nbyte
1292 DWORD dwBytesWritten;
1294 TEST_RAISE( "Write" );
1296 if ( myIO & FLAG_PIPE && myIO & FLAG_READ_PIPE )
1298 RAISE( "OSD_File :: Write (): attempt to write to read only pipe" );
1300 if (!WriteFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesWritten, NULL) ||
1301 dwBytesWritten != (DWORD)Nbyte)
1303 _osd_wnt_set_error ( myError, OSD_WFile );
1305 } // end OSD_File :: Write
1307 void OSD_File :: Seek (
1308 const Standard_Integer Offset, const OSD_FromWhere Whence
1311 DWORD dwMoveMethod = 0;
1313 TEST_RAISE( "Seek" );
1315 if ( myIO & FLAG_FILE || myIO & FLAG_DIRECTORY ) {
1319 case OSD_FromBeginning:
1321 dwMoveMethod = FILE_BEGIN;
1327 dwMoveMethod = FILE_CURRENT;
1333 dwMoveMethod = FILE_END;
1339 RAISE( "OSD_File :: Seek (): invalid parameter" );
1343 if (SetFilePointer (myFileHandle, (LONG)Offset, NULL, dwMoveMethod) == 0xFFFFFFFF)
1345 _osd_wnt_set_error ( myError, OSD_WFile );
1351 } // end OSD_File :: Seek
1353 // --------------------------------------------------------------------------
1355 // --------------------------------------------------------------------------
1357 void OSD_File :: Close () {
1359 TEST_RAISE( "Close" );
1361 CloseHandle (myFileHandle);
1363 myFileHandle = INVALID_HANDLE_VALUE;
1366 } // end OSD_File :: Close
1368 // --------------------------------------------------------------------------
1369 // Test if at end of file
1370 // --------------------------------------------------------------------------
1372 Standard_Boolean OSD_File :: IsAtEnd () {
1374 TEST_RAISE( "IsAtEnd" );
1376 if (myIO & FLAG_EOF)
1377 return Standard_True ;
1378 return Standard_False ;
1380 } // end OSD_File :: IsAtEnd
1382 OSD_KindFile OSD_File :: KindOfFile () const {
1384 OSD_KindFile retVal;
1385 Standard_Integer flags;
1387 if (myFileHandle == INVALID_HANDLE_VALUE) {
1389 TCollection_AsciiString fName;
1391 myPath.SystemName ( fName );
1393 if ( fName.IsEmpty () )
1395 RAISE( "OSD_File :: KindOfFile (): incorrent call - no filename given" );
1397 flags = _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE);
1403 switch ( flags & FLAG_TYPE ) {
1411 case FLAG_DIRECTORY:
1413 retVal = OSD_DIRECTORY;
1419 retVal = OSD_SOCKET;
1425 retVal = OSD_UNKNOWN;
1431 } // end OSD_File :: KindOfFile
1435 OSD_File OSD_File :: BuildTemporary () {
1441 char tmpbuf [MAX_PATH];
1442 if (GetTempPath (MAX_PATH, tmpbuf) == 0)
1444 perror ("ERROR in GetTempPath");
1447 char tmpbuf2 [MAX_PATH];
1448 if (GetTempFileName (tmpbuf, NULL, 0, tmpbuf2) == 0)
1450 perror ("ERROR in GetTempFileName");
1453 TCollection_AsciiString fileName ( tmpbuf2 );
1455 TCollection_AsciiString fileName ( _ttmpnam ( NULL ) );
1458 OSD_Path filePath ( fileName );
1460 retVal.SetPath ( filePath );
1461 retVal.Build ( OSD_ReadWrite, prt );
1465 } // end OSD_File :: BuildTemporary
1469 //-------------------------------------------------debutpri???980424
1471 typedef struct _osd_wnt_key {
1479 OSD_File OSD_File :: BuildTemporary () {
1484 TCHAR tmpPath[ MAX_PATH ];
1486 OSD_WNT_KEY regKey[ 2 ] = {
1488 { HKEY_LOCAL_MACHINE,
1489 "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
1492 ".DEFAULT\\Environment"
1497 for ( int i = 0; i < 2; ++i ) {
1500 regKey[ i ].hKey, regKey[ i ].keyPath, 0, KEY_QUERY_VALUE, &hKey
1507 if ( RegQueryValueEx (
1508 hKey, "TEMP", NULL, &dwType, NULL, &dwSize
1512 LPTSTR kVal = ( LPTSTR )HeapAlloc (
1513 GetProcessHeap (), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS,
1514 dwSize + sizeof ( TCHAR )
1517 RegQueryValueEx ( hKey, "TEMP", NULL, &dwType, ( LPBYTE )kVal, &dwSize );
1519 if ( dwType == REG_EXPAND_SZ )
1521 ExpandEnvironmentStrings ( kVal, tmpPath, MAX_PATH );
1525 lstrcpy ( tmpPath, kVal );
1527 HeapFree ( GetProcessHeap (), 0, ( LPVOID )kVal );
1532 RegCloseKey ( hKey );
1540 if ( !fOK ) lstrcpy ( tmpPath, "./" );
1542 GetTempFileName ( tmpPath, "CSF", 0, tmpPath );
1544 retVal.SetPath ( OSD_Path ( tmpPath ) );
1545 retVal.Build ( OSD_ReadWrite, prt );
1549 } // end OSD_File :: BuildTemporary
1553 //-------------------------------------------------finpri???980424
1555 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1558 #define __leave return
1561 void OSD_File :: SetLock ( const OSD_LockType Lock ) {
1566 TEST_RAISE( "SetLock" );
1568 ZeroMemory ( &ovlp, sizeof ( OVERLAPPED ) );
1572 if ( ( myLock = Lock ) == OSD_NoLock ) {
1577 } else if ( myLock == OSD_ReadLock || myLock == OSD_ExclusiveLock ) {
1579 dwFlags = LOCKFILE_EXCLUSIVE_LOCK;
1585 LARGE_INTEGER aSize;
1586 aSize.QuadPart = Size();
1587 if (!LockFileEx (myFileHandle, dwFlags, 0, aSize.LowPart, aSize.HighPart, &ovlp)) {
1589 _osd_wnt_set_error ( myError, OSD_WFile );
1594 ImperativeFlag = Standard_True;
1601 leave: ; // added for VisualAge
1603 } // end OSD_File :: SetLock
1605 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1611 void OSD_File :: UnLock () {
1613 TEST_RAISE( "Unlock" );
1615 if ( ImperativeFlag ) {
1617 LARGE_INTEGER aSize;
1618 aSize.QuadPart = Size();
1619 if (!UnlockFile (myFileHandle, 0, 0, aSize.LowPart, aSize.HighPart))
1621 _osd_wnt_set_error ( myError, OSD_WFile );
1623 ImperativeFlag = Standard_False;
1627 } // end OSD_File :: UnLock
1629 OSD_LockType OSD_File :: GetLock () {
1633 } // end OSD_File :: GetLock
1635 Standard_Boolean OSD_File :: IsLocked () {
1637 TEST_RAISE( "IsLocked" );
1639 return ImperativeFlag;
1641 } // end OSD_File :: IsLocked
1644 // --------------------------------------------------------------------------
1645 // Return size of a file
1646 // --------------------------------------------------------------------------
1648 Standard_Size OSD_File :: Size () {
1650 Standard_Integer retVal;
1652 TEST_RAISE( "Size" );
1654 LARGE_INTEGER aSize;
1656 retVal = GetFileSizeEx (myFileHandle, &aSize);
1660 _osd_wnt_set_error ( myError, OSD_WFile );
1662 return (Standard_Size)aSize.QuadPart;
1664 } // end OSD_File :: Size
1666 // --------------------------------------------------------------------------
1667 // Print contains of a file
1668 // --------------------------------------------------------------------------
1670 void OSD_File :: Print ( const OSD_Printer& WhichPrinter ) {
1672 if (myFileHandle != INVALID_HANDLE_VALUE)
1674 RAISE( "OSD_File :: Print (): incorrect call - file opened" );
1676 TCollection_AsciiString pName, fName;
1678 WhichPrinter.Name ( pName );
1679 myPath.SystemName ( fName );
1680 TCollection_ExtendedString fNameW(fName);
1682 if ( !_osd_print ( (Standard_PCharacter)pName.ToCString (),
1683 (const wchar_t*)fNameW.ToExtString () ) )
1685 _osd_wnt_set_error ( myError, OSD_WFile );
1687 } // end OSD_File :: Print
1689 // --------------------------------------------------------------------------
1690 // Test if a file is open
1691 // --------------------------------------------------------------------------
1693 Standard_Boolean OSD_File :: IsOpen () const {
1695 return myFileHandle != INVALID_HANDLE_VALUE;
1697 } // end OSD_File :: IsOpen
1699 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1702 #define __leave return retVal
1705 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd (
1706 const OSD_Protection& prot, BOOL fDir, wchar_t* fName
1712 HANDLE hProcess = NULL;
1717 DWORD dwAccessAdmin;
1718 DWORD dwAccessGroup;
1719 DWORD dwAccessOwner;
1720 DWORD dwAccessWorld;
1721 DWORD dwAccessAdminDir;
1722 DWORD dwAccessGroupDir;
1723 DWORD dwAccessOwnerDir;
1724 DWORD dwAccessWorldDir;
1725 DWORD dwACLsize = sizeof ( ACL );
1727 PTOKEN_OWNER pTkOwner = NULL;
1728 PTOKEN_GROUPS pTkGroups = NULL;
1729 PTOKEN_PRIMARY_GROUP pTkPrimaryGroup = NULL;
1730 PSECURITY_DESCRIPTOR retVal = NULL;
1731 PSECURITY_DESCRIPTOR pfSD = NULL;
1739 if ( !OpenProcessToken (
1740 GetCurrentProcess (), TOKEN_QUERY, &hProcess
1744 if ( ( pTkGroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
1745 hProcess, TokenGroups
1750 if ( ( pTkOwner = ( PTOKEN_OWNER )GetTokenInformationEx (
1751 hProcess, TokenOwner
1756 if ( ( pTkPrimaryGroup = ( PTOKEN_PRIMARY_GROUP )GetTokenInformationEx (
1757 hProcess, TokenPrimaryGroup
1764 if ( fName == NULL )
1766 pSIDowner = pTkOwner -> Owner;
1770 pfSD = GetFileSecurityEx ( fName, OWNER_SECURITY_INFORMATION );
1772 if ( pfSD == NULL || !GetSecurityDescriptorOwner ( pfSD, &pSIDowner, &fDummy ) ) {
1781 pSIDadmin = AdminSid ();
1782 pSIDworld = WorldSid ();
1784 dwAccessAdmin = _get_access_mask ( prot.System () );
1785 dwAccessGroup = _get_access_mask ( prot.Group () );
1786 dwAccessOwner = _get_access_mask ( prot.User () );
1787 dwAccessWorld = _get_access_mask ( prot.World () );
1789 dwAccessAdminDir = _get_dir_access_mask ( prot.System () );
1790 dwAccessGroupDir = _get_dir_access_mask ( prot.Group () );
1791 dwAccessOwnerDir = _get_dir_access_mask ( prot.User () );
1792 dwAccessWorldDir = _get_dir_access_mask ( prot.World () );
1794 if ( dwAccessGroup != 0 ) {
1796 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1798 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1800 if ( !NtPredefinedSid ( pSIDtemp ) &&
1801 !EqualSid ( pSIDtemp, pSIDworld ) &&
1802 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1803 IsValidSid ( pSIDtemp )
1806 dwACLsize += ( ( GetLengthSid ( pSIDtemp ) + ACE_HEADER_SIZE ) << j );
1812 dwACLsize += ( ( ( GetLengthSid ( pSIDowner ) + ACE_HEADER_SIZE ) << j ) +
1813 ( ( GetLengthSid ( pSIDadmin ) + ACE_HEADER_SIZE ) << j ) +
1814 ( ( GetLengthSid ( pSIDworld ) + ACE_HEADER_SIZE ) << j )
1817 if ( ( pACL = CreateAcl ( dwACLsize ) ) == NULL ) __leave;
1819 if ( dwAccessAdmin != 0 )
1821 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1828 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1832 pFileACE -> dwMask = dwAccessAdminDir;
1833 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1834 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1838 FreeAce ( pFileACE );
1842 if ( dwAccessOwner != 0 )
1844 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1845 dwAccessOwner, 0, pSIDowner
1850 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1854 pFileACE -> dwMask = dwAccessOwnerDir;
1855 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1856 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1860 FreeAce ( pFileACE );
1864 if ( dwAccessWorld != 0 )
1866 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1867 dwAccessWorld, 0, pSIDworld
1872 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1876 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1877 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1881 FreeAce ( pFileACE );
1885 if ( dwAccessGroup != 0 ) {
1887 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1889 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1891 if ( !NtPredefinedSid ( pSIDtemp ) &&
1892 !EqualSid ( pSIDtemp, pSIDworld ) &&
1893 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1894 IsValidSid ( pSIDtemp )
1897 if ( dwAccessGroup != 0 )
1899 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1900 dwAccessGroup, 0, pSIDtemp
1905 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1909 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1910 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1914 FreeAce ( pFileACE );
1924 if ( ( retVal = AllocSD () ) == NULL ) __leave;
1926 if ( !SetSecurityDescriptorDacl ( retVal, TRUE, pACL, TRUE ) ) __leave;
1936 if ( retVal != NULL )
1940 else if ( pACL != NULL )
1948 if ( hProcess != NULL ) CloseHandle ( hProcess );
1949 if ( pTkOwner != NULL ) FreeTokenInformation ( pTkOwner );
1950 if ( pTkGroups != NULL ) FreeTokenInformation ( pTkGroups );
1951 if ( pTkPrimaryGroup != NULL ) FreeTokenInformation ( pTkPrimaryGroup );
1952 if ( pfSD != NULL ) FreeFileSecurity ( pfSD );
1957 leave: ; // added for VisualAge
1962 } // end _osd_wnt_protection_to_sd */
1964 #if defined(__CYGWIN32__) || defined(__MINGW32__)
1970 static void __fastcall _test_raise ( HANDLE hFile, Standard_CString str ) {
1972 Standard_Character buff[ 64 ];
1974 if (hFile == INVALID_HANDLE_VALUE) {
1976 strcpy ( buff, "OSD_File :: " );
1977 strcat ( buff, str );
1978 strcat ( buff, " (): wrong access" );
1980 Standard_ProgramError :: Raise ( buff );
1984 } // end _test_raise
1986 // Modified so that we have <nl> at end of line if we have read <nl> or <cr>
1987 // by LD 17 dec 98 for B4.4
1989 static DWORDLONG __fastcall _get_line ( Standard_PCharacter& buffer, DWORD dwBuffSize ) {
1992 Standard_PCharacter ptr;
1994 buffer[ dwBuffSize ] = 0;
1997 while ( *ptr != 0 ) {
1999 if ( *ptr == '\n' ) {
2001 ptr++ ; // jump newline char.
2003 retVal = ptr - buffer - dwBuffSize;
2004 retVal &= 0x0000000FFFFFFFF;// import 32-bit to 64-bit
2006 retVal = (DWORDLONG) ( (unsigned __int64) retVal | (((unsigned __int64) ptr) << 32) );
2008 retVal |= ( ( ( DWORDLONG )( DWORD )ptr ) << 32 );
2012 } else if ( *ptr == '\r' && ptr[ 1 ] == '\n' ) {
2014 *(ptr++) = '\n' ; // Substitue carriage return by newline.
2016 retVal = ptr + 1 - buffer - dwBuffSize;
2017 retVal &= 0x0000000FFFFFFFF;// import 32-bit to 64-bit
2019 retVal = (DWORDLONG) ( (unsigned __int64) retVal | (((unsigned __int64) ptr) << 32) );
2021 retVal |= ( ( ( DWORDLONG )( DWORD )ptr ) << 32 );
2025 } else if ( *ptr == '\r' && ptr[ 1 ] == 0 ) {
2026 *ptr = '\n' ; // Substitue carriage return by newline
2029 return (DWORDLONG) (__int64) (-1);
2031 return 0xFFFFFFFFFFFFFFFF;
2039 retVal = (DWORDLONG) ( ( (unsigned __int64) ((DWORD) buffer + dwBuffSize) ) << 32 );
2040 retVal = (DWORDLONG) ( (unsigned __int64) retVal & (((unsigned __int64) 0xFFFFFFFF) << 32) );
2042 retVal = ( ( ( DWORDLONG )( ( DWORD )buffer + dwBuffSize ) ) << 32 );
2043 retVal &= 0xFFFFFFFF00000000;
2050 static int __fastcall _get_buffer (
2052 Standard_PCharacter& buffer,
2054 BOOL fPeek, BOOL fSocket
2064 flags = fPeek ? MSG_PEEK : 0;
2066 retVal = recv ( ( SOCKET )hChannel, buffer, ( int )dwSize, flags );
2068 if ( retVal == SOCKET_ERROR ) retVal = -1;
2074 if ( !PeekNamedPipe (
2075 hChannel, buffer, dwSize, &dwBytesRead, &dwDummy, &dwDummy
2076 ) && GetLastError () != ERROR_BROKEN_PIPE
2083 retVal = ( int )dwBytesRead;
2087 if ( !ReadFile ( hChannel, buffer, dwSize, &dwBytesRead, NULL ) )
2093 retVal = ( int )dwBytesRead;
2101 } // end _get_buffer
2104 static DWORD __fastcall _get_access_mask ( OSD_SingleProtection prt ) {
2118 retVal = FILE_GENERIC_READ;
2124 retVal = FILE_GENERIC_WRITE;
2130 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
2136 retVal = FILE_GENERIC_EXECUTE;
2142 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
2148 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2154 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2166 retVal = FILE_GENERIC_READ | DELETE;
2172 retVal = FILE_GENERIC_WRITE | DELETE;
2178 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE;
2184 retVal = FILE_GENERIC_EXECUTE | DELETE;
2190 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE;
2196 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2202 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2208 RAISE( "_get_access_mask (): incorrect parameter" );
2214 } // end _get_access_mask
2216 static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt ) {
2230 retVal = GENERIC_READ;
2236 retVal = GENERIC_WRITE;
2242 retVal = GENERIC_READ | GENERIC_WRITE;
2248 retVal = GENERIC_EXECUTE;
2254 retVal = GENERIC_READ | GENERIC_EXECUTE;
2260 retVal = GENERIC_WRITE | GENERIC_EXECUTE;
2266 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE;
2278 retVal = GENERIC_READ | DELETE;
2284 retVal = GENERIC_WRITE | DELETE;
2290 retVal = GENERIC_READ | GENERIC_WRITE | DELETE;
2296 retVal = GENERIC_EXECUTE | DELETE;
2302 retVal = GENERIC_READ | GENERIC_EXECUTE | DELETE;
2308 retVal = GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2314 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2320 RAISE( "_get_dir_access_mask (): incorrect parameter" );
2326 } // end _get_dir_access_mask
2328 static HANDLE __fastcall _open_file (
2329 Standard_CString fName,
2331 DWORD dwOptions, LPBOOL fNew
2334 HANDLE retVal = INVALID_HANDLE_VALUE;
2335 DWORD dwDesiredAccess = 0;
2336 DWORD dwCreationDistribution;
2342 dwDesiredAccess = GENERIC_READ;
2348 dwDesiredAccess = GENERIC_WRITE;
2354 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
2360 RAISE( "_open_file (): incorrect parameter" );
2364 dwCreationDistribution = ( dwOptions != OPEN_NEW ) ? OPEN_EXISTING : CREATE_ALWAYS;
2366 // make wide character string from UTF-8
2367 TCollection_ExtendedString fNameW(fName, Standard_True);
2368 retVal = CreateFileW (
2369 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
2370 FILE_SHARE_READ | FILE_SHARE_WRITE,
2371 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2374 if ( retVal == INVALID_HANDLE_VALUE &&
2375 dwOptions == OPEN_APPEND &&
2376 GetLastError () == ERROR_FILE_NOT_FOUND
2380 dwCreationDistribution = CREATE_ALWAYS;
2382 retVal = CreateFileW (
2383 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
2384 FILE_SHARE_READ | FILE_SHARE_WRITE,
2385 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2396 Standard_Integer __fastcall _get_file_type (
2397 Standard_CString fName, HANDLE fileHandle
2400 Standard_Integer retVal = 0;
2404 fileType = (fileHandle == INVALID_HANDLE_VALUE ?
2405 FILE_TYPE_DISK : GetFileType (fileHandle));
2407 switch ( fileType ) {
2409 case FILE_TYPE_UNKNOWN:
2411 retVal = FLAG_SOCKET;
2415 case FILE_TYPE_DISK:
2417 // make wide character string from UTF-8
2418 TCollection_ExtendedString fNameW(fName, Standard_True);
2419 dwType = GetFileAttributesW ( (const wchar_t*) fNameW.ToExtString() );
2420 if ( dwType != 0xFFFFFFFF )
2422 retVal = dwType & FILE_ATTRIBUTE_DIRECTORY ? FLAG_DIRECTORY : FLAG_FILE;
2426 retVal = 0x80000000;
2430 case FILE_TYPE_CHAR:
2432 retVal = FLAG_DEVICE;
2436 case FILE_TYPE_PIPE:
2446 } // end _get_file_type
2448 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2451 #define __leave return retVal
2454 BOOL __fastcall _osd_wnt_sd_to_protection (
2455 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL fDir
2466 DWORD dwAccessOwner = 0;
2467 DWORD dwAccessGroup = 0;
2468 DWORD dwAccessAdmin = 0;
2469 DWORD dwAccessWorld = 0;
2470 BOOL retVal = FALSE;
2471 GET_PROT_FUNC _get_prot_func = fDir ? &_get_protection_dir : &_get_protection;
2475 if ( !GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) ) __leave;
2477 if ( !GetSecurityDescriptorDacl ( pSD, &fPresent, &pACL, &fDefaulted ) ||
2481 if ( pSIDowner == NULL || pACL == NULL ) {
2483 SetLastError ( ERROR_NO_SECURITY_ON_OBJECT );
2488 pSIDadmin = AdminSid ();
2489 pSIDworld = WorldSid ();
2491 for ( i = 0; i < ( int )pACL -> AceCount; ++i ) {
2493 if ( GetAce ( pACL, i, &pACE ) ) {
2495 if ( EqualSid ( pSIDowner, GET_SID( pACE ) ) )
2497 dwAccessOwner = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2498 0 : *GET_MSK( pACE );
2500 else if ( EqualSid ( pSIDadmin, GET_SID( pACE ) ) )
2502 dwAccessAdmin = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2503 0 : *GET_MSK( pACE );
2505 else if ( EqualSid ( pSIDworld, GET_SID( pACE ) ) )
2507 dwAccessWorld = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2508 0 : *GET_MSK( pACE );
2512 dwAccessGroup = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2513 0 : *GET_MSK( pACE );
2520 ( *_get_prot_func ) ( dwAccessAdmin ),
2521 ( *_get_prot_func ) ( dwAccessOwner ),
2522 ( *_get_prot_func ) ( dwAccessGroup ),
2523 ( *_get_prot_func ) ( dwAccessWorld )
2533 leave: ; // added for VisualAge
2538 } // end _osd_wnt_sd_to_protection
2540 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2546 static OSD_SingleProtection __fastcall _get_protection ( DWORD mask ) {
2548 OSD_SingleProtection retVal;
2552 case FILE_GENERIC_READ:
2558 case FILE_GENERIC_WRITE:
2564 case FILE_GENERIC_READ | FILE_GENERIC_WRITE:
2570 case FILE_GENERIC_EXECUTE:
2576 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE:
2582 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2588 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2600 case FILE_GENERIC_READ | DELETE:
2606 case FILE_GENERIC_WRITE | DELETE:
2612 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE:
2618 case FILE_GENERIC_EXECUTE | DELETE:
2624 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE:
2630 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2636 case FILE_ALL_ACCESS:
2637 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2652 } // end _get_protection
2654 static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD mask ) {
2656 OSD_SingleProtection retVal;
2672 case GENERIC_READ | GENERIC_WRITE:
2678 case GENERIC_EXECUTE:
2684 case GENERIC_READ | GENERIC_EXECUTE:
2690 case GENERIC_WRITE | GENERIC_EXECUTE:
2696 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE:
2708 case GENERIC_READ | DELETE:
2714 case GENERIC_WRITE | DELETE:
2720 case GENERIC_READ | GENERIC_WRITE | DELETE:
2726 case GENERIC_EXECUTE | DELETE:
2732 case GENERIC_READ | GENERIC_EXECUTE | DELETE:
2738 case GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2744 case FILE_ALL_ACCESS:
2745 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2760 } // end _get_protection_dir
2762 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2765 #define __leave return fOK
2768 BOOL __fastcall _osd_print (const Standard_PCharacter pName, const wchar_t* fName ) {
2771 HANDLE hPrinter = NULL;
2772 BYTE jobInfo[ MAX_PATH + sizeof ( DWORD ) ];
2773 DWORD dwNeeded, dwCode = 0;
2779 if ( !OpenPrinter ( Standard_PCharacter(pName), &hPrinter, NULL ) ) {
2787 hPrinter, 1, jobInfo, MAX_PATH + sizeof ( DWORD ), &dwNeeded
2794 fName, (LPWSTR) ( ( ADDJOB_INFO_1* )jobInfo ) -> Path, FALSE
2799 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId
2812 DWORD dwBytesNeeded;
2814 dwCode = GetLastError ();
2816 if ( fJob && hPrinter != NULL ) {
2819 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId, 1,
2820 info, 1024, &dwBytesNeeded
2823 if ( fJob ) SetJob (
2825 ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId,
2826 1, info, JOB_CONTROL_CANCEL
2833 if ( hPrinter != NULL ) ClosePrinter ( hPrinter );
2838 leave: ; // added for VisualAge
2841 if ( !fOK ) SetLastError ( dwCode );
2847 #if defined(__CYGWIN32__) || defined(__MINGW32__)
2853 Standard_Boolean OSD_File::IsReadable()
2855 TCollection_AsciiString FileName ;
2858 myPath.SystemName(FileName) ;
2859 Channel = _open_file(FileName.ToCString(), OSD_ReadOnly, OPEN_OLD) ;
2860 if (Channel == INVALID_HANDLE_VALUE)
2861 return Standard_False ;
2863 CloseHandle (Channel) ;
2864 return Standard_True ;
2869 Standard_Boolean OSD_File::IsWriteable()
2871 TCollection_AsciiString FileName ;
2874 myPath.SystemName(FileName) ;
2875 Channel = _open_file(FileName.ToCString(), OSD_ReadWrite, OPEN_OLD) ;
2876 if (Channel == INVALID_HANDLE_VALUE)
2877 return Standard_False ;
2879 CloseHandle (Channel) ;
2880 return Standard_True ;
2884 Standard_Boolean OSD_File::IsExecutable()
2886 return IsReadable() ;
2888 // if (_access(FileName.ToCString(),0))
2894 // ---------------------------------------------------------------------
2895 // Read lines in a file while it is increasing.
2896 // Each line is terminated with a <nl>.
2897 // ---------------------------------------------------------------------
2901 Standard_Boolean OSD_File::ReadLastLine(TCollection_AsciiString& aLine,const Standard_Integer aDelay,const Standard_Integer aNbTries)
2903 static Standard_Integer MaxLength = 1000 ;
2904 Standard_Integer Len ;
2905 Standard_Integer Count = aNbTries ;
2908 return Standard_False ;
2910 ReadLine(aLine, MaxLength, Len) ;
2911 if (!aLine.IsEmpty())
2912 return Standard_True ;
2914 return Standard_False ;
2915 OSD::SecSleep(aDelay) ;
2920 Standard_Boolean OSD_File::Edit()
2922 cout << "Function OSD_File::Edit() not yet implemented." << endl;
2923 return Standard_False ;