63716b30fc9e3db9bdeda39265363c47bb1f7703
[occt.git] / src / OSD / OSD_FileNode.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _WIN32
16
17 //----------------------------------------------------------------------------
18 //------------------- Linux Sources of OSD_FileNode --------------------------
19 //----------------------------------------------------------------------------
20
21 #include <OSD_FileNode.hxx>
22 #include <OSD_OSDError.hxx>
23 #include <OSD_Path.hxx>
24 #include <OSD_Protection.hxx>
25 #include <OSD_WhoAmI.hxx>
26 #include <Quantity_Date.hxx>
27 #include <Standard_NullObject.hxx>
28 #include <Standard_ProgramError.hxx>
29
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 // For "system"
37 const OSD_WhoAmI Iam = OSD_WFileNode;
38
39
40 // Create a file/directory object
41
42 OSD_FileNode::OSD_FileNode ()
43 {
44 }
45
46 // Create and initialize a file/directory object
47
48 OSD_FileNode::OSD_FileNode (const OSD_Path& Name)
49 {
50  SetPath (Name);
51 }
52
53
54
55 // Get values of object
56
57 void OSD_FileNode::Path (OSD_Path& Name)const{
58  Name = myPath;
59 }
60
61
62
63
64 // Set values of object
65
66 void 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  
76 Standard_Boolean  OSD_FileNode::Exists(){
77 int status;
78
79
80 // if (myPath.Name().Length()==0)  A directory can have a null name field (ex: root)
81 //  throw OSD_OSDError("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
98 void  OSD_FileNode::Remove(){
99
100 // if (myPath.Name().Length()==0) A directory can have a null name field (ex: root)
101 //  throw OSD_OSDError("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
149 void  OSD_FileNode::Move(const OSD_Path& NewPath){
150 int status;
151 TCollection_AsciiString thisPath;
152
153 // if (myPath.Name().Length()==0)
154 //  throw OSD_OSDError("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
166 // Copy a file to another path and name
167 int 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
203 void  OSD_FileNode::Copy(const OSD_Path& ToPath)
204 {
205 int status;
206 TCollection_AsciiString second_name;
207
208 // if (myPath.Name().Length()==0)   Copy .login would raise !!
209 //  throw OSD_OSDError("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 OCCT_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
229 OSD_Protection  OSD_FileNode::Protection(){
230 OSD_Protection thisProt;
231 struct stat myStat;
232 int status;
233 int s,u,g,w;
234
235 // if (myPath.Name().Length()==0)
236 //  throw OSD_OSDError("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
271 void  OSD_FileNode::SetProtection(const OSD_Protection& Prot){
272 int status;
273
274 //  if (myPath.Name().Length()==0)
275 //  throw OSD_OSDError("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 // return the date of last access of file/directory
286
287 Quantity_Date  OSD_FileNode::CreationMoment(){
288
289  Quantity_Date result;
290  struct tm *decode;
291  struct stat buffer;
292
293 // if (myPath.Name().Length()==0)
294 //  throw OSD_OSDError("OSD_FileNode::CreationMoment : no name was given");
295
296 // if (Failed()) Perror();
297
298  /* Get File Informations */
299
300  TCollection_AsciiString aBuffer;
301  myPath.SystemName ( aBuffer );
302  if (!stat ( aBuffer.ToCString(), &buffer )) {
303    time_t aTime = (time_t)buffer.st_ctime;
304    decode = localtime (&aTime);
305    result.SetValues (decode->tm_mon+1, decode->tm_mday, decode->tm_year+1900,
306                      decode->tm_hour, decode->tm_min, decode->tm_sec , 0,0);
307  }
308  else
309    result.SetValues (1, 1, 1979, 0, 0, 0, 0, 0) ;
310  return (result);
311 }
312
313 // return Last access of file/directory
314
315 Quantity_Date  OSD_FileNode::AccessMoment(){
316
317  Quantity_Date result;
318  struct tm *decode;
319  struct stat buffer;
320
321 // if (myPath.Name().Length()==0)
322 //  throw OSD_OSDError("OSD_FileNode::AccessMoment : no name was given");
323
324 // if (Failed()) Perror();
325
326  /* Get File Informations */
327
328  TCollection_AsciiString aBuffer;
329  myPath.SystemName ( aBuffer );
330  if (!stat ( aBuffer.ToCString(), &buffer )) {
331    time_t aTime = (time_t)buffer.st_ctime;
332    decode = localtime (&aTime);
333    result.SetValues (decode->tm_mon+1, decode->tm_mday, decode->tm_year+1900,
334                      decode->tm_hour, decode->tm_min, decode->tm_sec, 0,0 );
335  }
336  else
337    result.SetValues (1, 1, 1979, 0, 0, 0, 0, 0) ;
338  return (result);
339 }
340
341
342 void OSD_FileNode::Reset(){
343  myError.Reset();
344 }
345
346 Standard_Boolean OSD_FileNode::Failed()const{
347  return( myError.Failed());
348 }
349
350 void OSD_FileNode::Perror() {
351  myError.Perror();
352 }
353
354
355 Standard_Integer OSD_FileNode::Error()const{
356  return( myError.Error());
357 }
358
359 #else /* _WIN32 */
360
361 //----------------------------------------------------------------------------
362 //-------------------  WNT Sources of OSD_FileNode ---------------------------
363 //----------------------------------------------------------------------------
364
365 #ifdef NONLS
366 #undef NONLS
367 #endif
368 #include <windows.h>
369
370 #include <OSD_FileNode.hxx>
371 #include <OSD_Protection.hxx>
372 #include <Quantity_Date.hxx>
373 #include <Standard_ProgramError.hxx>
374 #include <TCollection_ExtendedString.hxx>
375
376 #include <OSD_WNT.hxx>
377
378 #ifndef _INC_TCHAR
379 # include <tchar.h>
380 #endif  // _INC_TCHAR
381
382 #include <strsafe.h>
383
384 #define TEST_RAISE( arg ) _test_raise (  fName, ( arg )  )
385 #define RAISE( arg ) throw Standard_ProgramError (  ( arg )  )
386
387 #ifndef OCCT_UWP
388 // None of the existing security APIs are supported in a UWP applications
389 PSECURITY_DESCRIPTOR __fastcall _osd_wnt_protection_to_sd ( const OSD_Protection&, BOOL, const wchar_t* );
390 BOOL                 __fastcall _osd_wnt_sd_to_protection (
391                                  PSECURITY_DESCRIPTOR pSD, OSD_Protection& prot, BOOL
392                                 );
393 #endif
394 Standard_Integer     __fastcall _get_file_type ( Standard_CString, HANDLE );
395
396 void _osd_wnt_set_error ( OSD_Error&, Standard_Integer, ... );
397
398 static BOOL __fastcall _get_file_time (const wchar_t*, LPSYSTEMTIME, BOOL );
399 static void __fastcall _test_raise ( TCollection_AsciiString, Standard_CString );
400
401 //=======================================================================
402 //function : OSD_FileNode
403 //purpose  : Empty Constructor
404 //=======================================================================
405
406 OSD_FileNode::OSD_FileNode () 
407 {
408 }
409
410 //=======================================================================
411 //function : OSD_FileNode
412 //purpose  : Constructor
413 //=======================================================================
414
415 OSD_FileNode::OSD_FileNode ( const OSD_Path& Name )
416 {
417  myPath        = Name;
418 }  // end constructor ( 2 )
419
420 //=======================================================================
421 //function : Path
422 //purpose  : 
423 //=======================================================================
424
425 void OSD_FileNode::Path ( OSD_Path& Name ) const {
426
427  Name = myPath;
428
429 }  // end OSD_FileNode :: Path
430
431 //=======================================================================
432 //function : SetPath
433 //purpose  : 
434 //=======================================================================
435
436 void OSD_FileNode::SetPath ( const OSD_Path& Name ) {
437
438  myPath = Name;
439
440 }  // end OSD_FileNode :: SetPath
441
442 //=======================================================================
443 //function : Exists
444 //purpose  : 
445 //=======================================================================
446
447 Standard_Boolean OSD_FileNode::Exists ()
448 {
449   myError.Reset();
450
451  Standard_Boolean        retVal = Standard_False;
452  TCollection_AsciiString fName;
453
454  myPath.SystemName ( fName );
455
456  if (  fName.IsEmpty ()  ) return Standard_False;
457  TEST_RAISE(  "Exists"  );
458
459  // make wide character string from UTF-8
460  TCollection_ExtendedString fNameW(fName);
461
462  WIN32_FILE_ATTRIBUTE_DATA aFileInfo;
463
464  if (!GetFileAttributesExW (fNameW.ToWideString(), GetFileExInfoStandard, &aFileInfo))
465  {
466   if (GetLastError() != ERROR_FILE_NOT_FOUND)
467   {
468     _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString());
469   }
470  }
471  else
472  {
473   retVal = Standard_True;
474  }
475
476  return retVal;
477
478 }  // end OSD_FileNode :: Exists
479
480 //=======================================================================
481 //function : Remove
482 //purpose  : 
483 //=======================================================================
484
485 void OSD_FileNode::Remove () {
486
487  TCollection_AsciiString fName;
488
489  myPath.SystemName ( fName );
490  TCollection_ExtendedString fNameW(fName);
491
492  TEST_RAISE(  "Remove"  );
493
494  switch (_get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE)) {
495
496   case FLAG_FILE:
497
498    if (!DeleteFileW (fNameW.ToWideString()))
499      _osd_wnt_set_error (  myError, OSD_WFileNode, fNameW.ToWideString());
500   break;
501
502   case FLAG_DIRECTORY:
503
504
505 // LD : Suppression de l'appel a DeleteDirectory pour 
506 //      ne pas detruire un repertoire no vide.
507
508    if (!RemoveDirectoryW (fNameW.ToWideString()))
509      _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString());
510   break;
511
512   default:
513    RAISE("OSD_FileNode :: Remove (): invalid file type - neither file nor directory");
514  }  // end switch
515
516 }  // end OSD_FileNode :: Remove
517
518 //=======================================================================
519 //function : Move
520 //purpose  : 
521 //=======================================================================
522
523 void OSD_FileNode::Move ( const OSD_Path& NewPath ) {
524
525  TCollection_AsciiString fName;
526  TCollection_AsciiString fNameDst;
527
528  myPath.SystemName  ( fName );
529  TCollection_ExtendedString fNameW(fName);
530
531  TEST_RAISE(  "Move"  );
532
533  NewPath.SystemName ( fNameDst );
534  TCollection_ExtendedString fNameDstW(fNameDst);
535
536  switch (_get_file_type (fName.ToCString (), INVALID_HANDLE_VALUE)) {
537
538   case FLAG_FILE:
539
540    if (!MoveFileExW (fNameW.ToWideString (),
541                      fNameDstW.ToWideString (),
542                      MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
543      _osd_wnt_set_error(myError, OSD_WFileNode, fNameW.ToWideString(), fNameDstW.ToWideString());
544   break;
545
546   case FLAG_DIRECTORY:
547
548    if (!MoveDirectory (fNameW.ToWideString(), fNameDstW.ToWideString()))
549    _osd_wnt_set_error(myError, OSD_WFileNode, fNameW.ToWideString(), fNameDstW.ToWideString());
550   break;
551
552   default:
553    RAISE("OSD_FileNode :: Move (): invalid file type - neither file nor directory");
554  }  // end switch
555
556 }  // end OSD_FileNode :: Move
557
558 //=======================================================================
559 //function : Copy
560 //purpose  : 
561 //=======================================================================
562
563 void OSD_FileNode::Copy ( const OSD_Path& ToPath ) {
564
565  TCollection_AsciiString fName;
566  TCollection_AsciiString fNameDst;
567
568  myPath.SystemName ( fName );
569  TCollection_ExtendedString fNameW(fName);
570
571  TEST_RAISE(  "Copy"  );
572
573  ToPath.SystemName ( fNameDst );
574  TCollection_ExtendedString fNameDstW(fNameDst);
575
576  switch (_get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE)) {
577
578   case FLAG_FILE:
579 #ifndef OCCT_UWP
580     if (!CopyFileW (fNameW.ToWideString(), fNameDstW.ToWideString(), FALSE))
581 #else
582    if (CopyFile2 (fNameW.ToWideString(), fNameDstW.ToWideString(), FALSE) != S_OK)
583 #endif
584    _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString(), fNameDstW.ToWideString());
585   break;
586
587   case FLAG_DIRECTORY:
588
589    if (!CopyDirectory (fNameW.ToWideString(), fNameDstW.ToWideString()))
590    _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString(), fNameDstW.ToWideString());
591
592   break;
593
594   default:
595    RAISE("OSD_FileNode :: Copy (): invalid file type - neither file nor directory");
596
597  }  // end switch
598
599 }  // end OSD_FileNode :: Copy
600
601 // None of the existing security APIs are supported in a UWP applications
602 #ifndef OCCT_UWP
603
604 //=======================================================================
605 //function : Protection
606 //purpose  : 
607 //=======================================================================
608
609 OSD_Protection OSD_FileNode::Protection () {
610
611  OSD_Protection          retVal;
612  TCollection_AsciiString fName;
613  PSECURITY_DESCRIPTOR    pSD;
614
615  myPath.SystemName ( fName );
616  TCollection_ExtendedString fNameW(fName);
617
618  TEST_RAISE(  "Protection"  );
619
620  if ((pSD = GetFileSecurityEx (fNameW.ToWideString(), DACL_SECURITY_INFORMATION |OWNER_SECURITY_INFORMATION)) == NULL
621   || !_osd_wnt_sd_to_protection (
622           pSD, retVal,
623           _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE) == FLAG_DIRECTORY)
624  )
625    
626    _osd_wnt_set_error ( myError, OSD_WFileNode );
627
628  if ( pSD != NULL )
629
630   FreeFileSecurity ( pSD );
631
632  return retVal;
633
634 }  // end OSD_FileNode :: Protection
635
636 //=======================================================================
637 //function : SetProtection
638 //purpose  : 
639 //=======================================================================
640
641 void OSD_FileNode::SetProtection ( const OSD_Protection& Prot ) {
642
643  TCollection_AsciiString fName;
644  PSECURITY_DESCRIPTOR    pSD;
645
646  myPath.SystemName ( fName );
647  TCollection_ExtendedString fNameW(fName);
648
649  TEST_RAISE(  "SetProtection"  );
650
651  pSD = _osd_wnt_protection_to_sd (Prot,
652         _get_file_type (fName.ToCString(), INVALID_HANDLE_VALUE) == FLAG_DIRECTORY,
653         fNameW.ToWideString());
654  
655  if (pSD == NULL || !SetFileSecurityW (fNameW.ToWideString(), DACL_SECURITY_INFORMATION, pSD))
656   _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString());
657
658  if ( pSD != NULL )
659
660   FreeSD ( pSD );
661
662 }  // end OSD_FileNode :: SetProtection
663
664 #else /* UWP stub */
665
666 #include <io.h>
667
668 //=======================================================================
669 //function : Protection
670 //purpose  : 
671 //=======================================================================
672
673 OSD_Protection OSD_FileNode::Protection ()
674 {
675  TCollection_AsciiString fName;
676
677  myPath.SystemName ( fName );
678  TCollection_ExtendedString fNameW(fName);
679
680  OSD_SingleProtection aProt = OSD_None;
681  if (_waccess_s (fNameW.ToWideString(), 6))
682    aProt = OSD_RW;
683  else if (_waccess_s (fNameW.ToWideString(), 2))
684    aProt = OSD_W;
685  else if (_waccess_s (fNameW.ToWideString(), 4))
686    aProt = OSD_R;
687
688  // assume full access for system and none for everybody
689  OSD_Protection retVal (OSD_RWXD, aProt, aProt, OSD_None);
690  return retVal;
691 }  // end OSD_FileNode :: Protection
692
693 //=======================================================================
694 //function : SetProtection
695 //purpose  : 
696 //=======================================================================
697
698 void OSD_FileNode::SetProtection ( const OSD_Protection& /*Prot*/ )
699 {
700 }  // end OSD_FileNode :: SetProtection
701
702 #endif
703
704 //=======================================================================
705 //function : AccessMoment
706 //purpose  : 
707 //=======================================================================
708
709 Quantity_Date OSD_FileNode::AccessMoment () {
710
711  Quantity_Date           retVal;
712  SYSTEMTIME              stAccessMoment;
713  SYSTEMTIME              stAccessSystemMoment;
714  TCollection_AsciiString fName;
715
716  myPath.SystemName ( fName );
717  TCollection_ExtendedString fNameW(fName);
718
719  TEST_RAISE(  "AccessMoment"  );
720
721  if (_get_file_time (fNameW.ToWideString(), &stAccessSystemMoment, TRUE))
722 {
723   SYSTEMTIME * aSysTime = &stAccessMoment;
724   BOOL aFlag = SystemTimeToTzSpecificLocalTime (NULL ,
725                                                 &stAccessSystemMoment ,
726                                                 &stAccessMoment);
727   if (aFlag == 0) // AGV: test for success (e.g., unsupported on Win95/98)
728     aSysTime = &stAccessSystemMoment;
729   retVal.SetValues (aSysTime->wMonth,       aSysTime->wDay,
730                     aSysTime->wYear,        aSysTime->wHour,
731                     aSysTime->wMinute,      aSysTime->wSecond,
732                     aSysTime->wMilliseconds
733                     );
734 }
735  else
736  {
737   _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString());
738  }
739
740  return retVal;
741
742 }  // end OSD_FileNode :: AccessMoment
743
744 //=======================================================================
745 //function : CreationMoment
746 //purpose  : 
747 //=======================================================================
748
749 Quantity_Date OSD_FileNode::CreationMoment () {
750
751  Quantity_Date           retVal;
752  SYSTEMTIME              stCreationMoment;
753  SYSTEMTIME              stCreationSystemMoment;
754  TCollection_AsciiString fName;
755
756  myPath.SystemName ( fName );
757  TCollection_ExtendedString fNameW(fName);
758
759  TEST_RAISE(  "CreationMoment"  );
760
761  if (_get_file_time (fNameW.ToWideString(), &stCreationSystemMoment, FALSE))
762 {
763   SYSTEMTIME * aSysTime = &stCreationMoment;
764   BOOL aFlag = SystemTimeToTzSpecificLocalTime (NULL,
765                                                 &stCreationSystemMoment ,
766                                                 &stCreationMoment);
767   if (aFlag == 0) // AGV: test for success (e.g., unsupported on Win95/98)
768     aSysTime = &stCreationSystemMoment;
769   retVal.SetValues (aSysTime->wMonth,       aSysTime->wDay,
770                     aSysTime->wYear,        aSysTime->wHour,
771                     aSysTime->wMinute,      aSysTime->wSecond,
772                     aSysTime->wMilliseconds
773                     );
774 }
775  else
776  {
777   _osd_wnt_set_error (myError, OSD_WFileNode, fNameW.ToWideString());
778  }
779
780  return retVal;
781
782 }  // end OSD_FileNode :: CreationMoment
783
784 //=======================================================================
785 //function : Failed
786 //purpose  : 
787 //=======================================================================
788
789 Standard_Boolean OSD_FileNode::Failed () const {
790
791  return myError.Failed ();
792
793 }  // end OSD_FileNode :: Failed
794
795 //=======================================================================
796 //function : Reset
797 //purpose  : 
798 //=======================================================================
799
800 void OSD_FileNode::Reset () {
801
802  myError.Reset ();
803
804 }  // end OSD_FileNode :: Reset
805
806 //=======================================================================
807 //function : Perror
808 //purpose  : 
809 //=======================================================================
810
811 void OSD_FileNode::Perror () {
812
813  myError.Perror ();
814
815 }  // end OSD_FileNode :: Perror
816
817 //=======================================================================
818 //function : Error
819 //purpose  : 
820 //=======================================================================
821
822 Standard_Integer OSD_FileNode::Error () const {
823
824  return myError.Error ();
825
826 }  // end OSD_FileNode :: Error
827
828 void _osd_wnt_set_error ( OSD_Error& err, Standard_Integer who, ... ) {
829
830  DWORD              errCode;
831
832  wchar_t buffer[2048];
833
834  va_list            arg_ptr;
835
836  va_start ( arg_ptr, who);
837
838  errCode = GetLastError ();
839
840  if (  !FormatMessageW (
841          FORMAT_MESSAGE_FROM_SYSTEM,
842          0, errCode, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
843          buffer, 2048, &arg_ptr
844         )
845  ) {
846   StringCchPrintfW(buffer, _countof(buffer), L"error code %d", (Standard_Integer)errCode);
847
848   SetLastError ( errCode );
849
850  }  // end if
851
852  char aBufferA[2048];
853  WideCharToMultiByte(CP_UTF8, 0, buffer, -1, aBufferA, sizeof(aBufferA), NULL, NULL);
854  err.SetValue(errCode, who, aBufferA);
855
856  va_end ( arg_ptr );
857
858 }  // end _set_error
859
860 #if defined(__CYGWIN32__) || defined(__MINGW32__)
861 #ifdef __try /* is defined on MinGw as either "try" or "if (true)" */
862 #undef __try
863 #endif
864 #define __try
865 #define __finally
866 #define __leave return retVal
867 #endif
868
869 static BOOL __fastcall _get_file_time (const wchar_t* fName, LPSYSTEMTIME lpSysTime, BOOL fAccess)
870 {
871  BOOL       retVal = FALSE;
872  FILETIME   ftCreationTime;
873  FILETIME   ftLastWriteTime;
874  LPFILETIME lpftPtr;
875  HANDLE     hFile = INVALID_HANDLE_VALUE;
876
877  __try {
878 #ifndef OCCT_UWP
879    if ((hFile = CreateFileW (fName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
880      ) == INVALID_HANDLE_VALUE
881      )
882 #else
883    CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams = {};
884    pCreateExParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
885    pCreateExParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
886    pCreateExParams.lpSecurityAttributes = NULL;
887    pCreateExParams.hTemplateFile = NULL;
888    if ((hFile = CreateFile2 (fName, NULL, NULL, OPEN_EXISTING, &pCreateExParams)) == INVALID_HANDLE_VALUE)
889 #endif
890     __leave;
891
892   if (  !GetFileTime ( hFile, &ftCreationTime, NULL, &ftLastWriteTime )  ) __leave;
893
894   lpftPtr = fAccess ? &ftLastWriteTime : &ftCreationTime;
895
896   if (  !FileTimeToSystemTime ( lpftPtr, lpSysTime )  ) __leave;
897
898   retVal = TRUE;
899
900  }  // end __try
901
902  __finally {
903  
904   if ( hFile != INVALID_HANDLE_VALUE )
905
906    CloseHandle ( hFile );
907  
908  }  // end __finally
909
910  return retVal;
911
912 }  // end _get_file_time
913
914 #if defined(__CYGWIN32__) || defined(__MINGW32__)
915 #undef __try
916 #undef __finally
917 #undef __leave
918 #endif
919
920 static void __fastcall _test_raise ( TCollection_AsciiString fName, Standard_CString str ) {
921  if (  fName.IsEmpty ()  ) {
922    TCollection_AsciiString buff = "OSD_FileNode :: ";
923    buff += str;
924    buff += " (): wrong access";
925
926    throw Standard_ProgramError(buff.ToCString());
927  }  // end if
928
929 }  // end _test_raise
930
931 #endif /* _WIN32 */