summaryrefslogtreecommitdiff
path: root/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs
blob: 4a1fcf67d0bf406fe67f505710ee16a592a4707b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more 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, EntryPoint = "SystemNative_PathConf", SetLastError = true)]
        private static extern int PathConf(string path, PathConfName name);

        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetMaximumPath")]
        private static extern long GetMaximumPath();
    }
}