This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RazorPidFile.cs
71 lines (58 loc) · 2.11 KB
/
RazorPidFile.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Text;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.BuildServer
{
internal class RazorPidFile
{
public const string RazorServerType = "rzc";
public const string FilePrefix = "rzc-";
public RazorPidFile(FilePath path, int processId, FilePath serverPath, string pipeName)
{
Path = path;
ProcessId = processId;
ServerPath = serverPath;
PipeName = pipeName ?? throw new ArgumentNullException(pipeName);
}
public FilePath Path { get; }
public int ProcessId;
public FilePath ServerPath { get; }
public string PipeName { get; }
public static RazorPidFile Read(FilePath path, IFileSystem fileSystem = null)
{
fileSystem = fileSystem ?? FileSystemWrapper.Default;
using (var stream = fileSystem.File.OpenFile(
path.Value,
FileMode.Open,
FileAccess.Read,
FileShare.Write | FileShare.Delete,
4096,
FileOptions.None))
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
if (!int.TryParse(reader.ReadLine(), out var processId))
{
return null;
}
if (reader.ReadLine() != RazorServerType)
{
return null;
}
var serverPath = reader.ReadLine();
if (string.IsNullOrEmpty(serverPath))
{
return null;
}
var pipeName = reader.ReadLine();
if (string.IsNullOrEmpty(pipeName))
{
return null;
}
return new RazorPidFile(path, processId, new FilePath(serverPath), pipeName);
}
}
}
}