/
DriveInfo.Unix.cs
100 lines (91 loc) · 3.16 KB
/
DriveInfo.Unix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Security;
namespace System.IO
{
public sealed partial class DriveInfo
{
public DriveType DriveType
{
get
{
DriveType type;
int result = Interop.Sys.GetFormatInfoForMountPoint(Name, out type);
if (result == 0)
{
return type;
}
else
{
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
// This is one of the few properties that doesn't throw on failure,
// instead returning a value from the enum.
switch (errorInfo.Error)
{
case Interop.Error.ELOOP:
case Interop.Error.ENAMETOOLONG:
case Interop.Error.ENOENT:
case Interop.Error.ENOTDIR:
return DriveType.NoRootDirectory;
default:
return DriveType.Unknown;
}
}
}
}
public string DriveFormat
{
get
{
string format;
CheckStatfsResultAndThrowIfNecessary(Interop.Sys.GetFormatInfoForMountPoint(Name, out format));
return format;
}
}
public long AvailableFreeSpace
{
get
{
Interop.Sys.MountPointInformation mpi;
CheckStatfsResultAndThrowIfNecessary(Interop.Sys.GetSpaceInfoForMountPoint(Name, out mpi));
return checked((long)mpi.AvailableFreeSpace);
}
}
public long TotalFreeSpace
{
get
{
Interop.Sys.MountPointInformation mpi;
CheckStatfsResultAndThrowIfNecessary(Interop.Sys.GetSpaceInfoForMountPoint(Name, out mpi));
return checked((long)mpi.TotalFreeSpace);
}
}
public long TotalSize
{
get
{
Interop.Sys.MountPointInformation mpi;
CheckStatfsResultAndThrowIfNecessary(Interop.Sys.GetSpaceInfoForMountPoint(Name, out mpi));
return checked((long)mpi.TotalSize);
}
}
private void CheckStatfsResultAndThrowIfNecessary(int result)
{
if (result != 0)
{
var errorInfo = Interop.Sys.GetLastErrorInfo();
if (errorInfo.Error == Interop.Error.ENOENT)
{
throw new DriveNotFoundException(SR.Format(SR.IO_DriveNotFound_Drive, Name)); // match Win32
}
else
{
throw Interop.GetExceptionForIoErrno(errorInfo);
}
}
}
private static string[] GetMountPoints() => Interop.Sys.GetAllMountPoints();
}
}