Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merge pull request #3257 from sokket/pal
Browse files Browse the repository at this point in the history
Finishing up PAL'ing Unix\libc (non-networking and crypto) PInvoke calls
  • Loading branch information
Jonathan Miller committed Sep 17, 2015
2 parents 75fd2b2 + f8fd4b7 commit 6615490
Show file tree
Hide file tree
Showing 28 changed files with 308 additions and 294 deletions.
17 changes: 0 additions & 17 deletions src/Common/src/Interop/FreeBSD/libc/Interop.PathConfNames.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/Common/src/Interop/Linux/libc/Interop.PathConfNames.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Common/src/Interop/Linux/libc/Interop.prioritywhich.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/Common/src/Interop/OSX/Interop.prioritywhich.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/Common/src/Interop/OSX/libc/Interop.PathConfNames.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
using System;
using System.Runtime.InteropServices;

using size_t = System.IntPtr;

internal static partial class Interop
{
internal static partial class libc
internal static partial class Sys
{
internal static unsafe string getcwd()
{
const int StackLimit = 256;
[DllImport(Libraries.SystemNative, SetLastError = true)]
private static unsafe extern byte* GetCwd(byte* buffer, int bufferLength);

internal static unsafe string GetCwd()
{
const int StackLimit = 256;

// First try to get the path into a buffer on the stack
byte* stackBuf = stackalloc byte[StackLimit];
string result = getcwd(stackBuf, StackLimit);
string result = GetCwdHelper(stackBuf, StackLimit);
if (result != null)
{
return result;
}

// If that was too small, try increasing large buffer sizes
// until we get one that works or until we hit MaxPath.
int maxPath = Interop.libc.MaxPath;
int maxPath = Interop.Sys.MaxPath;
if (StackLimit < maxPath)
{
int bufferSize = StackLimit;
Expand All @@ -34,7 +35,7 @@ internal static unsafe string getcwd()
var buf = new byte[Math.Min(bufferSize, maxPath)];
fixed (byte* ptr = buf)
{
result = getcwd(ptr, buf.Length);
result = GetCwdHelper(ptr, buf.Length);
if (result != null)
{
return result;
Expand All @@ -48,10 +49,10 @@ internal static unsafe string getcwd()
throw Interop.GetExceptionForIoErrno(new ErrorInfo(Interop.Error.ENAMETOOLONG));
}

private static unsafe string getcwd(byte* ptr, int bufferSize)
private static unsafe string GetCwdHelper(byte* ptr, int bufferSize)
{
// Call the real getcwd
byte* result = getcwd(ptr, (size_t)bufferSize);
byte* result = GetCwd(ptr, bufferSize);

// If it returned non-null, the null-terminated path is in the buffer
if (result != null)
Expand All @@ -68,8 +69,5 @@ private static unsafe string getcwd(byte* ptr, int bufferSize)
}
throw Interop.GetExceptionForIoErrno(errorInfo);
}

[DllImport(Libraries.Libc, SetLastError = true)]
private static extern unsafe byte* getcwd(byte* buf, size_t bufSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,34 @@

internal static partial class Interop
{
internal static partial class libc
internal static partial class Sys
{
/// <summary>
/// Gets the priority (nice value) of a certain execution group
/// </summary>
/// <param name="which">The execution group to retrieve the nice value for</param>
/// <param name="who">The id of the group</param>
/// <returns>
/// Returns the nice value (from -20 to 20) of the group on success; on failure,
/// returns -1. To actually determine success or failure, clear errno before calling
/// (which should be done automatically by the runtime) and check errno after the call.
/// </returns>
[DllImport(Libraries.Libc, SetLastError = true)]
private static extern int getpriority(PriorityWhich which, int who);
internal enum PriorityWhich : int
{
PRIO_PROCESS = 0,
PRIO_PGRP = 1,
PRIO_USER = 2,
}

[DllImport(Libraries.SystemNative, SetLastError = true)]
private static extern int GetPriority(PriorityWhich which, int who);

[DllImport(Libraries.SystemNative, SetLastError = true)]
internal static extern int SetPriority(PriorityWhich which, int who, int nice);

/// <summary>
/// Wrapper around getpriority since getpriority can return from -20 to 20; therefore,
/// we cannot rely on the return value for success and failure. This wrapper makes the
/// getpriority call to act more naturally where the return value is the actual error
/// value (or 0 if success) instead of forcing the caller to retrieve the last error.
/// </summary>
/// <param name="which"></param>
/// <param name="who"></param>
/// <param name="priority"></param>
/// <returns></returns>
internal static int getpriority(PriorityWhich which, int who, out int priority)
/// <returns>Returns 0 on success; otherwise, returns the errno value</returns>
internal static int GetPriority(PriorityWhich which, int who, out int priority)
{
priority = getpriority(which, who);
priority = GetPriority(which, who);
return Marshal.GetLastWin32Error();
}

/// <summary>
/// Sets the priority (nice value) of the specified execution group.
/// </summary>
/// <param name="which">The execution group to change the priority of</param>
/// <param name="who">The ID of the group</param>
/// <param name="prio">The new priority</param>
/// <returns>Returns 0 on success, -1 on failure</returns>
[DllImport(Libraries.Libc, SetLastError = true)]
internal static extern int setpriority(PriorityWhich which, int who, int prio);

internal static System.Diagnostics.ThreadPriorityLevel GetThreadPriorityFromNiceValue(int nice)
{
Debug.Assert((nice >= -20) && (nice <= 20));
Expand Down
72 changes: 72 additions & 0 deletions src/Common/src/Interop/Unix/System.Native/Interop.PathConf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Sys
{
internal static int DEFAULT_PC_NAME_MAX = 255;

internal enum PathConfName : int
{
PC_LINK_MAX = 1,
PC_MAX_CANON = 2,
PC_MAX_INPUT = 3,
PC_NAME_MAX = 4,
PC_PATH_MAX = 5,
PC_PIPE_BUF = 6,
PC_CHOWN_RESTRICTED = 7,
PC_NO_TRUNC = 8,
PC_VDISABLE = 9,
}

/// <summary>The maximum path length for the system. -1 if it hasn't yet been initialized.</summary>
private static int s_maxPath = -1;

/// <summary>The maximum name length for the system. -1 if it hasn't yet been initialized.</summary>
private static int s_maxName = -1;

internal static int MaxPath
{
get
{
// Benign race condition on cached value
if (s_maxPath < 0)
{
// GetMaximumPath returns a long from PathConf
// but our callers expect an int so we need to convert.
long temp = GetMaximumPath();
if (temp > int.MaxValue)
s_maxPath = int.MaxValue;
else
s_maxPath = Convert.ToInt32(temp);
}
return s_maxPath;
}
}

internal static int MaxName
{
get
{
// Benign race condition on cached value
if (s_maxName < 0)
{
int result = PathConf("/", PathConfName.PC_NAME_MAX);
s_maxName = result >= 0 ? result : DEFAULT_PC_NAME_MAX;
}

return s_maxName;
}
}

[DllImport(Libraries.SystemNative, SetLastError = true)]
private static extern int PathConf(string path, PathConfName name);

[DllImport(Libraries.SystemNative)]
private static extern long GetMaximumPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

internal static partial class Interop
{
internal static partial class libc
internal static partial class Sys
{
// printf takes a variable number of arguments, which is difficult to represent in C#.
// Instead, since we only have a small and fixed number of call sites, we declare
// an overload for each of the specific argument sets we need.

[DllImport(Libraries.Libc)]
internal static extern unsafe int printf(string format, string arg1);
[DllImport(Libraries.SystemNative, SetLastError = true)]
internal static extern unsafe int PrintF(string format, string arg1);
}
}
47 changes: 0 additions & 47 deletions src/Common/src/Interop/Unix/libc/Interop.pathconf.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Common/src/System/IO/PathInternal.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ internal static bool IsDirectorySeparator(char c)
/// </summary>
internal static bool IsPathTooLong(string fullPath)
{
return fullPath.Length >= Interop.libc.MaxPath;
return fullPath.Length >= Interop.Sys.MaxPath;
}

/// <summary>
/// Returns true if the directory is too long
/// </summary>
internal static bool IsDirectoryTooLong(string fullPath)
{
return fullPath.Length >= Interop.libc.MaxPath;
return fullPath.Length >= Interop.Sys.MaxPath;
}
}
}
Loading

0 comments on commit 6615490

Please sign in to comment.