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 NCollection_String aFolder(aBuff);
184 DiskName = aFolder.ToCString();
191 } // end constructor ( 1 )
193 OSD_Disk :: OSD_Disk ( const OSD_Path& Name ) {
195 _osd_wnt_set_disk_name ( DiskName, Name );
197 } // end constructor ( 2 )
199 OSD_Disk :: OSD_Disk ( const Standard_CString PathName ) {
201 OSD_Path path ( PathName );
203 _osd_wnt_set_disk_name ( DiskName, path );
205 } // end constructor ( 3 )
207 OSD_Path OSD_Disk :: Name () const {
211 } // end OSD_Disk :: Name
213 void OSD_Disk :: SetName ( const OSD_Path& Name ) {
215 DiskName = Name.Disk ();
217 } // end OSD_Disk :: SetName
219 Standard_Integer OSD_Disk :: DiskSize () {
221 Standard_Integer retVal = 0;
229 // if ( !GetDiskFreeSpace ( DiskName.ToCString (), &dwSpC, &dwBpS, &dwFC, &dwC ) )
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
236 TCollection_ExtendedString DiskNameW(DiskName);
237 if (!GetDiskFreeSpaceExW ((const wchar_t*)DiskNameW.ToExtString(),
238 &lpFreeBytesAvailableToCaller,
239 &lpTotalNumberOfBytes,
240 &lpTotalNumberOfFreeBytes))
242 _osd_wnt_set_error ( myError, OSD_WDisk );
246 ULONGLONG aSize = lpTotalNumberOfBytes.QuadPart /512;
248 retVal = ( Standard_Integer ) aSize; // may be an overflow
250 // retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
256 } // end OSD_Disk :: DiskSize
258 Standard_Integer OSD_Disk :: DiskFree () {
260 Standard_Integer retVal = -1;
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
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))
280 _osd_wnt_set_error ( myError, OSD_WDisk );
284 ULONGLONG aSize = lpFreeBytesAvailableToCaller.QuadPart /512;
286 retVal = ( Standard_Integer ) aSize; // may be an overflow
288 // retVal = ( Standard_Integer )( dwSpC * dwBpS * dwFC );
293 } // end OSD_Disk :: DiskFree
295 Standard_Integer OSD_Disk :: DiskQuota () {
299 } // end OSD_Disk :: DiskQuota
301 void OSD_Disk :: SetDiskQuota ( const Standard_Integer /*QuotaSize*/ ) {
303 SetLastError ( ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION );
305 _osd_wnt_set_error ( myError, OSD_WDisk );
307 } // end OSD_Disk :: SetDiskQuota
309 void OSD_Disk :: SetQuotaOn () {
311 SetLastError ( ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION );
313 _osd_wnt_set_error ( myError, OSD_WDisk );
315 } // end OSD_Disk :: SetQuotaOn
317 void OSD_Disk :: SetQuotaOff () {
319 SetLastError ( ( DWORD )STG_E_UNIMPLEMENTEDFUNCTION );
321 _osd_wnt_set_error ( myError, OSD_WDisk );
323 } // end OSD_Disk :: SetQuotaOff
325 Standard_Boolean OSD_Disk :: Failed () const {
327 return myError.Failed ();
329 } // end OSD_Disk :: Failed
331 void OSD_Disk :: Reset () {
335 } // end OSD_Disk :: Reset
337 void OSD_Disk :: Perror () {
341 } // end OSD_Disk :: Perror
343 Standard_Integer OSD_Disk :: Error () const {
345 return myError.Error ();
347 } // end OSD_Disk :: Error
349 static void __fastcall _osd_wnt_set_disk_name ( TCollection_AsciiString& result, const OSD_Path& path ) {
351 TCollection_AsciiString dir;
353 result = path.Disk ();
355 if ( result.UsefullLength () == 0 ) {
361 if ( ( j = dir.UsefullLength () ) > 2 &&
362 dir.Value ( 1 ) == '|' &&
363 dir.Value ( 2 ) == '|'
366 dir.SetValue ( 1, '\\');
367 dir.SetValue ( 2, '\\');
369 for ( i = 3, k = 0; i <= j; ++i )
371 if ( dir.Value ( i ) == '|') {
375 dir.SetValue ( i, '\\');
381 dir.SetValue ( i, '\\');
388 if ( path.Name ().UsefullLength () == 0 && path.Extension ().UsefullLength () == 0 )
396 dir += path.Extension ();
401 if ( dir.Value ( dir.UsefullLength () ) != '\\') dir += '\\';
407 Standard_ProgramError :: Raise ( "OSD_Disk: bad disk name" );
411 } else result += '/';
413 } // end _osd_set_disk_name