Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2589d83

Browse files
author
Jonathan Miller
committed
Refactoring to a Unix DriveInfo for basic info and OS specific DriveInfo
instances for the GetDrives call
1 parent 1b4080c commit 2589d83

File tree

16 files changed

+447
-439
lines changed

16 files changed

+447
-439
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
using ino_t = System.IntPtr;
8+
using off_t = System.Int64; // Assuming either 64-bit machine or _FILE_OFFSET_BITS == 64
9+
10+
internal static partial class Interop
11+
{
12+
internal static partial class libc
13+
{
14+
[DllImport(Libraries.Libc, EntryPoint = "readdir" + Interop.Libraries.INODE64SUFFIX, SetLastError = true)]
15+
internal static extern IntPtr readdir(SafeDirHandle dirp);
16+
17+
internal static unsafe DType GetDirEntType(IntPtr dirEnt)
18+
{
19+
return ((dirent*)dirEnt)->d_type;
20+
}
21+
22+
internal enum DType : byte
23+
{
24+
DT_UNKNOWN = 0,
25+
DT_FIFO = 1,
26+
DT_CHR = 2,
27+
DT_DIR = 4,
28+
DT_BLK = 6,
29+
DT_REG = 8,
30+
DT_LNK = 10,
31+
DT_SOCK = 12,
32+
DT_WHT = 14
33+
}
34+
}
35+
}

src/Common/src/Interop/FreeBSD/libc/Interop.readdir.cs renamed to src/Common/src/Interop/FreeBSD/libc/Interop.dirent.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,11 @@ internal static partial class Interop
1111
{
1212
internal static partial class libc
1313
{
14-
[DllImport(Libraries.Libc, SetLastError = true)]
15-
internal static extern IntPtr readdir(SafeDirHandle dirp);
16-
17-
internal static unsafe DType GetDirEntType(IntPtr dirEnt)
18-
{
19-
return ((dirent*)dirEnt)->d_type;
20-
}
21-
2214
internal static unsafe string GetDirEntName(IntPtr dirEnt)
2315
{
2416
return Marshal.PtrToStringAnsi((IntPtr)((dirent*)dirEnt)->d_name);
2517
}
2618

27-
internal enum DType : byte
28-
{
29-
DT_UNKNOWN = 0,
30-
DT_FIFO = 1,
31-
DT_CHR = 2,
32-
DT_DIR = 4,
33-
DT_BLK = 6,
34-
DT_REG = 8,
35-
DT_LNK = 10,
36-
DT_SOCK = 12,
37-
DT_WHT = 14
38-
}
39-
4019
#pragma warning disable 0649 // fields are assigned by P/Invoke call
4120
private unsafe struct dirent
4221
{

src/Common/src/Interop/FreeBSD/libc/Interop.statfs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@ internal unsafe struct statfs
4545
internal fixed byte f_mntfromname[MNAMELEN]; /* mounted filesystem */
4646
internal fixed byte f_mntonname[MNAMELEN]; /* directory on which mounted */
4747
}
48+
49+
internal static unsafe String GetMountPointFsType(statfs data)
50+
{
51+
return Marshal.PtrToStringAnsi((IntPtr)data.f_fstypename);
52+
}
4853
}
4954
}

src/Common/src/Interop/Linux/Interop.Libraries.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ internal static partial class Interop
55
{
66
private static partial class Libraries
77
{
8+
/// <summary>
9+
/// We aren't OS X so we don't have a suffix
10+
/// </summary>
11+
internal const string INODE64SUFFIX = "";
12+
813
internal const string LibRt = "librt"; // POSIX Realtime Extensions library
14+
internal const string libc = "libc"; // C runtime
915
}
1016
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class libc
10+
{
11+
/// <summary>
12+
/// Internal FileSystem names and magic numbers taken from man(2) statfs
13+
/// </summary>
14+
internal enum LinuxFileSystemTypes : long
15+
{
16+
adfs = 0xadf5,
17+
affs = 0xADFF,
18+
befs = 0x42465331,
19+
bfs = 0x1BADFACE,
20+
cifs = 0xFF534D42,
21+
coda = 0x73757245,
22+
coherent = 0x012FF7B7,
23+
cramfs = 0x28cd3d45,
24+
devfs = 0x1373,
25+
efs = 0x00414A53,
26+
ext = 0x137D,
27+
ext2_old = 0xEF51,
28+
ext2 = 0xEF53,
29+
ext3 = 0xEF53,
30+
ext4 = 0xEF53,
31+
hfs = 0x4244,
32+
hpfs = 0xF995E849,
33+
hugetlbfs = 0x958458f6,
34+
isofs = 0x9660,
35+
jffs2 = 0x72b6,
36+
jfs = 0x3153464a,
37+
minix_old = 0x137F, /* orig. minix */
38+
minix = 0x138F, /* 30 char minix */
39+
minix2 = 0x2468, /* minix V2 */
40+
minix2v2 = 0x2478, /* minix V2, 30 char names */
41+
msdos = 0x4d44,
42+
ncpfs = 0x564c,
43+
nfs = 0x6969,
44+
ntfs = 0x5346544e,
45+
openprom = 0x9fa1,
46+
proc = 0x9fa0,
47+
qnx4 = 0x002f,
48+
reiserfs = 0x52654973,
49+
romfs = 0x7275,
50+
smb = 0x517B,
51+
sysv2 = 0x012FF7B6,
52+
sysv4 = 0x012FF7B5,
53+
tmpfs = 0x01021994,
54+
udf = 0x15013346,
55+
ufs = 0x00011954,
56+
usbdevice = 0x9fa2,
57+
vxfs = 0xa501FCF5,
58+
xenix = 0x012FF7B4,
59+
xfs = 0x58465342,
60+
xiafs = 0x012FD16D,
61+
}
62+
63+
[StructLayout(LayoutKind.Sequential)]
64+
internal unsafe struct fsid_t
65+
{
66+
internal fixed int val[2];
67+
}
68+
69+
[StructLayout(LayoutKind.Sequential)]
70+
internal unsafe struct statfs
71+
{
72+
internal int f_type;
73+
internal int f_bsize;
74+
internal ulong f_blocks;
75+
internal ulong f_bfree;
76+
internal ulong f_bavail;
77+
internal ulong f_files;
78+
internal ulong f_ffree;
79+
internal fsid_t f_fsid;
80+
internal int f_namelen;
81+
internal int f_frsize;
82+
internal int f_flags;
83+
internal fixed int f_space[4];
84+
}
85+
86+
internal static unsafe String GetMountPointFsType(statfs data)
87+
{
88+
return data.f_type.ToString();
89+
}
90+
}
91+
}

src/Common/src/Interop/OSX/libc/Interop.readdir.cs renamed to src/Common/src/Interop/OSX/libc/Interop.dirent.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,12 @@ internal static partial class Interop
88
{
99
internal static partial class libc
1010
{
11-
[DllImport(Libraries.Libc, EntryPoint = "readdir$INODE64", SetLastError = true)]
12-
internal static extern IntPtr readdir(SafeDirHandle dirp);
13-
14-
internal static unsafe DType GetDirEntType(IntPtr dirEnt)
15-
{
16-
return ((dirent*)dirEnt)->d_type;
17-
}
18-
1911
internal static unsafe string GetDirEntName(IntPtr dirEnt)
2012
{
2113
dirent* curEntryPtr = (dirent*)dirEnt;
2214
return Marshal.PtrToStringAnsi((IntPtr)curEntryPtr->d_name, curEntryPtr->d_namelen);
2315
}
2416

25-
internal enum DType : byte
26-
{
27-
DT_UNKNOWN = 0,
28-
DT_FIFO = 1,
29-
DT_CHR = 2,
30-
DT_DIR = 4,
31-
DT_BLK = 6,
32-
DT_REG = 8,
33-
DT_LNK = 10,
34-
DT_SOCK = 12,
35-
DT_WHT = 14
36-
}
37-
3817
private const int __DARWIN_MAXPATHLEN = 1024;
3918

4019
#pragma warning disable 0649 // fields are assigned by P/Invoke call

src/Common/src/Interop/OSX/libc/Interop.statfs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@ internal unsafe struct statfs
3939
internal fixed byte f_mntfromname[MAXPATHLEN]; /* mounted filesystem */
4040
internal fixed uint f_reserved[8]; /* For future use */
4141
}
42+
43+
internal static unsafe String GetMountPointFsType(statfs data)
44+
{
45+
return Marshal.PtrToStringAnsi((IntPtr)data.f_fstypename);
46+
}
4247
}
4348
}

src/Common/src/Interop/BSD/libc/Interop.mountpoints.cs renamed to src/Common/src/Interop/Unix/libc/Interop.mountpoints.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ internal static partial class Interop
1010
{
1111
internal static partial class libc
1212
{
13+
[DllImport("libc")]
14+
static extern int printf(string data);
1315
/// <summary>
1416
/// Gets all the current mount points on the system
1517
/// </summary>
@@ -20,7 +22,7 @@ internal static partial class libc
2022
/// Do NOT free this memory...this memory is allocated by the OS, which is responsible for it.
2123
/// This call could also block for a bit to wait for slow network drives.
2224
/// </remarks>
23-
[DllImport(Interop.Libraries.libc, EntryPoint = "getmntinfo" + Interop.Libraries.INODE64SUFFIX)]
25+
[DllImport(Interop.Libraries.libc, EntryPoint = "getmntinfo" + Interop.Libraries.INODE64SUFFIX, SetLastError = true)]
2426
internal static unsafe extern int getmntinfo(statfs** ppBuffer, int flags);
2527

2628
/// <summary>
@@ -29,7 +31,7 @@ internal static partial class libc
2931
/// <param name="path">The path to retrieve the statfs for</param>
3032
/// <param name="buffer">The output statfs struct describing the mount point</param>
3133
/// <returns>Returns 0 on success, -1 on failure</returns>
32-
[DllImport(Interop.Libraries.libc, EntryPoint = "statfs" + Interop.Libraries.INODE64SUFFIX)]
34+
[DllImport(Interop.Libraries.libc, EntryPoint = "statfs" + Interop.Libraries.INODE64SUFFIX, SetLastError = true)]
3335
private static unsafe extern int get_statfs(string path, statfs* buffer);
3436

3537
/// <summary>
@@ -42,19 +44,18 @@ internal static unsafe statfs GetStatFsForDriveName(string name)
4244
statfs data = default(statfs);
4345
int result = get_statfs(name, &data);
4446
if (result < 0)
45-
throw Interop.GetExceptionForIoErrno(Marshal.GetLastWin32Error());
47+
{
48+
int errno = Marshal.GetLastWin32Error();
49+
if (errno == Interop.Errors.ENOENT)
50+
throw new System.IO.DriveNotFoundException(SR.Format(SR.IO_DriveNotFound_Drive, name)); // match Win32 exception
51+
else
52+
throw Interop.GetExceptionForIoErrno(errno, isDirectory: true);
53+
}
4654
else
55+
{
56+
printf("Should be here\r\n");
4757
return data;
48-
}
49-
50-
internal static unsafe String GetMountPointName(statfs data)
51-
{
52-
return Marshal.PtrToStringAnsi((IntPtr)data.f_mntonname);
53-
}
54-
55-
internal static unsafe String GetMountPointFsType(statfs data)
56-
{
57-
return Marshal.PtrToStringAnsi((IntPtr)data.f_fstypename);
58+
}
5859
}
5960
}
6061
}

src/Common/src/Interop/Unix/libc/Interop.statvfs.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
</Compile>
8181
</ItemGroup>
8282
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
83+
<Compile Include="System\IO\DriveInfo.Unix.cs" />
8384
<Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
8485
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
8586
</Compile>
@@ -98,12 +99,12 @@
9899
<Compile Include="$(CommonPath)\System\IO\PathInternal.Unix.cs">
99100
<Link>Common\System\IO\PathInternal.Unix.cs</Link>
100101
</Compile>
102+
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.mountpoints.cs">
103+
<Link>Common\Interop\Unix\Interop.mountpoints.cs</Link>
104+
</Compile>
101105
</ItemGroup>
102106
<ItemGroup Condition=" '$(TargetsLinux)' == 'true' ">
103107
<Compile Include="System\IO\DriveInfo.Linux.cs" />
104-
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.statvfs.cs">
105-
<Link>Common\Interop\Unix\Interop.statvfs.cs</Link>
106-
</Compile>
107108
<Compile Include="$(CommonPath)\Interop\Linux\libc\Interop.endmntent.cs">
108109
<Link>Common\Interop\Unix\Interop.endmntent.cs</Link>
109110
</Compile>
@@ -119,12 +120,15 @@
119120
<Compile Include="$(CommonPath)\System\IO\PathInternal.CaseSensitive.cs">
120121
<Link>Common\System\IO\PathInternal.CaseSensitive.cs</Link>
121122
</Compile>
123+
<Compile Include="$(CommonPath)\Interop\Linux\libc\Interop.statfs.cs">
124+
<Link>Common\Interop\Linux\Interop.statfs.cs</Link>
125+
</Compile>
126+
<Compile Include="$(CommonPath)\Interop\Linux\Interop.Libraries.cs">
127+
<Link>Common\Interop\Linux\Interop.Libraries.cs</Link>
128+
</Compile>
122129
</ItemGroup>
123130
<ItemGroup Condition=" '$(TargetsBSD)' == 'true' ">
124131
<Compile Include="System\IO\DriveInfo.BSD.cs" />
125-
<Compile Include="$(CommonPath)\Interop\BSD\libc\Interop.mountpoints.cs">
126-
<Link>Common\Interop\BSD\Interop.mountpoints.cs</Link>
127-
</Compile>
128132
</ItemGroup>
129133
<ItemGroup Condition=" '$(TargetsOSX)' == 'true' ">
130134
<Compile Include="$(CommonPath)\Interop\OSX\Interop.Errors.cs">

0 commit comments

Comments
 (0)