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

Commit f2acba3

Browse files
committed
Merge pull request #2136 from sokket/driveinfo
Refactoring DriveInfo on OS X, Linux, and FreeBSD
2 parents 39cbff9 + b9f4180 commit f2acba3

20 files changed

+657
-449
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/Interop.Libraries.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ internal static partial class Interop
55
{
66
private static partial class Libraries
77
{
8-
internal const string LibRt = "librt"; // POSIX Realtime Extensions library
8+
/// <summary>
9+
/// We aren't OS X so don't have an INODE64 suffix to entry points
10+
/// </summary>
11+
internal const string INODE64SUFFIX = "";
12+
13+
internal const string LibRt = "librt"; // POSIX Realtime Extensions library
914
}
1015
}

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
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 MFSNAMELEN = 16; /* length of type name including null */
14+
internal const int MNAMELEN = 88; /* size of on/from name bufs */
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_version; /* structure version number */
26+
internal uint f_type; /* type of filesystem */
27+
internal ulong f_flags; /* copy of mount exported flags */
28+
internal ulong f_bsize; /* filesystem fragment size */
29+
internal ulong f_iosize; /* optimal transfer block size */
30+
internal ulong f_blocks; /* total data blocks in filesystem */
31+
internal ulong f_bfree; /* free blocks in filesystem */
32+
internal long f_bavail; /* free blocks avail to non-superuser */
33+
internal ulong f_files; /* total file nodes in filesystem */
34+
internal long f_ffree; /* free nodes avail to non-superuser */
35+
internal ulong f_syncwrites; /* count of sync writes since mount */
36+
internal ulong f_asyncwrites; /* count of async writes since mount */
37+
internal ulong f_syncreads; /* count of sync reads since mount */
38+
internal ulong f_asyncreads; /* count of async reads since mount */
39+
internal fixed ulong f_spare[10]; /* unused spare */
40+
internal uint f_namemax; /* maximum filename length */
41+
internal uid_t f_owner; /* user that mounted the filesystem */
42+
internal fsid_t f_fsid; /* filesystem id */
43+
internal fixed byte f_charspare[80]; /* spare string space */
44+
internal fixed byte f_fstypename[MFSNAMELEN]; /* filesystem type name */
45+
internal fixed byte f_mntfromname[MNAMELEN]; /* mounted filesystem */
46+
internal fixed byte f_mntonname[MNAMELEN]; /* directory on which mounted */
47+
}
48+
49+
internal static unsafe String GetMountPointFsType(statfs data)
50+
{
51+
return Marshal.PtrToStringAnsi((IntPtr)data.f_fstypename);
52+
}
53+
}
54+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ 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
914
}
1015
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
/// <remarks>
15+
/// These value names MUST be kept in sync with those in DriveInfo.Unix.GetDriveType
16+
/// </remarks>
17+
private enum LinuxFileSystemTypes : long
18+
{
19+
adfs = 0xadf5,
20+
affs = 0xADFF,
21+
befs = 0x42465331,
22+
bfs = 0x1BADFACE,
23+
cifs = 0xFF534D42,
24+
coda = 0x73757245,
25+
coherent = 0x012FF7B7,
26+
cramfs = 0x28cd3d45,
27+
devfs = 0x1373,
28+
efs = 0x00414A53,
29+
ext = 0x137D,
30+
ext2_old = 0xEF51,
31+
ext2 = 0xEF53,
32+
ext3 = 0xEF53,
33+
ext4 = 0xEF53,
34+
hfs = 0x4244,
35+
hpfs = 0xF995E849,
36+
hugetlbfs = 0x958458f6,
37+
isofs = 0x9660,
38+
jffs2 = 0x72b6,
39+
jfs = 0x3153464a,
40+
minix_old = 0x137F, /* orig. minix */
41+
minix = 0x138F, /* 30 char minix */
42+
minix2 = 0x2468, /* minix V2 */
43+
minix2v2 = 0x2478, /* minix V2, 30 char names */
44+
msdos = 0x4d44,
45+
ncpfs = 0x564c,
46+
nfs = 0x6969,
47+
ntfs = 0x5346544e,
48+
openprom = 0x9fa1,
49+
proc = 0x9fa0,
50+
qnx4 = 0x002f,
51+
reiserfs = 0x52654973,
52+
romfs = 0x7275,
53+
smb = 0x517B,
54+
sysv2 = 0x012FF7B6,
55+
sysv4 = 0x012FF7B5,
56+
tmpfs = 0x01021994,
57+
udf = 0x15013346,
58+
ufs = 0x00011954,
59+
usbdevice = 0x9fa2,
60+
vxfs = 0xa501FCF5,
61+
xenix = 0x012FF7B4,
62+
xfs = 0x58465342,
63+
xiafs = 0x012FD16D,
64+
}
65+
66+
[StructLayout(LayoutKind.Sequential)]
67+
internal unsafe struct fsid_t
68+
{
69+
internal fixed int val[2];
70+
}
71+
72+
[StructLayout(LayoutKind.Sequential)]
73+
internal unsafe struct statfs
74+
{
75+
internal long f_type;
76+
internal long f_bsize;
77+
internal ulong f_blocks;
78+
internal ulong f_bfree;
79+
internal ulong f_bavail;
80+
internal ulong f_files;
81+
internal ulong f_ffree;
82+
internal fsid_t f_fsid;
83+
internal long f_namelen;
84+
internal long f_frsize;
85+
internal long f_flags;
86+
internal fixed long f_space[4];
87+
}
88+
89+
internal static unsafe String GetMountPointFsType(statfs data)
90+
{
91+
return ((LinuxFileSystemTypes)data.f_type).ToString();
92+
}
93+
}
94+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ internal static partial class Interop
55
{
66
private static partial class Libraries
77
{
8+
/// <summary>
9+
/// Some functions in OS X have the __DARWIN_INODE64(<function_name>) decoration, which means
10+
/// that the function has the prefix $INODE64 natively, so we need to apply that decoration
11+
/// onto the end of the entry point name to PInvoke into the right function. We use this const
12+
/// so that other *nix systems that have the same function prototype but don't have the INODE64, since
13+
/// they aren't OS X, can use the same declaration and not redeclare it.
14+
/// </summary>
15+
internal const string INODE64SUFFIX = "$INODE64";
16+
817
internal const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
918
internal const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices";
1019
internal const string libproc = "libproc";

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
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
private const int MAXPATHLEN = 1024;
14+
private 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+
internal static unsafe String GetMountPointFsType(statfs data)
44+
{
45+
return Marshal.PtrToStringAnsi((IntPtr)data.f_fstypename);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)