| | 1 | | using System.Security.Cryptography; |
| | 2 | | using CliWrap; |
| | 3 | | using Dotnet.Installer.Core.Exceptions; |
| | 4 | | using Dotnet.Installer.Core.Services.Contracts; |
| | 5 | |
|
| | 6 | | namespace Dotnet.Installer.Core.Services.Implementations; |
| | 7 | |
|
| | 8 | | public class FileService : IFileService |
| | 9 | | { |
| | 10 | | public void DeleteFile(string path) |
| 0 | 11 | | { |
| 0 | 12 | | File.Delete(path); |
| 0 | 13 | | } |
| | 14 | |
|
| | 15 | | public async Task<string> DownloadFile(Uri url, string destinationDirectory) |
| 0 | 16 | | { |
| 0 | 17 | | using var client = new HttpClient(); |
| 0 | 18 | | await using var remoteFileStream = await client.GetStreamAsync(url); |
| | 19 | |
|
| 0 | 20 | | var fileName = Path.Combine(destinationDirectory, url.Segments.Last()); |
| | 21 | |
|
| | 22 | | try |
| 0 | 23 | | { |
| 0 | 24 | | if (File.Exists(fileName)) File.Delete(fileName); |
| 0 | 25 | | await using var writerStream = File.OpenWrite(fileName); |
| 0 | 26 | | await remoteFileStream.CopyToAsync(writerStream); |
| 0 | 27 | | } |
| 0 | 28 | | catch (UnauthorizedAccessException) |
| 0 | 29 | | { |
| 0 | 30 | | throw new NeedsSudoException(fileName); |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | return fileName; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public bool FileExists(string path) |
| 0 | 37 | | { |
| 0 | 38 | | return File.Exists(path); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | public async Task ExtractDeb(string debPath, string destinationDirectory, string snapConfigurationDirectory) |
| 0 | 42 | | { |
| 0 | 43 | | var scratchDirectory = Path.Combine(destinationDirectory, "scratch"); |
| | 44 | |
|
| 0 | 45 | | await Cli.Wrap("dpkg") |
| 0 | 46 | | .WithArguments([ "--extract", debPath, scratchDirectory ]) |
| 0 | 47 | | .WithWorkingDirectory(destinationDirectory) |
| 0 | 48 | | .ExecuteAsync(); |
| | 49 | |
|
| 0 | 50 | | var files = MoveDirectory($"{scratchDirectory}/usr/lib/dotnet", destinationDirectory); |
| | 51 | |
|
| 0 | 52 | | var packageName = Path.GetFileNameWithoutExtension(debPath); |
| 0 | 53 | | await SavePackageContentToFile(files, snapConfigurationDirectory, packageName); |
| | 54 | |
|
| 0 | 55 | | Directory.Delete(scratchDirectory, recursive: true); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task<string> GetFileHash(string filePath) |
| 0 | 59 | | { |
| 0 | 60 | | await using var readerStream = File.OpenRead(filePath); |
| 0 | 61 | | var result = await SHA256.HashDataAsync(readerStream); |
| 0 | 62 | | return Convert.ToHexString(result).ToLower(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public IEnumerable<string> MoveDirectory(string sourceDirectory, string destinationDirectory) |
| 0 | 66 | | { |
| 0 | 67 | | var result = new List<string>(); |
| 0 | 68 | | var dir = new DirectoryInfo(sourceDirectory); |
| | 69 | |
|
| 0 | 70 | | if (!dir.Exists) |
| 0 | 71 | | { |
| 0 | 72 | | throw new DirectoryNotFoundException( |
| 0 | 73 | | $"The source directory does not exist ({sourceDirectory})." |
| 0 | 74 | | ); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | // If the destination directory does not exist, create it. |
| 0 | 78 | | if (!Directory.Exists(destinationDirectory)) |
| 0 | 79 | | { |
| 0 | 80 | | Directory.CreateDirectory(destinationDirectory); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | // Get the files in the directory and copy them to the new location. |
| 0 | 84 | | var files = dir.GetFiles(); |
| 0 | 85 | | foreach (var file in files) |
| 0 | 86 | | { |
| 0 | 87 | | var path = Path.Combine(destinationDirectory, file.Name); |
| 0 | 88 | | result.Add(path); |
| 0 | 89 | | file.MoveTo(path, overwrite: true); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | // Copy subdirectories and their contents to the new location. |
| 0 | 93 | | var subDirs = dir.GetDirectories(); |
| 0 | 94 | | foreach (var subDir in subDirs) |
| 0 | 95 | | { |
| 0 | 96 | | var path = Path.Combine(destinationDirectory, subDir.Name); |
| 0 | 97 | | result.AddRange(MoveDirectory(subDir.FullName, path)); |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | return result; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public Stream OpenRead(string path) |
| 0 | 104 | | { |
| 0 | 105 | | return File.OpenRead(path); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public Task<string[]> ReadAllLines(string fileName) |
| 0 | 109 | | { |
| 0 | 110 | | return File.ReadAllLinesAsync(fileName); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public void RemoveEmptyDirectories(string root) |
| 0 | 114 | | { |
| 0 | 115 | | var dir = new DirectoryInfo(root); |
| | 116 | |
|
| 0 | 117 | | if (!dir.Exists) |
| 0 | 118 | | { |
| 0 | 119 | | throw new DirectoryNotFoundException( |
| 0 | 120 | | $"The directory does not exist ({root})." |
| 0 | 121 | | ); |
| | 122 | | } |
| | 123 | |
|
| | 124 | | // Recursively search for empty directories and remove them |
| 0 | 125 | | foreach (var directory in dir.GetDirectories()) |
| 0 | 126 | | { |
| 0 | 127 | | RemoveEmptyDirectories(directory.FullName); |
| | 128 | |
|
| 0 | 129 | | if (IsDirectoryEmpty(directory.FullName)) |
| 0 | 130 | | { |
| 0 | 131 | | directory.Delete(); |
| 0 | 132 | | } |
| 0 | 133 | | } |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | private static bool IsDirectoryEmpty(string path) |
| 0 | 137 | | { |
| 0 | 138 | | return Directory.GetFiles(path).Length == 0 && Directory.GetDirectories(path).Length == 0; |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private static async Task SavePackageContentToFile(IEnumerable<string> files, string snapConfigurationDirectory, str |
| 0 | 142 | | { |
| 0 | 143 | | var registrationFile = Path.Combine(snapConfigurationDirectory, $"{packageName}.files"); |
| 0 | 144 | | await File.WriteAllLinesAsync(registrationFile, files); |
| 0 | 145 | | } |
| | 146 | | } |