0026290: It is neccessary to separate visualization part from TKCAF
[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// --------------------------------------------------------------------------
387void OSD_File::Read( Standard_Address& Buffer,
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
844#define _int64 int
845#endif
846
847#pragma comment( lib, "WSOCK32.LIB" )
848#pragma comment( lib, "WINSPOOL.LIB" )
849
850#define ACE_HEADER_SIZE ( sizeof ( ACCESS_ALLOWED_ACE ) - sizeof ( DWORD ) )
851
852#define RAISE( arg ) Standard_ProgramError :: Raise ( ( arg ) )
6ff736d8 853#define TEST_RAISE( arg ) _test_raise ( myFileHandle, ( arg ) )
7fd59977 854
855#define OPEN_NEW 0
856#define OPEN_OLD 1
857#define OPEN_APPEND 2
858
859void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
d9ff84e8 860PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, wchar_t* = NULL );
7fd59977 861BOOL __fastcall _osd_wnt_sd_to_protection (
862 PSECURITY_DESCRIPTOR, OSD_Protection&, BOOL
863 );
d9ff84e8 864BOOL __fastcall _osd_print (const Standard_PCharacter, const wchar_t* );
7fd59977 865
6ff736d8 866static void __fastcall _test_raise ( HANDLE, Standard_CString );
68299304 867static Standard_Integer __fastcall _get_line (Standard_PCharacter& buffer, DWORD dwBuffSize, LONG& theSeekPos);
7fd59977 868static int __fastcall _get_buffer ( HANDLE, Standard_PCharacter&, DWORD, BOOL, BOOL );
869static DWORD __fastcall _get_access_mask ( OSD_SingleProtection );
870static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt );
871static HANDLE __fastcall _open_file ( Standard_CString, OSD_OpenMode, DWORD, LPBOOL = NULL );
872
873static OSD_SingleProtection __fastcall _get_protection ( DWORD );
874static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD );
875
876typedef OSD_SingleProtection ( __fastcall *GET_PROT_FUNC ) ( DWORD );
877
6ff736d8 878Standard_Integer __fastcall _get_file_type ( Standard_CString, HANDLE );
7fd59977 879
880// ---------------------------------------------------------------------
881// Create an empty file object
882// ---------------------------------------------------------------------
883
6ff736d8 884OSD_File :: OSD_File ()
885{
7fd59977 886 ImperativeFlag = Standard_False;
887 myLock = OSD_NoLock;
888 myIO = 0;
6ff736d8 889 myFileChannel = -1;
890 myFileHandle = INVALID_HANDLE_VALUE;
7fd59977 891} // end constructor ( 1 )
892
893// ---------------------------------------------------------------------
894// Create and initialize a file object
895// ---------------------------------------------------------------------
896
6ff736d8 897OSD_File :: OSD_File ( const OSD_Path& Name ) : OSD_FileNode ( Name )
898{
7fd59977 899 ImperativeFlag = Standard_False;
900 myLock = OSD_NoLock;
901 myIO = 0;
902 myPath = Name;
6ff736d8 903 myFileChannel = -1;
904 myFileHandle = INVALID_HANDLE_VALUE;
7fd59977 905} // end constructor ( 2 )
906
95e05159 907// ---------------------------------------------------------------------
908// Redirect a standard handle (fileno(stdout), fileno(stdin) or
909// fileno(stderr) to this OSD_File and return the copy of the original
910// standard handle.
911// Example:
912// OSD_File aTmp;
913// aTmp.BuildTemporary();
914// int stdfd = _fileno(stdout);
915//
916// int oldout = aTmp.Capture(stdfd);
917// cout << "Some output to the file" << endl;
918// cout << flush;
919// fflush(stdout);
920//
921// _dup2(oldout, stdfd); // Restore standard output
922// aTmp.Close();
923// ---------------------------------------------------------------------
924int OSD_File::Capture(int theDescr) {
925 // Get POSIX file descriptor from this file handle
926 int dFile = _open_osfhandle(reinterpret_cast<intptr_t>(myFileHandle), myMode);
927
928 if (0 > dFile)
929 {
930 _osd_wnt_set_error ( myError, OSD_WFile, myFileHandle );
931 return -1;
932 }
933
934 // Duplicate an old file descriptor of the given one to be able to restore output to it later.
935 int oldDescr = _dup(theDescr);
936 // Redirect the output to this file
937 _dup2(dFile, theDescr);
938
939 // Return the old descriptor
940 return oldDescr;
941}
942
943void OSD_File::Rewind() {
944 SetFilePointer( myFileHandle, 0, NULL, FILE_BEGIN );
945}
946
6ff736d8 947// protect against occasional use of myFileHande in Windows code
948#define myFileChannel myFileChannel_is_only_for_Linux
949
7fd59977 950// ---------------------------------------------------------------------
951// Build a file if it doesn't exist or create again if it already exists
952// ---------------------------------------------------------------------
953
954void OSD_File :: Build (
955 const OSD_OpenMode Mode, const OSD_Protection& Protect
956 ) {
957
958 TCollection_AsciiString fName;
959
960 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
961 Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
962 }
963
6ff736d8 964 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 965
d9ff84e8 966 RAISE( "OSD_File :: Build (): incorrect call - file already opened" );
7fd59977 967
968 myMode = Mode;
969 myPath.SystemName ( fName );
970
971 if ( fName.IsEmpty () )
972
d9ff84e8 973 RAISE( "OSD_File :: Build (): incorrent call - no filename given" );
7fd59977 974
6ff736d8 975 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_NEW );
7fd59977 976
6ff736d8 977 if (myFileHandle == INVALID_HANDLE_VALUE)
7fd59977 978
979 _osd_wnt_set_error ( myError, OSD_WFile );
980
981 else {
982
983 SetProtection ( Protect );
984 myIO |= FLAG_FILE;
985
986 } // end else
987
988} // end OSD_File :: Build
989
990
991
992// ---------------------------------------------------------------------
993// Open a file
994// ---------------------------------------------------------------------
995
302f96fb 996void OSD_File :: Open (const OSD_OpenMode Mode, const OSD_Protection& /*Protect*/)
997{
7fd59977 998
999 TCollection_AsciiString fName;
1000
1001
6ff736d8 1002 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 1003
d9ff84e8 1004 RAISE( "OSD_File :: Open (): incorrect call - file already opened" );
7fd59977 1005
1006 myMode = Mode;
1007 myPath.SystemName ( fName );
1008
1009 if ( fName.IsEmpty () )
1010
d9ff84e8 1011 RAISE( "OSD_File :: Open (): incorrent call - no filename given" );
7fd59977 1012
6ff736d8 1013 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_OLD );
7fd59977 1014
6ff736d8 1015 if (myFileHandle == INVALID_HANDLE_VALUE) {
7fd59977 1016
1017 _osd_wnt_set_error ( myError, OSD_WFile );
1018 }
1019 else
1020 {
6ff736d8 1021 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
7fd59977 1022 }
1023} // end OSD_File :: Open
1024
1025// ---------------------------------------------------------------------
1026// Append to an existing file
1027// ---------------------------------------------------------------------
1028
1029void OSD_File :: Append (
1030 const OSD_OpenMode Mode, const OSD_Protection& Protect
1031 ) {
1032
1033 BOOL fNew = FALSE;
1034 TCollection_AsciiString fName;
1035
6ff736d8 1036 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 1037
d9ff84e8 1038 RAISE( "OSD_File :: Append (): incorrect call - file already opened" );
7fd59977 1039
1040 myMode = Mode;
1041 myPath.SystemName ( fName );
1042
1043 if ( fName.IsEmpty () )
1044
d9ff84e8 1045 RAISE( "OSD_File :: Append (): incorrent call - no filename given" );
7fd59977 1046
6ff736d8 1047 myFileHandle = _open_file ( fName.ToCString (), Mode, OPEN_APPEND, &fNew );
7fd59977 1048
6ff736d8 1049 if (myFileHandle == INVALID_HANDLE_VALUE)
7fd59977 1050
1051 _osd_wnt_set_error ( myError, OSD_WFile );
1052
1053 else {
1054
1055 if ( !fNew ) {
1056
6ff736d8 1057 myIO |= _get_file_type ( fName.ToCString (), myFileHandle );
7fd59977 1058 Seek ( 0, OSD_FromEnd );
1059
1060 } else {
1061
1062 SetProtection ( Protect );
1063 myIO |= FLAG_FILE;
1064
1065 } // end else
1066
1067 } // end else;
1068
1069} // end OSD_File :: Append
1070
1071// ---------------------------------------------------------------------
1072// Read content of a file
1073// ---------------------------------------------------------------------
1074
1075void OSD_File :: Read (
1076 TCollection_AsciiString& Buffer, const Standard_Integer Nbyte
1077 ) {
1078
1079 if ( OSD_File::KindOfFile ( ) == OSD_DIRECTORY ) {
0797d9d3 1080#ifdef OCCT_DEBUG
7fd59977 1081 cout << " OSD_File::Read : it is a directory " << endl;
63c629aa 1082#endif
7fd59977 1083 return ;
1084// Standard_ProgramError::Raise("OSD_File::Read : it is a directory");
1085 }
1086
1087 Standard_Integer NbyteRead;
1088 Standard_Address buff;
1089
d9ff84e8 1090 TEST_RAISE( "Read" );
7fd59977 1091
1092 buff = ( Standard_Address )new Standard_Character[ Nbyte + 1 ];
1093
1094 Read ( buff, Nbyte, NbyteRead );
1095
1096 ( ( Standard_PCharacter )buff )[ NbyteRead ] = 0;
1097
1098 if ( NbyteRead != 0 )
1099
1100 Buffer = ( Standard_PCharacter )buff;
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 (
1262 Standard_Address& Buffer,
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;
1459 LPTSTR keyPath;
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
6ff736d8 1630Standard_Size OSD_File :: Size () {
7fd59977 1631
1632 Standard_Integer retVal;
1633
d9ff84e8 1634 TEST_RAISE( "Size" );
7fd59977 1635
6ff736d8 1636 LARGE_INTEGER aSize;
1637 aSize.QuadPart = 0;
1638 retVal = GetFileSizeEx (myFileHandle, &aSize);
7fd59977 1639
6ff736d8 1640 if ( retVal == 0 )
7fd59977 1641
1642 _osd_wnt_set_error ( myError, OSD_WFile );
1643
6ff736d8 1644 return (Standard_Size)aSize.QuadPart;
7fd59977 1645
1646} // end OSD_File :: Size
1647
1648// --------------------------------------------------------------------------
1649// Print contains of a file
1650// --------------------------------------------------------------------------
1651
1652void OSD_File :: Print ( const OSD_Printer& WhichPrinter ) {
1653
6ff736d8 1654 if (myFileHandle != INVALID_HANDLE_VALUE)
7fd59977 1655
d9ff84e8 1656 RAISE( "OSD_File :: Print (): incorrect call - file opened" );
7fd59977 1657
1658 TCollection_AsciiString pName, fName;
1659
1660 WhichPrinter.Name ( pName );
1661 myPath.SystemName ( fName );
d9ff84e8 1662 TCollection_ExtendedString fNameW(fName);
7fd59977 1663
d9ff84e8 1664 if ( !_osd_print ( (Standard_PCharacter)pName.ToCString (),
1665 (const wchar_t*)fNameW.ToExtString () ) )
7fd59977 1666
1667 _osd_wnt_set_error ( myError, OSD_WFile );
1668
1669} // end OSD_File :: Print
1670
1671// --------------------------------------------------------------------------
1672// Test if a file is open
1673// --------------------------------------------------------------------------
1674
1675Standard_Boolean OSD_File :: IsOpen () const {
1676
6ff736d8 1677 return myFileHandle != INVALID_HANDLE_VALUE;
7fd59977 1678
1679} // end OSD_File :: IsOpen
1680
1681#if defined(__CYGWIN32__) || defined(__MINGW32__)
1682#define __try
1683#define __finally
1684#define __leave return retVal
1685#endif
1686
1687PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd (
d9ff84e8 1688 const OSD_Protection& prot, BOOL fDir, wchar_t* fName
7fd59977 1689 ) {
1690
1691 int i, j;
1692 BOOL fOK = FALSE;
1693 PACL pACL = NULL;
1694 HANDLE hProcess = NULL;
1695 PSID pSIDowner;
1696 PSID pSIDadmin;
1697 PSID pSIDworld;
1698 PSID pSIDtemp;
1699 DWORD dwAccessAdmin;
1700 DWORD dwAccessGroup;
1701 DWORD dwAccessOwner;
1702 DWORD dwAccessWorld;
1703 DWORD dwAccessAdminDir;
1704 DWORD dwAccessGroupDir;
1705 DWORD dwAccessOwnerDir;
1706 DWORD dwAccessWorldDir;
1707 DWORD dwACLsize = sizeof ( ACL );
1708 DWORD dwIndex = 0;
1709 PTOKEN_OWNER pTkOwner = NULL;
1710 PTOKEN_GROUPS pTkGroups = NULL;
1711 PTOKEN_PRIMARY_GROUP pTkPrimaryGroup = NULL;
302f96fb 1712 PSECURITY_DESCRIPTOR retVal = NULL;
7fd59977 1713 PSECURITY_DESCRIPTOR pfSD = NULL;
1714 BOOL fDummy;
1715 PFILE_ACE pFileACE;
1716
1717 __try {
1718
1719 j = fDir ? 1 : 0;
1720
1721 if ( !OpenProcessToken (
1722 GetCurrentProcess (), TOKEN_QUERY, &hProcess
1723 )
1724 ) __leave;
1725
1726 if ( ( pTkGroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
1727 hProcess, TokenGroups
1728 )
1729 ) == NULL
1730 ) __leave;
1731
1732 if ( ( pTkOwner = ( PTOKEN_OWNER )GetTokenInformationEx (
1733 hProcess, TokenOwner
1734 )
1735 ) == NULL
1736 ) __leave;
1737
1738 if ( ( pTkPrimaryGroup = ( PTOKEN_PRIMARY_GROUP )GetTokenInformationEx (
1739 hProcess, TokenPrimaryGroup
1740 )
1741 ) == NULL
1742 ) __leave;
1743
1744
1745retry:
1746 if ( fName == NULL )
1747
1748 pSIDowner = pTkOwner -> Owner;
1749
1750 else {
1751
1752 pfSD = GetFileSecurityEx ( fName, OWNER_SECURITY_INFORMATION );
1753
1754 if ( pfSD == NULL || !GetSecurityDescriptorOwner ( pfSD, &pSIDowner, &fDummy ) ) {
1755
1756 fName = NULL;
1757 goto retry;
1758
1759 } // end if
1760
1761 } // end else
1762
1763 pSIDadmin = AdminSid ();
1764 pSIDworld = WorldSid ();
1765
1766 dwAccessAdmin = _get_access_mask ( prot.System () );
1767 dwAccessGroup = _get_access_mask ( prot.Group () );
1768 dwAccessOwner = _get_access_mask ( prot.User () );
1769 dwAccessWorld = _get_access_mask ( prot.World () );
1770
1771 dwAccessAdminDir = _get_dir_access_mask ( prot.System () );
1772 dwAccessGroupDir = _get_dir_access_mask ( prot.Group () );
1773 dwAccessOwnerDir = _get_dir_access_mask ( prot.User () );
1774 dwAccessWorldDir = _get_dir_access_mask ( prot.World () );
1775
1776 if ( dwAccessGroup != 0 ) {
1777
1778 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1779
1780 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1781
1782 if ( !NtPredefinedSid ( pSIDtemp ) &&
1783 !EqualSid ( pSIDtemp, pSIDworld ) &&
1784 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1785 IsValidSid ( pSIDtemp )
1786 )
1787
1788 dwACLsize += ( ( GetLengthSid ( pSIDtemp ) + ACE_HEADER_SIZE ) << j );
1789
1790 } // end for
1791
1792 } // end if
1793
1794 dwACLsize += ( ( ( GetLengthSid ( pSIDowner ) + ACE_HEADER_SIZE ) << j ) +
1795 ( ( GetLengthSid ( pSIDadmin ) + ACE_HEADER_SIZE ) << j ) +
1796 ( ( GetLengthSid ( pSIDworld ) + ACE_HEADER_SIZE ) << j )
1797 );
1798
1799 if ( ( pACL = CreateAcl ( dwACLsize ) ) == NULL ) __leave;
1800
1801 if ( dwAccessAdmin != 0 )
1802
1803 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1804 dwAccessAdmin, 0,
1805 pSIDadmin
1806 )
1807 ) != NULL
1808 ) {
1809
1810 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1811
1812 if ( fDir ) {
1813
1814 pFileACE -> dwMask = dwAccessAdminDir;
1815 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1816 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1817
1818 } // end if
1819
1820 FreeAce ( pFileACE );
1821
1822 } // end if
1823
1824 if ( dwAccessOwner != 0 )
1825
1826 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1827 dwAccessOwner, 0, pSIDowner
1828 )
1829 ) != NULL
1830 ) {
1831
1832 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1833
1834 if ( fDir ) {
1835
1836 pFileACE -> dwMask = dwAccessOwnerDir;
1837 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1838 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1839
1840 } // end if
1841
1842 FreeAce ( pFileACE );
1843
1844 } // end if
1845
1846 if ( dwAccessWorld != 0 )
1847
1848 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1849 dwAccessWorld, 0, pSIDworld
1850 )
1851 ) != NULL
1852 ) {
1853
1854 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1855
1856 if ( fDir ) {
1857
1858 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1859 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1860
1861 } // end if
1862
1863 FreeAce ( pFileACE );
1864
1865 } // end if
1866
1867 if ( dwAccessGroup != 0 ) {
1868
1869 for ( i = 0; i < ( int )pTkGroups -> GroupCount; ++i ) {
1870
1871 pSIDtemp = pTkGroups -> Groups[ i ].Sid;
1872
1873 if ( !NtPredefinedSid ( pSIDtemp ) &&
1874 !EqualSid ( pSIDtemp, pSIDworld ) &&
1875 !EqualSid ( pSIDtemp, pTkPrimaryGroup -> PrimaryGroup ) &&
1876 IsValidSid ( pSIDtemp )
1877 ) {
1878
1879 if ( dwAccessGroup != 0 )
1880
1881 if ( ( pFileACE = ( PFILE_ACE )AllocAccessAllowedAce (
1882 dwAccessGroup, 0, pSIDtemp
1883 )
1884 ) != NULL
1885 ) {
1886
1887 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1888
1889 if ( fDir ) {
1890
1891 pFileACE -> header.AceFlags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE;
1892 AddAce ( pACL, ACL_REVISION, dwIndex++, pFileACE, pFileACE -> header.AceSize );
1893
1894 } // end if
1895
1896 FreeAce ( pFileACE );
1897
1898 } // end if
1899
1900 } // end if
1901
1902 } // end for
1903
1904 } // end if
1905
1906 if ( ( retVal = AllocSD () ) == NULL ) __leave;
1907
1908 if ( !SetSecurityDescriptorDacl ( retVal, TRUE, pACL, TRUE ) ) __leave;
1909
1910 fOK = TRUE;
1911
1912 } // end __try
1913
1914 __finally {
1915
1916 if ( !fOK ) {
1917
1918 if ( retVal != NULL )
1919
1920 FreeSD ( retVal );
1921
1922 else if ( pACL != NULL )
1923
1924 FreeAcl ( pACL );
1925
1926 retVal = NULL;
1927
1928 } // end if
1929
1930 if ( hProcess != NULL ) CloseHandle ( hProcess );
1931 if ( pTkOwner != NULL ) FreeTokenInformation ( pTkOwner );
1932 if ( pTkGroups != NULL ) FreeTokenInformation ( pTkGroups );
1933 if ( pTkPrimaryGroup != NULL ) FreeTokenInformation ( pTkPrimaryGroup );
1934 if ( pfSD != NULL ) FreeFileSecurity ( pfSD );
1935
1936 } // end __finally
1937
1938#ifdef VAC
1939leave: ; // added for VisualAge
1940#endif
1941
1942 return retVal;
1943
1944} // end _osd_wnt_protection_to_sd */
1945
1946#if defined(__CYGWIN32__) || defined(__MINGW32__)
1947#undef __try
1948#undef __finally
1949#undef __leave
1950#endif
1951
6ff736d8 1952static void __fastcall _test_raise ( HANDLE hFile, Standard_CString str ) {
7fd59977 1953
1954 Standard_Character buff[ 64 ];
1955
6ff736d8 1956 if (hFile == INVALID_HANDLE_VALUE) {
7fd59977 1957
d9ff84e8 1958 strcpy ( buff, "OSD_File :: " );
1959 strcat ( buff, str );
1960 strcat ( buff, " (): wrong access" );
7fd59977 1961
1962 Standard_ProgramError :: Raise ( buff );
1963
1964 } // end if
1965
1966} // end _test_raise
1967
68299304 1968// Returns number of bytes in the string (including end \n, but excluding \r);
1969//
1970static Standard_Integer __fastcall _get_line (Standard_PCharacter& buffer, DWORD dwBuffSize, LONG& theSeekPos)
1971{
7fd59977 1972
7fd59977 1973 Standard_PCharacter ptr;
1974
1975 buffer[ dwBuffSize ] = 0;
1976 ptr = buffer;
1977
1978 while ( *ptr != 0 ) {
1979
68299304 1980 if ( *ptr == '\n' )
1981 {
1982 ptr++ ; // jump newline char.
1983 *ptr = 0 ;
1984 theSeekPos = (LONG)(ptr - buffer - dwBuffSize);
1985 return (Standard_Integer)(ptr - buffer);
1986 }
1987 else if ( *ptr == '\r' && ptr[ 1 ] == '\n' )
1988 {
1989 *(ptr++) = '\n' ; // Substitue carriage return by newline.
1990 *ptr = 0 ;
1991 theSeekPos = (LONG)(ptr + 1 - buffer - dwBuffSize);
1992 return (Standard_Integer)(ptr - buffer);
1993 }
1994 else if ( *ptr == '\r' && ptr[ 1 ] == 0 ) {
7fd59977 1995 *ptr = '\n' ; // Substitue carriage return by newline
68299304 1996 return -1;
7fd59977 1997 }
1998 ++ptr;
1999
2000 } // end while
2001
68299304 2002 theSeekPos = 0;
2003 return dwBuffSize;
7fd59977 2004} // end _get_line
2005
7fd59977 2006static int __fastcall _get_buffer (
2007 HANDLE hChannel,
2008 Standard_PCharacter& buffer,
2009 DWORD dwSize,
2010 BOOL fPeek, BOOL fSocket
2011 ) {
2012
2013 int retVal = -1;
2014 int flags;
2015 DWORD dwDummy;
2016 DWORD dwBytesRead;
2017
2018 if ( fSocket ) {
2019
2020 flags = fPeek ? MSG_PEEK : 0;
2021
2022 retVal = recv ( ( SOCKET )hChannel, buffer, ( int )dwSize, flags );
2023
2024 if ( retVal == SOCKET_ERROR ) retVal = -1;
2025
2026 } else {
2027
2028 if ( fPeek ) {
2029
2030 if ( !PeekNamedPipe (
2031 hChannel, buffer, dwSize, &dwBytesRead, &dwDummy, &dwDummy
2032 ) && GetLastError () != ERROR_BROKEN_PIPE
2033 )
2034
2035 retVal = -1;
2036
2037 else
2038
2039 retVal = ( int )dwBytesRead;
2040
2041 } else {
2042
2043 if ( !ReadFile ( hChannel, buffer, dwSize, &dwBytesRead, NULL ) )
2044
2045 retVal = -1;
2046
2047 else
2048
2049 retVal = ( int )dwBytesRead;
2050
2051 } // end else
2052
2053 } // end else
2054
2055 return retVal;
2056
2057} // end _get_buffer
2058
2059
2060static DWORD __fastcall _get_access_mask ( OSD_SingleProtection prt ) {
2061
302f96fb 2062 DWORD retVal = 0;
7fd59977 2063
2064 switch ( prt ) {
2065
2066 case OSD_None:
2067
2068 retVal = 0;
2069
2070 break;
2071
2072 case OSD_R:
2073
2074 retVal = FILE_GENERIC_READ;
2075
2076 break;
2077
2078 case OSD_W:
2079
2080 retVal = FILE_GENERIC_WRITE;
2081
2082 break;
2083
2084 case OSD_RW:
2085
2086 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
2087
2088 break;
2089
2090 case OSD_X:
2091
2092 retVal = FILE_GENERIC_EXECUTE;
2093
2094 break;
2095
2096 case OSD_RX:
2097
2098 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
2099
2100 break;
2101
2102 case OSD_WX:
2103
2104 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2105
2106 break;
2107
2108 case OSD_RWX:
2109
2110 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE;
2111
2112 break;
2113
2114 case OSD_D:
2115
2116 retVal = DELETE;
2117
2118 break;
2119
2120 case OSD_RD:
2121
2122 retVal = FILE_GENERIC_READ | DELETE;
2123
2124 break;
2125
2126 case OSD_WD:
2127
2128 retVal = FILE_GENERIC_WRITE | DELETE;
2129
2130 break;
2131
2132 case OSD_RWD:
2133
2134 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE;
2135
2136 break;
2137
2138 case OSD_XD:
2139
2140 retVal = FILE_GENERIC_EXECUTE | DELETE;
2141
2142 break;
2143
2144 case OSD_RXD:
2145
2146 retVal = FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE;
2147
2148 break;
2149
2150 case OSD_WXD:
2151
2152 retVal = FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2153
2154 break;
2155
2156 case OSD_RWXD:
2157
2158 retVal = FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE;
2159
2160 break;
2161
2162 default:
2163
d9ff84e8 2164 RAISE( "_get_access_mask (): incorrect parameter" );
7fd59977 2165
2166 } // end switch
2167
2168 return retVal;
2169
2170} // end _get_access_mask
2171
2172static DWORD __fastcall _get_dir_access_mask ( OSD_SingleProtection prt ) {
2173
302f96fb 2174 DWORD retVal = 0;
7fd59977 2175
2176 switch ( prt ) {
2177
2178 case OSD_None:
2179
2180 retVal = 0;
2181
2182 break;
2183
2184 case OSD_R:
2185
2186 retVal = GENERIC_READ;
2187
2188 break;
2189
2190 case OSD_W:
2191
2192 retVal = GENERIC_WRITE;
2193
2194 break;
2195
2196 case OSD_RW:
2197
2198 retVal = GENERIC_READ | GENERIC_WRITE;
2199
2200 break;
2201
2202 case OSD_X:
2203
2204 retVal = GENERIC_EXECUTE;
2205
2206 break;
2207
2208 case OSD_RX:
2209
2210 retVal = GENERIC_READ | GENERIC_EXECUTE;
2211
2212 break;
2213
2214 case OSD_WX:
2215
2216 retVal = GENERIC_WRITE | GENERIC_EXECUTE;
2217
2218 break;
2219
2220 case OSD_RWX:
2221
2222 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE;
2223
2224 break;
2225
2226 case OSD_D:
2227
2228 retVal = DELETE;
2229
2230 break;
2231
2232 case OSD_RD:
2233
2234 retVal = GENERIC_READ | DELETE;
2235
2236 break;
2237
2238 case OSD_WD:
2239
2240 retVal = GENERIC_WRITE | DELETE;
2241
2242 break;
2243
2244 case OSD_RWD:
2245
2246 retVal = GENERIC_READ | GENERIC_WRITE | DELETE;
2247
2248 break;
2249
2250 case OSD_XD:
2251
2252 retVal = GENERIC_EXECUTE | DELETE;
2253
2254 break;
2255
2256 case OSD_RXD:
2257
2258 retVal = GENERIC_READ | GENERIC_EXECUTE | DELETE;
2259
2260 break;
2261
2262 case OSD_WXD:
2263
2264 retVal = GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2265
2266 break;
2267
2268 case OSD_RWXD:
2269
2270 retVal = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
2271
2272 break;
2273
2274 default:
2275
d9ff84e8 2276 RAISE( "_get_dir_access_mask (): incorrect parameter" );
7fd59977 2277
2278 } // end switch
2279
2280 return retVal;
2281
2282} // end _get_dir_access_mask
2283
2284static HANDLE __fastcall _open_file (
2285 Standard_CString fName,
2286 OSD_OpenMode oMode,
2287 DWORD dwOptions, LPBOOL fNew
2288 ) {
2289
2290 HANDLE retVal = INVALID_HANDLE_VALUE;
302f96fb 2291 DWORD dwDesiredAccess = 0;
7fd59977 2292 DWORD dwCreationDistribution;
2293
2294 switch ( oMode ) {
2295
2296 case OSD_ReadOnly:
2297
2298 dwDesiredAccess = GENERIC_READ;
2299
2300 break;
2301
2302 case OSD_WriteOnly:
2303
2304 dwDesiredAccess = GENERIC_WRITE;
2305
2306 break;
2307
2308 case OSD_ReadWrite:
2309
2310 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
2311
2312 break;
2313
2314 default:
2315
d9ff84e8 2316 RAISE( "_open_file (): incorrect parameter" );
7fd59977 2317
2318 } // end switch
2319
2320 dwCreationDistribution = ( dwOptions != OPEN_NEW ) ? OPEN_EXISTING : CREATE_ALWAYS;
2321
d9ff84e8 2322 // make wide character string from UTF-8
2323 TCollection_ExtendedString fNameW(fName, Standard_True);
2324 retVal = CreateFileW (
2325 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
7fd59977 2326 FILE_SHARE_READ | FILE_SHARE_WRITE,
2327 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2328 );
2329
2330 if ( retVal == INVALID_HANDLE_VALUE &&
2331 dwOptions == OPEN_APPEND &&
2332 GetLastError () == ERROR_FILE_NOT_FOUND
2333 ) {
2334
2335
2336 dwCreationDistribution = CREATE_ALWAYS;
2337
d9ff84e8 2338 retVal = CreateFileW (
2339 (const wchar_t*) fNameW.ToExtString(), dwDesiredAccess,
7fd59977 2340 FILE_SHARE_READ | FILE_SHARE_WRITE,
2341 NULL, dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL
2342 );
2343
2344 *fNew = TRUE;
2345
2346 } // end if
2347
2348 return retVal;
2349
2350} // end _open_file
2351
2352Standard_Integer __fastcall _get_file_type (
6ff736d8 2353 Standard_CString fName, HANDLE fileHandle
7fd59977 2354 ) {
2355
302f96fb 2356 Standard_Integer retVal = 0;
7fd59977 2357 DWORD dwType;
2358 int fileType;
2359
6ff736d8 2360 fileType = (fileHandle == INVALID_HANDLE_VALUE ?
2361 FILE_TYPE_DISK : GetFileType (fileHandle));
7fd59977 2362
2363 switch ( fileType ) {
2364
2365 case FILE_TYPE_UNKNOWN:
2366
2367 retVal = FLAG_SOCKET;
2368
2369 break;
2370
2371 case FILE_TYPE_DISK:
d9ff84e8 2372 {
2373 // make wide character string from UTF-8
2374 TCollection_ExtendedString fNameW(fName, Standard_True);
2375 dwType = GetFileAttributesW ( (const wchar_t*) fNameW.ToExtString() );
2376 if ( dwType != 0xFFFFFFFF )
7fd59977 2377
2378 retVal = dwType & FILE_ATTRIBUTE_DIRECTORY ? FLAG_DIRECTORY : FLAG_FILE;
2379
2380 else
2381
2382 retVal = 0x80000000;
d9ff84e8 2383 }
7fd59977 2384 break;
2385
2386 case FILE_TYPE_CHAR:
2387
2388 retVal = FLAG_DEVICE;
2389
2390 break;
2391
2392 case FILE_TYPE_PIPE:
2393
2394 retVal = FLAG_PIPE;
2395
2396 break;
2397
2398 } // end switch
2399
2400 return retVal;
2401
2402} // end _get_file_type
2403
2404#if defined(__CYGWIN32__) || defined(__MINGW32__)
2405#define __try
2406#define __finally
2407#define __leave return retVal
2408#endif
2409
2410BOOL __fastcall _osd_wnt_sd_to_protection (
2411 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL fDir
2412 ) {
2413
2414 int i;
2415 BOOL fPresent;
2416 BOOL fDefaulted;
2417 PACL pACL;
2418 PSID pSIDowner;
2419 PSID pSIDadmin;
2420 PSID pSIDworld;
2421 LPVOID pACE;
2422 DWORD dwAccessOwner = 0;
2423 DWORD dwAccessGroup = 0;
2424 DWORD dwAccessAdmin = 0;
2425 DWORD dwAccessWorld = 0;
2426 BOOL retVal = FALSE;
2427 GET_PROT_FUNC _get_prot_func = fDir ? &_get_protection_dir : &_get_protection;
2428
2429 __try {
2430
2431 if ( !GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) ) __leave;
2432
2433 if ( !GetSecurityDescriptorDacl ( pSD, &fPresent, &pACL, &fDefaulted ) ||
2434 !fPresent
2435 ) __leave;
2436
2437 if ( pSIDowner == NULL || pACL == NULL ) {
2438
2439 SetLastError ( ERROR_NO_SECURITY_ON_OBJECT );
2440 __leave;
2441
2442 } // end if
2443
2444 pSIDadmin = AdminSid ();
2445 pSIDworld = WorldSid ();
2446
2447 for ( i = 0; i < ( int )pACL -> AceCount; ++i ) {
2448
2449 if ( GetAce ( pACL, i, &pACE ) ) {
2450
2451 if ( EqualSid ( pSIDowner, GET_SID( pACE ) ) )
2452
2453 dwAccessOwner = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2454 0 : *GET_MSK( pACE );
2455
2456 else if ( EqualSid ( pSIDadmin, GET_SID( pACE ) ) )
2457
2458 dwAccessAdmin = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2459 0 : *GET_MSK( pACE );
2460
2461 else if ( EqualSid ( pSIDworld, GET_SID( pACE ) ) )
2462
2463 dwAccessWorld = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2464 0 : *GET_MSK( pACE );
2465
2466 else
2467
2468 dwAccessGroup = ( ( PACE_HEADER )pACE ) -> AceType == ACCESS_DENIED_ACE_TYPE ?
2469 0 : *GET_MSK( pACE );
2470
2471 } // end if
2472
2473 } // end for
2474
2475 prot.SetValues (
2476 ( *_get_prot_func ) ( dwAccessAdmin ),
2477 ( *_get_prot_func ) ( dwAccessOwner ),
2478 ( *_get_prot_func ) ( dwAccessGroup ),
2479 ( *_get_prot_func ) ( dwAccessWorld )
2480 );
2481
2482 retVal = TRUE;
2483
2484 } // end __try
2485
2486 __finally {}
2487
2488#ifdef VAC
2489leave: ; // added for VisualAge
2490#endif
2491
2492 return retVal;
2493
2494} // end _osd_wnt_sd_to_protection
2495
2496#if defined(__CYGWIN32__) || defined(__MINGW32__)
2497#undef __try
2498#undef __finally
2499#undef __leave
2500#endif
2501
2502static OSD_SingleProtection __fastcall _get_protection ( DWORD mask ) {
2503
2504 OSD_SingleProtection retVal;
2505
2506 switch ( mask ) {
2507
2508 case FILE_GENERIC_READ:
2509
2510 retVal = OSD_R;
2511
2512 break;
2513
2514 case FILE_GENERIC_WRITE:
2515
2516 retVal = OSD_W;
2517
2518 break;
2519
2520 case FILE_GENERIC_READ | FILE_GENERIC_WRITE:
2521
2522 retVal = OSD_RW;
2523
2524 break;
2525
2526 case FILE_GENERIC_EXECUTE:
2527
2528 retVal = OSD_X;
2529
2530 break;
2531
2532 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE:
2533
2534 retVal = OSD_RX;
2535
2536 break;
2537
2538 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2539
2540 retVal = OSD_WX;
2541
2542 break;
2543
2544 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE:
2545
2546 retVal = OSD_RWX;
2547
2548 break;
2549
2550 case DELETE:
2551
2552 retVal = OSD_D;
2553
2554 break;
2555
2556 case FILE_GENERIC_READ | DELETE:
2557
2558 retVal = OSD_RD;
2559
2560 break;
2561
2562 case FILE_GENERIC_WRITE | DELETE:
2563
2564 retVal = OSD_WD;
2565
2566 break;
2567
2568 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE:
2569
2570 retVal = OSD_RWD;
2571
2572 break;
2573
2574 case FILE_GENERIC_EXECUTE | DELETE:
2575
2576 retVal = OSD_XD;
2577
2578 break;
2579
2580 case FILE_GENERIC_READ | FILE_GENERIC_EXECUTE | DELETE:
2581
2582 retVal = OSD_RXD;
2583
2584 break;
2585
2586 case FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2587
2588 retVal = OSD_WXD;
2589
2590 break;
2591
2592 case FILE_ALL_ACCESS:
2593 case FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE:
2594
2595 retVal = OSD_RWXD;
2596
2597 break;
2598
2599 case 0:
2600 default:
2601
2602 retVal = OSD_None;
2603
2604 } // end switch
2605
2606 return retVal;
2607
2608} // end _get_protection
2609
2610static OSD_SingleProtection __fastcall _get_protection_dir ( DWORD mask ) {
2611
2612 OSD_SingleProtection retVal;
2613
2614 switch ( mask ) {
2615
2616 case GENERIC_READ:
2617
2618 retVal = OSD_R;
2619
2620 break;
2621
2622 case GENERIC_WRITE:
2623
2624 retVal = OSD_W;
2625
2626 break;
2627
2628 case GENERIC_READ | GENERIC_WRITE:
2629
2630 retVal = OSD_RW;
2631
2632 break;
2633
2634 case GENERIC_EXECUTE:
2635
2636 retVal = OSD_X;
2637
2638 break;
2639
2640 case GENERIC_READ | GENERIC_EXECUTE:
2641
2642 retVal = OSD_RX;
2643
2644 break;
2645
2646 case GENERIC_WRITE | GENERIC_EXECUTE:
2647
2648 retVal = OSD_WX;
2649
2650 break;
2651
2652 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE:
2653
2654 retVal = OSD_RWX;
2655
2656 break;
2657
2658 case DELETE:
2659
2660 retVal = OSD_D;
2661
2662 break;
2663
2664 case GENERIC_READ | DELETE:
2665
2666 retVal = OSD_RD;
2667
2668 break;
2669
2670 case GENERIC_WRITE | DELETE:
2671
2672 retVal = OSD_WD;
2673
2674 break;
2675
2676 case GENERIC_READ | GENERIC_WRITE | DELETE:
2677
2678 retVal = OSD_RWD;
2679
2680 break;
2681
2682 case GENERIC_EXECUTE | DELETE:
2683
2684 retVal = OSD_XD;
2685
2686 break;
2687
2688 case GENERIC_READ | GENERIC_EXECUTE | DELETE:
2689
2690 retVal = OSD_RXD;
2691
2692 break;
2693
2694 case GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2695
2696 retVal = OSD_WXD;
2697
2698 break;
2699
2700 case FILE_ALL_ACCESS:
2701 case GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE:
2702
2703 retVal = OSD_RWXD;
2704
2705 break;
2706
2707 case 0:
2708 default:
2709
2710 retVal = OSD_None;
2711
2712 } // end switch
2713
2714 return retVal;
2715
2716} // end _get_protection_dir
2717
2718#if defined(__CYGWIN32__) || defined(__MINGW32__)
2719#define __try
2720#define __finally
2721#define __leave return fOK
2722#endif
2723
d9ff84e8 2724BOOL __fastcall _osd_print (const Standard_PCharacter pName, const wchar_t* fName ) {
7fd59977 2725
2726 BOOL fOK, fJob;
302f96fb 2727 HANDLE hPrinter = NULL;
7fd59977 2728 BYTE jobInfo[ MAX_PATH + sizeof ( DWORD ) ];
302f96fb 2729 DWORD dwNeeded, dwCode = 0;
7fd59977 2730
2731 fOK = fJob = FALSE;
2732
2733 __try {
2734
2735 if ( !OpenPrinter ( Standard_PCharacter(pName), &hPrinter, NULL ) ) {
2736
2737 hPrinter = NULL;
2738 __leave;
2739
2740 } // end if
2741
d9ff84e8 2742 if ( !AddJobW (
7fd59977 2743 hPrinter, 1, jobInfo, MAX_PATH + sizeof ( DWORD ), &dwNeeded
2744 )
2745 ) __leave;
2746
2747 fJob = TRUE;
2748
d9ff84e8 2749 if ( !CopyFileW (
2750 fName, (LPWSTR) ( ( ADDJOB_INFO_1* )jobInfo ) -> Path, FALSE
7fd59977 2751 )
2752 ) __leave;
2753
2754 if ( !ScheduleJob (
2755 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId
2756 )
2757 ) __leave;
2758
2759 fOK = TRUE;
2760
2761 } // end __try
2762
2763 __finally {
2764
2765 if ( !fOK ) {
2766
2767 BYTE info[ 1024 ];
2768 DWORD dwBytesNeeded;
2769
2770 dwCode = GetLastError ();
2771
2772 if ( fJob && hPrinter != NULL ) {
2773
2774 GetJob (
2775 hPrinter, ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId, 1,
2776 info, 1024, &dwBytesNeeded
2777 );
2778
2779 if ( fJob ) SetJob (
2780 hPrinter,
2781 ( ( ADDJOB_INFO_1* )jobInfo ) -> JobId,
2782 1, info, JOB_CONTROL_CANCEL
2783 );
2784
2785 } // end if
2786
2787 } // end if
2788
2789 if ( hPrinter != NULL ) ClosePrinter ( hPrinter );
2790
2791 } // end __finally
2792
2793#ifdef VAC
2794leave: ; // added for VisualAge
2795#endif
2796
2797 if ( !fOK ) SetLastError ( dwCode );
2798
2799 return fOK;
2800
2801} // end _osd_print
2802
2803#if defined(__CYGWIN32__) || defined(__MINGW32__)
2804#undef __try
2805#undef __finally
2806#undef __leave
2807#endif
2808
2809Standard_Boolean OSD_File::IsReadable()
2810{
2811 TCollection_AsciiString FileName ;
2812 HANDLE Channel ;
2813
2814 myPath.SystemName(FileName) ;
2815 Channel = _open_file(FileName.ToCString(), OSD_ReadOnly, OPEN_OLD) ;
2816 if (Channel == INVALID_HANDLE_VALUE)
2817 return Standard_False ;
2818 else {
2819 CloseHandle (Channel) ;
2820 return Standard_True ;
2821 }
2822}
2823
2824
2825Standard_Boolean OSD_File::IsWriteable()
2826{
2827 TCollection_AsciiString FileName ;
2828 HANDLE Channel ;
2829
2830 myPath.SystemName(FileName) ;
2831 Channel = _open_file(FileName.ToCString(), OSD_ReadWrite, OPEN_OLD) ;
2832 if (Channel == INVALID_HANDLE_VALUE)
2833 return Standard_False ;
2834 else {
2835 CloseHandle (Channel) ;
2836 return Standard_True ;
2837 }
2838}
2839
2840Standard_Boolean OSD_File::IsExecutable()
2841{
2842 return IsReadable() ;
2843
2844// if (_access(FileName.ToCString(),0))
2845}
2846
6ff736d8 2847#endif /* _WIN32 */
7fd59977 2848
4485f3d0 2849// ---------------------------------------------------------------------
2850// Destructs a file object (unlocks and closes file if it is open)
2851// ---------------------------------------------------------------------
2852
2853OSD_File::~OSD_File()
2854{
2855 if (IsOpen())
2856 {
2857 if (IsLocked())
2858 UnLock();
2859 Close();
2860 }
2861}
7fd59977 2862
2863// ---------------------------------------------------------------------
2864// Read lines in a file while it is increasing.
2865// Each line is terminated with a <nl>.
2866// ---------------------------------------------------------------------
2867
2868#include <OSD.hxx>
2869
2870Standard_Boolean OSD_File::ReadLastLine(TCollection_AsciiString& aLine,const Standard_Integer aDelay,const Standard_Integer aNbTries)
2871{
2872 static Standard_Integer MaxLength = 1000 ;
2873 Standard_Integer Len ;
2874 Standard_Integer Count = aNbTries ;
2875
2876 if (Count <= 0)
2877 return Standard_False ;
302f96fb 2878 for(;;) {
7fd59977 2879 ReadLine(aLine, MaxLength, Len) ;
2880 if (!aLine.IsEmpty())
2881 return Standard_True ;
2882 if (!--Count)
2883 return Standard_False ;
2884 OSD::SecSleep(aDelay) ;
2885 }
2886}
2887
2888
2889Standard_Boolean OSD_File::Edit()
2890{
2891 cout << "Function OSD_File::Edit() not yet implemented." << endl;
2892 return Standard_False ;
2893}
2894
2895
2896