0027197: Configuration - fix compilation issues when using mingw
[occt.git] / src / OSD / OSD_File.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 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
973c2be1 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.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
7fd59977 15//------------------------------------------------------------------------
16// UNIX Part
17//------------------------------------------------------------------------
18
6ff736d8 19#ifndef _WIN32
7fd59977 20
42cf5bc1 21
22#include <OSD_File.hxx>
23#include <OSD_FromWhere.hxx>
7fd59977 24#include <OSD_OSDError.hxx>
42cf5bc1 25#include <OSD_Path.hxx>
26#include <OSD_Printer.hxx>
27#include <OSD_Protection.hxx>
7fd59977 28#include <OSD_WhoAmI.hxx>
7fd59977 29#include <Standard_PCharacter.hxx>
42cf5bc1 30#include <Standard_ProgramError.hxx>
31#include <TCollection_AsciiString.hxx>
7fd59977 32
33const OSD_WhoAmI Iam = OSD_WFile;
34
35#if defined (sun) || defined(SOLARIS)
36#define POSIX
37#else
38#define SYSV
39#endif
40
41#include <errno.h>
7fd59977 42#include <stdlib.h>
43#include <stdio.h>
44#include <fcntl.h>
03155c18 45#include <unistd.h>
46#include <sys/stat.h>
7fd59977 47
48#define NEWLINE '\10';
49
50// ---------------------------------------------------------------------
51// Create an empty file object
52// ---------------------------------------------------------------------
53
6ff736d8 54OSD_File::OSD_File():OSD_FileNode()
55{
7fd59977 56 ImperativeFlag = Standard_False;
57 myLock = OSD_NoLock;
58 myIO = 0;
59 myMode = OSD_ReadWrite;
7fd59977 60 myFILE = (Standard_Address) NULL;
6ff736d8 61 myFileChannel = -1;
62 myFileHandle = 0;
7fd59977 63}
64
65// ---------------------------------------------------------------------
66// Create and initialize a file object
67// ---------------------------------------------------------------------
68
6ff736d8 69OSD_File::OSD_File(const OSD_Path& Name):OSD_FileNode(Name)
70{
7fd59977 71 ImperativeFlag = Standard_False;
72 myLock = OSD_NoLock;
73 myIO = 0;
74 myMode = OSD_ReadWrite;
7fd59977 75 myFILE = (Standard_Address) NULL;
6ff736d8 76 myFileChannel = -1;
77 myFileHandle = 0;
7fd59977 78}
79
6ff736d8 80// protect against occasional use of myFileHande in Linux code
81#define myFileHandle myFileHandle_is_only_for_Windows
7fd59977 82
83// ---------------------------------------------------------------------
84// Build a file if it doesn't exist or create again if it already exists
85// ---------------------------------------------------------------------
86
87void OSD_File::Build(const OSD_OpenMode Mode,
88 const OSD_Protection& Protect){
89
90 Standard_Integer internal_prot;
91 Standard_Integer internal_mode = O_CREAT | O_TRUNC ;
92 TCollection_AsciiString aBuffer;
93
94 if (myPath.Name().Length()==0)
95 Standard_ProgramError::Raise("OSD_File::Build : no name was given");
96
97 if (myFileChannel != -1)
98 Standard_ProgramError::Raise("OSD_File::Build : file is already open");
99
100
101 myMode = Mode;
102
103 internal_prot = Protect.Internal();
104
6ff736d8 105 const char* CMode = "r";
7fd59977 106
107 switch (Mode){
108 case OSD_ReadOnly:
109 internal_mode |= O_RDONLY;
6ff736d8 110 CMode = "r";
7fd59977 111 break;
112 case OSD_WriteOnly:
113 internal_mode |= O_WRONLY;
6ff736d8 114 CMode = "w";
7fd59977 115 break;
116 case OSD_ReadWrite:
117 internal_mode |= O_RDWR;
6ff736d8 118 CMode = "w+";
7fd59977 119 break;
120 }
121
122 myPath.SystemName( aBuffer );
6ff736d8 123 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
7fd59977 124 if (myFileChannel >= 0) {
6ff736d8 125 myFILE = fdopen (myFileChannel, CMode);
7fd59977 126 }
127 else
128 /* Handle OPEN errors */
129
130 myError.SetValue (errno, Iam, "Open");
131
132}
133
134
135
136// ---------------------------------------------------------------------
137// Append to an existing file
138// ---------------------------------------------------------------------
139
140void OSD_File::Append(const OSD_OpenMode Mode,
141 const OSD_Protection& Protect){
142
143 Standard_Integer internal_prot;
144 Standard_Integer internal_mode = O_APPEND;;
145 TCollection_AsciiString aBuffer;
146
147 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
148 Standard_ProgramError::Raise("OSD_File::Append : it is a directory");
149 }
150
151 if (myPath.Name().Length()==0)
152 Standard_ProgramError::Raise("OSD_File::Append : no name was given");
153
154 if (myFileChannel != -1)
155 Standard_ProgramError::Raise("OSD_File::Append : file is already open");
156
157 internal_prot = Protect.Internal();
7fd59977 158 myMode = Mode;
6ff736d8 159 const char* CMode = "r";
7fd59977 160
161 switch (Mode){
162 case OSD_ReadOnly:
163 internal_mode |= O_RDONLY;
6ff736d8 164 CMode = "r";
7fd59977 165 break;
166 case OSD_WriteOnly:
167 internal_mode |= O_WRONLY;
6ff736d8 168 CMode = "a";
7fd59977 169 break;
170 case OSD_ReadWrite:
171 internal_mode |= O_RDWR;
6ff736d8 172 CMode = "a+";
7fd59977 173 break;
174 }
175
176 // If file doesn't exist, creates it.
177
178 if (!Exists()) internal_mode |= O_CREAT;
179
180 myPath.SystemName ( aBuffer );
6ff736d8 181 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
7fd59977 182 if (myFileChannel >= 0)
6ff736d8 183 myFILE = fdopen (myFileChannel, CMode);
7fd59977 184 else
185 /* Handle OPEN errors */
186
187 myError.SetValue (errno, Iam, "Open");
188}
189
190// ---------------------------------------------------------------------
191// Open a file
192// ---------------------------------------------------------------------
193
194void OSD_File::Open(const OSD_OpenMode Mode,
195 const OSD_Protection& Protect){
196
197 Standard_Integer internal_prot;
198 Standard_Integer internal_mode = 0;
199 TCollection_AsciiString aBuffer;
200
201 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
202 myError.SetValue (1, Iam, "Could not be open : it is a directory");
203 }
204
205 if (myPath.Name().Length()==0)
206 Standard_ProgramError::Raise("OSD_File::Open : no name was given");
207
208 if (myFileChannel != -1)
209 Standard_ProgramError::Raise("OSD_File::Open : file is already open");
210
211 internal_prot = Protect.Internal();
7fd59977 212 myMode = Mode;
6ff736d8 213 const char* CMode = "r";
7fd59977 214
215 switch (Mode){
216 case OSD_ReadOnly:
217 internal_mode |= O_RDONLY;
6ff736d8 218 CMode = "r";
7fd59977 219 break;
220 case OSD_WriteOnly:
221 internal_mode |= O_WRONLY;
6ff736d8 222 CMode = "w";
7fd59977 223 break;
224 case OSD_ReadWrite:
225 internal_mode |= O_RDWR;
6ff736d8 226 CMode = "w+";
7fd59977 227 break;
228 }
229
230 myPath.SystemName ( aBuffer );
6ff736d8 231 myFileChannel = open (aBuffer.ToCString(), internal_mode, internal_prot);
7fd59977 232 if (myFileChannel >= 0)
6ff736d8 233 myFILE = fdopen (myFileChannel, CMode);
7fd59977 234 else
235 /* Handle OPEN errors */
236
237 myError.SetValue (errno, Iam, "Open");
238}
239
240
241
242// ---------------------------------------------------------------------
243// ---------------------------------------------------------------------
95e05159 244void OSD_File::BuildTemporary(){
245
246 if ( IsOpen() )
247 Close();
7fd59977 248
249#if defined(vax) || defined(__vms) || defined(VAXVMS)
250 FILE *fic;
7fd59977 251 int dummy;
252
253 fic = tmpfile();
6ff736d8 254 dummy = open("dummy", O_RDWR | O_CREAT); // Open a dummy file
95e05159 255 myFileChannel = dummy - 1; // This is file channel of "fic" +1
7fd59977 256 close(dummy); // Close dummy file
257 unlink("dummy"); // Removes dummy file
258
259#else
95e05159 260 char name[] = "/tmp/CSFXXXXXX";
261 myFileChannel = mkstemp( name );
7fd59977 262
263 TCollection_AsciiString aName ( name ) ;
264 OSD_Path aPath( aName ) ;
265
95e05159 266 SetPath( aPath ) ;
7fd59977 267
95e05159 268 myFILE = fdopen( myFileChannel, "w+" ) ;
7fd59977 269
270#endif
271
95e05159 272 myMode = OSD_ReadWrite;
7fd59977 273}
274
275
276
277// ---------------------------------------------------------------------
278// Read content of a file
279// ---------------------------------------------------------------------
280
281void OSD_File::Read(TCollection_AsciiString& Buffer,
282 const Standard_Integer Nbyte){
283 Standard_PCharacter readbuf;
284 int status;
285
286 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
287 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
288 }
289
290 if (myFileChannel == -1)
291 Standard_ProgramError::Raise("OSD_File::Read : file is not open");
292
293 if (Failed()) Perror();
294
295 if (myMode == OSD_WriteOnly)
296 Standard_ProgramError::Raise("OSD_File::Read : file is Write only");
297
298 if (Nbyte <= 0)
299 Standard_ProgramError::Raise("OSD_File::Read : Nbyte is null");
300
301 TCollection_AsciiString transfert(Nbyte,' ');
302 readbuf = (Standard_PCharacter)transfert.ToCString();
303
6ff736d8 304 status = read (myFileChannel, readbuf, Nbyte);
7fd59977 305 //
306 Buffer = transfert; // Copy transfert to Buffer
307
308 if (status == -1) myError.SetValue (errno, Iam, "Read");
309 else
310 if ( status < Nbyte ) myIO = EOF;
311}
312
313
314// ---------------------------------------------------------------------
315// Read a line from a file
316// ---------------------------------------------------------------------
317
318void OSD_File::ReadLine(TCollection_AsciiString& Buffer,
319 const Standard_Integer Nbyte,
320 Standard_Integer& NByteRead)
321{
322 Standard_PCharacter readbuf, abuffer;
323 //
324 if (OSD_File::KindOfFile() == OSD_DIRECTORY ) {
325 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
326 }
327 if (myFileChannel == -1){
328 Standard_ProgramError::Raise("OSD_File::ReadLine : file is not open");
329 }
330 if (Failed()) {
331 Perror();
332 }
333 if (myMode == OSD_WriteOnly) {
334 Standard_ProgramError::Raise("OSD_File::ReadLine : file is Write only");
335 }
336 if (Nbyte <= 0){
337 Standard_ProgramError::Raise("OSD_File::ReadLine : Nbyte is null");
338 }
339 //
340 TCollection_AsciiString transfert(Nbyte,' ');
341 readbuf = (Standard_PCharacter)transfert.ToCString();
342 //
343 abuffer = fgets(readbuf, Nbyte, (FILE *) myFILE);
344 //
345 if (abuffer == NULL) {
346 if (!feof((FILE *) myFILE)) {
347 myError.SetValue (errno, Iam, "ReadLine");
348 return;
349 }
350 else {
351 myIO = EOF;
352 Buffer.Clear();
353 NByteRead = 0 ;
354 }
355 }
356 else {
6ff736d8 357 NByteRead = (Standard_Integer)strlen(abuffer);
7fd59977 358 Buffer.SetValue(1,abuffer); // Copy transfert to Buffer
359 Buffer.Trunc(NByteRead);
360 }
361}
362// --------------------------------------------------------------------------
363// OSD::KindOfFile Retourne le type de fichier.
364// --------------------------------------------------------------------------
365OSD_KindFile OSD_File::KindOfFile ( ) const{
366int status;
367struct stat buffer;
368TCollection_AsciiString FullName;
369OSD_Path aPath;
370
371Path(aPath);
372aPath.SystemName (FullName);
373status = stat (FullName.ToCString() , &buffer );
374if ( status == 0) {
375 if ( S_ISDIR(buffer.st_mode) ) { return OSD_DIRECTORY ; }
376 else if ( S_ISREG(buffer.st_mode) ) { return OSD_FILE ; }
377 else if ( S_ISLNK(buffer.st_mode) ) { return OSD_LINK ; }
378 else if ( S_ISSOCK(buffer.st_mode) ) { return OSD_SOCKET ; }
379 else { return OSD_UNKNOWN ; }
380}
381return OSD_UNKNOWN ;
382
383}
384// --------------------------------------------------------------------------
385// Read content of a file
386// --------------------------------------------------------------------------
7c65581d 387void OSD_File::Read(const Standard_Address Buffer,
7fd59977 388 const Standard_Integer Nbyte,
389 Standard_Integer& Readbyte)
390{
391 int status;
392
393 Readbyte = 0;
394 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
395 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
396 }
397
398 if (myFileChannel == -1)
399 Standard_ProgramError::Raise("OSD_File::Read : file is not open");
400
401 if (Failed()) Perror();
402
403 if (myMode == OSD_WriteOnly)
404 Standard_ProgramError::Raise("OSD_File::Read : file is Write only");
405
406 if (Nbyte <= 0)
407 Standard_ProgramError::Raise("OSD_File::Read : Nbyte is null");
408
409 if (Buffer == NULL)
410 Standard_ProgramError::Raise("OSD_File::Read : Buffer is null");
411
6ff736d8 412 status = read (myFileChannel, (char*) Buffer, Nbyte);
7fd59977 413
414 if (status == -1) myError.SetValue (errno, Iam, "Read");
415 else{
416 if ( status < Nbyte ) myIO = EOF;
6ff736d8 417 Readbyte = status;
7fd59977 418 }
419}
420
421// Write content of a file
422
423void OSD_File::Write(const TCollection_AsciiString &Buffer,
424 const Standard_Integer Nbyte){
425
426 Standard_CString writebuf;
427 int status;
428
429 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
430 Standard_ProgramError::Raise("OSD_File::Write : it is a directory");
431 }
432
433 if (myFileChannel == -1)
434 Standard_ProgramError::Raise("OSD_File::Write : file is not open");
435
436 if (Failed()) Perror();
437
438 if (myMode == OSD_ReadOnly)
439 Standard_ProgramError::Raise("OSD_File::Write : file is Read only");
440
441 if (Nbyte <= 0)
442 Standard_ProgramError::Raise("OSD_File::Write : Nbyte is null");
443
444 writebuf = Buffer.ToCString();
445
6ff736d8 446 status = write (myFileChannel, writebuf, Nbyte);
7fd59977 447
448 if ( status == -1) myError.SetValue (errno, Iam, "Write");
449 else
450 if ( status < Nbyte ) myIO = EOF;
451}
452
453
454void OSD_File::Write(const Standard_Address Buffer,
455 const Standard_Integer Nbyte)
456{
457
458 int status;
459
460 if (myFileChannel == -1)
461 Standard_ProgramError::Raise("OSD_File::Write : file is not open");
462
463 if (Failed()) Perror();
464
465 if (myMode == OSD_ReadOnly)
466 Standard_ProgramError::Raise("OSD_File::Write : file is Read only");
467
468 if (Nbyte <= 0)
469 Standard_ProgramError::Raise("OSD_File::Write : Nbyte is null");
470
6ff736d8 471 status = write (myFileChannel, (const char *)Buffer, Nbyte);
7fd59977 472
473 if ( status == -1) myError.SetValue (errno, Iam, "Write");
474 else
475 if ( status < Nbyte ) myIO = EOF;
476}
477
478
479
480
481
482// Move file pointer to a specified position
483
484void OSD_File::Seek(const Standard_Integer Offset,
485 const OSD_FromWhere Whence){
6ff736d8 486 int iwhere=0;
7fd59977 487
488 if (myFileChannel == -1)
489 Standard_ProgramError::Raise("OSD_File::Seek : file is not open");
490
491 if (Failed()) Perror();
492
493 switch (Whence){
494 case OSD_FromBeginning :
6ff736d8 495 iwhere = SEEK_SET;
7fd59977 496 break;
497 case OSD_FromHere:
6ff736d8 498 iwhere = SEEK_CUR;
7fd59977 499 break;
500 case OSD_FromEnd:
6ff736d8 501 iwhere = SEEK_END;
7fd59977 502 break;
503 default :
504 myError.SetValue (EINVAL, Iam, "Seek");
505 }
7fd59977 506
6ff736d8 507 off_t status = lseek (myFileChannel, Offset, iwhere);
7fd59977 508 if (status == -1) myError.SetValue (errno, Iam, "Seek");
509}
510
511
512
513
514
515
516// Close a file
517
518void OSD_File::Close(){
519 int status;
520
521 if (myFileChannel == -1)
522 Standard_ProgramError::Raise("OSD_File::Close : file is not open");
523
524 if (Failed()) Perror();
525
6ff736d8 526 // note: it probably should be single call to fclose()...
527 status = close (myFileChannel);
7fd59977 528
529 if (status == -1) myError.SetValue (errno, Iam, "Close");
530 myFileChannel = -1;
531 if ( myFILE != NULL ) {
532 status = fclose ( (FILE*) myFILE );
6ff736d8 533 myFILE = NULL;
7fd59977 534 }
535 myIO = 0;
536}
537
538
539
540
541// --------------------------------------------------------------------------
542// Test if at end of file
543// --------------------------------------------------------------------------
544
545Standard_Boolean OSD_File::IsAtEnd(){
546 if (myFileChannel == -1)
547 Standard_ProgramError::Raise("OSD_File::IsAtEnd : file is not open");
548
549 if (myIO == EOF) return (Standard_True);
550 return (Standard_False);
551}
552
553
554
555/*void OSD_File::Link(const TCollection_AsciiString& ToFile){
556 if (myFileChannel == -1)
557 Standard_ProgramError::Raise("OSD_File::Link : file is not open");
558
559 TCollection_AsciiString aBuffer;
560 myPath.SystemName ( aBuffer );
561 link ( aBuffer.ToCString(), ToFile.ToCString() );
562
563}*/
564
565
566
567void OSD_File::SetLock(const OSD_LockType Lock){
568int status;
569
570 if (myFileChannel == -1)
571 Standard_ProgramError::Raise("OSD_File::SetLock : file is not open");
572
573
574#ifdef POSIX
575 int lock;
576 struct stat buf;
577
578 switch (Lock){
579 case OSD_ExclusiveLock :
580 case OSD_WriteLock : lock = F_LOCK;
581 break;
582 case OSD_ReadLock : return;
583 break;
584 default : myError.SetValue (EINVAL, Iam, "SetLock");
585 return;
586 }
587
6ff736d8 588 if (fstat (myFileChannel, &buf) == -1) {
7fd59977 589 myError.SetValue (errno, Iam, "SetLock");
590 return;
591 }
592
593 status = lockf(myFileChannel, lock, buf.st_size);
594 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
595 else myLock = Lock;
596
597#else
598#ifdef SYSV
599 struct stat buf;
600 struct flock key;
601
602 switch (Lock){
603 case OSD_ExclusiveLock :
604 case OSD_WriteLock : key.l_type = F_WRLCK;
605 break;
606 case OSD_ReadLock : key.l_type = F_RDLCK;
607 break;
608 case OSD_NoLock : return;
609 // default : myError.SetValue (EINVAL, Iam, "SetLock");
610 }
611
612 key.l_whence = 0;
613 key.l_start = 0;
614 key.l_len = 0;
615
6ff736d8 616 status = fcntl (myFileChannel, F_SETLKW, &key);
7fd59977 617 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
618 else myLock = Lock;
619
620 if (Lock == OSD_ExclusiveLock){
6ff736d8 621 fstat (myFileChannel, &buf);
7fd59977 622 TCollection_AsciiString aBuffer;
623 myPath.SystemName ( aBuffer );
624 chmod( aBuffer.ToCString() ,buf.st_mode | S_ISGID);
625 ImperativeFlag = Standard_True;
626 }
627
628#else /* BSD */
629 int lock;
630
631 switch (Lock){
632 case OSD_ExclusiveLock :
633 case OSD_WriteLock : lock = F_WRLCK;
634 break;
635 case OSD_ReadLock : lock = F_RDLCK;
636 break;
637 default : myError.SetValue (EINVAL, Iam, "SetLock");
638 }
639
6ff736d8 640 status = flock (myFileChannel, lock);
7fd59977 641 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
642 else myLock = Lock;
643#endif // SysV
644#endif // POSIX
645}
646
647
648
649
650// Remove a lock from a file
651
652void OSD_File::UnLock(){
653int status;
654
655 if (myFileChannel == -1)
656 Standard_ProgramError::Raise("OSD_File::UnLock : file is not open");
657
658#ifdef POSIX
659 struct stat buf;
660
661 if (fstat(myFileChannel, &buf) == -1) {
662 myError.SetValue(errno, Iam, "UnsetLock");
663 return;
664 }
665
666 status = lockf(myFileChannel,F_ULOCK, buf.st_size);
667 if (status == -1) myError.SetValue (errno, Iam, "SetLock");
668
669#else
670#ifdef SYSV
671 struct stat buf;
672 struct flock key;
673
674 if (ImperativeFlag){
6ff736d8 675 fstat (myFileChannel, &buf);
7fd59977 676 TCollection_AsciiString aBuffer;
677 myPath.SystemName ( aBuffer );
678 chmod(aBuffer.ToCString(),buf.st_mode & ~S_ISGID);
679 ImperativeFlag = Standard_False;
680 }
681 key.l_type = F_UNLCK;
6ff736d8 682 status = fcntl (myFileChannel, F_SETLK, &key);
7fd59977 683 if (status == -1) myError.SetValue (errno, Iam, "UnSetLock");
684#else
685
6ff736d8 686 status = flock (myFileChannel, LOCK_UN);
7fd59977 687 if (status == -1) myError.SetValue (errno, Iam, "UnSetLock");
688#endif
689#endif // POSIX
690 else myLock = OSD_NoLock;
691}
692
693
694
695
696
697// Return lock of a file
698
699OSD_LockType OSD_File::GetLock(){
7fd59977 700 return(myLock);
701}
702
703
704
705
706// --------------------------------------------------------------------------
707// Return size of a file
708// --------------------------------------------------------------------------
709
6ff736d8 710Standard_Size OSD_File::Size(){
7fd59977 711 struct stat buffer;
712 int status;
713
714 if (myPath.Name().Length()==0)
715 Standard_ProgramError::Raise("OSD_File::Size : empty file name");
716
717 TCollection_AsciiString aBuffer;
718 myPath.SystemName ( aBuffer );
719 status = stat( aBuffer.ToCString() ,&buffer );
720 if (status == -1) {
721 myError.SetValue (errno, Iam, "Size");
6ff736d8 722 return 0;
7fd59977 723 }
724
6ff736d8 725 return (Standard_Size)buffer.st_size;
7fd59977 726}
727
728
729// --------------------------------------------------------------------------
730// Print contains of a file
731// --------------------------------------------------------------------------
732
733void OSD_File::Print (const OSD_Printer &WhichPrinter ){
734char buffer[255];
735TCollection_AsciiString PrinterName;
736
737 if (myPath.Name().Length()==0)
738 Standard_ProgramError::Raise("OSD_File::Print : empty file name");
739
740 WhichPrinter.Name(PrinterName);
741
742 TCollection_AsciiString aBuffer;
743 myPath.SystemName ( aBuffer );
744
745 if (PrinterName.Length()==0)
746 sprintf(buffer,"lp %s",aBuffer.ToCString());
747 else
748 sprintf(buffer,"lpr -P%s %s",PrinterName.ToCString(),aBuffer.ToCString());
749
98160038 750 if (system(buffer) != 0)
751 Standard_ProgramError::Raise("OSD_File::Print : No output device was available, or an error occurred");
7fd59977 752}
753
754
755// --------------------------------------------------------------------------
756// Test if a file is open
757// --------------------------------------------------------------------------
758
759Standard_Boolean OSD_File::IsOpen()const{
7fd59977 760 return (myFileChannel != -1);
761}
762
763
764Standard_Boolean OSD_File::IsLocked(){
7fd59977 765 return(myLock != OSD_NoLock);
766}
767
768Standard_Boolean OSD_File::IsReadable()
769{
770 TCollection_AsciiString FileName ;
771
772 myPath.SystemName(FileName) ;
773
774 if (access(FileName.ToCString(),F_OK|R_OK))
775 return Standard_False;
776 else
777 return Standard_True;
778}
779
780Standard_Boolean OSD_File::IsWriteable()
781{
782 TCollection_AsciiString FileName ;
783
784 myPath.SystemName(FileName) ;
785
786 if (access(FileName.ToCString(),F_OK|R_OK|W_OK))
787 return Standard_False;
788 else
789 return Standard_True;
790}
791
792Standard_Boolean OSD_File::IsExecutable()
793{
794 TCollection_AsciiString FileName ;
795
796 myPath.SystemName(FileName) ;
797
798 if (access(FileName.ToCString(),F_OK|X_OK))
799 return Standard_False;
800 else
801 return Standard_True;
802}
803
95e05159 804int OSD_File::Capture(int theDescr) {
805 // Duplicate an old file descriptor of the given one to be able to restore output to it later.
806 int oldDescr = dup(theDescr);
807 // Redirect the output to this file
808 dup2(myFileChannel, theDescr);
809
810 // Return the old descriptor
811 return oldDescr;
812}
813
814void OSD_File::Rewind() {
815 rewind((FILE*)myFILE);
816}
817
6ff736d8 818#else /* _WIN32 */
7fd59977 819
820//------------------------------------------------------------------------
821//------------------- Windows NT sources for OSD_File -------------------
822//------------------------------------------------------------------------
823
824#include <windows.h>
825
826#include <OSD_File.hxx>
827#include <OSD_Protection.hxx>
828#include <OSD_Printer.hxx>
829#include <Standard_ProgramError.hxx>
830
831#include <OSD_WNT_1.hxx>
832
833#include <stdio.h>
834#include <io.h>
835#include <Standard_PCharacter.hxx>
d9ff84e8 836#include <TCollection_ExtendedString.hxx>
7fd59977 837
838#ifndef _INC_TCHAR
839# include <tchar.h>
840#endif // _INC_TCHAR
841
842#if defined(__CYGWIN32__) || defined(__MINGW32__)
843#define VAC
7fd59977 844#endif
845
7c65581d 846#if defined(_MSC_VER)
847 #pragma comment( lib, "WSOCK32.LIB" )
848 #pragma comment( lib, "WINSPOOL.LIB" )
849#endif
7fd59977 850
851#define ACE_HEADER_SIZE ( sizeof ( ACCESS_ALLOWED_ACE ) - sizeof ( DWORD ) )
852
853#define RAISE( arg ) Standard_ProgramError :: Raise ( ( arg ) )
6ff736d8 854#define TEST_RAISE( arg ) _test_raise ( myFileHandle, ( arg ) )
7fd59977 855
856#define OPEN_NEW 0
857#define OPEN_OLD 1
858#define OPEN_APPEND 2
859
860void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
d9ff84e8 861PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, wchar_t* = NULL );
7fd59977 862BOOL __fastcall _osd_wnt_sd_to_protection (
863 PSECURITY_DESCRIPTOR, OSD_Protection&, BOOL
864 );
d9ff84e8 865BOOL __fastcall _osd_print (const Standard_PCharacter, const wchar_t* );
7fd59977 866
6ff736d8 867static void __fastcall _test_raise ( HANDLE, Standard_CString );
68299304 868static Standard_Integer __fastcall _get_line (Standard_PCharacter& buffer, DWORD dwBuffSize, LONG& theSeekPos);
7fd59977 869static int __fastcall _get_buffer ( HANDLE, Standard_PCharacter&, DWORD, BOOL, BOOL );
870static DWORD __fastcall _get_access_mask ( OSD_SingleProtection );
871static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt );
872static HANDLE __fastcall _open_file ( Standard_CString, OSD_OpenMode, DWORD, LPBOOL = NULL );
873
874static OSD_SingleProtection __fastcall _get_protection ( DWORD );
875static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD );
876
877typedef OSD_SingleProtection ( __fastcall *GET_PROT_FUNC ) ( DWORD );
878
6ff736d8 879Standard_Integer __fastcall _get_file_type ( Standard_CString, HANDLE );
7fd59977 880
881// ---------------------------------------------------------------------
882// Create an empty file object
883// ---------------------------------------------------------------------
884
6ff736d8 885OSD_File :: OSD_File ()
886{
7fd59977 887 ImperativeFlag = Standard_False;
888 myLock = OSD_NoLock;
889 myIO = 0;
6ff736d8 890 myFileChannel = -1;
891 myFileHandle = INVALID_HANDLE_VALUE;
7fd59977 892} // end constructor ( 1 )
893
894// ---------------------------------------------------------------------
895// Create and initialize a file object
896// ---------------------------------------------------------------------
897
6ff736d8 898OSD_File :: OSD_File ( const OSD_Path& Name ) : OSD_FileNode ( Name )
899{
7fd59977 900 ImperativeFlag = Standard_False;
901 myLock = OSD_NoLock;
902 myIO = 0;
903 myPath = Name;
6ff736d8 904 myFileChannel = -1;
905 myFileHandle = INVALID_HANDLE_VALUE;
7fd59977 906} // end constructor ( 2 )
907
95e05159 908// ---------------------------------------------------------------------
909// Redirect a standard handle (fileno(stdout), fileno(stdin) or
910// fileno(stderr) to this OSD_File and return the copy of the original
911// standard handle.
912// Example:
913// OSD_File aTmp;
914// aTmp.BuildTemporary();
915// int stdfd = _fileno(stdout);
916//
917// int oldout = aTmp.Capture(stdfd);
918// cout << "Some output to the file" << endl;
919// cout << flush;
920// fflush(stdout);
921//
922// _dup2(oldout, stdfd); // Restore standard output
923// aTmp.Close();
924// ---------------------------------------------------------------------
925int OSD_File::Capture(int theDescr) {
926 // Get POSIX file descriptor from this file handle
927 int dFile = _open_osfhandle(reinterpret_cast<intptr_t>(myFileHandle), myMode);
928
929 if (0 > dFile)
930 {
931 _osd_wnt_set_error ( myError, OSD_WFile, myFileHandle );
932 return -1;
933 }
934
935 // Duplicate an old file descriptor of the given one to be able to restore output to it later.
936 int oldDescr = _dup(theDescr);
937 // Redirect the output to this file
938 _dup2(dFile, theDescr);
939
940 // Return the old descriptor
941 return oldDescr;
942}
943
944void OSD_File::Rewind() {
945 SetFilePointer( myFileHandle, 0, NULL, FILE_BEGIN );
946}
947
6ff736d8 948// protect against occasional use of myFileHande in Windows code
949#define myFileChannel myFileChannel_is_only_for_Linux
950
7fd59977 951// ---------------------------------------------------------------------
952// Build a file if it doesn't exist or create again if it already exists
953// ---------------------------------------------------------------------
954
955void OSD_File :: Build (
956 const OSD_OpenMode Mode, const OSD_Protection& Protect
957 ) {
958
959 TCollection_AsciiString fName;
960
961 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
962 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
963 }
964
6ff736d8 965 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 966
d9ff84e8 967 RAISE( "OSD_File :: Build (): incorrect call - file already opened" );
7fd59977 968
969 myMode = Mode;
970 myPath.SystemName ( fName );
971
972 if ( fName.IsEmpty () )
973
d9ff84e8 974 RAISE( "OSD_File :: Build (): incorrent call - no filename given" );
7fd59977 975
6ff736d8 976 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_NEW );
7fd59977 977
6ff736d8 978 if (myFileHandle == INVALID_HANDLE_VALUE)
7fd59977 979
980 _osd_wnt_set_error ( myError, OSD_WFile );
981
982 else {
983
984 SetProtection ( Protect );
985 myIO |= FLAG_FILE;
986
987 } // end else
988
989} // end OSD_File :: Build
990
991
992
993// ---------------------------------------------------------------------
994// Open a file
995// ---------------------------------------------------------------------
996
302f96fb 997void OSD_File :: Open (const OSD_OpenMode Mode, const OSD_Protection& /*Protect*/)
998{
7fd59977 999
1000 TCollection_AsciiString fName;
1001
1002
6ff736d8 1003 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 1004
d9ff84e8 1005 RAISE( "OSD_File :: Open (): incorrect call - file already opened" );
7fd59977 1006
1007 myMode = Mode;
1008 myPath.SystemName ( fName );
1009
1010 if ( fName.IsEmpty () )
1011
d9ff84e8 1012 RAISE( "OSD_File :: Open (): incorrent call - no filename given" );
7fd59977 1013
6ff736d8 1014 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_OLD );
7fd59977 1015
6ff736d8 1016 if (myFileHandle == INVALID_HANDLE_VALUE) {
7fd59977 1017
1018 _osd_wnt_set_error ( myError, OSD_WFile );
1019 }
1020 else
1021 {
6ff736d8 1022 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
7fd59977 1023 }
1024} // end OSD_File :: Open
1025
1026// ---------------------------------------------------------------------
1027// Append to an existing file
1028// ---------------------------------------------------------------------
1029
1030void OSD_File :: Append (
1031 const OSD_OpenMode Mode, const OSD_Protection& Protect
1032 ) {
1033
1034 BOOL fNew = FALSE;
1035 TCollection_AsciiString fName;
1036
6ff736d8 1037 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 1038
d9ff84e8 1039 RAISE( "OSD_File :: Append (): incorrect call - file already opened" );
7fd59977 1040
1041 myMode = Mode;
1042 myPath.SystemName ( fName );
1043
1044 if ( fName.IsEmpty () )
1045
d9ff84e8 1046 RAISE( "OSD_File :: Append (): incorrent call - no filename given" );
7fd59977 1047
6ff736d8 1048 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_APPEND, &fNew );
7fd59977 1049
6ff736d8 1050 if (myFileHandle == INVALID_HANDLE_VALUE)
7fd59977 1051
1052 _osd_wnt_set_error ( myError, OSD_WFile );
1053
1054 else {
1055
1056 if ( !fNew ) {
1057
6ff736d8 1058 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
7fd59977 1059 Seek ( 0, OSD_FromEnd );
1060
1061 } else {
1062
1063 SetProtection ( Protect );
1064 myIO |= FLAG_FILE;
1065
1066 } // end else
1067
1068 } // end else;
1069
1070} // end OSD_File :: Append
1071
1072// ---------------------------------------------------------------------
1073// Read content of a file
1074// ---------------------------------------------------------------------
1075
1076void OSD_File :: Read (
1077 TCollection_AsciiString& Buffer, const Standard_Integer Nbyte
1078 ) {
1079
1080 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
0797d9d3 1081#ifdef OCCT_DEBUG
7fd59977 1082 cout << " OSD_File::Read : it is a directory " << endl;
63c629aa 1083#endif
7fd59977 1084 return ;
1085// Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1086 }
1087
1088 Standard_Integer NbyteRead;
7fd59977 1089
d9ff84e8 1090 TEST_RAISE( "Read" );
7fd59977 1091
7c65581d 1092 char* buff = new Standard_Character[ Nbyte + 1 ];
7fd59977 1093
1094 Read ( buff, Nbyte, NbyteRead );
1095
7c65581d 1096 buff[ NbyteRead ] = 0;
7fd59977 1097
1098 if ( NbyteRead != 0 )
1099
7c65581d 1100 Buffer = buff;
7fd59977 1101
1102 else
1103
1104 Buffer.Clear ();
1105
1106 delete [] buff;
1107
1108} // end OSD_File :: Read
1109
7fd59977 1110// ---------------------------------------------------------------------
1111// Read a line from a file
1112// ---------------------------------------------------------------------
1113
7fd59977 1114// Modified so that we have <nl> at end of line if we have read <nl> or <cr>
1115// in the file.
1116// by LD 17 dec 98 for B4.4
1117
1118void OSD_File :: ReadLine (
1119 TCollection_AsciiString& Buffer,
1120 const Standard_Integer NByte, Standard_Integer& NbyteRead
1121 ) {
1122
7fd59977 1123 DWORD dwBytesRead;
1124 DWORD dwDummy;
1125 Standard_Character peekChar;
1126 Standard_PCharacter ppeekChar;
1127 Standard_PCharacter cBuffer;
68299304 1128 LONG aSeekPos;
7fd59977 1129
1130 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1131 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1132 }
1133
d9ff84e8 1134 TEST_RAISE( "ReadLine" );
7fd59977 1135
1136 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1137
d9ff84e8 1138 RAISE( "OSD_File :: ReadLine (): attempt to read from write only pipe" );
7fd59977 1139
1140 // +----> leave space for end-of-string
1141 // | plus <CR><LF> sequence
1142 // |
1143
1144 ppeekChar=&peekChar;
1145 cBuffer = new Standard_Character[ NByte + 3 ];
1146
1147 if ( myIO & FLAG_FILE ) {
1148
68299304 1149 if (!ReadFile (myFileHandle, cBuffer, NByte, &dwBytesRead, NULL)) { // an error occured
7fd59977 1150
1151 _osd_wnt_set_error ( myError, OSD_WFile );
1152 Buffer.Clear ();
1153 NbyteRead = 0;
1154
1155 } else if ( dwBytesRead == 0 ) { // end-of-file reached
1156
1157 Buffer.Clear ();
1158 NbyteRead = 0;
1159 myIO |= FLAG_EOF;
1160
1161 } else {
1162 myIO &= ~FLAG_EOF ; // if the file increased since last read (LD)
68299304 1163 NbyteRead = _get_line (cBuffer, dwBytesRead, aSeekPos);
7fd59977 1164
68299304 1165 if ( NbyteRead == -1 ) // last character in the buffer is <CR> -
1166 { // peek next character to see if it is a <LF>
6ff736d8 1167 if (!ReadFile (myFileHandle, ppeekChar, 1, &dwDummy, NULL)) {
7fd59977 1168
1169 _osd_wnt_set_error ( myError, OSD_WFile );
1170
1171 } else if ( dwDummy != 0 ) { // end-of-file reached ?
1172
1173 if ( peekChar != '\n' ) // if we did not get a <CR><LF> sequence
1174
1175 // adjust file position
1176
6ff736d8 1177 SetFilePointer (myFileHandle, -1, NULL, FILE_CURRENT);
7fd59977 1178
1179 } else
1180 myIO |= FLAG_EOF;
1181
1182 NbyteRead = dwBytesRead;
1183
68299304 1184 } else if ( aSeekPos != 0 )
1185 {
1186 SetFilePointer (myFileHandle, aSeekPos, NULL, FILE_CURRENT);
7fd59977 1187 }
1188
1189 } // end else
1190
1191 } else if ( myIO & FLAG_SOCKET || myIO & FLAG_PIPE || myIO & FLAG_NAMED_PIPE ) {
1192
6ff736d8 1193 dwBytesRead = (DWORD)_get_buffer (myFileHandle, cBuffer,
1194 (DWORD)NByte, TRUE, myIO & FLAG_SOCKET);
7fd59977 1195
1196 if ( ( int )dwBytesRead == -1 ) { // an error occured
1197
1198 _osd_wnt_set_error ( myError, OSD_WFile );
1199 Buffer.Clear ();
1200 NbyteRead = 0;
1201
1202 } else if ( dwBytesRead == 0 ) { // connection closed - set end-of-file flag
1203
1204 Buffer.Clear ();
1205 NbyteRead = 0;
1206 myIO |= FLAG_EOF;
1207
1208 } else {
1209
68299304 1210 NbyteRead = _get_line (cBuffer, dwBytesRead, aSeekPos);
7fd59977 1211
68299304 1212 if (NbyteRead == -1) // last character in the buffer is <CR> -
1213 { // peek next character to see if it is a <LF>
7fd59977 1214 NbyteRead = dwBytesRead; // (LD) always fits this case.
1215
6ff736d8 1216 dwDummy = _get_buffer (myFileHandle, ppeekChar, 1, TRUE, myIO & FLAG_SOCKET);
7fd59977 1217 if ( ( int )dwDummy == -1 ) { // an error occured
1218
1219 _osd_wnt_set_error ( myError, OSD_WFile );
1220
1221 } else if ( dwDummy != 0 ) { // connection closed ?
1222
1223 if ( peekChar == '\n' ) // we got a <CR><LF> sequence
1224
1225 dwBytesRead++ ; // (LD) we have to jump <LF>
1226 } else
1227
1228 myIO |= FLAG_EOF;
1229
68299304 1230 } else if (aSeekPos != 0)
1231 {
1232 dwBytesRead = dwBytesRead + aSeekPos;
7fd59977 1233 }
1234
1235 // Don't rewrite datas in cBuffer.
1236
1237 Standard_PCharacter cDummyBuffer = new Standard_Character[ NByte + 3 ];
1238
6ff736d8 1239 // remove pending input
1240 _get_buffer (myFileHandle, cDummyBuffer, dwBytesRead, FALSE, myIO & FLAG_SOCKET);
7fd59977 1241 delete [] cDummyBuffer ;
1242
1243 } // end else
1244
1245 } else
1246
d9ff84e8 1247 RAISE( "OSD_File :: ReadLine (): incorrect call - file is a directory" );
7fd59977 1248
1249 if ( !Failed () && !IsAtEnd () )
1250
1251 Buffer = cBuffer;
1252
1253 delete [] (Standard_PCharacter)cBuffer;
1254
1255} // end OSD_File :: ReadLine
1256
7fd59977 1257// --------------------------------------------------------------------------
1258// Read content of a file
1259// --------------------------------------------------------------------------
1260
1261void OSD_File :: Read (
7c65581d 1262 const Standard_Address Buffer,
7fd59977 1263 const Standard_Integer Nbyte, Standard_Integer& Readbyte
1264 ) {
1265
1266 DWORD dwBytesRead;
1267
1268 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
1269 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1270 }
1271
d9ff84e8 1272 TEST_RAISE( "Read" );
7fd59977 1273
1274 if ( myIO & FLAG_PIPE && !( myIO & FLAG_READ_PIPE ) )
1275
d9ff84e8 1276 RAISE( "OSD_File :: Read (): attempt to read from write only pipe" );
7fd59977 1277
6ff736d8 1278 if (!ReadFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesRead, NULL)) {
7fd59977 1279
1280 _osd_wnt_set_error ( myError, OSD_WFile );
1281 dwBytesRead = 0;
1282
1283 } else if ( dwBytesRead == 0 )
1284
1285 myIO |= FLAG_EOF;
1286
1287 else
1288
1289 myIO &= ~FLAG_EOF;
1290
1291 Readbyte = ( Standard_Integer )dwBytesRead;
1292
1293} // end OSD_File :: Read
1294
1295void OSD_File :: Write (
1296 const TCollection_AsciiString& Buffer,
1297 const Standard_Integer Nbyte
1298 ) {
1299
1300 Write ( ( Standard_Address )Buffer.ToCString (), Nbyte );
1301
1302} // end OSD_File :: Write
1303
1304// --------------------------------------------------------------------------
1305// Write content of a file
1306// --------------------------------------------------------------------------
1307
1308void OSD_File :: Write (
1309 const Standard_Address Buffer,
1310 const Standard_Integer Nbyte
1311 ) {
1312
1313 DWORD dwBytesWritten;
1314
d9ff84e8 1315 TEST_RAISE( "Write" );
7fd59977 1316
1317 if ( myIO & FLAG_PIPE && myIO & FLAG_READ_PIPE )
1318
d9ff84e8 1319 RAISE( "OSD_File :: Write (): attempt to write to read only pipe" );
7fd59977 1320
6ff736d8 1321 if (!WriteFile (myFileHandle, Buffer, (DWORD)Nbyte, &dwBytesWritten, NULL) ||
1322 dwBytesWritten != (DWORD)Nbyte)
7fd59977 1323
1324 _osd_wnt_set_error ( myError, OSD_WFile );
1325
1326} // end OSD_File :: Write
1327
1328void OSD_File :: Seek (
1329 const Standard_Integer Offset, const OSD_FromWhere Whence
1330 ) {
1331
302f96fb 1332 DWORD dwMoveMethod = 0;
7fd59977 1333
d9ff84e8 1334 TEST_RAISE( "Seek" );
7fd59977 1335
1336 if ( myIO & FLAG_FILE || myIO & FLAG_DIRECTORY ) {
1337
1338 switch ( Whence ) {
1339
1340 case OSD_FromBeginning:
1341
1342 dwMoveMethod = FILE_BEGIN;
1343
1344 break;
1345
1346 case OSD_FromHere:
1347
1348 dwMoveMethod = FILE_CURRENT;
1349
1350 break;
1351
1352 case OSD_FromEnd:
1353
1354 dwMoveMethod = FILE_END;
1355
1356 break;
1357
1358 default:
1359
d9ff84e8 1360 RAISE( "OSD_File :: Seek (): invalid parameter" );
7fd59977 1361
1362 } // end switch
1363
6ff736d8 1364 if (SetFilePointer (myFileHandle, (LONG)Offset, NULL, dwMoveMethod) == 0xFFFFFFFF)
7fd59977 1365
1366 _osd_wnt_set_error ( myError, OSD_WFile );
1367
1368 } // end if
1369
1370 myIO &= ~FLAG_EOF;
1371
1372} // end OSD_File :: Seek
1373
1374// --------------------------------------------------------------------------
1375// Close a file
1376// --------------------------------------------------------------------------
1377
1378void OSD_File :: Close () {
1379
d9ff84e8 1380 TEST_RAISE( "Close" );
7fd59977 1381
6ff736d8 1382 CloseHandle (myFileHandle);
7fd59977 1383
6ff736d8 1384 myFileHandle = INVALID_HANDLE_VALUE;
7fd59977 1385 myIO = 0;
1386
1387} // end OSD_File :: Close
1388
1389// --------------------------------------------------------------------------
1390// Test if at end of file
1391// --------------------------------------------------------------------------
1392
1393Standard_Boolean OSD_File :: IsAtEnd () {
1394
d9ff84e8 1395 TEST_RAISE( "IsAtEnd" );
7fd59977 1396
1397 if (myIO & FLAG_EOF)
1398 return Standard_True ;
1399 return Standard_False ;
1400
1401} // end OSD_File :: IsAtEnd
1402
1403OSD_KindFile OSD_File :: KindOfFile () const {
1404
1405 OSD_KindFile retVal;
1406 Standard_Integer flags;
1407
6ff736d8 1408 if (myFileHandle == INVALID_HANDLE_VALUE) {
7fd59977 1409
1410 TCollection_AsciiString fName;
1411
1412 myPath.SystemName ( fName );
1413
1414 if ( fName.IsEmpty () )
1415
d9ff84e8 1416 RAISE( "OSD_File :: KindOfFile (): incorrent call - no filename given" );
7fd59977 1417
6ff736d8 1418 flags = _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE);
7fd59977 1419
1420 } else
1421
1422 flags = myIO;
1423
1424 switch ( flags & FLAG_TYPE ) {
1425
1426 case FLAG_FILE:
1427
1428 retVal = OSD_FILE;
1429
1430 break;
1431
1432 case FLAG_DIRECTORY:
1433
1434 retVal = OSD_DIRECTORY;
1435
1436 break;
1437
1438 case FLAG_SOCKET:
1439
1440 retVal = OSD_SOCKET;
1441
1442 break;
1443
1444 default:
1445
1446 retVal = OSD_UNKNOWN;
1447
1448 } // end switch
1449
1450 return retVal;
1451
1452} // end OSD_File :: KindOfFile
7fd59977 1453
7fd59977 1454//-------------------------------------------------debutpri???980424
1455
1456typedef struct _osd_wnt_key {
1457
1458 HKEY hKey;
7c65581d 1459 const char* keyPath;
7fd59977 1460
1461 } OSD_WNT_KEY;
1462
1463
95e05159 1464 void OSD_File::BuildTemporary () {
7fd59977 1465
1466 OSD_Protection prt;
7fd59977 1467 HKEY hKey;
1468 TCHAR tmpPath[ MAX_PATH ];
1469 BOOL fOK = FALSE;
1470 OSD_WNT_KEY regKey[ 2 ] = {
1471
1472 { HKEY_LOCAL_MACHINE,
d9ff84e8 1473 "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
7fd59977 1474 },
1475 { HKEY_USERS,
d9ff84e8 1476 ".DEFAULT\\Environment"
7fd59977 1477 }
1478
1479 };
1480
1481 for ( int i = 0; i < 2; ++i ) {
1482
1483 if ( RegOpenKeyEx (
1484 regKey[ i ].hKey, regKey[ i ].keyPath, 0, KEY_QUERY_VALUE, &hKey
1485 ) == ERROR_SUCCESS
1486 ) {
1487
1488 DWORD dwType;
1489 DWORD dwSize = 0;
1490
1491 if ( RegQueryValueEx (
d9ff84e8 1492 hKey, "TEMP", NULL, &dwType, NULL, &dwSize
7fd59977 1493 ) == ERROR_SUCCESS
1494 ) {
1495
1496 LPTSTR kVal = ( LPTSTR )HeapAlloc (
1497 GetProcessHeap (), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS,
1498 dwSize + sizeof ( TCHAR )
1499 );
1500
d9ff84e8 1501 RegQueryValueEx ( hKey, "TEMP", NULL, &dwType, ( LPBYTE )kVal, &dwSize );
7fd59977 1502
1503 if ( dwType == REG_EXPAND_SZ )
1504
1505 ExpandEnvironmentStrings ( kVal, tmpPath, MAX_PATH );
1506
1507 else
1508
1509 lstrcpy ( tmpPath, kVal );
1510
1511 HeapFree ( GetProcessHeap (), 0, ( LPVOID )kVal );
1512 fOK = TRUE;
1513
1514 } // end if
1515
1516 RegCloseKey ( hKey );
1517
1518 } // end if
1519
1520 if ( fOK ) break;
1521
1522 } // end for
1523
d9ff84e8 1524 if ( !fOK ) lstrcpy ( tmpPath, "./" );
7fd59977 1525
1526 GetTempFileName ( tmpPath, "CSF", 0, tmpPath );
1527
95e05159 1528 if ( IsOpen() )
1529 Close();
7fd59977 1530
95e05159 1531 SetPath ( OSD_Path ( tmpPath ) );
1532 Build ( OSD_ReadWrite, prt );
7fd59977 1533} // end OSD_File :: BuildTemporary
1534
7fd59977 1535//-------------------------------------------------finpri???980424
1536
1537#if defined(__CYGWIN32__) || defined(__MINGW32__)
1538#define __try
1539#define __finally
1540#define __leave return
1541#endif
1542
1543void OSD_File :: SetLock ( const OSD_LockType Lock ) {
1544
1545 DWORD dwFlags;
1546 OVERLAPPED ovlp;
1547
d9ff84e8 1548 TEST_RAISE( "SetLock" );
7fd59977 1549
1550 ZeroMemory ( &ovlp, sizeof ( OVERLAPPED ) );
1551
1552 __try {
1553
1554 if ( ( myLock = Lock ) == OSD_NoLock ) {
1555
1556 UnLock ();
1557 __leave;
1558
1559 } else if ( myLock == OSD_ReadLock || myLock == OSD_ExclusiveLock ) {
1560
1561 dwFlags = LOCKFILE_EXCLUSIVE_LOCK;
1562
1563 } else
1564
1565 dwFlags = 0;
1566
6ff736d8 1567 LARGE_INTEGER aSize;
1568 aSize.QuadPart = Size();
1569 if (!LockFileEx (myFileHandle, dwFlags, 0, aSize.LowPart, aSize.HighPart, &ovlp)) {
7fd59977 1570
1571 _osd_wnt_set_error ( myError, OSD_WFile );
1572 __leave;
1573
1574 } // end if
1575
1576 ImperativeFlag = Standard_True;
1577
1578 } // end __try
1579
1580 __finally {}
1581
1582#ifdef VAC
1583leave: ; // added for VisualAge
1584#endif
1585} // end OSD_File :: SetLock
1586
1587#if defined(__CYGWIN32__) || defined(__MINGW32__)
1588#undef __try
1589#undef __finally
1590#undef __leave
1591#endif
1592
1593void OSD_File :: UnLock () {
1594
d9ff84e8 1595 TEST_RAISE( "Unlock" );
7fd59977 1596
1597 if ( ImperativeFlag ) {
1598
6ff736d8 1599 LARGE_INTEGER aSize;
1600 aSize.QuadPart = Size();
1601 if (!UnlockFile (myFileHandle, 0, 0, aSize.LowPart, aSize.HighPart))
7fd59977 1602
1603 _osd_wnt_set_error ( myError, OSD_WFile );
1604
1605 ImperativeFlag = Standard_False;
1606
1607 } // end if
1608
1609} // end OSD_File :: UnLock
1610
1611OSD_LockType OSD_File :: GetLock () {
1612
1613 return myLock;
1614
1615} // end OSD_File :: GetLock
1616
1617Standard_Boolean OSD_File :: IsLocked () {
1618
d9ff84e8 1619 TEST_RAISE( "IsLocked" );
7fd59977 1620
1621 return ImperativeFlag;
1622
1623} // end OSD_File :: IsLocked
1624
1625
1626// --------------------------------------------------------------------------
1627// Return size of a file
1628// --------------------------------------------------------------------------
1629
7c65581d 1630Standard_Size OSD_File::Size()
1631{
1632 TEST_RAISE("Size");
1633#if (_WIN32_WINNT >= 0x0500)
1634 LARGE_INTEGER aSize;
1635 aSize.QuadPart = 0;
1636 if (GetFileSizeEx (myFileHandle, &aSize) == 0)
1637 {
1638 _osd_wnt_set_error (myError, OSD_WFile);
1639 }
1640 return (Standard_Size)aSize.QuadPart;
1641#else
1642 DWORD aSize = GetFileSize (myFileHandle, NULL);
1643 if (aSize == INVALID_FILE_SIZE)
1644 {
1645 _osd_wnt_set_error (myError, OSD_WFile);
1646 }
1647 return aSize;
1648#endif
1649}
7fd59977 1650
1651// --------------------------------------------------------------------------
1652// Print contains of a file
1653// --------------------------------------------------------------------------
1654
1655void OSD_File :: Print ( const OSD_Printer& WhichPrinter ) {
1656
6ff736d8 1657 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 1658
d9ff84e8 1659 RAISE( "OSD_File :: Print (): incorrect call - file opened" );
7fd59977 1660
1661 TCollection_AsciiString pName, fName;
1662
1663 WhichPrinter.Name ( pName );
1664 myPath.SystemName ( fName );
d9ff84e8 1665 TCollection_ExtendedString fNameW(fName);
7fd59977 1666
d9ff84e8 1667 if ( !_osd_print ( (Standard_PCharacter)pName.ToCString (),
1668 (const wchar_t*)fNameW.ToExtString () ) )
7fd59977 1669
1670 _osd_wnt_set_error ( myError, OSD_WFile );
1671
1672} // end OSD_File :: Print
1673
1674// --------------------------------------------------------------------------
1675// Test if a file is open
1676// --------------------------------------------------------------------------
1677
1678Standard_Boolean OSD_File :: IsOpen () const {
1679
6ff736d8 1680 return myFileHandle != INVALID_HANDLE_VALUE;
7fd59977 1681
1682} // end OSD_File :: IsOpen
1683
1684#if defined(__CYGWIN32__) || defined(__MINGW32__)
1685#define __try
1686#define __finally
1687#define __leave return retVal
1688#endif
1689
1690PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd (
d9ff84e8 1691 const OSD_Protection& prot, BOOL fDir, wchar_t* fName
7fd59977 1692 ) {
1693
1694 int i, j;
1695 BOOL fOK = FALSE;
1696 PACL pACL = NULL;
1697 HANDLE hProcess = NULL;
1698 PSID pSIDowner;
1699 PSID pSIDadmin;
1700 PSID pSIDworld;
1701 PSID pSIDtemp;
1702 DWORD dwAccessAdmin;
1703 DWORD dwAccessGroup;
1704 DWORD dwAccessOwner;
1705 DWORD dwAccessWorld;
1706 DWORD dwAccessAdminDir;
7c65581d 1707// DWORD dwAccessGroupDir;
7fd59977 1708 DWORD dwAccessOwnerDir;
7c65581d 1709// DWORD dwAccessWorldDir;
7fd59977 1710 DWORD dwACLsize = sizeof ( ACL );
1711 DWORD dwIndex = 0;
1712 PTOKEN_OWNER pTkOwner = NULL;
1713 PTOKEN_GROUPS pTkGroups = NULL;
1714 PTOKEN_PRIMARY_GROUP pTkPrimaryGroup = NULL;
302f96fb 1715 PSECURITY_DESCRIPTOR retVal = NULL;
7fd59977 1716 PSECURITY_DESCRIPTOR pfSD = NULL;
1717 BOOL fDummy;
1718 PFILE_ACE pFileACE;
1719
1720 __try {
1721
1722 j = fDir ? 1 : 0;
1723
1724 if ( !OpenProcessToken (
1725 GetCurrentProcess (), TOKEN_QUERY, &hProcess
1726 )
1727 ) __leave;
1728
1729 if ( ( pTkGroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
1730 hProcess, TokenGroups
1731 )
1732 ) == NULL
1733 ) __leave;
1734
1735 if ( ( pTkOwner = ( PTOKEN_OWNER )GetTokenInformationEx (
1736 hProcess, TokenOwner
1737 )
1738 ) == NULL
1739 ) __leave;
1740
1741 if ( ( pTkPrimaryGroup = ( PTOKEN_PRIMARY_GROUP )GetTokenInformationEx (
1742 hProcess, TokenPrimaryGroup
1743 )
1744 ) == NULL
1745 ) __leave;
1746
1747
1748retry:
1749 if ( fName == NULL )
1750
1751 pSIDowner = pTkOwner -> Owner;
1752
1753 else {
1754
1755 pfSD = GetFileSecurityEx ( fName, OWNER_SECURITY_INFORMATION );
1756
1757 if ( pfSD == NULL || !GetSecurityDescriptorOwner ( pfSD, &pSIDowner, &fDummy ) ) {
1758
1759 fName = NULL;
1760 goto retry;
1761
1762 } // end if
1763
1764 } // end else
1765
1766 pSIDadmin = AdminSid ();
1767 pSIDworld = WorldSid ();
1768
1769 dwAccessAdmin = _get_access_mask ( prot.System () );
1770 dwAccessGroup = _get_access_mask ( prot.Group () );
1771 dwAccessOwner = _get_access_mask ( prot.User () );
1772 dwAccessWorld = _get_access_mask ( prot.World () );
1773
1774 dwAccessAdminDir = _get_dir_access_mask ( prot.System () );
7c65581d 1775// dwAccessGroupDir = _get_dir_access_mask ( prot.Group () );
7fd59977 1776 dwAccessOwnerDir = _get_dir_access_mask ( prot.User () );
7c65581d 1777// dwAccessWorldDir = _get_dir_access_mask ( prot.World () );
7fd59977 1778
1779 if ( dwAccessGroup != 0 ) {
1780
1781 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1782
1783 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1784
1785 if ( !NtPredefinedSid ( pSIDtemp ) &&
1786 !EqualSid ( pSIDtemp, pSIDworld ) &&
1787 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1788 IsValidSid ( pSIDtemp )
1789 )
1790
1791 dwACLsize += ( ( GetLengthSid ( pSIDtemp ) + ACE_HEADER_SIZE ) << j );
1792
1793 } // end for
1794
1795 } // end if
1796
1797 dwACLsize += ( ( ( GetLengthSid ( pSIDowner ) + ACE_HEADER_SIZE ) << j ) +
1798 ( ( GetLengthSid ( pSIDadmin ) + ACE_HEADER_SIZE ) << j ) +
1799 ( ( GetLengthSid ( pSIDworld ) + ACE_HEADER_SIZE ) << j )
1800 );
1801
1802 if ( ( pACL = CreateAcl ( dwACLsize ) ) == NULL ) __leave;
1803
1804 if ( dwAccessAdmin != 0 )
1805
1806 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1807 dwAccessAdmin, 0,
1808 pSIDadmin
1809 )
1810 ) != NULL
1811 ) {
1812
1813 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1814
1815 if ( fDir ) {
1816
1817 pFileACE -> dwMask = dwAccessAdminDir;
1818 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1819 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1820
1821 } // end if
1822
1823 FreeAce ( pFileACE );
1824
1825 } // end if
1826
1827 if ( dwAccessOwner != 0 )
1828
1829 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1830 dwAccessOwner, 0, pSIDowner
1831 )
1832 ) != NULL
1833 ) {
1834
1835 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1836
1837 if ( fDir ) {
1838
1839 pFileACE -> dwMask = dwAccessOwnerDir;
1840 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1841 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1842
1843 } // end if
1844
1845 FreeAce ( pFileACE );
1846
1847 } // end if
1848
1849 if ( dwAccessWorld != 0 )
1850
1851 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1852 dwAccessWorld, 0, pSIDworld
1853 )
1854 ) != NULL
1855 ) {
1856
1857 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1858
1859 if ( fDir ) {
1860
1861 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1862 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1863
1864 } // end if
1865
1866 FreeAce ( pFileACE );
1867
1868 } // end if
1869
1870 if ( dwAccessGroup != 0 ) {
1871
1872 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1873
1874 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1875
1876 if ( !NtPredefinedSid ( pSIDtemp ) &&
1877 !EqualSid ( pSIDtemp, pSIDworld ) &&
1878 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1879 IsValidSid ( pSIDtemp )
1880 ) {
1881
1882 if ( dwAccessGroup != 0 )
1883
1884 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1885 dwAccessGroup, 0, pSIDtemp
1886 )
1887 ) != NULL
1888 ) {
1889
1890 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1891
1892 if ( fDir ) {
1893
1894 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1895 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1896
1897 } // end if
1898
1899 FreeAce ( pFileACE );
1900
1901 } // end if
1902
1903 } // end if
1904
1905 } // end for
1906
1907 } // end if
1908
1909 if ( ( retVal = AllocSD () ) == NULL ) __leave;
1910
1911 if ( !SetSecurityDescriptorDacl ( retVal, TRUE, pACL, TRUE ) ) __leave;
1912
1913 fOK = TRUE;
1914
1915 } // end __try
1916
1917 __finally {
1918
1919 if ( !fOK ) {
1920
1921 if ( retVal != NULL )
1922
1923 FreeSD ( retVal );
1924
1925 else if ( pACL != NULL )
1926
1927 FreeAcl ( pACL );
1928
1929 retVal = NULL;
1930
1931 } // end if
1932
1933 if ( hProcess != NULL ) CloseHandle ( hProcess );
1934 if ( pTkOwner != NULL ) FreeTokenInformation ( pTkOwner );
1935 if ( pTkGroups != NULL ) FreeTokenInformation ( pTkGroups );
1936 if ( pTkPrimaryGroup != NULL ) FreeTokenInformation ( pTkPrimaryGroup );
1937 if ( pfSD != NULL ) FreeFileSecurity ( pfSD );
1938
1939 } // end __finally
1940
1941#ifdef VAC
1942leave: ; // added for VisualAge
1943#endif
1944
1945 return retVal;
1946
1947} // end _osd_wnt_protection_to_sd */
1948
1949#if defined(__CYGWIN32__) || defined(__MINGW32__)
1950#undef __try
1951#undef __finally
1952#undef __leave
1953#endif
1954
6ff736d8 1955static void __fastcall _test_raise ( HANDLE hFile, Standard_CString str ) {
7fd59977 1956
1957 Standard_Character buff[ 64 ];
1958
6ff736d8 1959 if (hFile == INVALID_HANDLE_VALUE) {
7fd59977 1960
d9ff84e8 1961 strcpy ( buff, "OSD_File :: " );
1962 strcat ( buff, str );
1963 strcat ( buff, " (): wrong access" );
7fd59977 1964
1965 Standard_ProgramError :: Raise ( buff );
1966
1967 } // end if
1968
1969} // end _test_raise
1970
68299304 1971// Returns number of bytes in the string (including end \n, but excluding \r);
1972//
1973static Standard_Integer __fastcall _get_line (Standard_PCharacter& buffer, DWORD dwBuffSize, LONG& theSeekPos)
1974{
7fd59977 1975
7fd59977 1976 Standard_PCharacter ptr;
1977
1978 buffer[ dwBuffSize ] = 0;
1979 ptr = buffer;
1980
1981 while ( *ptr != 0 ) {
1982
68299304 1983 if ( *ptr == '\n' )
1984 {
1985 ptr++ ; // jump newline char.
1986 *ptr = 0 ;
1987 theSeekPos = (LONG)(ptr - buffer - dwBuffSize);
1988 return (Standard_Integer)(ptr - buffer);
1989 }
1990 else if ( *ptr == '\r' && ptr[ 1 ] == '\n' )
1991 {
1992 *(ptr++) = '\n' ; // Substitue carriage return by newline.
1993 *ptr = 0 ;
1994 theSeekPos = (LONG)(ptr + 1 - buffer - dwBuffSize);
1995 return (Standard_Integer)(ptr - buffer);
1996 }
1997 else if ( *ptr == '\r' && ptr[ 1 ] == 0 ) {
7fd59977 1998 *ptr = '\n' ; // Substitue carriage return by newline
68299304 1999 return -1;
7fd59977 2000 }
2001 ++ptr;
2002
2003 } // end while
2004
68299304 2005 theSeekPos = 0;
2006 return dwBuffSize;
7fd59977 2007} // end _get_line
2008
7fd59977 2009static int __fastcall _get_buffer (
2010 HANDLE hChannel,
2011 Standard_PCharacter& buffer,
2012 DWORD dwSize,
2013 BOOL fPeek, BOOL fSocket
2014 ) {
2015
2016 int retVal = -1;
2017 int flags;
2018 DWORD dwDummy;
2019 DWORD dwBytesRead;
2020
2021 if ( fSocket ) {
2022
2023 flags = fPeek ? MSG_PEEK : 0;
2024
2025 retVal = recv ( ( SOCKET )hChannel, buffer, ( int )dwSize, flags );
2026
2027 if ( retVal == SOCKET_ERROR ) retVal = -1;
2028
2029 } else {
2030
2031 if ( fPeek ) {
2032
2033 if ( !PeekNamedPipe (
2034 hChannel, buffer, dwSize, &dwBytesRead, &dwDummy, &dwDummy
2035 ) && GetLastError () != ERROR_BROKEN_PIPE
2036 )
2037
2038 retVal = -1;
2039
2040 else
2041
2042 retVal = ( int )dwBytesRead;
2043
2044 } else {
2045
2046 if ( !ReadFile ( hChannel, buffer, dwSize, &dwBytesRead, NULL ) )
2047
2048 retVal = -1;
2049
2050 else
2051
2052 retVal = ( int )dwBytesRead;
2053
2054 } // end else
2055
2056 } // end else
2057
2058 return retVal;
2059
2060} // end _get_buffer
2061
2062
2063static DWORD __fastcall _get_access_mask ( OSD_SingleProtection prt ) {
2064
302f96fb 2065 DWORD retVal = 0;
7fd59977 2066
2067 switch ( prt ) {
2068
2069 case OSD_None:
2070
2071 retVal = 0;
2072
2073 break;
2074
2075 case OSD_R:
2076
2077 retVal = FILE_GENERIC_READ;
2078
2079 break;
2080
2081 case OSD_W:
2082
2083 retVal = FILE_GENERIC_WRITE;
2084
2085 break;
2086
2087 case OSD_RW:
2088
2089 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
2090
2091 break;
2092
2093 case OSD_X:
2094
2095 retVal = FILE_GENERIC_EXECUTE;
2096
2097 break;
2098
2099 case OSD_RX:
2100
2101 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
2102
2103 break;
2104
2105 case OSD_WX:
2106
2107 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2108
2109 break;
2110
2111 case OSD_RWX:
2112
2113 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2114
2115 break;
2116
2117 case OSD_D:
2118
2119 retVal = DELETE;
2120
2121 break;
2122
2123 case OSD_RD:
2124
2125 retVal = FILE_GENERIC_READ | DELETE;
2126
2127 break;
2128
2129 case OSD_WD:
2130
2131 retVal = FILE_GENERIC_WRITE | DELETE;
2132
2133 break;
2134
2135 case OSD_RWD:
2136
2137 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE;
2138
2139 break;
2140
2141 case OSD_XD:
2142
2143 retVal = FILE_GENERIC_EXECUTE | DELETE;
2144
2145 break;
2146
2147 case OSD_RXD:
2148
2149 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE;
2150
2151 break;
2152
2153 case OSD_WXD:
2154
2155 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2156
2157 break;
2158
2159 case OSD_RWXD:
2160
2161 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2162
2163 break;
2164
2165 default:
2166
d9ff84e8 2167 RAISE( "_get_access_mask (): incorrect parameter" );
7fd59977 2168
2169 } // end switch
2170
2171 return retVal;
2172
2173} // end _get_access_mask
2174
2175static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt ) {
2176
302f96fb 2177 DWORD retVal = 0;
7fd59977 2178
2179 switch ( prt ) {
2180
2181 case OSD_None:
2182
2183 retVal = 0;
2184
2185 break;
2186
2187 case OSD_R:
2188
2189 retVal = GENERIC_READ;
2190
2191 break;
2192
2193 case OSD_W:
2194
2195 retVal = GENERIC_WRITE;
2196
2197 break;
2198
2199 case OSD_RW:
2200
2201 retVal = GENERIC_READ | GENERIC_WRITE;
2202
2203 break;
2204
2205 case OSD_X:
2206
2207 retVal = GENERIC_EXECUTE;
2208
2209 break;
2210
2211 case OSD_RX:
2212
2213 retVal = GENERIC_READ | GENERIC_EXECUTE;
2214
2215 break;
2216
2217 case OSD_WX:
2218
2219 retVal = GENERIC_WRITE | GENERIC_EXECUTE;
2220
2221 break;
2222
2223 case OSD_RWX:
2224
2225 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE;
2226
2227 break;
2228
2229 case OSD_D:
2230
2231 retVal = DELETE;
2232
2233 break;
2234
2235 case OSD_RD:
2236
2237 retVal = GENERIC_READ | DELETE;
2238
2239 break;
2240
2241 case OSD_WD:
2242
2243 retVal = GENERIC_WRITE | DELETE;
2244
2245 break;
2246
2247 case OSD_RWD:
2248
2249 retVal = GENERIC_READ | GENERIC_WRITE | DELETE;
2250
2251 break;
2252
2253 case OSD_XD:
2254
2255 retVal = GENERIC_EXECUTE | DELETE;
2256
2257 break;
2258
2259 case OSD_RXD:
2260
2261 retVal = GENERIC_READ | GENERIC_EXECUTE | DELETE;
2262
2263 break;
2264
2265 case OSD_WXD:
2266
2267 retVal = GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2268
2269 break;
2270
2271 case OSD_RWXD:
2272
2273 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2274
2275 break;
2276
2277 default:
2278
d9ff84e8 2279 RAISE( "_get_dir_access_mask (): incorrect parameter" );
7fd59977 2280
2281 } // end switch
2282
2283 return retVal;
2284
2285} // end _get_dir_access_mask
2286
2287static HANDLE __fastcall _open_file (
2288 Standard_CString fName,
2289 OSD_OpenMode oMode,
2290 DWORD dwOptions, LPBOOL fNew
2291 ) {
2292
2293 HANDLE retVal = INVALID_HANDLE_VALUE;
302f96fb 2294 DWORD dwDesiredAccess = 0;
7fd59977 2295 DWORD dwCreationDistribution;
2296
2297 switch ( oMode ) {
2298
2299 case OSD_ReadOnly:
2300
2301 dwDesiredAccess = GENERIC_READ;
2302
2303 break;
2304
2305 case OSD_WriteOnly:
2306
2307 dwDesiredAccess = GENERIC_WRITE;
2308
2309 break;
2310
2311 case OSD_ReadWrite:
2312
2313 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
2314
2315 break;
2316
2317 default:
2318
d9ff84e8 2319 RAISE( "_open_file (): incorrect parameter" );
7fd59977 2320
2321 } // end switch
2322
2323 dwCreationDistribution = ( dwOptions != OPEN_NEW ) ? OPEN_EXISTING : CREATE_ALWAYS;
2324
d9ff84e8 2325 // make wide character string from UTF-8
2326 TCollection_ExtendedString fNameW(fName, Standard_True);
2327 retVal = CreateFileW (
2328 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
7fd59977 2329 FILE_SHARE_READ | FILE_SHARE_WRITE,
2330 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2331 );
2332
2333 if ( retVal == INVALID_HANDLE_VALUE &&
2334 dwOptions == OPEN_APPEND &&
2335 GetLastError () == ERROR_FILE_NOT_FOUND
2336 ) {
2337
2338
2339 dwCreationDistribution = CREATE_ALWAYS;
2340
d9ff84e8 2341 retVal = CreateFileW (
2342 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
7fd59977 2343 FILE_SHARE_READ | FILE_SHARE_WRITE,
2344 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2345 );
2346
2347 *fNew = TRUE;
2348
2349 } // end if
2350
2351 return retVal;
2352
2353} // end _open_file
2354
2355Standard_Integer __fastcall _get_file_type (
6ff736d8 2356 Standard_CString fName, HANDLE fileHandle
7fd59977 2357 ) {
2358
302f96fb 2359 Standard_Integer retVal = 0;
7fd59977 2360 DWORD dwType;
2361 int fileType;
2362
6ff736d8 2363 fileType = (fileHandle == INVALID_HANDLE_VALUE ?
2364 FILE_TYPE_DISK : GetFileType (fileHandle));
7fd59977 2365
2366 switch ( fileType ) {
2367
2368 case FILE_TYPE_UNKNOWN:
2369
2370 retVal = FLAG_SOCKET;
2371
2372 break;
2373
2374 case FILE_TYPE_DISK:
d9ff84e8 2375 {
2376 // make wide character string from UTF-8
2377 TCollection_ExtendedString fNameW(fName, Standard_True);
2378 dwType = GetFileAttributesW ( (const wchar_t*) fNameW.ToExtString() );
2379 if ( dwType != 0xFFFFFFFF )
7fd59977 2380
2381 retVal = dwType & FILE_ATTRIBUTE_DIRECTORY ? FLAG_DIRECTORY : FLAG_FILE;
2382
2383 else
2384
2385 retVal = 0x80000000;
d9ff84e8 2386 }
7fd59977 2387 break;
2388
2389 case FILE_TYPE_CHAR:
2390
2391 retVal = FLAG_DEVICE;
2392
2393 break;
2394
2395 case FILE_TYPE_PIPE:
2396
2397 retVal = FLAG_PIPE;
2398
2399 break;
2400
2401 } // end switch
2402
2403 return retVal;
2404
2405} // end _get_file_type
2406
2407#if defined(__CYGWIN32__) || defined(__MINGW32__)
2408#define __try
2409#define __finally
2410#define __leave return retVal
2411#endif
2412
2413BOOL __fastcall _osd_wnt_sd_to_protection (
2414 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL fDir
2415 ) {
2416
2417 int i;
2418 BOOL fPresent;
2419 BOOL fDefaulted;
2420 PACL pACL;
2421 PSID pSIDowner;
2422 PSID pSIDadmin;
2423 PSID pSIDworld;
2424 LPVOID pACE;
2425 DWORD dwAccessOwner = 0;
2426 DWORD dwAccessGroup = 0;
2427 DWORD dwAccessAdmin = 0;
2428 DWORD dwAccessWorld = 0;
2429 BOOL retVal = FALSE;
2430 GET_PROT_FUNC _get_prot_func = fDir ? &_get_protection_dir : &_get_protection;
2431
2432 __try {
2433
2434 if ( !GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) ) __leave;
2435
2436 if ( !GetSecurityDescriptorDacl ( pSD, &fPresent, &pACL, &fDefaulted ) ||
2437 !fPresent
2438 ) __leave;
2439
2440 if ( pSIDowner == NULL || pACL == NULL ) {
2441
2442 SetLastError ( ERROR_NO_SECURITY_ON_OBJECT );
2443 __leave;
2444
2445 } // end if
2446
2447 pSIDadmin = AdminSid ();
2448 pSIDworld = WorldSid ();
2449
2450 for ( i = 0; i < ( int )pACL -> AceCount; ++i ) {
2451
2452 if ( GetAce ( pACL, i, &pACE ) ) {
2453
2454 if ( EqualSid ( pSIDowner, GET_SID( pACE ) ) )
2455
2456 dwAccessOwner = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2457 0 : *GET_MSK( pACE );
2458
2459 else if ( EqualSid ( pSIDadmin, GET_SID( pACE ) ) )
2460
2461 dwAccessAdmin = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2462 0 : *GET_MSK( pACE );
2463
2464 else if ( EqualSid ( pSIDworld, GET_SID( pACE ) ) )
2465
2466 dwAccessWorld = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2467 0 : *GET_MSK( pACE );
2468
2469 else
2470
2471 dwAccessGroup = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2472 0 : *GET_MSK( pACE );
2473
2474 } // end if
2475
2476 } // end for
2477
2478 prot.SetValues (
2479 ( *_get_prot_func ) ( dwAccessAdmin ),
2480 ( *_get_prot_func ) ( dwAccessOwner ),
2481 ( *_get_prot_func ) ( dwAccessGroup ),
2482 ( *_get_prot_func ) ( dwAccessWorld )
2483 );
2484
2485 retVal = TRUE;
2486
2487 } // end __try
2488
2489 __finally {}
2490
2491#ifdef VAC
2492leave: ; // added for VisualAge
2493#endif
2494
2495 return retVal;
2496
2497} // end _osd_wnt_sd_to_protection
2498
2499#if defined(__CYGWIN32__) || defined(__MINGW32__)
2500#undef __try
2501#undef __finally
2502#undef __leave
2503#endif
2504
2505static OSD_SingleProtection __fastcall _get_protection ( DWORD mask ) {
2506
2507 OSD_SingleProtection retVal;
2508
2509 switch ( mask ) {
2510
2511 case FILE_GENERIC_READ:
2512
2513 retVal = OSD_R;
2514
2515 break;
2516
2517 case FILE_GENERIC_WRITE:
2518
2519 retVal = OSD_W;
2520
2521 break;
2522
2523 case FILE_GENERIC_READ | FILE_GENERIC_WRITE:
2524
2525 retVal = OSD_RW;
2526
2527 break;
2528
2529 case FILE_GENERIC_EXECUTE:
2530
2531 retVal = OSD_X;
2532
2533 break;
2534
2535 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE:
2536
2537 retVal = OSD_RX;
2538
2539 break;
2540
2541 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2542
2543 retVal = OSD_WX;
2544
2545 break;
2546
2547 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2548
2549 retVal = OSD_RWX;
2550
2551 break;
2552
2553 case DELETE:
2554
2555 retVal = OSD_D;
2556
2557 break;
2558
2559 case FILE_GENERIC_READ | DELETE:
2560
2561 retVal = OSD_RD;
2562
2563 break;
2564
2565 case FILE_GENERIC_WRITE | DELETE:
2566
2567 retVal = OSD_WD;
2568
2569 break;
2570
2571 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE:
2572
2573 retVal = OSD_RWD;
2574
2575 break;
2576
2577 case FILE_GENERIC_EXECUTE | DELETE:
2578
2579 retVal = OSD_XD;
2580
2581 break;
2582
2583 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE:
2584
2585 retVal = OSD_RXD;
2586
2587 break;
2588
2589 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2590
2591 retVal = OSD_WXD;
2592
2593 break;
2594
2595 case FILE_ALL_ACCESS:
2596 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2597
2598 retVal = OSD_RWXD;
2599
2600 break;
2601
2602 case 0:
2603 default:
2604
2605 retVal = OSD_None;
2606
2607 } // end switch
2608
2609 return retVal;
2610
2611} // end _get_protection
2612
2613static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD mask ) {
2614
2615 OSD_SingleProtection retVal;
2616
2617 switch ( mask ) {
2618
2619 case GENERIC_READ:
2620
2621 retVal = OSD_R;
2622
2623 break;
2624
2625 case GENERIC_WRITE:
2626
2627 retVal = OSD_W;
2628
2629 break;
2630
2631 case GENERIC_READ | GENERIC_WRITE:
2632
2633 retVal = OSD_RW;
2634
2635 break;
2636
2637 case GENERIC_EXECUTE:
2638
2639 retVal = OSD_X;
2640
2641 break;
2642
2643 case GENERIC_READ | GENERIC_EXECUTE:
2644
2645 retVal = OSD_RX;
2646
2647 break;
2648
2649 case GENERIC_WRITE | GENERIC_EXECUTE:
2650
2651 retVal = OSD_WX;
2652
2653 break;
2654
2655 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE:
2656
2657 retVal = OSD_RWX;
2658
2659 break;
2660
2661 case DELETE:
2662
2663 retVal = OSD_D;
2664
2665 break;
2666
2667 case GENERIC_READ | DELETE:
2668
2669 retVal = OSD_RD;
2670
2671 break;
2672
2673 case GENERIC_WRITE | DELETE:
2674
2675 retVal = OSD_WD;
2676
2677 break;
2678
2679 case GENERIC_READ | GENERIC_WRITE | DELETE:
2680
2681 retVal = OSD_RWD;
2682
2683 break;
2684
2685 case GENERIC_EXECUTE | DELETE:
2686
2687 retVal = OSD_XD;
2688
2689 break;
2690
2691 case GENERIC_READ | GENERIC_EXECUTE | DELETE:
2692
2693 retVal = OSD_RXD;
2694
2695 break;
2696
2697 case GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2698
2699 retVal = OSD_WXD;
2700
2701 break;
2702
2703 case FILE_ALL_ACCESS:
2704 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2705
2706 retVal = OSD_RWXD;
2707
2708 break;
2709
2710 case 0:
2711 default:
2712
2713 retVal = OSD_None;
2714
2715 } // end switch
2716
2717 return retVal;
2718
2719} // end _get_protection_dir
2720
2721#if defined(__CYGWIN32__) || defined(__MINGW32__)
2722#define __try
2723#define __finally
2724#define __leave return fOK
2725#endif
2726
d9ff84e8 2727BOOL __fastcall _osd_print (const Standard_PCharacter pName, const wchar_t* fName ) {
7fd59977 2728
2729 BOOL fOK, fJob;
302f96fb 2730 HANDLE hPrinter = NULL;
7fd59977 2731 BYTE jobInfo[ MAX_PATH + sizeof ( DWORD ) ];
302f96fb 2732 DWORD dwNeeded, dwCode = 0;
7fd59977 2733
2734 fOK = fJob = FALSE;
2735
2736 __try {
2737
2738 if ( !OpenPrinter ( Standard_PCharacter(pName), &hPrinter, NULL ) ) {
2739
2740 hPrinter = NULL;
2741 __leave;
2742
2743 } // end if
2744
d9ff84e8 2745 if ( !AddJobW (
7fd59977 2746 hPrinter, 1, jobInfo, MAX_PATH + sizeof ( DWORD ), &dwNeeded
2747 )
2748 ) __leave;
2749
2750 fJob = TRUE;
2751
d9ff84e8 2752 if ( !CopyFileW (
2753 fName, (LPWSTR) ( ( ADDJOB_INFO_1* )jobInfo ) -> Path, FALSE
7fd59977 2754 )
2755 ) __leave;
2756
2757 if ( !ScheduleJob (
2758 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId
2759 )
2760 ) __leave;
2761
2762 fOK = TRUE;
2763
2764 } // end __try
2765
2766 __finally {
2767
2768 if ( !fOK ) {
2769
2770 BYTE info[ 1024 ];
2771 DWORD dwBytesNeeded;
2772
2773 dwCode = GetLastError ();
2774
2775 if ( fJob && hPrinter != NULL ) {
2776
2777 GetJob (
2778 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId, 1,
2779 info, 1024, &dwBytesNeeded
2780 );
2781
2782 if ( fJob ) SetJob (
2783 hPrinter,
2784 ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId,
2785 1, info, JOB_CONTROL_CANCEL
2786 );
2787
2788 } // end if
2789
2790 } // end if
2791
2792 if ( hPrinter != NULL ) ClosePrinter ( hPrinter );
2793
2794 } // end __finally
2795
2796#ifdef VAC
2797leave: ; // added for VisualAge
2798#endif
2799
2800 if ( !fOK ) SetLastError ( dwCode );
2801
2802 return fOK;
2803
2804} // end _osd_print
2805
2806#if defined(__CYGWIN32__) || defined(__MINGW32__)
2807#undef __try
2808#undef __finally
2809#undef __leave
2810#endif
2811
2812Standard_Boolean OSD_File::IsReadable()
2813{
2814 TCollection_AsciiString FileName ;
2815 HANDLE Channel ;
2816
2817 myPath.SystemName(FileName) ;
2818 Channel = _open_file(FileName.ToCString(), OSD_ReadOnly, OPEN_OLD) ;
2819 if (Channel == INVALID_HANDLE_VALUE)
2820 return Standard_False ;
2821 else {
2822 CloseHandle (Channel) ;
2823 return Standard_True ;
2824 }
2825}
2826
2827
2828Standard_Boolean OSD_File::IsWriteable()
2829{
2830 TCollection_AsciiString FileName ;
2831 HANDLE Channel ;
2832
2833 myPath.SystemName(FileName) ;
2834 Channel = _open_file(FileName.ToCString(), OSD_ReadWrite, OPEN_OLD) ;
2835 if (Channel == INVALID_HANDLE_VALUE)
2836 return Standard_False ;
2837 else {
2838 CloseHandle (Channel) ;
2839 return Standard_True ;
2840 }
2841}
2842
2843Standard_Boolean OSD_File::IsExecutable()
2844{
2845 return IsReadable() ;
2846
2847// if (_access(FileName.ToCString(),0))
2848}
2849
6ff736d8 2850#endif /* _WIN32 */
7fd59977 2851
4485f3d0 2852// ---------------------------------------------------------------------
2853// Destructs a file object (unlocks and closes file if it is open)
2854// ---------------------------------------------------------------------
2855
2856OSD_File::~OSD_File()
2857{
2858 if (IsOpen())
2859 {
2860 if (IsLocked())
2861 UnLock();
2862 Close();
2863 }
2864}
7fd59977 2865
2866// ---------------------------------------------------------------------
2867// Read lines in a file while it is increasing.
2868// Each line is terminated with a <nl>.
2869// ---------------------------------------------------------------------
2870
2871#include <OSD.hxx>
2872
2873Standard_Boolean OSD_File::ReadLastLine(TCollection_AsciiString& aLine,const Standard_Integer aDelay,const Standard_Integer aNbTries)
2874{
2875 static Standard_Integer MaxLength = 1000 ;
2876 Standard_Integer Len ;
2877 Standard_Integer Count = aNbTries ;
2878
2879 if (Count <= 0)
2880 return Standard_False ;
302f96fb 2881 for(;;) {
7fd59977 2882 ReadLine(aLine, MaxLength, Len) ;
2883 if (!aLine.IsEmpty())
2884 return Standard_True ;
2885 if (!--Count)
2886 return Standard_False ;
2887 OSD::SecSleep(aDelay) ;
2888 }
2889}
2890
2891
2892Standard_Boolean OSD_File::Edit()
2893{
2894 cout << "Function OSD_File::Edit() not yet implemented." << endl;
2895 return Standard_False ;
2896}
2897
2898
2899