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.
15 #include <OSD_Disk.hxx>
17 #include <OSD_OSDError.hxx>
18 #include <OSD_Path.hxx>
19 #include <OSD_WhoAmI.hxx>
20 #include <Standard_ProgramError.hxx>
21 #include <NCollection_Array1.hxx>
22 #include <NCollection_String.hxx>
23 #include <TCollection_ExtendedString.hxx>
28 void _osd_wnt_set_error (OSD_Error&, Standard_Integer, ... );
30 static TCollection_AsciiString _osd_wnt_set_disk_name (const OSD_Path& thePath)
33 TCollection_AsciiString aDisk = thePath.Disk();
34 if (aDisk.UsefullLength() != 0)
40 TCollection_AsciiString aDir = thePath.Trek();
41 const int j = aDir.UsefullLength();
43 || aDir.Value (1) != '|'
44 || aDir.Value (2) != '|')
46 throw Standard_ProgramError ("OSD_Disk: bad disk name");
49 aDir.SetValue (1, '\\');
50 aDir.SetValue (2, '\\');
52 for (int i = 3; i <= j; ++i)
54 if (aDir.Value (i) == '|')
58 aDir.SetValue (i, '\\');
63 aDir.SetValue (i, '\\');
70 if (thePath.Name().UsefullLength() == 0
71 && thePath.Extension().UsefullLength() == 0)
73 throw Standard_ProgramError ("OSD_Disk: bad disk name");
78 aDir += thePath.Name();
79 aDir += thePath.Extension();
83 if (aDir.Value (aDir.UsefullLength()) != '\\')
91 const OSD_WhoAmI Iam = OSD_WDisk;
93 #if defined(__ANDROID__)
95 #define statvfs statfs
96 #define fstatvfs fstatfs
98 #include <sys/statvfs.h>
104 // =======================================================================
105 // function : OSD_Disk
107 // =======================================================================
111 const DWORD aBuffLen = GetCurrentDirectoryW (0, NULL);
112 NCollection_Array1<wchar_t> aBuff (0, aBuffLen);
113 GetCurrentDirectoryW (aBuffLen, &aBuff.ChangeFirst());
114 aBuff.ChangeValue (aBuffLen - 1) = (aBuff.Value (aBuffLen - 2) == L'\\') ? L'\0' : L'\\';
115 aBuff.ChangeLast() = L'\0';
116 if (aBuffLen > 3 && aBuff.First() != L'\\')
118 aBuff.ChangeValue (3) = L'\0';
119 myDiskName = TCollection_AsciiString (&aBuff.ChangeFirst());
124 // =======================================================================
125 // function : OSD_Disk
127 // =======================================================================
128 OSD_Disk::OSD_Disk (const OSD_Path& theName)
129 : myDiskName (theName.Disk())
132 myDiskName = _osd_wnt_set_disk_name (theName);
136 OSD_Disk::OSD_Disk (const Standard_CString theName)
137 : myDiskName (theName)
140 OSD_Path aPath (theName);
141 myDiskName = _osd_wnt_set_disk_name (aPath);
145 // =======================================================================
146 // function : SetName
148 // =======================================================================
149 void OSD_Disk::SetName (const OSD_Path& theName)
151 myDiskName = theName.Disk();
154 // =======================================================================
157 // =======================================================================
158 OSD_Path OSD_Disk::Name() const
164 aPath.SetDisk (myDiskName);
169 // =======================================================================
170 // function : DiskSize
172 // =======================================================================
173 Standard_Integer OSD_Disk::DiskSize()
176 ULARGE_INTEGER aNbFreeAvailableBytes, aNbTotalBytes, aNbTotalFreeBytes;
177 const TCollection_ExtendedString aDiskNameW (myDiskName);
178 if (!GetDiskFreeSpaceExW (aDiskNameW.ToWideString(),
179 &aNbFreeAvailableBytes,
183 _osd_wnt_set_error (myError, OSD_WDisk);
187 ULONGLONG aSize = aNbTotalBytes.QuadPart / 512;
188 return (Standard_Integer )aSize; // may be an overflow
190 struct statvfs aBuffer;
191 if (statvfs (myDiskName.ToCString(), &aBuffer) == 0)
193 unsigned long aBSize512 = aBuffer.f_frsize / 512;
194 return Standard_Integer(aBuffer.f_blocks * aBSize512);
196 myError.SetValue (errno, Iam, "OSD_Disk: statvfs failed.");
201 // =======================================================================
202 // function : DiskFree
204 // =======================================================================
205 Standard_Integer OSD_Disk::DiskFree()
208 ULARGE_INTEGER aNbFreeAvailableBytes, aNbTotalBytes, aNbTotalFreeBytes;
209 const TCollection_ExtendedString aDiskNameW (myDiskName);
210 if (!GetDiskFreeSpaceExW (aDiskNameW.ToWideString(),
211 &aNbFreeAvailableBytes,
215 _osd_wnt_set_error (myError, OSD_WDisk);
219 ULONGLONG aSize = aNbFreeAvailableBytes.QuadPart / 512;
220 return (Standard_Integer )aSize; // may be an overflow
222 struct statvfs aBuffer;
223 if (statvfs (myDiskName.ToCString(), &aBuffer) == 0)
225 unsigned long aBSize512 = aBuffer.f_frsize / 512;
226 return Standard_Integer(aBuffer.f_bavail * aBSize512);
228 myError.SetValue (errno, Iam, "OSD_Disk: statvfs failed.");