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

Finishing up PAL'ing Unix\libc (non-networking and crypto) PInvoke calls #3257

Merged
merged 1 commit into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEFAULT_PC_PATH_MAX was removed and put behind GetMaximumPath. The same thing doesn't need to be done for DEFAULT_PC_NAME_MAX?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason the Max Path default value needed to be pushed into the PAL was because the PATH_MAX value changes between OS X, Linux, and other *nix flavors that we support. The PC Name Max does not look to have different default values (from our original implementation) so it does not need to be pushed down into the PAL and we can use the platform-agnostic 255 value


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