< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.StorageHelpers.StorageHelper
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/StorageHelpers/StorageHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 60
Coverable lines: 60
Total lines: 151
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/42) Branch coverage: 0% (0/6) Total lines: 1084/10/2026 - 12:14:18 AM Line coverage: 0% (0/60) Branch coverage: 0% (0/20) Total lines: 151 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/42) Branch coverage: 0% (0/6) Total lines: 1084/10/2026 - 12:14:18 AM Line coverage: 0% (0/60) Branch coverage: 0% (0/20) Total lines: 151

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
TestCommonPathsForStorageCapacity(...)100%210%
GetFreeSpaceOf(...)0%110100%
ResolvePath(...)0%2040%
TestDataDirectorySize(...)0%2040%
HumanizeStorageSize(...)0%620%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/StorageHelpers/StorageHelper.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.IO;
 4using MediaBrowser.Common.Configuration;
 5using MediaBrowser.Model.System;
 6using Microsoft.Extensions.Logging;
 7
 8namespace 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>
 13public static class StorageHelper
 14{
 15    private const long TwoGigabyte = 2_147_483_647L;
 016    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    {
 025        TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte);
 026        TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte);
 027        TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte);
 028    }
 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.
 040            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.
 045            var allDrives = DriveInfo.GetDrives();
 046            DriveInfo? bestMatch = null;
 047            foreach (DriveInfo d in allDrives)
 48            {
 049                if (resolvedPath.StartsWith(d.RootDirectory.FullName, StringComparison.InvariantCultureIgnoreCase) &&
 050                    (bestMatch is null || d.RootDirectory.FullName.Length > bestMatch.RootDirectory.FullName.Length))
 51                {
 052                    bestMatch = d;
 53                }
 54            }
 55
 056            if (bestMatch is null)
 57            {
 058                throw new InvalidOperationException($"The path `{path}` has no matching parent device. Space check inval
 59            }
 60
 061            return new FolderStorageInfo()
 062            {
 063                Path = path,
 064                ResolvedPath = resolvedPath,
 065                FreeSpace = bestMatch.AvailableFreeSpace,
 066                UsedSpace = bestMatch.TotalSize - bestMatch.AvailableFreeSpace,
 067                StorageType = bestMatch.DriveType.ToString(),
 068                DeviceId = bestMatch.Name,
 069            };
 70        }
 071        catch
 72        {
 073            return new FolderStorageInfo()
 074            {
 075                Path = path,
 076                ResolvedPath = path,
 077                FreeSpace = -1,
 078                UsedSpace = -1,
 079                StorageType = null,
 080                DeviceId = null
 081            };
 82        }
 083    }
 84
 85    /// <summary>
 86    /// Walk a path and fully resolve any symlinks within it.
 87    /// </summary>
 88    private static string ResolvePath(string path)
 89    {
 090        var parts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
 091        var current = Path.DirectorySeparatorChar.ToString();
 092        foreach (var part in parts)
 93        {
 094            current = Path.Combine(current, part);
 095            var resolved = new DirectoryInfo(current).ResolveLinkTarget(returnFinalTarget: true);
 096            if (resolved is not null)
 97            {
 098                current = resolved.FullName;
 99            }
 100        }
 101
 0102        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    {
 0114        logger.LogDebug("Check path {TestPath} for storage capacity", path);
 0115        Directory.CreateDirectory(path);
 116
 0117        var drive = new DriveInfo(path);
 0118        if (threshold != -1 && drive.AvailableFreeSpace < threshold)
 119        {
 0120            throw new InvalidOperationException($"The path `{path}` has insufficient free space. Available: {HumanizeSto
 121        }
 122
 0123        logger.LogInformation(
 0124            "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the mini
 0125            path,
 0126            drive.DriveType,
 0127            HumanizeStorageSize(drive.AvailableFreeSpace),
 0128            HumanizeStorageSize(threshold));
 0129    }
 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    {
 0141        if (byteCount == 0)
 142        {
 0143            return $"0{_byteHumanizedSuffixes[0]}";
 144        }
 145
 0146        var bytes = Math.Abs(byteCount);
 0147        var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
 0148        var num = Math.Round(bytes / Math.Pow(1024, place), 1);
 0149        return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place];
 150    }
 151}