| | 1 | | using System.Text; |
| | 2 | | using Dotnet.Installer.Core.Services.Contracts; |
| | 3 | | using Dotnet.Installer.Core.Types; |
| | 4 | |
|
| | 5 | | namespace Dotnet.Installer.Core.Services.Implementations; |
| | 6 | |
|
| | 7 | | public class FileService : IFileService |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Enumerates the .mount unit files for a .NET content snap by looking at the files present in the directory |
| | 11 | | /// <c>$SNAP/mounts</c>. |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="contentSnapName">The .NET content snap name.</param> |
| | 14 | | /// <returns>A list of absolute paths of the .mount unit files for <c>contentSnapName</c>.</returns> |
| | 15 | | public IEnumerable<string> EnumerateContentSnapMountFiles(string contentSnapName) |
| 0 | 16 | | { |
| 0 | 17 | | return Directory.EnumerateFiles(Path.Join("/", "snap", contentSnapName, "current", "mounts")); |
| 0 | 18 | | } |
| | 19 | |
|
| | 20 | | /// <inheritdoc cref="File.Exists"/> |
| | 21 | | public bool FileExists(string path) |
| 0 | 22 | | { |
| 0 | 23 | | return File.Exists(path); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public void InstallSystemdMountUnit(string unitPath) |
| 0 | 27 | | { |
| 0 | 28 | | var destination = Path.Join("/", "usr", "lib", "systemd", "system"); |
| | 29 | |
|
| 0 | 30 | | File.Copy(unitPath,Path.Join(destination, unitPath.Split('/').Last()), overwrite: true); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public void UninstallSystemdMountUnit(string unitName) |
| 0 | 34 | | { |
| 0 | 35 | | var unitPath = Path.Join("/", "usr", "lib", "systemd", "system", unitName); |
| | 36 | |
|
| 0 | 37 | | if (File.Exists(unitPath)) File.Delete(unitPath); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public void InstallSystemdPathUnit(string snapName) |
| 0 | 41 | | { |
| 0 | 42 | | var destination = Path.Join("/", "usr", "lib", "systemd", "system"); |
| 0 | 43 | | var unitsLocation = Path.Join("/", "snap", "dotnet", "current", "Scripts"); |
| 0 | 44 | | var unitFilesInLocation = Directory.GetFiles(unitsLocation, "{SNAP}*"); |
| 0 | 45 | | foreach (var unitFile in unitFilesInLocation) |
| 0 | 46 | | { |
| 0 | 47 | | var originFilePath = unitFile.Replace("{SNAP}", snapName); |
| 0 | 48 | | var fileContent = File.ReadAllText(unitFile).Replace("{SNAP}", snapName); |
| 0 | 49 | | File.WriteAllText(Path.Join(destination, originFilePath.Split('/').Last()), fileContent, Encoding.UTF8); |
| 0 | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void UninstallSystemdPathUnit(string snapName) |
| 0 | 54 | | { |
| 0 | 55 | | var destination = Path.Join("/", "usr", "lib", "systemd", "system"); |
| 0 | 56 | | var unitsLocation = Path.Join("/", "snap", "dotnet", "current", "Scripts"); |
| 0 | 57 | | var unitFilesInLocation = Directory.GetFiles(unitsLocation, "{SNAP}*"); |
| 0 | 58 | | foreach (var unitFile in unitFilesInLocation) |
| 0 | 59 | | { |
| 0 | 60 | | var originFilePath = unitFile.Replace("{SNAP}", snapName); |
| 0 | 61 | | File.Delete(Path.Join(destination, originFilePath.Split('/').Last())); |
| 0 | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// The linkage file is a flag that marks that a .NET content snap is present in a system |
| | 67 | | /// and the .NET installer tool is tracking its updates for eventual .mount path updates |
| | 68 | | /// on content snap refreshes. Check each .NET content snap's refresh hook for the code that |
| | 69 | | /// checks for the present of the <c>$SNAP_COMMON/dotnet-installer</c> file. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="contentSnapName">The name of the .NET content snap.</param> |
| | 72 | | /// <returns></returns> |
| | 73 | | public Task PlaceLinkageFile(string contentSnapName) |
| 0 | 74 | | { |
| 0 | 75 | | return File.WriteAllTextAsync( |
| 0 | 76 | | Path.Join("/", "var", "snap", contentSnapName, "common", "dotnet-installer"), |
| 0 | 77 | | "installer linkage ok\n", |
| 0 | 78 | | Encoding.UTF8); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public Task PlaceUnitsFile(string snapConfigDirLocation, string contentSnapName, string units) |
| 0 | 82 | | { |
| 0 | 83 | | var mountsFileName = $"{contentSnapName}.mounts"; |
| 0 | 84 | | return File.WriteAllTextAsync( |
| 0 | 85 | | Path.Join(snapConfigDirLocation, mountsFileName), |
| 0 | 86 | | contents: units, |
| 0 | 87 | | Encoding.UTF8); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public Task<string[]> ReadUnitsFile(string snapConfigDirLocation, string contentSnapName) |
| 0 | 91 | | { |
| 0 | 92 | | var mountsFileName = $"{contentSnapName}.mounts"; |
| 0 | 93 | | return File.ReadAllLinesAsync(Path.Join(snapConfigDirLocation, mountsFileName)); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void DeleteUnitsFile(string snapConfigDirLocation, string contentSnapName) |
| 0 | 97 | | { |
| 0 | 98 | | var mountsFileName = $"{contentSnapName}.mounts"; |
| 0 | 99 | | File.Delete(Path.Join(snapConfigDirLocation, mountsFileName)); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Reads a .version file and returns the .NET version in it. |
| | 104 | | /// </summary> |
| | 105 | | /// <param name="dotNetRoot">The root of the .NET directory hive.</param> |
| | 106 | | /// <param name="componentPath">The relative path to the .NET shared component, |
| | 107 | | /// e.g. <c>shared/Microsoft.NETCore.App</c>, <c>shared/Microsoft.AspNetCore.App</c> or <c>sdk</c>.</param> |
| | 108 | | /// <param name="majorVersion">The .NET major version of the component being analyzed.</param> |
| | 109 | | /// <returns>The .NET version in the .version file.</returns> |
| | 110 | | public DotnetVersion ReadDotVersionFile(string dotNetRoot, string componentPath, int majorVersion) |
| 0 | 111 | | { |
| 0 | 112 | | var location = Path.Join(dotNetRoot, componentPath); |
| 0 | 113 | | foreach (var directory in Directory.EnumerateDirectories(location)) |
| 0 | 114 | | { |
| 0 | 115 | | if (directory.Split(Path.DirectorySeparatorChar).Last().StartsWith(majorVersion.ToString())) |
| 0 | 116 | | { |
| 0 | 117 | | location = Path.Join(directory, ".version"); |
| 0 | 118 | | break; |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | // Search files matching the pattern |
| 0 | 123 | | if (File.Exists(location)) |
| 0 | 124 | | { |
| 0 | 125 | | var lines = File.ReadAllLines(location); |
| | 126 | | // Ensure there are enough lines to read the version string |
| 0 | 127 | | if (lines.Length > 1) |
| 0 | 128 | | { |
| 0 | 129 | | return DotnetVersion.Parse(lines[1]); |
| | 130 | | } |
| 0 | 131 | | } |
| | 132 | |
|
| 0 | 133 | | throw new FileNotFoundException($".version file not found at {location}"); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Iterates recursively through all the directories within <c>root</c> and deletes any empty directories found. |
| | 138 | | /// <c>root</c> will NOT be deleted even if it ends up being an empty directory itself. |
| | 139 | | /// </summary> |
| | 140 | | /// <param name="root">A path to the top-level directory.</param> |
| | 141 | | /// <exception cref="DirectoryNotFoundException">When <c>root</c> does not exist.</exception> |
| | 142 | | public void RemoveEmptyDirectories(string root) |
| 0 | 143 | | { |
| 0 | 144 | | var dir = new DirectoryInfo(root); |
| | 145 | |
|
| 0 | 146 | | if (!dir.Exists) |
| 0 | 147 | | { |
| 0 | 148 | | throw new DirectoryNotFoundException( |
| 0 | 149 | | $"The directory does not exist ({root})." |
| 0 | 150 | | ); |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | foreach (var directory in dir.GetDirectories()) |
| 0 | 154 | | { |
| 0 | 155 | | RemoveEmptyDirectories(directory.FullName); |
| | 156 | |
|
| 0 | 157 | | if (IsDirectoryEmpty(directory.FullName)) |
| 0 | 158 | | { |
| 0 | 159 | | directory.Delete(); |
| 0 | 160 | | } |
| 0 | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | private static bool IsDirectoryEmpty(string path) |
| 0 | 165 | | { |
| 0 | 166 | | return Directory.GetFiles(path).Length == 0 && Directory.GetDirectories(path).Length == 0; |
| 0 | 167 | | } |
| | 168 | | } |