-
Notifications
You must be signed in to change notification settings - Fork 693
/
DirectoryUtility.cs
107 lines (97 loc) · 4.06 KB
/
DirectoryUtility.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace NuGet.Common
{
/// <summary>
/// Directory operation helpers.
/// </summary>
public static class DirectoryUtility
{
public static ILogger Logger { get; set; }
/// <summary>
/// Creates all directories and subdirectories in the specified path unless they already exist.
/// New directories can be read and written by all users.
/// </summary>
public static void CreateSharedDirectory(string path)
{
Logger?.LogMinimal($"CreateSharedDirectory: {path}");
if (RuntimeEnvironmentHelper.IsWindows)
{
Directory.CreateDirectory(path);
}
else
{
path = Path.GetFullPath(path);
if (Directory.Exists(path))
{
return;
}
// ensure directories exists starting from the root
var root = Path.GetPathRoot(path);
var sepPos = root.Length - 1;
do
{
sepPos = path.IndexOf(Path.DirectorySeparatorChar, sepPos + 1);
var currentPath = sepPos == -1 ? path : path.Substring(0, sepPos);
Logger?.LogMinimal($"CreateSharedDirectory: {path} In the loop currently at: {currentPath}");
if (!Directory.Exists(currentPath))
{
Logger?.LogMinimal($"CreateSharedDirectory: {path} Running CreateSingleSharedDirectory {currentPath}");
CreateSingleSharedDirectory(currentPath);
}
} while (sepPos != -1);
}
}
private static void CreateSingleSharedDirectory(string path)
{
// Creating a directory and setting the permissions are two operations. To avoid race
// conditions, we create a different directory, set the permissions and rename it. We
// create it under the parent directory to make sure it is on the same volume.
var parentDir = Path.GetDirectoryName(path);
var tempDir = Path.Combine(parentDir, Guid.NewGuid().ToString());
Logger?.LogMinimal($"CreateSingleSharedDirectory {path} parentDir: {parentDir} tempDir: {tempDir} CreateDirectory temp!");
Directory.CreateDirectory(tempDir);
if (chmod(tempDir, UGO_RWX) == -1)
{
// it's very unlikely we can't set the permissions of a directory we just created
TryDeleteDirectory(tempDir);
var errno = Marshal.GetLastWin32Error();
throw new InvalidOperationException($"{DateTime.Now.Ticks}: Unable to set permission while creating {path}, errno={errno}.");
}
try
{
Logger?.LogMinimal($"CreateSingleSharedDirectory {path} moving directory {tempDir} to {path}");
Directory.Move(tempDir, path);
Logger?.LogMinimal($"CreateSingleSharedDirectory {path} Done moving {tempDir} to {path}");
}
catch
{
Logger?.LogMinimal($"CreateSingleSharedDirectory {path} Failed moving {tempDir} to {path}");
TryDeleteDirectory(tempDir);
if (Directory.Exists(path))
{
return;
}
else
{
throw;
}
}
}
private static void TryDeleteDirectory(string path)
{
try
{
Directory.Delete(path);
}
catch
{}
}
private const int UGO_RWX = 0x1ff; // 0777
[DllImport("libc", SetLastError = true)]
private static extern int chmod(string pathname, int mode);
}
}