|
| 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.Diagnostics; |
| 5 | + |
| 6 | +namespace System.IO |
| 7 | +{ |
| 8 | + internal static class PersistedFiles |
| 9 | + { |
| 10 | + // If we ever need system persisted data, /etc/dotnet/corefx/ |
| 11 | + private const string TopLevelDirectory = "dotnet"; |
| 12 | + // User persisted data, ~/.dotnet/corefx/ |
| 13 | + private const string TopLevelUserDirectory = "." + TopLevelDirectory; |
| 14 | + private const string SecondLevelDirectory = "corefx"; |
| 15 | + |
| 16 | + private static string s_userProductDirectory; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Get the location of where to persist information for a particular aspect of the framework, |
| 20 | + /// such as "cryptography". |
| 21 | + /// </summary> |
| 22 | + /// <param name="featureName">The directory name for the feature</param> |
| 23 | + /// <returns>A path within the user's home directory for persisting data for the feature</returns> |
| 24 | + internal static string GetUserFeatureDirectory(string featureName) |
| 25 | + { |
| 26 | + if (s_userProductDirectory == null) |
| 27 | + { |
| 28 | + EnsureUserDirectories(); |
| 29 | + } |
| 30 | + |
| 31 | + return Path.Combine(s_userProductDirectory, featureName); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Get the location of where to persist information for a particular aspect of a feature of |
| 36 | + /// the framework, such as "x509stores" within "cryptography". |
| 37 | + /// </summary> |
| 38 | + /// <param name="featureName">The directory name for the feature</param> |
| 39 | + /// <param name="subFeatureName">The directory name for the sub-feature</param> |
| 40 | + /// <returns>A path within the user's home directory for persisting data for the sub-feature</returns> |
| 41 | + internal static string GetUserFeatureDirectory(string featureName, string subFeatureName) |
| 42 | + { |
| 43 | + if (s_userProductDirectory == null) |
| 44 | + { |
| 45 | + EnsureUserDirectories(); |
| 46 | + } |
| 47 | + |
| 48 | + return Path.Combine(s_userProductDirectory, featureName, subFeatureName); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Get the location of where to persist information for a particular aspect of the framework, |
| 53 | + /// with a lot of hierarchy, such as ["cryptography", "x509stores", "my"] |
| 54 | + /// </summary> |
| 55 | + /// <param name="featurePathParts">A non-empty set of directories to use for the storage hierarchy</param> |
| 56 | + /// <returns>A path within the user's home directory for persisting data for the feature</returns> |
| 57 | + internal static string GetUserFeatureDirectory(params string[] featurePathParts) |
| 58 | + { |
| 59 | + Debug.Assert(featurePathParts != null); |
| 60 | + Debug.Assert(featurePathParts.Length > 0); |
| 61 | + |
| 62 | + if (s_userProductDirectory == null) |
| 63 | + { |
| 64 | + EnsureUserDirectories(); |
| 65 | + } |
| 66 | + |
| 67 | + return Path.Combine(s_userProductDirectory, Path.Combine(featurePathParts)); |
| 68 | + } |
| 69 | + |
| 70 | + private static void EnsureUserDirectories() |
| 71 | + { |
| 72 | + // TODO (2820): Use getpwuid_r(geteuid(), ...) instead of the HOME environment variable |
| 73 | + string userHomeDirectory = Environment.GetEnvironmentVariable("HOME"); |
| 74 | + |
| 75 | + if (string.IsNullOrEmpty(userHomeDirectory)) |
| 76 | + { |
| 77 | + throw new InvalidOperationException(SR.PersistedFiles_NoHomeDirectory); |
| 78 | + } |
| 79 | + |
| 80 | + s_userProductDirectory = Path.Combine( |
| 81 | + userHomeDirectory, |
| 82 | + TopLevelUserDirectory, |
| 83 | + SecondLevelDirectory); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments