DriveInfo.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Text;
  26. using System.Runtime.Serialization;
  27. using System.Runtime.CompilerServices;
  28. using System.Runtime.InteropServices;
  29. namespace System.IO {
  30. [SerializableAttribute]
  31. [ComVisibleAttribute(true)]
  32. public sealed class DriveInfo : ISerializable {
  33. string drive_format;
  34. string path;
  35. DriveInfo (string path, string fstype)
  36. {
  37. this.drive_format = fstype;
  38. this.path = path;
  39. }
  40. public DriveInfo (string driveName)
  41. {
  42. if (!Environment.IsUnix) {
  43. if (driveName == null || driveName.Length == 0)
  44. throw new ArgumentException ("The drive name is null or empty", "driveName");
  45. if (driveName.Length >= 2 && driveName [1] != ':')
  46. throw new ArgumentException ("Invalid drive name", "driveName");
  47. // Convert the path to a standard format so we can find it later.
  48. driveName = String.Concat (Char.ToUpperInvariant (driveName [0]).ToString (), ":\\");
  49. }
  50. DriveInfo [] drives = GetDrives ();
  51. foreach (DriveInfo d in drives){
  52. if (d.path == driveName){
  53. this.path = d.path;
  54. this.drive_format = d.drive_format;
  55. this.path = d.path;
  56. return;
  57. }
  58. }
  59. throw new ArgumentException ("The drive name does not exist", "driveName");
  60. }
  61. static void GetDiskFreeSpace (string path, out ulong availableFreeSpace, out ulong totalSize, out ulong totalFreeSpace)
  62. {
  63. MonoIOError error;
  64. if (!GetDiskFreeSpaceInternal (path, out availableFreeSpace, out totalSize, out totalFreeSpace, out error))
  65. throw MonoIO.GetException (path, error);
  66. }
  67. public long AvailableFreeSpace {
  68. get {
  69. ulong availableFreeSpace;
  70. ulong totalSize;
  71. ulong totalFreeSpace;
  72. GetDiskFreeSpace (path, out availableFreeSpace, out totalSize, out totalFreeSpace);
  73. return availableFreeSpace > long.MaxValue ? long.MaxValue : (long) availableFreeSpace;
  74. }
  75. }
  76. public long TotalFreeSpace {
  77. get {
  78. ulong availableFreeSpace;
  79. ulong totalSize;
  80. ulong totalFreeSpace;
  81. GetDiskFreeSpace (path, out availableFreeSpace, out totalSize, out totalFreeSpace);
  82. return totalFreeSpace > long.MaxValue ? long.MaxValue : (long) totalFreeSpace;
  83. }
  84. }
  85. public long TotalSize {
  86. get {
  87. ulong availableFreeSpace;
  88. ulong totalSize;
  89. ulong totalFreeSpace;
  90. GetDiskFreeSpace (path, out availableFreeSpace, out totalSize, out totalFreeSpace);
  91. return totalSize > long.MaxValue ? long.MaxValue : (long) totalSize;
  92. }
  93. }
  94. [MonoTODO ("Currently get only works on Mono/Unix; set not implemented")]
  95. public string VolumeLabel {
  96. get {
  97. return path;
  98. }
  99. set {
  100. throw new NotImplementedException ();
  101. }
  102. }
  103. public string DriveFormat {
  104. get {
  105. return drive_format;
  106. }
  107. }
  108. public DriveType DriveType {
  109. get {
  110. return (DriveType) GetDriveTypeInternal (path);
  111. }
  112. }
  113. public string Name {
  114. get {
  115. return path;
  116. }
  117. }
  118. public DirectoryInfo RootDirectory {
  119. get {
  120. return new DirectoryInfo (path);
  121. }
  122. }
  123. public bool IsReady {
  124. get {
  125. return Directory.Exists (Name);
  126. }
  127. }
  128. [MonoTODO("In windows, alldrives are 'Fixed'")]
  129. public static DriveInfo[] GetDrives ()
  130. {
  131. var drives = Environment.GetLogicalDrives ();
  132. DriveInfo [] infos = new DriveInfo [drives.Length];
  133. int i = 0;
  134. foreach (string s in drives)
  135. infos [i++] = new DriveInfo (s, GetDriveFormat (s));
  136. return infos;
  137. }
  138. void ISerializable.GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
  139. {
  140. throw new NotImplementedException ();
  141. }
  142. public override string ToString ()
  143. {
  144. return(Name);
  145. }
  146. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  147. unsafe extern static bool GetDiskFreeSpaceInternal (char *pathName, int pathName_length, out ulong freeBytesAvail,
  148. out ulong totalNumberOfBytes, out ulong totalNumberOfFreeBytes,
  149. out MonoIOError error);
  150. unsafe static bool GetDiskFreeSpaceInternal (string pathName, out ulong freeBytesAvail,
  151. out ulong totalNumberOfBytes, out ulong totalNumberOfFreeBytes,
  152. out MonoIOError error)
  153. {
  154. // FIXME Check for embedded nuls here or in native.
  155. fixed (char *fixed_pathName = pathName) {
  156. return GetDiskFreeSpaceInternal (fixed_pathName, pathName?.Length ?? 0,
  157. out freeBytesAvail, out totalNumberOfBytes, out totalNumberOfFreeBytes,
  158. out error);
  159. }
  160. }
  161. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  162. unsafe extern static uint GetDriveTypeInternal (char *rootPathName, int rootPathName_length);
  163. unsafe static uint GetDriveTypeInternal (string rootPathName)
  164. {
  165. // FIXME Check for embedded nuls here or in native.
  166. fixed (char *fixed_rootPathName = rootPathName) {
  167. return GetDriveTypeInternal (fixed_rootPathName, rootPathName?.Length ?? 0);
  168. }
  169. }
  170. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  171. unsafe extern static string GetDriveFormatInternal (char *rootPathName, int rootPathName_length);
  172. unsafe static string GetDriveFormat (string rootPathName)
  173. {
  174. // FIXME Check for embedded nuls here or in native.
  175. fixed (char *fixed_rootPathName = rootPathName) {
  176. return GetDriveFormatInternal (fixed_rootPathName, rootPathName?.Length ?? 0);
  177. }
  178. }
  179. }
  180. }