| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using MediaBrowser.Common.Configuration; |
| | | 5 | | using MediaBrowser.Model.System; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace Jellyfin.Server.Implementations.StorageHelpers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Contains methods to help with checking for storage and returning storage data for jellyfin folders. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class StorageHelper |
| | | 14 | | { |
| | | 15 | | private const long TwoGigabyte = 2_147_483_647L; |
| | 0 | 16 | | private static readonly string[] _byteHumanizedSuffixes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Tests the available storage capacity on the jellyfin paths with estimated minimum values. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="applicationPaths">The application paths.</param> |
| | | 22 | | /// <param name="logger">Logger.</param> |
| | | 23 | | public static void TestCommonPathsForStorageCapacity(IApplicationPaths applicationPaths, ILogger logger) |
| | | 24 | | { |
| | 0 | 25 | | TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte); |
| | 0 | 26 | | TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte); |
| | 0 | 27 | | TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the free space of the parent filesystem of a specific directory. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="path">Path to a folder.</param> |
| | | 34 | | /// <returns>Various details about the parent filesystem containing the directory.</returns> |
| | | 35 | | public static FolderStorageInfo GetFreeSpaceOf(string path) |
| | | 36 | | { |
| | | 37 | | try |
| | | 38 | | { |
| | | 39 | | // Fully resolve the given path to an actual filesystem target, in case it's a symlink or similar. |
| | 0 | 40 | | var resolvedPath = ResolvePath(path); |
| | | 41 | | // We iterate all filesystems reported by GetDrives() here, and attempt to find the best |
| | | 42 | | // match that contains, as deep as possible, the given path. |
| | | 43 | | // This is required because simply calling `DriveInfo` on a path returns that path as |
| | | 44 | | // the Name and RootDevice, which is not at all how this should work. |
| | 0 | 45 | | var allDrives = DriveInfo.GetDrives(); |
| | 0 | 46 | | DriveInfo? bestMatch = null; |
| | 0 | 47 | | foreach (DriveInfo d in allDrives) |
| | | 48 | | { |
| | 0 | 49 | | if (resolvedPath.StartsWith(d.RootDirectory.FullName, StringComparison.InvariantCultureIgnoreCase) && |
| | 0 | 50 | | (bestMatch is null || d.RootDirectory.FullName.Length > bestMatch.RootDirectory.FullName.Length)) |
| | | 51 | | { |
| | 0 | 52 | | bestMatch = d; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | if (bestMatch is null) |
| | | 57 | | { |
| | 0 | 58 | | throw new InvalidOperationException($"The path `{path}` has no matching parent device. Space check inval |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return new FolderStorageInfo() |
| | 0 | 62 | | { |
| | 0 | 63 | | Path = path, |
| | 0 | 64 | | ResolvedPath = resolvedPath, |
| | 0 | 65 | | FreeSpace = bestMatch.AvailableFreeSpace, |
| | 0 | 66 | | UsedSpace = bestMatch.TotalSize - bestMatch.AvailableFreeSpace, |
| | 0 | 67 | | StorageType = bestMatch.DriveType.ToString(), |
| | 0 | 68 | | DeviceId = bestMatch.Name, |
| | 0 | 69 | | }; |
| | | 70 | | } |
| | 0 | 71 | | catch |
| | | 72 | | { |
| | 0 | 73 | | return new FolderStorageInfo() |
| | 0 | 74 | | { |
| | 0 | 75 | | Path = path, |
| | 0 | 76 | | ResolvedPath = path, |
| | 0 | 77 | | FreeSpace = -1, |
| | 0 | 78 | | UsedSpace = -1, |
| | 0 | 79 | | StorageType = null, |
| | 0 | 80 | | DeviceId = null |
| | 0 | 81 | | }; |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Walk a path and fully resolve any symlinks within it. |
| | | 87 | | /// </summary> |
| | | 88 | | private static string ResolvePath(string path) |
| | | 89 | | { |
| | 0 | 90 | | var parts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 91 | | var current = Path.DirectorySeparatorChar.ToString(); |
| | 0 | 92 | | foreach (var part in parts) |
| | | 93 | | { |
| | 0 | 94 | | current = Path.Combine(current, part); |
| | 0 | 95 | | var resolved = new DirectoryInfo(current).ResolveLinkTarget(returnFinalTarget: true); |
| | 0 | 96 | | if (resolved is not null) |
| | | 97 | | { |
| | 0 | 98 | | current = resolved.FullName; |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | return current; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the underlying drive data from a given path and checks if the available storage capacity matches the thresh |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="path">The path to a folder to evaluate.</param> |
| | | 109 | | /// <param name="logger">The logger.</param> |
| | | 110 | | /// <param name="threshold">The threshold to check for or -1 to just log the data.</param> |
| | | 111 | | /// <exception cref="InvalidOperationException">Thrown when the threshold is not available on the underlying storage |
| | | 112 | | private static void TestDataDirectorySize(string path, ILogger logger, long threshold = -1) |
| | | 113 | | { |
| | 0 | 114 | | logger.LogDebug("Check path {TestPath} for storage capacity", path); |
| | 0 | 115 | | Directory.CreateDirectory(path); |
| | | 116 | | |
| | 0 | 117 | | var drive = new DriveInfo(path); |
| | 0 | 118 | | if (threshold != -1 && drive.AvailableFreeSpace < threshold) |
| | | 119 | | { |
| | 0 | 120 | | throw new InvalidOperationException($"The path `{path}` has insufficient free space. Available: {HumanizeSto |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | logger.LogInformation( |
| | 0 | 124 | | "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the mini |
| | 0 | 125 | | path, |
| | 0 | 126 | | drive.DriveType, |
| | 0 | 127 | | HumanizeStorageSize(drive.AvailableFreeSpace), |
| | 0 | 128 | | HumanizeStorageSize(threshold)); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Formats a size in bytes into a common human readable form. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <remarks> |
| | | 135 | | /// Taken and slightly modified from https://stackoverflow.com/a/4975942/1786007 . |
| | | 136 | | /// </remarks> |
| | | 137 | | /// <param name="byteCount">The size in bytes.</param> |
| | | 138 | | /// <returns>A human readable approximate representation of the argument.</returns> |
| | | 139 | | public static string HumanizeStorageSize(long byteCount) |
| | | 140 | | { |
| | 0 | 141 | | if (byteCount == 0) |
| | | 142 | | { |
| | 0 | 143 | | return $"0{_byteHumanizedSuffixes[0]}"; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | var bytes = Math.Abs(byteCount); |
| | 0 | 147 | | var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); |
| | 0 | 148 | | var num = Math.Round(bytes / Math.Pow(1024, place), 1); |
| | 0 | 149 | | return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place]; |
| | | 150 | | } |
| | | 151 | | } |