1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
18 #include <OSD_Disk.hxx>
19 #include <OSD_OSDError.hxx>
20 #include <OSD_Path.hxx>
21 #include <OSD_WhoAmI.hxx>
23 const OSD_WhoAmI Iam = OSD_WDisk;
29 #if defined(__ANDROID__)
31 #define statvfs statfs
32 #define fstatvfs fstatfs
34 #include <sys/statvfs.h>
43 OSD_Disk::OSD_Disk() : myQuotaSize(0) {}
46 OSD_Disk::OSD_Disk(const OSD_Path& name){
47 DiskName = name.Disk();
51 OSD_Disk::OSD_Disk(const Standard_CString name)
57 void OSD_Disk::SetName(const OSD_Path& name){
58 DiskName = name.Disk();
62 OSD_Path OSD_Disk::Name()const{
64 result.SetDisk(DiskName);
68 Standard_Integer OSD_Disk::DiskSize(){
70 struct statvfs buffer;
72 if ( statvfs(DiskName.ToCString(),&buffer) == 0 ){
73 int BSize512 = buffer.f_frsize / 512 ;
74 return buffer.f_blocks * BSize512 ;
77 myError.SetValue(errno, Iam, "OSD_Disk: statvfs failed.");
82 Standard_Integer OSD_Disk::DiskFree(){
84 struct statvfs buffer;
85 if ( statvfs (DiskName.ToCString(),&buffer) == 0 ){
86 int BSize512 = buffer.f_frsize / 512 ;
87 return buffer.f_bavail * BSize512 ;
90 myError.SetValue(errno, Iam, "OSD_Disk: statvfs failed.");
95 Standard_Integer OSD_Disk::DiskQuota(){
101 void OSD_Disk::SetDiskQuota(const Standard_Integer ){
103 // struct dqblk quota_info;
105 // status = quota(Q_SETDLIM,<< User Id>>,"a_info);
107 // status = quotactl(Q_SETQLIM,"",<< User Id >>,"a_info);
113 void OSD_Disk::SetQuotaOff(){
116 // status = setquota("","");
118 // status = quotactl(Q_QUOTAOFF,"",0,NULL);
123 void OSD_Disk::SetQuotaOn(){
124 //TCollection_AsciiString quota_file="????";
127 // status = setquota("",quota_file);
129 // status = quotactl(Q_QUOTAON,"",0,quota_file);
135 void OSD_Disk::Reset(){
139 Standard_Boolean OSD_Disk::Failed()const{
140 return( myError.Failed());
143 void OSD_Disk::Perror() {
148 Standard_Integer OSD_Disk::Error()const{
149 return( myError.Error());
154 //-------------------------------------------------------------------------------
155 //---------------------------- Windows NT System --------------------------------
156 //-------------------------------------------------------------------------------
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>
170 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
172 static void __fastcall _osd_wnt_set_disk_name ( TCollection_AsciiString&, const OSD_Path& );
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'\\')
183 DiskName = TCollection_AsciiString (aBuff);
190 } // end constructor ( 1 )
192 OSD_Disk :: OSD_Disk ( const OSD_Path& Name ) {
194 _osd_wnt_set_disk_name ( DiskName, Name );
196 } // end constructor ( 2 )
198 OSD_Disk :: OSD_Disk ( const Standard_CString PathName ) {
200 OSD_Path path ( PathName );
202 _osd_wnt_set_disk_name ( DiskName, path );
204 } // end constructor ( 3 )
206 OSD_Path OSD_Disk :: Name () const {
210 } // end OSD_Disk :: Name
212 void OSD_Disk :: SetName ( const OSD_Path& Name ) {
214 DiskName = Name.Disk ();
216 } // end OSD_Disk :: SetName
218 Standard_Integer OSD_Disk :: DiskSize () {
220 Standard_Integer retVal = 0;
228 // if ( !GetDiskFreeSpace ( DiskName.ToCString (), &dwSpC, &dwBpS, &dwFC, &dwC ) )
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
235 TCollection_ExtendedString DiskNameW(DiskName);
236 if (!GetDiskFreeSpaceExW (DiskNameW.ToWideString(),
237 &lpFreeBytesAvailableToCaller,
238 &lpTotalNumberOfBytes,
239 &lpTotalNumberOfFreeBytes))
241 _osd_wnt_set_error ( myError, OSD_WDisk );
245 ULONGLONG aSize = lpTotalNumberOfBytes.QuadPart /512;
247 retVal = ( Standard_Integer ) aSize; // may be an overflow
249 // retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
255 } // end OSD_Disk :: DiskSize
257 Standard_Integer OSD_Disk :: DiskFree () {
259 Standard_Integer retVal = -1;
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
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))
279 _osd_wnt_set_error ( myError, OSD_WDisk );
283 ULONGLONG aSize = lpFreeBytesAvailableToCaller.QuadPart /512;
285 retVal = ( Standard_Integer ) aSize; // may be an overflow
287 // retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
292 } // end OSD_Disk :: DiskFree
294 Standard_Integer OSD_Disk :: DiskQuota () {
298 } // end OSD_Disk :: DiskQuota
300 void OSD_Disk :: SetDiskQuota ( const Standard_Integer /*QuotaSize*/ ) {
302 SetLastError ( ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION );
304 _osd_wnt_set_error ( myError, OSD_WDisk );
306 } // end OSD_Disk :: SetDiskQuota
308 void OSD_Disk :: SetQuotaOn () {
310 SetLastError ( ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION );
312 _osd_wnt_set_error ( myError, OSD_WDisk );
314 } // end OSD_Disk :: SetQuotaOn
316 void OSD_Disk :: SetQuotaOff () {
318 SetLastError ( ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION );
320 _osd_wnt_set_error ( myError, OSD_WDisk );
322 } // end OSD_Disk :: SetQuotaOff
324 Standard_Boolean OSD_Disk :: Failed () const {
326 return myError.Failed ();
328 } // end OSD_Disk :: Failed
330 void OSD_Disk :: Reset () {
334 } // end OSD_Disk :: Reset
336 void OSD_Disk :: Perror () {
340 } // end OSD_Disk :: Perror
342 Standard_Integer OSD_Disk :: Error () const {
344 return myError.Error ();
346 } // end OSD_Disk :: Error
348 static void __fastcall _osd_wnt_set_disk_name ( TCollection_AsciiString& result, const OSD_Path& path ) {
350 TCollection_AsciiString dir;
352 result = path.Disk ();
354 if ( result.UsefullLength () == 0 ) {
360 if ( ( j = dir.UsefullLength () ) > 2 &&
361 dir.Value ( 1 ) == '|' &&
362 dir.Value ( 2 ) == '|'
365 dir.SetValue ( 1, '\\');
366 dir.SetValue ( 2, '\\');
368 for ( i = 3, k = 0; i <= j; ++i )
370 if ( dir.Value ( i ) == '|') {
374 dir.SetValue ( i, '\\');
380 dir.SetValue ( i, '\\');
387 if ( path.Name ().UsefullLength () == 0 && path.Extension ().UsefullLength () == 0 )
395 dir += path.Extension ();
400 if ( dir.Value ( dir.UsefullLength () ) != '\\') dir += '\\';
406 throw Standard_ProgramError ( "OSD_Disk: bad disk name" );
410 } else result += '/';
412 } // end _osd_set_disk_name