2adf192ea449ad5cba02b19e0b1b6beee8a66c4b
[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    DiskName = TCollection_AsciiString (aBuff);
184    delete[] aBuff;
185  }
186  else
187  {
188    DiskName = "";
189  }
190 }  // end constructor ( 1 )
191
192 OSD_Disk :: OSD_Disk ( const OSD_Path& Name ) {
193
194  _osd_wnt_set_disk_name ( DiskName, Name );
195
196 }  // end constructor ( 2 )
197
198 OSD_Disk :: OSD_Disk ( const Standard_CString PathName ) {
199
200  OSD_Path path ( PathName );
201
202  _osd_wnt_set_disk_name ( DiskName, path );
203
204 }  // end constructor ( 3 )
205
206 OSD_Path OSD_Disk :: Name () const {
207
208  return DiskName;
209
210 }  // end OSD_Disk :: Name
211
212 void OSD_Disk :: SetName ( const OSD_Path& Name ) {
213
214  DiskName = Name.Disk ();
215
216 }  // end OSD_Disk :: SetName
217
218 Standard_Integer OSD_Disk :: DiskSize () {
219
220  Standard_Integer retVal = 0;
221
222
223 //  DWORD            dwSpC;
224 //  DWORD            dwBpS;
225 //  DWORD            dwFC;
226 //  DWORD            dwC;
227
228 //  if (   !GetDiskFreeSpace (  DiskName.ToCString (), &dwSpC, &dwBpS, &dwFC, &dwC  )   )
229
230   ULARGE_INTEGER lpFreeBytesAvailableToCaller; // receives the number of bytes on
231                                                 // disk available to the caller
232   ULARGE_INTEGER lpTotalNumberOfBytes;    // receives the number of bytes on disk
233   ULARGE_INTEGER lpTotalNumberOfFreeBytes;// receives the free bytes on disk
234
235   TCollection_ExtendedString DiskNameW(DiskName);
236   if (!GetDiskFreeSpaceExW (DiskNameW.ToWideString(),
237                            &lpFreeBytesAvailableToCaller,
238                            &lpTotalNumberOfBytes,
239                            &lpTotalNumberOfFreeBytes))
240     
241     _osd_wnt_set_error ( myError, OSD_WDisk );
242   
243   else {
244     
245     ULONGLONG  aSize = lpTotalNumberOfBytes.QuadPart /512;
246    
247     retVal = ( Standard_Integer ) aSize; // may be an overflow
248     
249     // retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
250   }
251
252
253  return retVal;
254
255 }  // end OSD_Disk :: DiskSize
256
257 Standard_Integer OSD_Disk :: DiskFree () {
258
259  Standard_Integer retVal = -1;
260
261
262 //  DWORD            dwSpC;
263 //  DWORD            dwBpS;
264 //  DWORD            dwFC;
265 //  DWORD            dwC;
266
267   ULARGE_INTEGER lpFreeBytesAvailableToCaller; // receives the number of bytes on
268                                                 // disk available to the caller
269   ULARGE_INTEGER lpTotalNumberOfBytes;    // receives the number of bytes on disk
270   ULARGE_INTEGER lpTotalNumberOfFreeBytes;// receives the free bytes on disk
271
272   // if (   !GetDiskFreeSpace (  DiskName.ToCString (), &dwSpC, &dwBpS, &dwFC, &dwC  )   )
273   TCollection_ExtendedString DiskNameW(DiskName);
274   if (!GetDiskFreeSpaceExW (DiskNameW.ToWideString(),
275                            &lpFreeBytesAvailableToCaller,
276                            &lpTotalNumberOfBytes,
277                            &lpTotalNumberOfFreeBytes))
278     
279     _osd_wnt_set_error ( myError, OSD_WDisk );
280   
281   else {
282     
283     ULONGLONG  aSize = lpFreeBytesAvailableToCaller.QuadPart /512;
284     
285     retVal = ( Standard_Integer ) aSize; // may be an overflow
286
287     //  retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
288   }
289
290  return retVal;
291
292 }  // end OSD_Disk :: DiskFree
293
294 Standard_Integer OSD_Disk :: DiskQuota () {
295
296  return DiskSize ();
297
298 }  // end OSD_Disk :: DiskQuota
299
300 void OSD_Disk :: SetDiskQuota ( const Standard_Integer /*QuotaSize*/ ) {
301
302  SetLastError (  ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION  );
303
304  _osd_wnt_set_error ( myError, OSD_WDisk );
305
306 }  // end OSD_Disk :: SetDiskQuota
307
308 void OSD_Disk :: SetQuotaOn () {
309
310  SetLastError (  ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION  );
311
312  _osd_wnt_set_error ( myError, OSD_WDisk );
313
314 }  // end OSD_Disk :: SetQuotaOn
315
316 void OSD_Disk :: SetQuotaOff () {
317
318  SetLastError (  ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION  );
319
320  _osd_wnt_set_error ( myError, OSD_WDisk );
321
322 }  // end OSD_Disk :: SetQuotaOff
323
324 Standard_Boolean OSD_Disk :: Failed () const {
325
326  return myError.Failed ();
327
328 }  // end OSD_Disk :: Failed
329
330 void OSD_Disk :: Reset () {
331
332  myError.Reset ();
333
334 }  // end OSD_Disk :: Reset
335
336 void OSD_Disk :: Perror () {
337
338  myError.Perror ();
339
340 }  // end OSD_Disk :: Perror
341
342 Standard_Integer OSD_Disk :: Error () const {
343
344  return myError.Error ();
345
346 }  // end OSD_Disk :: Error
347
348 static void __fastcall _osd_wnt_set_disk_name ( TCollection_AsciiString& result, const OSD_Path& path ) {
349
350  TCollection_AsciiString dir;
351
352  result = path.Disk ();
353
354  if (  result.UsefullLength () == 0  ) {
355  
356   int i, j, k;
357
358   dir = path.Trek ();
359   
360   if (   (  j = dir.UsefullLength ()  ) > 2 &&
361          dir.Value ( 1 ) == '|'     &&
362          dir.Value ( 2 ) == '|'
363   ) {
364   
365    dir.SetValue (  1, '\\');
366    dir.SetValue (  2, '\\');
367    
368    for ( i = 3, k = 0; i <= j; ++i )
369
370     if (  dir.Value ( i ) == '|') {
371     
372      if ( k == 0 ) {
373
374       dir.SetValue (  i, '\\');
375       ++k;
376       continue; 
377      
378      }  // end if
379
380      dir.SetValue (  i, '\\');
381      break;
382      
383     }  /* end if */
384
385     if ( k == 0 )
386     {
387      if (  path.Name ().UsefullLength () == 0 && path.Extension ().UsefullLength () == 0  )
388      
389       goto badPath;
390
391      else {
392
393       dir += '\\';
394       dir += path.Name ();
395       dir += path.Extension ();
396       
397      }  // end else    
398     }
399
400     if (   dir.Value (  dir.UsefullLength ()  ) != '\\') dir += '\\';
401
402     result = dir;
403   
404   } else {
405 badPath:  
406    throw Standard_ProgramError ( "OSD_Disk: bad disk name" );
407
408   }  // end else
409  
410  } else result += '/';
411
412 }  // end _osd_set_disk_name
413
414 #endif