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

Commit 1e9abf4

Browse files
author
Jonathan Miller
committed
Initial OSX DriveInfo changes
1 parent 4b4b862 commit 1e9abf4

File tree

6 files changed

+279
-169
lines changed

6 files changed

+279
-169
lines changed

src/Common/src/Interop/OSX/Interop.Libraries.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ internal static partial class Interop
55
{
66
private static partial class Libraries
77
{
8+
internal const string INODE64SUFFIX = "$INODE64";
89
internal const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
910
internal const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices";
11+
internal const string libc = "libc";
1012
internal const string libproc = "libproc";
1113
internal const string LibSystemKernel = "/usr/lib/system/libsystem_kernel";
1214
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 uid_t = System.UInt32;
8+
9+
internal static partial class Interop
10+
{
11+
internal static partial class libc
12+
{
13+
internal const int MAXPATHLEN = 1024;
14+
internal const int MFSTYPENAMELEN = 16;
15+
16+
[StructLayout(LayoutKind.Sequential)]
17+
internal unsafe struct fsid_t
18+
{
19+
internal fixed int val[2];
20+
}
21+
22+
[StructLayout(LayoutKind.Sequential)]
23+
internal unsafe struct statfs
24+
{
25+
internal uint f_bsize; /* fundamental file system block size */
26+
internal int f_iosize; /* optimal transfer block size */
27+
internal ulong f_blocks; /* total data blocks in file system */
28+
internal ulong f_bfree; /* free blocks in fs */
29+
internal ulong f_bavail; /* free blocks avail to non-superuser */
30+
internal ulong f_files; /* total file nodes in file system */
31+
internal ulong f_ffree; /* free file nodes in fs */
32+
internal fsid_t f_fsid; /* file system id */
33+
internal uid_t f_owner; /* user that mounted the filesystem */
34+
internal uint f_type; /* type of filesystem */
35+
internal uint f_flags; /* copy of mount exported flags */
36+
internal uint f_fssubtype; /* fs sub-type (flavor) */
37+
internal fixed byte f_fstypename[MFSTYPENAMELEN]; /* fs type name */
38+
internal fixed byte f_mntonname[MAXPATHLEN]; /* directory on which mounted */
39+
internal fixed byte f_mntfromname[MAXPATHLEN]; /* mounted filesystem */
40+
internal fixed uint f_reserved[8]; /* For future use */
41+
}
42+
43+
/// <summary>
44+
/// Gets all the current mount points on the system
45+
/// </summary>
46+
/// <param name="ppBuffer">A pointer to an array of mount points</param>
47+
/// <param name="flags">Flags that are passed to the getfsstat call for each statfs struct</param>
48+
/// <returns>Returns the number of retrieved statfs structs</returns>
49+
/// <remarks
50+
/// Do NOT free this memory...this memory is allocated by the OS, which is responsible for it.
51+
/// This call could also block for a bit to wait for slow network drives.
52+
/// </remarks>
53+
[DllImport(Interop.Libraries.libc, EntryPoint = "getmntinfo" + Interop.Libraries.INODE64SUFFIX)]
54+
internal static unsafe extern int getmntinfo(statfs** ppBuffer, int flags);
55+
56+
/// <summary>
57+
/// Gets a statfs struct for the given path that describes that mount point
58+
/// </summary>
59+
/// <param name="path">The path to retrieve the statfs for</param>
60+
/// <param name="buffer">The output statfs struct describing the mount point</param>
61+
/// <returns>Returns 0 on success, -1 on failure</returns>
62+
[DllImport(Interop.Libraries.libc, EntryPoint = "statfs" + Interop.Libraries.INODE64SUFFIX)]
63+
private static unsafe extern int get_statfs(string path, statfs* buffer);
64+
65+
/// <summary>
66+
/// Gets a statfs struct for a given mount point
67+
/// </summary>
68+
/// <param name="name">The drive name to retrieve the statfs data for</param>
69+
/// <returns>Returns </returns>
70+
internal static unsafe statfs GetStatFsForDriveName(string name)
71+
{
72+
statfs data = default(statfs);
73+
int result = get_statfs(name, &data);
74+
if (result < 0)
75+
throw Interop.GetExceptionForIoErrno(Marshal.GetLastWin32Error());
76+
else
77+
return data;
78+
}
79+
80+
internal static unsafe String GetMountPointName(statfs data)
81+
{
82+
return Marshal.PtrToStringAnsi((IntPtr)data.f_mntonname);
83+
}
84+
85+
internal static unsafe String GetMountPointFsType(statfs data)
86+
{
87+
return Marshal.PtrToStringAnsi((IntPtr)data.f_fstypename);
88+
}
89+
}
90+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,18 @@
116116
</ItemGroup>
117117
<ItemGroup Condition=" '$(TargetsOSX)' == 'true' ">
118118
<Compile Include="System\IO\DriveInfo.OSX.cs" />
119-
<Compile Include="$(CommonPath)\System\NotImplemented.cs">
120-
<!-- TODO: Remove once implemented -->
121-
<Link>Common\System\NotImplemented.cs</Link>
122-
</Compile>
123119
<Compile Include="$(CommonPath)\Interop\OSX\Interop.Errors.cs">
124120
<Link>Common\Interop\OSX\Interop.Errors.cs</Link>
125121
</Compile>
126122
<Compile Include="$(CommonPath)\System\IO\PathInternal.CaseInsensitive.cs">
127123
<Link>Common\System\IO\PathInternal.CaseInsensitive.cs</Link>
128124
</Compile>
125+
<Compile Include="$(CommonPath)\Interop\OSX\Interop.Libraries.cs">
126+
<Link>Common\Interop\OSX\Interop.Libraries.cs</Link>
127+
</Compile>
128+
<Compile Include="$(CommonPath)\Interop\OSX\libc\Interop.Mount.cs">
129+
<Link>Common\Interop\OSX\Interop.Mount.cs</Link>
130+
</Compile>
129131
</ItemGroup>
130132
<ItemGroup>
131133
<None Include="project.json" />

src/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Linux.cs

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -171,151 +171,5 @@ private static unsafe string DecodeString(byte* str)
171171
{
172172
return str != null ? Marshal.PtrToStringAnsi((IntPtr)str) : null;
173173
}
174-
175-
/// <summary>Categorizes a file system name into a drive type.</summary>
176-
/// <param name="fileSystemName">The name to categorize.</param>
177-
/// <returns>The recognized drive type.</returns>
178-
private static DriveType GetDriveType(string fileSystemName)
179-
{
180-
// This list is based primarily on "man fs", "man mount", "mntent.h", "/proc/filesystems",
181-
// and "wiki.debian.org/FileSystem". It can be extended over time as we
182-
// find additional file systems that should be recognized as a particular drive type.
183-
switch (fileSystemName)
184-
{
185-
case "iso":
186-
case "isofs":
187-
case "iso9660":
188-
case "fuseiso":
189-
case "fuseiso9660":
190-
case "umview-mod-umfuseiso9660":
191-
return DriveType.CDRom;
192-
193-
case "adfs":
194-
case "affs":
195-
case "befs":
196-
case "bfs":
197-
case "btrfs":
198-
case "ecryptfs":
199-
case "efs":
200-
case "ext":
201-
case "ext2":
202-
case "ext3":
203-
case "ext4":
204-
case "ext4dev":
205-
case "fat":
206-
case "fuseblk":
207-
case "fuseext2":
208-
case "fusefat":
209-
case "hfs":
210-
case "hfsplus":
211-
case "hpfs":
212-
case "jbd":
213-
case "jbd2":
214-
case "jfs":
215-
case "jffs":
216-
case "jffs2":
217-
case "minix":
218-
case "msdos":
219-
case "ocfs2":
220-
case "omfs":
221-
case "ntfs":
222-
case "qnx4":
223-
case "reiserfs":
224-
case "squashfs":
225-
case "swap":
226-
case "sysv":
227-
case "ubifs":
228-
case "udf":
229-
case "ufs":
230-
case "umsdos":
231-
case "umview-mod-umfuseext2":
232-
case "xenix":
233-
case "xfs":
234-
case "xiafs":
235-
case "xmount":
236-
case "zfs-fuse":
237-
return DriveType.Fixed;
238-
239-
case "9p":
240-
case "autofs":
241-
case "autofs4":
242-
case "beaglefs":
243-
case "cifs":
244-
case "coda":
245-
case "coherent":
246-
case "curlftpfs":
247-
case "davfs2":
248-
case "dlm":
249-
case "flickrfs":
250-
case "fusedav":
251-
case "fusesmb":
252-
case "gfs2":
253-
case "glusterfs-client":
254-
case "gmailfs":
255-
case "kafs":
256-
case "ltspfs":
257-
case "ncpfs":
258-
case "nfs":
259-
case "nfs4":
260-
case "obexfs":
261-
case "s3ql":
262-
case "smb":
263-
case "smbfs":
264-
case "sshfs":
265-
case "sysfs":
266-
case "wikipediafs":
267-
return DriveType.Network;
268-
269-
case "anon_inodefs":
270-
case "aptfs":
271-
case "avfs":
272-
case "bdev":
273-
case "binfmt_misc":
274-
case "cgroup":
275-
case "configfs":
276-
case "cramfs":
277-
case "cryptkeeper":
278-
case "cpuset":
279-
case "debugfs":
280-
case "devpts":
281-
case "devtmpfs":
282-
case "encfs":
283-
case "fuse":
284-
case "fuse.gvfsd-fuse":
285-
case "fusectl":
286-
case "hugetlbfs":
287-
case "libpam-encfs":
288-
case "ibpam-mount":
289-
case "mtpfs":
290-
case "mythtvfs":
291-
case "mqueue":
292-
case "pipefs":
293-
case "plptools":
294-
case "proc":
295-
case "pstore":
296-
case "pytagsfs":
297-
case "ramfs":
298-
case "rofs":
299-
case "romfs":
300-
case "rootfs":
301-
case "securityfs":
302-
case "sockfs":
303-
case "tmpfs":
304-
return DriveType.Ram;
305-
306-
case "gphotofs":
307-
case "usbfs":
308-
case "vfat":
309-
return DriveType.Removable;
310-
311-
case "aufs": // marking all unions as unknown
312-
case "funionfs":
313-
case "unionfs-fuse":
314-
case "mhddfs":
315-
default:
316-
return DriveType.Unknown;
317-
}
318-
}
319-
320174
}
321175
}

0 commit comments

Comments
 (0)