summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_FileNode.cxx
blob: 74128d0d62205437d90029ebb2aba758d74f1f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#ifndef WNT

#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <Standard_ProgramError.hxx>
#include <OSD_OSDError.hxx>
#include <Standard_NullObject.hxx>
#include <OSD_WhoAmI.hxx>
#include <OSD_FileNode.ixx>

#if!defined(TM_IN_SYS_TIME) && defined(HAVE_TIME_H)
# include <time.h>
#elif defined(HAVE_SYS_TIME_H)
# include <sys/time.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>  // for "stat"
#endif

#include <stdlib.h>    // For "system"
#include <errno.h>
#include <fcntl.h>

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <stdio.h>

#ifdef HAVE_OSFCN_H
# include <osfcn.h>
#endif


const OSD_WhoAmI Iam = OSD_WFileNode;


// Create a file/directory object

OSD_FileNode::OSD_FileNode (){
 myFileChannel = -1;
}



// Create and initialize a file/directory object

OSD_FileNode::OSD_FileNode (const OSD_Path& Name){
 SetPath (Name);
}



// Get values of object

void OSD_FileNode::Path (OSD_Path& Name)const{
 Name = myPath;
}




// Set values of object

void OSD_FileNode::SetPath (const OSD_Path& Name){
 myError.Reset();
 myPath = Name;
}




// Test if specified file/directory exists
 
Standard_Boolean  OSD_FileNode::Exists(){
int status;


// if (myPath.Name().Length()==0)  A directory can have a null name field (ex: root)
//  OSD_OSDError::Raise("OSD_FileNode::Exists : no name was given"); (LD)

// if (Failed()) Perror();

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 status = access ( aBuffer.ToCString() , F_OK );
 
 if (status == 0) return (Standard_True);
   else return ( Standard_False );
}




// Physically remove a file/directory

void  OSD_FileNode::Remove(){

// if (myPath.Name().Length()==0) A directory can have a null name field (ex: root)
//  OSD_OSDError::Raise("OSD_FileNode::Remove : no name was given"); (LD)

// if (Failed()) Perror();

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );

 if(access(aBuffer.ToCString(), W_OK))
   {
     myError.SetValue (errno, Iam, "Remove");
     return;
   }

 struct stat  stat_buf;

 if(stat(aBuffer.ToCString(), &stat_buf))
   {
     myError.SetValue (errno, Iam, "Remove");
     return;
   }
  
 if (  S_ISDIR(stat_buf.st_mode))  {
   // DIRECTORY

   if(rmdir(aBuffer.ToCString()))
     {
       myError.SetValue (errno, Iam, "Remove");
       return;
     }
   return; 

 }
 else if  (  S_ISREG(stat_buf.st_mode) || S_ISLNK(stat_buf.st_mode) ||
             S_ISFIFO(stat_buf.st_mode)   )  { 
   
   if (unlink ( aBuffer.ToCString()) == -1) 
     myError.SetValue (errno, Iam, "Remove");
   return;
 }
 myError.SetValue (EINVAL, Iam, "Remove");
 return;
}




// Move a file/directory to another path

void  OSD_FileNode::Move(const OSD_Path& NewPath){
int status;
TCollection_AsciiString thisPath;

// if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::Move : no name was given");

// if (Failed()) Perror();

 NewPath.SystemName( thisPath );        // Get internal path name
 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 status = rename (aBuffer.ToCString(), thisPath.ToCString());

 if (status == -1) myError.SetValue (errno, Iam, "Move");
}





// Copy a file to another path and name


#ifndef WNT

int static copy_file( const char* src, const char* trg )
{
  int err=0;
  errno=0;
  int fds = open( src, O_RDONLY );
  if ( fds <0 )
    return errno;

  int fdo = open( trg, O_WRONLY|O_TRUNC| O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  if ( fdo <0 )
  {
    err = errno;
    close( fds );
    return err;
  }

  const int BUFSIZE=4096;
  char buf[BUFSIZE];
  int n=0;
  while ( ( n = read ( fds, buf, BUFSIZE )) >0 )
  {
    if ( write ( fdo, buf, n ) != n ) { // writing error
      if ( ! errno )
        errno = ENOSPC;
      break;
    }
  }

  err=errno;
  close( fdo );
  if (!err) err=errno;
  close( fds );
  if (!err) err=errno;
  return err;
}

#endif




void  OSD_FileNode::Copy(const OSD_Path& ToPath)
{
int status;
TCollection_AsciiString second_name;

// if (myPath.Name().Length()==0)   Copy .login would raise !!
//  OSD_OSDError::Raise("OSD_FileNode::Copy : no name was given");
// if (Failed()) Perror();

 ToPath.SystemName (second_name);

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 status =  copy_file(aBuffer.ToCString(), second_name.ToCString());
 if (status != 0) myError.SetValue (-1, Iam, "Copy failed") ;// (LD)
#ifdef DEBUG
 printf("Status %d : errno # %d\n",status,errno);
#endif
}





// Get protections of a file/directory

OSD_Protection  OSD_FileNode::Protection(){
OSD_Protection thisProt;
struct stat myStat;
int status;
int s,u,g,w;

// if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::Protection : no name was given");

// if (Failed()) Perror();

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 status = stat(aBuffer.ToCString(), &myStat);
 if (status == -1) myError.SetValue (errno, Iam, "Protection");

 u = g = w = OSD_None;

 if (myStat.st_mode & S_IRUSR)  u |= OSD_R;
 if (myStat.st_mode & S_IWUSR)  u |= OSD_W;
 if (myStat.st_mode & S_IXUSR)  u |= OSD_X;

 if (myStat.st_mode & S_IRGRP)  g |= OSD_R;
 if (myStat.st_mode & S_IWGRP)  g |= OSD_W;
 if (myStat.st_mode & S_IXGRP)  g |= OSD_X;

 if (myStat.st_mode & S_IROTH)  w |= OSD_R;
 if (myStat.st_mode & S_IWOTH)  w |= OSD_W;
 if (myStat.st_mode & S_IXOTH)  w |= OSD_X;

 s = g;
 thisProt.SetValues ((OSD_SingleProtection)s,
                     (OSD_SingleProtection)u,
                     (OSD_SingleProtection)g,
                     (OSD_SingleProtection)w);

 return (thisProt);
}


// Set protections of a file/directory

void  OSD_FileNode::SetProtection(const OSD_Protection& Prot){
int status;

//  if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::SetProtection : no name was given");

// if (Failed()) Perror();

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 status = chmod (aBuffer.ToCString(), (mode_t)Prot.Internal() );
 if (status == -1) myError.SetValue (errno, Iam, "SetProtection");
}



// Returns User Id

Standard_Integer OSD_FileNode::UserId(){
struct stat buffer;

// if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::UserId : no name was given");

// if (Failed()) Perror();

 /* Get File Informations */

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 stat ( aBuffer.ToCString(), &buffer );

 return ( buffer.st_uid );
}


// Returns Group Id

Standard_Integer OSD_FileNode::GroupId(){
struct stat buffer;

// if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::GroupId : no name was given");

// if (Failed()) Perror();

 /* Get File Informations */

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 stat ( aBuffer.ToCString(), &buffer );

 return ( buffer.st_gid );
}




// return the date of last access of file/directory

Quantity_Date  OSD_FileNode::CreationMoment(){

 Quantity_Date result;
 struct tm *decode;
 struct stat buffer;

// if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::CreationMoment : no name was given");

// if (Failed()) Perror();

 /* Get File Informations */

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 if (!stat ( aBuffer.ToCString(), &buffer )) {
   decode = localtime(&buffer.st_ctime);
   result.SetValues ( 
		     decode->tm_mon+1, decode->tm_mday, decode->tm_year+1900,
		     decode->tm_hour, decode->tm_min, decode->tm_sec , 0,0);
 }
 else
   result.SetValues (1, 1, 1979, 0, 0, 0, 0, 0) ;
 return (result);
}

// return Last access of file/directory

Quantity_Date  OSD_FileNode::AccessMoment(){

 Quantity_Date result;
 struct tm *decode;
 struct stat buffer;

// if (myPath.Name().Length()==0)
//  OSD_OSDError::Raise("OSD_FileNode::AccessMoment : no name was given");

// if (Failed()) Perror();

 /* Get File Informations */

 TCollection_AsciiString aBuffer;
 myPath.SystemName ( aBuffer );
 if (!stat ( aBuffer.ToCString(), &buffer )) {
   decode = localtime(&buffer.st_atime);
   result.SetValues (
		     decode->tm_mon+1, decode->tm_mday, decode->tm_year+1900,
		     decode->tm_hour, decode->tm_min, decode->tm_sec, 0,0 );
 }
 else
   result.SetValues (1, 1, 1979, 0, 0, 0, 0, 0) ;
 return (result);
}


void OSD_FileNode::Reset(){
 myError.Reset();
}

Standard_Boolean OSD_FileNode::Failed()const{
 return( myError.Failed());
}

void OSD_FileNode::Perror() {
 myError.Perror();
}


Standard_Integer OSD_FileNode::Error()const{
 return( myError.Error());
}

#else

//----------------------------------------------------------------------------
//-------------------  WNT Sources of OSD_FileNode ---------------------------
//----------------------------------------------------------------------------

#define STRICT
#include <OSD_FileNode.hxx>
#include <OSD_Protection.hxx>
#include <Quantity_Date.hxx>
#include <Standard_ProgramError.hxx>

#include <OSD_WNT_1.hxx>

#ifndef _INC_TCHAR
# include <tchar.h>
#endif  // _INC_TCHAR

#include <stdio.h>

#define TEST_RAISE( arg ) _test_raise (  fName, ( arg )  )
#define RAISE( arg ) Standard_ProgramError :: Raise (  ( arg )  )

PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, char* = NULL );
BOOL                 __fastcall _osd_wnt_sd_to_protection (
                                 PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL
                                );
Standard_Integer     __fastcall _get_file_type ( Standard_CString, Standard_Integer );

void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );

static BOOL __fastcall _get_file_time ( Standard_CString, LPSYSTEMTIME, BOOL );
static void __fastcall _test_raise ( TCollection_AsciiString, Standard_CString );

//=======================================================================
//function : OSD_FileNode
//purpose  : Empty Constructor
//=======================================================================

OSD_FileNode::OSD_FileNode () {

 myFileChannel = ( Standard_Integer )INVALID_HANDLE_VALUE;

}  // end constructor ( 1 )

//=======================================================================
//function : OSD_FileNode
//purpose  : Constructor
//=======================================================================

OSD_FileNode::OSD_FileNode ( const OSD_Path& Name ) {

 myFileChannel = ( Standard_Integer )INVALID_HANDLE_VALUE;
 myPath        = Name;

}  // end constructor ( 2 )

//=======================================================================
//function : Path
//purpose  : 
//=======================================================================

void OSD_FileNode::Path ( OSD_Path& Name ) const {

 Name = myPath;

}  // end OSD_FileNode :: Path

//=======================================================================
//function : SetPath
//purpose  : 
//=======================================================================

void OSD_FileNode::SetPath ( const OSD_Path& Name ) {

 myPath = Name;

}  // end OSD_FileNode :: SetPath

//=======================================================================
//function : Exists
//purpose  : 
//=======================================================================

Standard_Boolean OSD_FileNode::Exists () {

 Standard_Boolean        retVal = Standard_False;;
 TCollection_AsciiString fName;

 myPath.SystemName ( fName );

 if (  fName.IsEmpty ()  ) return Standard_False;
 TEST_RAISE(  TEXT( "Exists" )  );

 if (  GetFileAttributes (  fName.ToCString ()  ) == 0xFFFFFFFF  ) {
 
  if (  GetLastError () != ERROR_FILE_NOT_FOUND  )

   _osd_wnt_set_error (  myError, OSD_WFileNode, fName.ToCString ()  );
 
 } else

  retVal = Standard_True;

 return retVal;

}  // end OSD_FileNode :: Exists

//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

void OSD_FileNode::Remove () {

 TCollection_AsciiString fName;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "Remove" )  );

 switch (  _get_file_type (  fName.ToCString (),
                             ( Standard_Integer )INVALID_HANDLE_VALUE )  ) {

  case FLAG_FILE:

   if (   !DeleteFile (  fName.ToCString ()  )   )

    _osd_wnt_set_error (  myError, OSD_WFileNode,  fName.ToCString ()  );

  break;

  case FLAG_DIRECTORY:


// LD : Suppression de l'appel a DeleteDirectory pour 
//      ne pas detruire un repertoire no vide.

   if (   !RemoveDirectory (  fName.ToCString ()  )   )

    _osd_wnt_set_error (  myError, OSD_WFileNode, fName.ToCString ()  );

  break;

  default:

   RAISE(  TEXT( "OSD_FileNode :: Remove ():"
                 " invalid file type - neither file nor directory" )  );

 }  // end switch

}  // end OSD_FileNode :: Remove

//=======================================================================
//function : Move
//purpose  : 
//=======================================================================

void OSD_FileNode::Move ( const OSD_Path& NewPath ) {

 TCollection_AsciiString fName;
 TCollection_AsciiString fNameDst;

 myPath.SystemName  ( fName );

 TEST_RAISE(  TEXT( "Move" )  );

 NewPath.SystemName ( fNameDst );

 switch (  _get_file_type ( fName.ToCString (),
                            ( Standard_Integer )INVALID_HANDLE_VALUE )  ) {

  case FLAG_FILE:

   if (!MoveFileEx (fName.ToCString (), fNameDst.ToCString (),
                    MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED
                    )
       )
 
     _osd_wnt_set_error ( myError, OSD_WFileNode,
                          fName.ToCString (), fNameDst.ToCString ()  );
  break;

  case FLAG_DIRECTORY:

   if (   !MoveDirectory (
            fName.ToCString (), fNameDst.ToCString ()
           )
   )
 
    _osd_wnt_set_error ( myError, OSD_WFileNode,
                         fName.ToCString (), fNameDst.ToCString ()  );

  break;

  default:

   RAISE(  TEXT( "OSD_FileNode :: Move (): "
                 "invalid file type - neither file nor directory" )  );

 }  // end switch

}  // end OSD_FileNode :: Move

//=======================================================================
//function : Copy
//purpose  : 
//=======================================================================

void OSD_FileNode::Copy ( const OSD_Path& ToPath ) {

 TCollection_AsciiString fName;
 TCollection_AsciiString fNameDst;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "Copy" )  );

 ToPath.SystemName ( fNameDst );

 switch (  _get_file_type ( fName.ToCString (),
                            ( Standard_Integer )INVALID_HANDLE_VALUE )  ) {

  case FLAG_FILE:

   if (!CopyFile (fName.ToCString (), fNameDst.ToCString (), FALSE ))
     _osd_wnt_set_error (myError, OSD_WFileNode,
                         fName.ToCString (), fNameDst.ToCString ());
  break;

  case FLAG_DIRECTORY:

   if (   !CopyDirectory (
            fName.ToCString (), fNameDst.ToCString ()
          )
   )
 
    _osd_wnt_set_error (
     myError, OSD_WFileNode, fName.ToCString (), fNameDst.ToCString ()
    );

  break;

  default:

   RAISE(  TEXT( "OSD_FileNode :: Copy ():"
                 " invalid file type - neither file nor directory" )  );

 }  // end switch

}  // end OSD_FileNode :: Copy

//=======================================================================
//function : Protection
//purpose  : 
//=======================================================================

OSD_Protection OSD_FileNode::Protection () {

 OSD_Protection          retVal;
 TCollection_AsciiString fName;
 PSECURITY_DESCRIPTOR    pSD;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "Protection" )  );

 if (   (  pSD = GetFileSecurityEx (
                  fName.ToCString (), DACL_SECURITY_INFORMATION |
                  OWNER_SECURITY_INFORMATION
                 )
        ) == NULL ||
        !_osd_wnt_sd_to_protection (
          pSD, retVal,
          _get_file_type (  fName.ToCString (), ( Standard_Integer )INVALID_HANDLE_VALUE  ) ==
          FLAG_DIRECTORY
         )
 )
   
   _osd_wnt_set_error ( myError, OSD_WFileNode );

 if ( pSD != NULL )

  FreeFileSecurity ( pSD );

 return retVal;

}  // end OSD_FileNode :: Protection

//=======================================================================
//function : SetProtection
//purpose  : 
//=======================================================================

void OSD_FileNode::SetProtection ( const OSD_Protection& Prot ) {

 TCollection_AsciiString fName;
 PSECURITY_DESCRIPTOR    pSD;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "SetProtection" )  );

 pSD = _osd_wnt_protection_to_sd (
        Prot,
        _get_file_type ( fName.ToCString (),
                         ( Standard_Integer )INVALID_HANDLE_VALUE  ) ==
        FLAG_DIRECTORY,
        (char *)fName.ToCString ()
       );
 
 if ( pSD == NULL || !SetFileSecurity (
                       fName.ToCString (), DACL_SECURITY_INFORMATION, pSD
                      )
 )
  
  _osd_wnt_set_error (  myError, OSD_WFileNode, fName.ToCString ()  );

 if ( pSD != NULL )

  FreeSD ( pSD );

}  // end OSD_FileNode :: SetProtection

//=======================================================================
//function : AccessMoment
//purpose  : 
//=======================================================================

Quantity_Date OSD_FileNode::AccessMoment () {

 Quantity_Date           retVal;
 SYSTEMTIME              stAccessMoment;
 SYSTEMTIME              stAccessSystemMoment;
 TCollection_AsciiString fName;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "AccessMoment" )  );

// if (   _get_file_time (  fName.ToCString (), &stAccessMoment, TRUE  )   )
 if (   _get_file_time (  fName.ToCString (), &stAccessSystemMoment, TRUE  ) )
//POP
{
  SYSTEMTIME * aSysTime = &stAccessMoment;
  BOOL aFlag = SystemTimeToTzSpecificLocalTime (NULL ,
                                                &stAccessSystemMoment ,
                                                &stAccessMoment);
  if (aFlag == 0) // AGV: test for success (e.g., unsupported on Win95/98)
    aSysTime = &stAccessSystemMoment;
  retVal.SetValues (aSysTime->wMonth,       aSysTime->wDay,
                    aSysTime->wYear,        aSysTime->wHour,
                    aSysTime->wMinute,      aSysTime->wSecond,
                    aSysTime->wMilliseconds
                    );
}
 else

  _osd_wnt_set_error (  myError, OSD_WFileNode, fName.ToCString ()  );

 return retVal;

}  // end OSD_FileNode :: AccessMoment

//=======================================================================
//function : CreationMoment
//purpose  : 
//=======================================================================

Quantity_Date OSD_FileNode::CreationMoment () {

 Quantity_Date           retVal;
 SYSTEMTIME              stCreationMoment;
 SYSTEMTIME              stCreationSystemMoment;
 TCollection_AsciiString fName;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "CreationMoment" )  );

// if (   _get_file_time (  fName.ToCString (), &stCreationMoment, FALSE  )   )
 if ( _get_file_time ( fName.ToCString (), &stCreationSystemMoment, TRUE  )   ) 
//POP 
{
  SYSTEMTIME * aSysTime = &stCreationMoment;
  BOOL aFlag = SystemTimeToTzSpecificLocalTime (NULL,
                                                &stCreationSystemMoment ,
                                                &stCreationMoment);
  if (aFlag == 0) // AGV: test for success (e.g., unsupported on Win95/98)
    aSysTime = &stCreationSystemMoment;
  retVal.SetValues (aSysTime->wMonth,       aSysTime->wDay,
                    aSysTime->wYear,        aSysTime->wHour,
                    aSysTime->wMinute,      aSysTime->wSecond,
                    aSysTime->wMilliseconds
                    );
}
 else

  _osd_wnt_set_error (  myError, OSD_WFileNode, fName.ToCString ()  );

 return retVal;

}  // end OSD_FileNode :: CreationMoment

//=======================================================================
//function : UserId
//purpose  : 
//=======================================================================

Standard_Integer OSD_FileNode::UserId () {

 PSID                    pSIDowner = NULL;
 PSID                    retVal = NULL;
 BOOL                    fDefaulted;
 TCollection_AsciiString fName;
 PSECURITY_DESCRIPTOR    pSD;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "UserId" )  );

 if (   (  pSD = GetFileSecurityEx (
                  fName.ToCString (),
                  OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION
                 )
        ) != NULL                                                   &&
        GetSecurityDescriptorOwner ( pSD, &pSIDowner, &fDefaulted ) &&
        pSIDowner != NULL
 )
 
  retVal = CopySidEx ( pSIDowner );

 else

  _osd_wnt_set_error ( myError, OSD_WFileNode );

 if ( pSD != NULL )

  FreeFileSecurity ( pSD );
 
 return ( Standard_Integer )retVal;

}  // end OSD_FileNode :: UserId

//=======================================================================
//function : GroupId
//purpose  : 
//=======================================================================

Standard_Integer OSD_FileNode::GroupId () {

 PGROUP_SID              retVal = NULL;
 TCollection_AsciiString fName;
 PSECURITY_DESCRIPTOR    pSD;

 myPath.SystemName ( fName );

 TEST_RAISE(  TEXT( "GroupId" )  );

 if (   (  pSD = GetFileSecurityEx (
                  fName.ToCString (),
                  OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION
                 )
        ) != NULL
 ) {

  retVal = AllocGroupSid ( pSD );
  FreeFileSecurity ( pSD );

 } else

  _osd_wnt_set_error ( myError, OSD_WFileNode );

 return ( Standard_Integer )retVal;

}  // end OSD_FileNode :: GroupId

//=======================================================================
//function : Failed
//purpose  : 
//=======================================================================

Standard_Boolean OSD_FileNode::Failed () const {

 return myError.Failed ();

}  // end OSD_FileNode :: Failed

//=======================================================================
//function : Reset
//purpose  : 
//=======================================================================

void OSD_FileNode::Reset () {

 myError.Reset ();

}  // end OSD_FileNode :: Reset

//=======================================================================
//function : Perror
//purpose  : 
//=======================================================================

void OSD_FileNode::Perror () {

 myError.Perror ();

}  // end OSD_FileNode :: Perror

//=======================================================================
//function : Error
//purpose  : 
//=======================================================================

Standard_Integer OSD_FileNode::Error () const {

 return myError.Error ();

}  // end OSD_FileNode :: Error

void _osd_wnt_set_error ( OSD_Error& err, OSD_WhoAmI who, ... ) {

 DWORD              errCode;
 Standard_Character buffer[ 2048 ];
 va_list            arg_ptr;

 va_start ( arg_ptr, err );

 errCode = GetLastError ();

 if (  !FormatMessage (
         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
         0, errCode, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
         buffer, 2048, &arg_ptr
        )
 ) {
 
  sprintf ( buffer, "error code %d", (Standard_Integer)errCode );
  SetLastError ( errCode );

 }  // end if

 err.SetValue ( errCode, who, buffer );

 va_end ( arg_ptr );

}  // end _set_error


static BOOL __fastcall _get_file_time (
                        Standard_CString fName, LPSYSTEMTIME lpSysTime, BOOL fAccess
                       ) {

 BOOL       retVal = FALSE;
 FILETIME   ftCreationTime;
 FILETIME   ftLastWriteTime;
 LPFILETIME lpftPtr;
 HANDLE     hFile = INVALID_HANDLE_VALUE;
 bool       Ret = true;
 
  if (   (  hFile = CreateFile (
                     fName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
                    )
         ) == INVALID_HANDLE_VALUE
  ) Ret = false;

  if (Ret)
  if (  !GetFileTime ( hFile, &ftCreationTime, NULL, &ftLastWriteTime )  ) Ret = false;

  if (Ret)
  {
  lpftPtr = fAccess ? &ftLastWriteTime : &ftCreationTime;

  if (  !FileTimeToSystemTime ( lpftPtr, lpSysTime )  ) Ret = false;
  }

  if(Ret)
  retVal = TRUE;

  if ( hFile != INVALID_HANDLE_VALUE )
   CloseHandle ( hFile );

 return retVal;

}  // end _get_file_time


static void __fastcall _test_raise ( TCollection_AsciiString fName, Standard_CString str ) {

 Standard_Character buff[ 64 ];

 if (  fName.IsEmpty ()  ) {
 
  _tcscpy (  buff, TEXT( "OSD_FileNode :: " )  );
  _tcscat (  buff, str );
  _tcscat (  buff, TEXT( " (): wrong access" )  );

  Standard_ProgramError :: Raise ( buff );
 
 }  // end if

}  // end _test_raise

#endif