0020716: Eliminate usage of "config.h" header file
[occt.git] / src / OSD / OSD_FileNode.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
6ff736d8 15#ifndef _WIN32
16
17//----------------------------------------------------------------------------
18//------------------- Linux Sources of OSD_FileNode --------------------------
19//----------------------------------------------------------------------------
7fd59977 20
7fd59977 21#include <errno.h>
7fd59977 22
23#include <Standard_ProgramError.hxx>
24#include <OSD_OSDError.hxx>
25#include <Standard_NullObject.hxx>
26#include <OSD_WhoAmI.hxx>
27#include <OSD_FileNode.ixx>
28
7fd59977 29#include <stdlib.h> // For "system"
30#include <errno.h>
31#include <fcntl.h>
32
7fd59977 33#include <stdio.h>
03155c18 34#include <unistd.h>
35#include <sys/stat.h>
7fd59977 36
37const OSD_WhoAmI Iam = OSD_WFileNode;
38
39
40// Create a file/directory object
41
6ff736d8 42OSD_FileNode::OSD_FileNode ()
43{
7fd59977 44}
45
7fd59977 46// Create and initialize a file/directory object
47
6ff736d8 48OSD_FileNode::OSD_FileNode (const OSD_Path& Name)
49{
7fd59977 50 SetPath (Name);
51}
52
53
54
55// Get values of object
56
57void OSD_FileNode::Path (OSD_Path& Name)const{
58 Name = myPath;
59}
60
61
62
63
64// Set values of object
65
66void OSD_FileNode::SetPath (const OSD_Path& Name){
67 myError.Reset();
68 myPath = Name;
69}
70
71
72
73
74// Test if specified file/directory exists
75
76Standard_Boolean OSD_FileNode::Exists(){
77int status;
78
79
80// if (myPath.Name().Length()==0) A directory can have a null name field (ex: root)
81// OSD_OSDError::Raise("OSD_FileNode::Exists : no name was given"); (LD)
82
83// if (Failed()) Perror();
84
85 TCollection_AsciiString aBuffer;
86 myPath.SystemName ( aBuffer );
87 status = access ( aBuffer.ToCString() , F_OK );
88
89 if (status == 0) return (Standard_True);
90 else return ( Standard_False );
91}
92
93
94
95
96// Physically remove a file/directory
97
98void OSD_FileNode::Remove(){
99
100// if (myPath.Name().Length()==0) A directory can have a null name field (ex: root)
101// OSD_OSDError::Raise("OSD_FileNode::Remove : no name was given"); (LD)
102
103// if (Failed()) Perror();
104
105 TCollection_AsciiString aBuffer;
106 myPath.SystemName ( aBuffer );
107
108 if(access(aBuffer.ToCString(), W_OK))
109 {
110 myError.SetValue (errno, Iam, "Remove");
111 return;
112 }
113
114 struct stat stat_buf;
115
116 if(stat(aBuffer.ToCString(), &stat_buf))
117 {
118 myError.SetValue (errno, Iam, "Remove");
119 return;
120 }
121
122 if ( S_ISDIR(stat_buf.st_mode)) {
123 // DIRECTORY
124
125 if(rmdir(aBuffer.ToCString()))
126 {
127 myError.SetValue (errno, Iam, "Remove");
128 return;
129 }
130 return;
131
132 }
133 else if ( S_ISREG(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode) ||
134 S_ISFIFO(stat_buf.st_mode) ) {
135
136 if (unlink ( aBuffer.ToCString()) == -1)
137 myError.SetValue (errno, Iam, "Remove");
138 return;
139 }
140 myError.SetValue (EINVAL, Iam, "Remove");
141 return;
142}
143
144
145
146
147// Move a file/directory to another path
148
149void OSD_FileNode::Move(const OSD_Path& NewPath){
150int status;
151TCollection_AsciiString thisPath;
152
153// if (myPath.Name().Length()==0)
154// OSD_OSDError::Raise("OSD_FileNode::Move : no name was given");
155
156// if (Failed()) Perror();
157
158 NewPath.SystemName( thisPath ); // Get internal path name
159 TCollection_AsciiString aBuffer;
160 myPath.SystemName ( aBuffer );
161 status = rename (aBuffer.ToCString(), thisPath.ToCString());
162
163 if (status == -1) myError.SetValue (errno, Iam, "Move");
164}
165
7fd59977 166// Copy a file to another path and name
7fd59977 167int static copy_file( const char* src, const char* trg )
168{
169 int err=0;
170 errno=0;
171 int fds = open( src, O_RDONLY );
172 if ( fds <0 )
173 return errno;
174
175 int fdo = open( trg, O_WRONLY|O_TRUNC| O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
176 if ( fdo <0 )
177 {
178 err = errno;
179 close( fds );
180 return err;
181 }
182
183 const int BUFSIZE=4096;
184 char buf[BUFSIZE];
185 int n=0;
186 while ( ( n = read ( fds, buf, BUFSIZE )) >0 )
187 {
188 if ( write ( fdo, buf, n ) != n ) { // writing error
189 if ( ! errno )
190 errno = ENOSPC;
191 break;
192 }
193 }
194
195 err=errno;
196 close( fdo );
197 if (!err) err=errno;
198 close( fds );
199 if (!err) err=errno;
200 return err;
201}
202
7fd59977 203void OSD_FileNode::Copy(const OSD_Path& ToPath)
204{
205int status;
206TCollection_AsciiString second_name;
207
208// if (myPath.Name().Length()==0) Copy .login would raise !!
209// OSD_OSDError::Raise("OSD_FileNode::Copy : no name was given");
210// if (Failed()) Perror();
211
212 ToPath.SystemName (second_name);
213
214 TCollection_AsciiString aBuffer;
215 myPath.SystemName ( aBuffer );
216 status = copy_file(aBuffer.ToCString(), second_name.ToCString());
217 if (status != 0) myError.SetValue (-1, Iam, "Copy failed") ;// (LD)
218#ifdef DEBUG
219 printf("Status %d : errno # %d\n",status,errno);
220#endif
221}
222
223
224
225
226
227// Get protections of a file/directory
228
229OSD_Protection OSD_FileNode::Protection(){
230OSD_Protection thisProt;
231struct stat myStat;
232int status;
233int s,u,g,w;
234
235// if (myPath.Name().Length()==0)
236// OSD_OSDError::Raise("OSD_FileNode::Protection : no name was given");
237
238// if (Failed()) Perror();
239
240 TCollection_AsciiString aBuffer;
241 myPath.SystemName ( aBuffer );
242 status = stat(aBuffer.ToCString(), &myStat);
243 if (status == -1) myError.SetValue (errno, Iam, "Protection");
244
245 u = g = w = OSD_None;
246
247 if (myStat.st_mode & S_IRUSR) u |= OSD_R;
248 if (myStat.st_mode & S_IWUSR) u |= OSD_W;
249 if (myStat.st_mode & S_IXUSR) u |= OSD_X;
250
251 if (myStat.st_mode & S_IRGRP) g |= OSD_R;
252 if (myStat.st_mode & S_IWGRP) g |= OSD_W;
253 if (myStat.st_mode & S_IXGRP) g |= OSD_X;
254
255 if (myStat.st_mode & S_IROTH) w |= OSD_R;
256 if (myStat.st_mode & S_IWOTH) w |= OSD_W;
257 if (myStat.st_mode & S_IXOTH) w |= OSD_X;
258
259 s = g;
260 thisProt.SetValues ((OSD_SingleProtection)s,
261 (OSD_SingleProtection)u,
262 (OSD_SingleProtection)g,
263 (OSD_SingleProtection)w);
264
265 return (thisProt);
266}
267
268
269// Set protections of a file/directory
270
271void OSD_FileNode::SetProtection(const OSD_Protection& Prot){
272int status;
273
274// if (myPath.Name().Length()==0)
275// OSD_OSDError::Raise("OSD_FileNode::SetProtection : no name was given");
276
277// if (Failed()) Perror();
278
279 TCollection_AsciiString aBuffer;
280 myPath.SystemName ( aBuffer );
281 status = chmod (aBuffer.ToCString(), (mode_t)Prot.Internal() );
282 if (status == -1) myError.SetValue (errno, Iam, "SetProtection");
283}
284
285
286
287// Returns User Id
288
289Standard_Integer OSD_FileNode::UserId(){
290struct stat buffer;
291
292// if (myPath.Name().Length()==0)
293// OSD_OSDError::Raise("OSD_FileNode::UserId : no name was given");
294
295// if (Failed()) Perror();
296
297 /* Get File Informations */
298
299 TCollection_AsciiString aBuffer;
300 myPath.SystemName ( aBuffer );
301 stat ( aBuffer.ToCString(), &buffer );
302
303 return ( buffer.st_uid );
304}
305
306
307// Returns Group Id
308
309Standard_Integer OSD_FileNode::GroupId(){
310struct stat buffer;
311
312// if (myPath.Name().Length()==0)
313// OSD_OSDError::Raise("OSD_FileNode::GroupId : no name was given");
314
315// if (Failed()) Perror();
316
317 /* Get File Informations */
318
319 TCollection_AsciiString aBuffer;
320 myPath.SystemName ( aBuffer );
321 stat ( aBuffer.ToCString(), &buffer );
322
323 return ( buffer.st_gid );
324}
325
326
327
328
329// return the date of last access of file/directory
330
331Quantity_Date OSD_FileNode::CreationMoment(){
332
333 Quantity_Date result;
334 struct tm *decode;
335 struct stat buffer;
336
337// if (myPath.Name().Length()==0)
338// OSD_OSDError::Raise("OSD_FileNode::CreationMoment : no name was given");
339
340// if (Failed()) Perror();
341
342 /* Get File Informations */
343
344 TCollection_AsciiString aBuffer;
345 myPath.SystemName ( aBuffer );
346 if (!stat ( aBuffer.ToCString(), &buffer )) {
0304f711 347 time_t aTime = (time_t)buffer.st_ctime;
348 decode = localtime (&aTime);
349 result.SetValues (decode->tm_mon+1, decode->tm_mday, decode->tm_year+1900,
7fd59977 350 decode->tm_hour, decode->tm_min, decode->tm_sec , 0,0);
351 }
352 else
353 result.SetValues (1, 1, 1979, 0, 0, 0, 0, 0) ;
354 return (result);
355}
356
357// return Last access of file/directory
358
359Quantity_Date OSD_FileNode::AccessMoment(){
360
361 Quantity_Date result;
362 struct tm *decode;
363 struct stat buffer;
364
365// if (myPath.Name().Length()==0)
366// OSD_OSDError::Raise("OSD_FileNode::AccessMoment : no name was given");
367
368// if (Failed()) Perror();
369
370 /* Get File Informations */
371
372 TCollection_AsciiString aBuffer;
373 myPath.SystemName ( aBuffer );
374 if (!stat ( aBuffer.ToCString(), &buffer )) {
0304f711 375 time_t aTime = (time_t)buffer.st_ctime;
376 decode = localtime (&aTime);
377 result.SetValues (decode->tm_mon+1, decode->tm_mday, decode->tm_year+1900,
7fd59977 378 decode->tm_hour, decode->tm_min, decode->tm_sec, 0,0 );
379 }
380 else
381 result.SetValues (1, 1, 1979, 0, 0, 0, 0, 0) ;
382 return (result);
383}
384
385
386void OSD_FileNode::Reset(){
387 myError.Reset();
388}
389
390Standard_Boolean OSD_FileNode::Failed()const{
391 return( myError.Failed());
392}
393
394void OSD_FileNode::Perror() {
395 myError.Perror();
396}
397
398
399Standard_Integer OSD_FileNode::Error()const{
400 return( myError.Error());
401}
402
6ff736d8 403#else /* _WIN32 */
7fd59977 404
405//----------------------------------------------------------------------------
406//------------------- WNT Sources of OSD_FileNode ---------------------------
407//----------------------------------------------------------------------------
408
409#define STRICT
410#include <OSD_FileNode.hxx>
411#include <OSD_Protection.hxx>
412#include <Quantity_Date.hxx>
413#include <Standard_ProgramError.hxx>
414
415#include <OSD_WNT_1.hxx>
416
417#ifndef _INC_TCHAR
418# include <tchar.h>
419#endif // _INC_TCHAR
420
421#define TEST_RAISE( arg ) _test_raise ( fName, ( arg ) )
422#define RAISE( arg ) Standard_ProgramError :: Raise ( ( arg ) )
423
424PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, char* = NULL );
425BOOL __fastcall _osd_wnt_sd_to_protection (
426 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL
427 );
6ff736d8 428Standard_Integer __fastcall _get_file_type ( Standard_CString, HANDLE );
7fd59977 429
430void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
431
432static BOOL __fastcall _get_file_time ( Standard_CString, LPSYSTEMTIME, BOOL );
433static void __fastcall _test_raise ( TCollection_AsciiString, Standard_CString );
434
435//=======================================================================
436//function : OSD_FileNode
437//purpose : Empty Constructor
438//=======================================================================
439
6ff736d8 440OSD_FileNode::OSD_FileNode ()
441{
442}
7fd59977 443
444//=======================================================================
445//function : OSD_FileNode
446//purpose : Constructor
447//=======================================================================
448
6ff736d8 449OSD_FileNode::OSD_FileNode ( const OSD_Path& Name )
450{
7fd59977 451 myPath = Name;
7fd59977 452} // end constructor ( 2 )
453
454//=======================================================================
455//function : Path
456//purpose :
457//=======================================================================
458
459void OSD_FileNode::Path ( OSD_Path& Name ) const {
460
461 Name = myPath;
462
463} // end OSD_FileNode :: Path
464
465//=======================================================================
466//function : SetPath
467//purpose :
468//=======================================================================
469
470void OSD_FileNode::SetPath ( const OSD_Path& Name ) {
471
472 myPath = Name;
473
474} // end OSD_FileNode :: SetPath
475
476//=======================================================================
477//function : Exists
478//purpose :
479//=======================================================================
480
481Standard_Boolean OSD_FileNode::Exists () {
482
483 Standard_Boolean retVal = Standard_False;;
484 TCollection_AsciiString fName;
485
486 myPath.SystemName ( fName );
487
488 if ( fName.IsEmpty () ) return Standard_False;
489 TEST_RAISE( TEXT( "Exists" ) );
490
491 if ( GetFileAttributes ( fName.ToCString () ) == 0xFFFFFFFF ) {
492
493 if ( GetLastError () != ERROR_FILE_NOT_FOUND )
494
495 _osd_wnt_set_error ( myError, OSD_WFileNode, fName.ToCString () );
496
497 } else
498
499 retVal = Standard_True;
500
501 return retVal;
502
503} // end OSD_FileNode :: Exists
504
505//=======================================================================
506//function : Remove
507//purpose :
508//=======================================================================
509
510void OSD_FileNode::Remove () {
511
512 TCollection_AsciiString fName;
513
514 myPath.SystemName ( fName );
515
516 TEST_RAISE( TEXT( "Remove" ) );
517
6ff736d8 518 switch (_get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE)) {
7fd59977 519
520 case FLAG_FILE:
521
522 if ( !DeleteFile ( fName.ToCString () ) )
523
524 _osd_wnt_set_error ( myError, OSD_WFileNode, fName.ToCString () );
525
526 break;
527
528 case FLAG_DIRECTORY:
529
530
531// LD : Suppression de l'appel a DeleteDirectory pour
532// ne pas detruire un repertoire no vide.
533
534 if ( !RemoveDirectory ( fName.ToCString () ) )
535
536 _osd_wnt_set_error ( myError, OSD_WFileNode, fName.ToCString () );
537
538 break;
539
540 default:
541
542 RAISE( TEXT( "OSD_FileNode :: Remove ():"
543 " invalid file type - neither file nor directory" ) );
544
545 } // end switch
546
547} // end OSD_FileNode :: Remove
548
549//=======================================================================
550//function : Move
551//purpose :
552//=======================================================================
553
554void OSD_FileNode::Move ( const OSD_Path& NewPath ) {
555
556 TCollection_AsciiString fName;
557 TCollection_AsciiString fNameDst;
558
559 myPath.SystemName ( fName );
560
561 TEST_RAISE( TEXT( "Move" ) );
562
563 NewPath.SystemName ( fNameDst );
564
6ff736d8 565 switch (_get_file_type (fName.ToCString (), INVALID_HANDLE_VALUE)) {
7fd59977 566
567 case FLAG_FILE:
568
569 if (!MoveFileEx (fName.ToCString (), fNameDst.ToCString (),
570 MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED
571 )
572 )
573
574 _osd_wnt_set_error ( myError, OSD_WFileNode,
575 fName.ToCString (), fNameDst.ToCString () );
576 break;
577
578 case FLAG_DIRECTORY:
579
580 if ( !MoveDirectory (
581 fName.ToCString (), fNameDst.ToCString ()
582 )
583 )
584
585 _osd_wnt_set_error ( myError, OSD_WFileNode,
586 fName.ToCString (), fNameDst.ToCString () );
587
588 break;
589
590 default:
591
592 RAISE( TEXT( "OSD_FileNode :: Move (): "
593 "invalid file type - neither file nor directory" ) );
594
595 } // end switch
596
597} // end OSD_FileNode :: Move
598
599//=======================================================================
600//function : Copy
601//purpose :
602//=======================================================================
603
604void OSD_FileNode::Copy ( const OSD_Path& ToPath ) {
605
606 TCollection_AsciiString fName;
607 TCollection_AsciiString fNameDst;
608
609 myPath.SystemName ( fName );
610
611 TEST_RAISE( TEXT( "Copy" ) );
612
613 ToPath.SystemName ( fNameDst );
614
6ff736d8 615 switch (_get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE)) {
7fd59977 616
617 case FLAG_FILE:
618
619 if (!CopyFile (fName.ToCString (), fNameDst.ToCString (), FALSE ))
620 _osd_wnt_set_error (myError, OSD_WFileNode,
621 fName.ToCString (), fNameDst.ToCString ());
622 break;
623
624 case FLAG_DIRECTORY:
625
626 if ( !CopyDirectory (
627 fName.ToCString (), fNameDst.ToCString ()
628 )
629 )
630
631 _osd_wnt_set_error (
632 myError, OSD_WFileNode, fName.ToCString (), fNameDst.ToCString ()
633 );
634
635 break;
636
637 default:
638
639 RAISE( TEXT( "OSD_FileNode :: Copy ():"
640 " invalid file type - neither file nor directory" ) );
641
642 } // end switch
643
644} // end OSD_FileNode :: Copy
645
646//=======================================================================
647//function : Protection
648//purpose :
649//=======================================================================
650
651OSD_Protection OSD_FileNode::Protection () {
652
653 OSD_Protection retVal;
654 TCollection_AsciiString fName;
655 PSECURITY_DESCRIPTOR pSD;
656
657 myPath.SystemName ( fName );
658
659 TEST_RAISE( TEXT( "Protection" ) );
660
661 if ( ( pSD = GetFileSecurityEx (
662 fName.ToCString (), DACL_SECURITY_INFORMATION |
663 OWNER_SECURITY_INFORMATION
664 )
665 ) == NULL ||
666 !_osd_wnt_sd_to_protection (
667 pSD, retVal,
6ff736d8 668 _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE) == FLAG_DIRECTORY)
7fd59977 669 )
670
671 _osd_wnt_set_error ( myError, OSD_WFileNode );
672
673 if ( pSD != NULL )
674
675 FreeFileSecurity ( pSD );
676
677 return retVal;
678
679} // end OSD_FileNode :: Protection
680
681//=======================================================================
682//function : SetProtection
683//purpose :
684//=======================================================================
685
686void OSD_FileNode::SetProtection ( const OSD_Protection& Prot ) {
687
688 TCollection_AsciiString fName;
689 PSECURITY_DESCRIPTOR pSD;
690
691 myPath.SystemName ( fName );
692
693 TEST_RAISE( TEXT( "SetProtection" ) );
694
695 pSD = _osd_wnt_protection_to_sd (
696 Prot,
6ff736d8 697 _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE) ==
7fd59977 698 FLAG_DIRECTORY,
699 (char *)fName.ToCString ()
700 );
701
702 if ( pSD == NULL || !SetFileSecurity (
703 fName.ToCString (), DACL_SECURITY_INFORMATION, pSD
704 )
705 )
706
707 _osd_wnt_set_error ( myError, OSD_WFileNode, fName.ToCString () );
708
709 if ( pSD != NULL )
710
711 FreeSD ( pSD );
712
713} // end OSD_FileNode :: SetProtection
714
715//=======================================================================
716//function : AccessMoment
717//purpose :
718//=======================================================================
719
720Quantity_Date OSD_FileNode::AccessMoment () {
721
722 Quantity_Date retVal;
723 SYSTEMTIME stAccessMoment;
724 SYSTEMTIME stAccessSystemMoment;
725 TCollection_AsciiString fName;
726
727 myPath.SystemName ( fName );
728
729 TEST_RAISE( TEXT( "AccessMoment" ) );
730
731// if ( _get_file_time ( fName.ToCString (), &stAccessMoment, TRUE ) )
732 if ( _get_file_time ( fName.ToCString (), &stAccessSystemMoment, TRUE ) )
733//POP
734{
735 SYSTEMTIME * aSysTime = &stAccessMoment;
736 BOOL aFlag = SystemTimeToTzSpecificLocalTime (NULL ,
737 &stAccessSystemMoment ,
738 &stAccessMoment);
739 if (aFlag == 0) // AGV: test for success (e.g., unsupported on Win95/98)
740 aSysTime = &stAccessSystemMoment;
741 retVal.SetValues (aSysTime->wMonth, aSysTime->wDay,
742 aSysTime->wYear, aSysTime->wHour,
743 aSysTime->wMinute, aSysTime->wSecond,
744 aSysTime->wMilliseconds
745 );
746}
747 else
748
749 _osd_wnt_set_error ( myError, OSD_WFileNode, fName.ToCString () );
750
751 return retVal;
752
753} // end OSD_FileNode :: AccessMoment
754
755//=======================================================================
756//function : CreationMoment
757//purpose :
758//=======================================================================
759
760Quantity_Date OSD_FileNode::CreationMoment () {
761
762 Quantity_Date retVal;
763 SYSTEMTIME stCreationMoment;
764 SYSTEMTIME stCreationSystemMoment;
765 TCollection_AsciiString fName;
766
767 myPath.SystemName ( fName );
768
769 TEST_RAISE( TEXT( "CreationMoment" ) );
770
771// if ( _get_file_time ( fName.ToCString (), &stCreationMoment, FALSE ) )
772 if ( _get_file_time ( fName.ToCString (), &stCreationSystemMoment, TRUE ) )
773//POP
774{
775 SYSTEMTIME * aSysTime = &stCreationMoment;
776 BOOL aFlag = SystemTimeToTzSpecificLocalTime (NULL,
777 &stCreationSystemMoment ,
778 &stCreationMoment);
779 if (aFlag == 0) // AGV: test for success (e.g., unsupported on Win95/98)
780 aSysTime = &stCreationSystemMoment;
781 retVal.SetValues (aSysTime->wMonth, aSysTime->wDay,
782 aSysTime->wYear, aSysTime->wHour,
783 aSysTime->wMinute, aSysTime->wSecond,
784 aSysTime->wMilliseconds
785 );
786}
787 else
788
789 _osd_wnt_set_error ( myError, OSD_WFileNode, fName.ToCString () );
790
791 return retVal;
792
793} // end OSD_FileNode :: CreationMoment
794
795//=======================================================================
796//function : UserId
797//purpose :
798//=======================================================================
799
800Standard_Integer OSD_FileNode::UserId () {
801
802 PSID pSIDowner = NULL;
302f96fb 803 PSID retVal = NULL;
7fd59977 804 BOOL fDefaulted;
805 TCollection_AsciiString fName;
806 PSECURITY_DESCRIPTOR pSD;
807
808 myPath.SystemName ( fName );
809
810 TEST_RAISE( TEXT( "UserId" ) );
811
812 if ( ( pSD = GetFileSecurityEx (
813 fName.ToCString (),
814 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION
815 )
816 ) != NULL &&
817 GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) &&
818 pSIDowner != NULL
819 )
820
821 retVal = CopySidEx ( pSIDowner );
822
823 else
824
825 _osd_wnt_set_error ( myError, OSD_WFileNode );
826
827 if ( pSD != NULL )
828
829 FreeFileSecurity ( pSD );
830
831 return ( Standard_Integer )retVal;
832
833} // end OSD_FileNode :: UserId
834
835//=======================================================================
836//function : GroupId
837//purpose :
838//=======================================================================
839
840Standard_Integer OSD_FileNode::GroupId () {
841
842 PGROUP_SID retVal = NULL;
843 TCollection_AsciiString fName;
844 PSECURITY_DESCRIPTOR pSD;
845
846 myPath.SystemName ( fName );
847
848 TEST_RAISE( TEXT( "GroupId" ) );
849
850 if ( ( pSD = GetFileSecurityEx (
851 fName.ToCString (),
852 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION
853 )
854 ) != NULL
855 ) {
856
857 retVal = AllocGroupSid ( pSD );
858 FreeFileSecurity ( pSD );
859
860 } else
861
862 _osd_wnt_set_error ( myError, OSD_WFileNode );
863
864 return ( Standard_Integer )retVal;
865
866} // end OSD_FileNode :: GroupId
867
868//=======================================================================
869//function : Failed
870//purpose :
871//=======================================================================
872
873Standard_Boolean OSD_FileNode::Failed () const {
874
875 return myError.Failed ();
876
877} // end OSD_FileNode :: Failed
878
879//=======================================================================
880//function : Reset
881//purpose :
882//=======================================================================
883
884void OSD_FileNode::Reset () {
885
886 myError.Reset ();
887
888} // end OSD_FileNode :: Reset
889
890//=======================================================================
891//function : Perror
892//purpose :
893//=======================================================================
894
895void OSD_FileNode::Perror () {
896
897 myError.Perror ();
898
899} // end OSD_FileNode :: Perror
900
901//=======================================================================
902//function : Error
903//purpose :
904//=======================================================================
905
906Standard_Integer OSD_FileNode::Error () const {
907
908 return myError.Error ();
909
910} // end OSD_FileNode :: Error
911
912void _osd_wnt_set_error ( OSD_Error& err, OSD_WhoAmI who, ... ) {
913
914 DWORD errCode;
915 Standard_Character buffer[ 2048 ];
916 va_list arg_ptr;
917
918 va_start ( arg_ptr, err );
919
920 errCode = GetLastError ();
921
922 if ( !FormatMessage (
923 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
924 0, errCode, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
925 buffer, 2048, &arg_ptr
926 )
927 ) {
928
929 sprintf ( buffer, "error code %d", (Standard_Integer)errCode );
930 SetLastError ( errCode );
931
932 } // end if
933
934 err.SetValue ( errCode, who, buffer );
935
936 va_end ( arg_ptr );
937
938} // end _set_error
939
940#if defined(__CYGWIN32__) || defined(__MINGW32__)
941#define __try
942#define __finally
943#define __leave return retVal
944#endif
945
946static BOOL __fastcall _get_file_time (
947 Standard_CString fName, LPSYSTEMTIME lpSysTime, BOOL fAccess
948 ) {
949
950 BOOL retVal = FALSE;
951 FILETIME ftCreationTime;
952 FILETIME ftLastWriteTime;
953 LPFILETIME lpftPtr;
302f96fb 954 HANDLE hFile = INVALID_HANDLE_VALUE;
7fd59977 955
956 __try {
957
958 if ( ( hFile = CreateFile (
959 fName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
960 )
961 ) == INVALID_HANDLE_VALUE
962 ) __leave;
963
964 if ( !GetFileTime ( hFile, &ftCreationTime, NULL, &ftLastWriteTime ) ) __leave;
965
966 lpftPtr = fAccess ? &ftLastWriteTime : &ftCreationTime;
967
968 if ( !FileTimeToSystemTime ( lpftPtr, lpSysTime ) ) __leave;
969
970 retVal = TRUE;
971
972 } // end __try
973
974 __finally {
975
976 if ( hFile != INVALID_HANDLE_VALUE )
977
978 CloseHandle ( hFile );
979
980 } // end __finally
981
982#ifdef VAC
983leave: ; // added for VisualAge
984#endif
985
986 return retVal;
987
988} // end _get_file_time
989
990#if defined(__CYGWIN32__) || defined(__MINGW32__)
991#undef __try
992#undef __finally
993#undef __leave
994#endif
995
996static void __fastcall _test_raise ( TCollection_AsciiString fName, Standard_CString str ) {
997
998 Standard_Character buff[ 64 ];
999
1000 if ( fName.IsEmpty () ) {
1001
1002 _tcscpy ( buff, TEXT( "OSD_FileNode :: " ) );
1003 _tcscat ( buff, str );
1004 _tcscat ( buff, TEXT( " (): wrong access" ) );
1005
1006 Standard_ProgramError :: Raise ( buff );
1007
1008 } // end if
1009
1010} // end _test_raise
1011
6ff736d8 1012#endif /* _WIN32 */