0027350: Support for Universal Windows Platform
[occt.git] / src / OSD / OSD_Disk.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 #include <OSD_Disk.hxx>
19 #include <OSD_OSDError.hxx>
20 #include <OSD_Path.hxx>
21 #include <OSD_WhoAmI.hxx>
22
23 const OSD_WhoAmI Iam = OSD_WDisk;
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #if defined(__ANDROID__)
30   #include <sys/vfs.h>
31   #define statvfs  statfs
32   #define fstatvfs fstatfs
33 #else
34   #include <sys/statvfs.h>
35 #endif
36
37 #ifdef __cplusplus
38 }
39 #endif
40
41 #include <errno.h>
42
43 OSD_Disk::OSD_Disk() : myQuotaSize(0) {}
44
45
46 OSD_Disk::OSD_Disk(const OSD_Path& name){
47  DiskName = name.Disk();
48  myQuotaSize = 0;
49 }
50
51 OSD_Disk::OSD_Disk(const Standard_CString name)
52 {
53  DiskName = name;
54  myQuotaSize = 0;
55 }
56
57 void OSD_Disk::SetName(const OSD_Path& name){
58  DiskName = name.Disk();
59 }
60
61
62 OSD_Path OSD_Disk::Name()const{
63  OSD_Path result;
64  result.SetDisk(DiskName);
65  return(result);
66 }
67
68 Standard_Integer OSD_Disk::DiskSize(){
69
70 struct statvfs buffer;
71
72   if ( statvfs(DiskName.ToCString(),&buffer) == 0 ){
73     int BSize512 = buffer.f_frsize / 512 ;
74     return buffer.f_blocks * BSize512 ;
75   }
76   else {
77     myError.SetValue(errno, Iam, "OSD_Disk: statvfs failed.");
78     return 0;
79   }
80 }
81
82 Standard_Integer OSD_Disk::DiskFree(){
83
84 struct statvfs buffer;
85   if ( statvfs (DiskName.ToCString(),&buffer) == 0 ){
86     int BSize512 = buffer.f_frsize / 512 ;
87     return buffer.f_bavail * BSize512 ;
88   }
89   else {
90     myError.SetValue(errno, Iam, "OSD_Disk: statvfs failed.");
91     return 0;
92   }
93 }
94
95 Standard_Integer OSD_Disk::DiskQuota(){
96 //@@@ A faire
97 return 0;
98 }
99
100
101 void OSD_Disk::SetDiskQuota(const Standard_Integer ){
102 // int status;
103 // struct dqblk quota_info;
104 #ifdef ULTRIX
105 // status = quota(Q_SETDLIM,<< User Id>>,&quota_info);
106 #else
107 // status = quotactl(Q_SETQLIM,"",<< User Id >>,&quota_info);
108 #endif
109 //@@@ A terminer
110 }
111
112
113 void OSD_Disk::SetQuotaOff(){
114 //int status;
115 #ifdef ULTRIX
116 // status = setquota("","");
117 #else
118 // status = quotactl(Q_QUOTAOFF,"",0,NULL);
119 #endif
120 //@@@ A faire
121 }
122
123 void OSD_Disk::SetQuotaOn(){
124 //TCollection_AsciiString quota_file="????";
125 //int status;
126 #ifdef ULTRIX
127 // status = setquota("",quota_file);
128 #else
129 // status = quotactl(Q_QUOTAON,"",0,quota_file);
130 #endif
131 //@@@ A faire
132 }
133
134
135 void OSD_Disk::Reset(){
136  myError.Reset();
137 }
138
139 Standard_Boolean OSD_Disk::Failed()const{
140  return( myError.Failed());
141 }
142
143 void OSD_Disk::Perror() {
144  myError.Perror();
145 }
146
147
148 Standard_Integer OSD_Disk::Error()const{
149  return( myError.Error());
150 }
151
152 #else
153
154 //-------------------------------------------------------------------------------
155 //---------------------------- Windows NT System --------------------------------
156 //-------------------------------------------------------------------------------
157
158 #define STRICT
159
160
161 #include <OSD_Disk.hxx>
162 #include <OSD_OSDError.hxx>
163 #include <OSD_Path.hxx>
164 #include <Standard_ProgramError.hxx>
165 #include <NCollection_String.hxx>
166 #include <TCollection_ExtendedString.hxx>
167
168 #include <windows.h>
169
170 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
171
172 static void __fastcall _osd_wnt_set_disk_name ( TCollection_AsciiString&, const OSD_Path& );
173
174 OSD_Disk :: OSD_Disk () {
175  DWORD aBuffLen = GetCurrentDirectoryW(0, NULL);
176  wchar_t* aBuff = new wchar_t[size_t(aBuffLen) + 1];
177  GetCurrentDirectoryW(aBuffLen, aBuff);
178  aBuff[aBuffLen - 1] = (aBuff[aBuffLen - 2] == L'\\') ? L'\0' : L'\\';
179  aBuff[aBuffLen] = L'\0';
180  if (aBuffLen > 3 && aBuff[0] != L'\\')
181  {
182    aBuff[3] = L'\0';
183    NCollection_String aFolder(aBuff);
184    DiskName = aFolder.ToCString();
185    delete[] aBuff;
186  }
187  else
188  {
189    DiskName = "";
190  }
191 }  // end constructor ( 1 )
192
193 OSD_Disk :: OSD_Disk ( const OSD_Path& Name ) {
194
195  _osd_wnt_set_disk_name ( DiskName, Name );
196
197 }  // end constructor ( 2 )
198
199 OSD_Disk :: OSD_Disk ( const Standard_CString PathName ) {
200
201  OSD_Path path ( PathName );
202
203  _osd_wnt_set_disk_name ( DiskName, path );
204
205 }  // end constructor ( 3 )
206
207 OSD_Path OSD_Disk :: Name () const {
208
209  return DiskName;
210
211 }  // end OSD_Disk :: Name
212
213 void OSD_Disk :: SetName ( const OSD_Path& Name ) {
214
215  DiskName = Name.Disk ();
216
217 }  // end OSD_Disk :: SetName
218
219 Standard_Integer OSD_Disk :: DiskSize () {
220
221  Standard_Integer retVal = 0;
222
223
224 //  DWORD            dwSpC;
225 //  DWORD            dwBpS;
226 //  DWORD            dwFC;
227 //  DWORD            dwC;
228
229 //  if (   !GetDiskFreeSpace (  DiskName.ToCString (), &dwSpC, &dwBpS, &dwFC, &dwC  )   )
230
231   ULARGE_INTEGER lpFreeBytesAvailableToCaller; // receives the number of bytes on
232                                                 // disk available to the caller
233   ULARGE_INTEGER lpTotalNumberOfBytes;    // receives the number of bytes on disk
234   ULARGE_INTEGER lpTotalNumberOfFreeBytes;// receives the free bytes on disk
235
236   TCollection_ExtendedString DiskNameW(DiskName);
237   if (!GetDiskFreeSpaceExW ((const wchar_t*)DiskNameW.ToExtString(),
238                            &lpFreeBytesAvailableToCaller,
239                            &lpTotalNumberOfBytes,
240                            &lpTotalNumberOfFreeBytes))
241     
242     _osd_wnt_set_error ( myError, OSD_WDisk );
243   
244   else {
245     
246     ULONGLONG  aSize = lpTotalNumberOfBytes.QuadPart /512;
247    
248     retVal = ( Standard_Integer ) aSize; // may be an overflow
249     
250     // retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
251   }
252
253
254  return retVal;
255
256 }  // end OSD_Disk :: DiskSize
257
258 Standard_Integer OSD_Disk :: DiskFree () {
259
260  Standard_Integer retVal = -1;
261
262
263 //  DWORD            dwSpC;
264 //  DWORD            dwBpS;
265 //  DWORD            dwFC;
266 //  DWORD            dwC;
267
268   ULARGE_INTEGER lpFreeBytesAvailableToCaller; // receives the number of bytes on
269                                                 // disk available to the caller
270   ULARGE_INTEGER lpTotalNumberOfBytes;    // receives the number of bytes on disk
271   ULARGE_INTEGER lpTotalNumberOfFreeBytes;// receives the free bytes on disk
272
273   // if (   !GetDiskFreeSpace (  DiskName.ToCString (), &dwSpC, &dwBpS, &dwFC, &dwC  )   )
274   TCollection_ExtendedString DiskNameW(DiskName);
275   if (!GetDiskFreeSpaceExW((const wchar_t*)DiskNameW.ToExtString(),
276                            &lpFreeBytesAvailableToCaller,
277                            &lpTotalNumberOfBytes,
278                            &lpTotalNumberOfFreeBytes))
279     
280     _osd_wnt_set_error ( myError, OSD_WDisk );
281   
282   else {
283     
284     ULONGLONG  aSize = lpFreeBytesAvailableToCaller.QuadPart /512;
285     
286     retVal = ( Standard_Integer ) aSize; // may be an overflow
287
288     //  retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
289   }
290
291  return retVal;
292
293 }  // end OSD_Disk :: DiskFree
294
295 Standard_Integer OSD_Disk :: DiskQuota () {
296
297  return DiskSize ();
298
299 }  // end OSD_Disk :: DiskQuota
300
301 void OSD_Disk :: SetDiskQuota ( const Standard_Integer /*QuotaSize*/ ) {
302
303  SetLastError (  ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION  );
304
305  _osd_wnt_set_error ( myError, OSD_WDisk );
306
307 }  // end OSD_Disk :: SetDiskQuota
308
309 void OSD_Disk :: SetQuotaOn () {
310
311  SetLastError (  ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION  );
312
313  _osd_wnt_set_error ( myError, OSD_WDisk );
314
315 }  // end OSD_Disk :: SetQuotaOn
316
317 void OSD_Disk :: SetQuotaOff () {
318
319  SetLastError (  ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION  );
320
321  _osd_wnt_set_error ( myError, OSD_WDisk );
322
323 }  // end OSD_Disk :: SetQuotaOff
324
325 Standard_Boolean OSD_Disk :: Failed () const {
326
327  return myError.Failed ();
328
329 }  // end OSD_Disk :: Failed
330
331 void OSD_Disk :: Reset () {
332
333  myError.Reset ();
334
335 }  // end OSD_Disk :: Reset
336
337 void OSD_Disk :: Perror () {
338
339  myError.Perror ();
340
341 }  // end OSD_Disk :: Perror
342
343 Standard_Integer OSD_Disk :: Error () const {
344
345  return myError.Error ();
346
347 }  // end OSD_Disk :: Error
348
349 static void __fastcall _osd_wnt_set_disk_name ( TCollection_AsciiString& result, const OSD_Path& path ) {
350
351  TCollection_AsciiString dir;
352
353  result = path.Disk ();
354
355  if (  result.UsefullLength () == 0  ) {
356  
357   int i, j, k;
358
359   dir = path.Trek ();
360   
361   if (   (  j = dir.UsefullLength ()  ) > 2 &&
362          dir.Value ( 1 ) == '|'     &&
363          dir.Value ( 2 ) == '|'
364   ) {
365   
366    dir.SetValue (  1, '\\');
367    dir.SetValue (  2, '\\');
368    
369    for ( i = 3, k = 0; i <= j; ++i )
370
371     if (  dir.Value ( i ) == '|') {
372     
373      if ( k == 0 ) {
374
375       dir.SetValue (  i, '\\');
376       ++k;
377       continue; 
378      
379      }  // end if
380
381      dir.SetValue (  i, '\\');
382      break;
383      
384     }  /* end if */
385
386     if ( k == 0 )
387     {
388      if (  path.Name ().UsefullLength () == 0 && path.Extension ().UsefullLength () == 0  )
389      
390       goto badPath;
391
392      else {
393
394       dir += '\\';
395       dir += path.Name ();
396       dir += path.Extension ();
397       
398      }  // end else    
399     }
400
401     if (   dir.Value (  dir.UsefullLength ()  ) != '\\') dir += '\\';
402
403     result = dir;
404   
405   } else {
406 badPath:  
407    Standard_ProgramError :: Raise ( "OSD_Disk: bad disk name" );
408
409   }  // end else
410  
411  } else result += '/';
412
413 }  // end _osd_set_disk_name
414
415 #endif