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