Skip to content

Commit

Permalink
Handle paths without DriveInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 7, 2025
1 parent 5f37065 commit 2d5d05e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 30 deletions.
19 changes: 14 additions & 5 deletions Cmdline/Action/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,20 @@ private void printCacheInfo()
{
if (manager?.Cache != null)
{
manager.Cache.GetSizeInfo(out int fileCount, out long bytes, out long bytesFree);
user?.RaiseMessage(Properties.Resources.CacheInfo,
fileCount,
CkanModule.FmtSize(bytes),
CkanModule.FmtSize(bytesFree));
manager.Cache.GetSizeInfo(out int fileCount, out long bytes, out long? bytesFree);
if (bytesFree.HasValue)
{
user?.RaiseMessage(Properties.Resources.CacheInfo,
fileCount,
CkanModule.FmtSize(bytes),
CkanModule.FmtSize(bytesFree.Value));
}
else
{
user?.RaiseMessage(Properties.Resources.CacheInfoFreeSpaceUnknown,
fileCount,
CkanModule.FmtSize(bytes));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Cmdline/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ If you must run as an administrator, the `--asroot` parameter will bypass this c
<data name="CacheResetFailed" xml:space="preserve"><value>Can't reset cache path: {0}</value></data>
<data name="CacheUnlimited" xml:space="preserve"><value>Unlimited</value></data>
<data name="CacheInfo" xml:space="preserve"><value>{0} files, {1}, {2} free</value></data>
<data name="CacheInfoFreeSpaceUnknown" xml:space="preserve"><value>{0} files, {1}, free space unknown</value></data>
<data name="CompareSame" xml:space="preserve"><value>"{0}" and "{1}" are the same versions.</value></data>
<data name="CompareLower" xml:space="preserve"><value>"{0}" is lower than "{1}".</value></data>
<data name="CompareHigher" xml:space="preserve"><value>"{0}" is higher than "{1}".</value></data>
Expand Down
4 changes: 2 additions & 2 deletions Core/CKANPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public static void CheckFreeSpace(DirectoryInfo where,
long bytesToStore,
string errorDescription)
{
if (bytesToStore > 0)
if (bytesToStore > 0
&& where.GetDrive()?.AvailableFreeSpace is long bytesFree)
{
var bytesFree = where.GetDrive().AvailableFreeSpace;
if (bytesToStore > bytesFree) {
throw new NotEnoughSpaceKraken(errorDescription, where,
bytesFree, bytesToStore);
Expand Down
4 changes: 2 additions & 2 deletions Core/Extensions/IOExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static class IOExtensions
/// </summary>
/// <param name="dir">Any DirectoryInfo object</param>
/// <returns>The DriveInfo associated with this directory, if any, else null</returns>
public static DriveInfo GetDrive(this DirectoryInfo dir)
=> new DriveInfo(dir.FullName);
public static DriveInfo? GetDrive(this DirectoryInfo dir)
=> Utilities.DefaultIfThrows(() => new DriveInfo(dir.FullName));

/// <summary>
/// A version of Stream.CopyTo with progress updates.
Expand Down
15 changes: 8 additions & 7 deletions Core/GameInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,23 +615,24 @@ public bool TrySetupCache(string? path,
}
else
{
// Make sure we can access it
var bytesFree = new DirectoryInfo(path).GetDrive().AvailableFreeSpace;
Cache = new NetModuleCache(this, path);
Configuration.DownloadCacheDir = path;
}
if (origPath != null && origCache != null)
{
origCache.GetSizeInfo(out _, out long oldNumBytes, out _);
Cache.GetSizeInfo(out _, out _, out long bytesFree);
Cache.GetSizeInfo(out _, out _, out long? bytesFree);

if (oldNumBytes > 0)
{
switch (User.RaiseSelectionDialog(
string.Format(Properties.Resources.GameInstanceManagerCacheMigrationPrompt,
CkanModule.FmtSize(oldNumBytes),
CkanModule.FmtSize(bytesFree)),
oldNumBytes < bytesFree ? 0 : 2,
bytesFree.HasValue
? string.Format(Properties.Resources.GameInstanceManagerCacheMigrationPrompt,
CkanModule.FmtSize(oldNumBytes),
CkanModule.FmtSize(bytesFree.Value))
: string.Format(Properties.Resources.GameInstanceManagerCacheMigrationPromptFreeSpaceUnknown,
CkanModule.FmtSize(oldNumBytes)),
oldNumBytes < (bytesFree ?? 0) ? 0 : 2,
Properties.Resources.GameInstanceManagerCacheMigrationMove,
Properties.Resources.GameInstanceManagerCacheMigrationDelete,
Properties.Resources.GameInstanceManagerCacheMigrationOpen,
Expand Down
9 changes: 3 additions & 6 deletions Core/Net/NetFileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public NetFileCache(string path)
}
inProgressPath = new DirectoryInfo(Path.Combine(path, "downloading"));

// Make sure we can access it
var bytesFree = cachePath.GetDrive().AvailableFreeSpace;

// Establish a watch on our cache. This means we can cache the directory contents,
// and discard that cache if we spot changes.
watcher = new FileSystemWatcher(cachePath.FullName, "*.zip")
Expand Down Expand Up @@ -257,9 +254,9 @@ public bool IsMaybeCachedZip(Uri url, DateTime? remoteTimestamp = null)
/// <param name="numFiles">Output parameter set to number of files in cache</param>
/// <param name="numBytes">Output parameter set to number of bytes in cache</param>
/// <param name="bytesFree">Output parameter set to number of bytes free</param>
public void GetSizeInfo(out int numFiles, out long numBytes, out long bytesFree)
public void GetSizeInfo(out int numFiles, out long numBytes, out long? bytesFree)
{
bytesFree = cachePath.GetDrive().AvailableFreeSpace;
bytesFree = cachePath.GetDrive()?.AvailableFreeSpace;
(numFiles, numBytes) = Enumerable.Repeat(cachePath, 1)
.Concat(legacyDirs())
.Select(GetDirSizeInfo)
Expand Down Expand Up @@ -292,7 +289,7 @@ private IEnumerable<DirectoryInfo> legacyDirs()

public void EnforceSizeLimit(long bytes, Registry registry)
{
GetSizeInfo(out int numFiles, out long curBytes, out long _);
GetSizeInfo(out int numFiles, out long curBytes, out _);
if (curBytes > bytes)
{
// This object will let us determine whether a module is compatible with any of our instances
Expand Down
2 changes: 1 addition & 1 deletion Core/Net/NetModuleCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public bool IsMaybeCachedZip(CkanModule m)
public string? GetCachedFilename(CkanModule m)
=> m.download?.Select(dlUri => cache.GetCachedFilename(dlUri, m.release_date))
.FirstOrDefault(filename => filename != null);
public void GetSizeInfo(out int numFiles, out long numBytes, out long bytesFree)
public void GetSizeInfo(out int numFiles, out long numBytes, out long? bytesFree)
{
cache.GetSizeInfo(out numFiles, out numBytes, out bytesFree);
}
Expand Down
2 changes: 2 additions & 0 deletions Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Install the `mono-complete` package or equivalent for your operating system.</va
<data name="GameInstanceManagerAuto" xml:space="preserve"><value>Auto {0}</value></data>
<data name="GameInstanceManagerSelectGamePrompt" xml:space="preserve"><value>Please select the game that is installed at {0}</value></data>
<data name="GameInstanceManagerCacheMigrationPrompt" xml:space="preserve"><value>Old cache folder contains {0}. New cache folder has {1} free.
What would you like to do?</value></data>
<data name="GameInstanceManagerCacheMigrationPromptFreeSpaceUnknown" xml:space="preserve"><value>Old cache folder contains {0}. New cache folder has unknown free space.
What would you like to do?</value></data>
<data name="GameInstanceManagerCacheMigrationMove" xml:space="preserve"><value>Move old cached files to new cache folder</value></data>
<data name="GameInstanceManagerCacheMigrationDelete" xml:space="preserve"><value>Delete cached files</value></data>
Expand Down
18 changes: 11 additions & 7 deletions GUI/Dialogs/SettingsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ private void UpdateCacheInfo(string newPath)
{
try
{
manager.Cache.GetSizeInfo(out int cacheFileCount,
out long cacheSize,
out long cacheFreeSpace);
manager.Cache.GetSizeInfo(out int cacheFileCount,
out long cacheSize,
out long? cacheFreeSpace);

Util.Invoke(this, () =>
{
Expand All @@ -116,10 +116,14 @@ private void UpdateCacheInfo(string newPath)
// Show setting in MiB
CacheLimit.Text = (coreConfig.CacheSizeLimit.Value / 1024 / 1024).ToString();
}
CacheSummary.Text = string.Format(Properties.Resources.SettingsDialogSummmary,
cacheFileCount,
CkanModule.FmtSize(cacheSize),
CkanModule.FmtSize(cacheFreeSpace));
CacheSummary.Text = cacheFreeSpace.HasValue
? string.Format(Properties.Resources.SettingsDialogSummmary,
cacheFileCount,
CkanModule.FmtSize(cacheSize),
CkanModule.FmtSize(cacheFreeSpace.Value))
: string.Format(Properties.Resources.SettingsDialogSummmaryFreeSpaceUnknown,
cacheFileCount,
CkanModule.FmtSize(cacheSize));
CacheSummary.ForeColor = SystemColors.ControlText;
OpenCacheButton.Enabled = true;
ClearCacheButton.Enabled = (cacheSize > 0);
Expand Down
1 change: 1 addition & 0 deletions GUI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ Find the folder where your game is installed and choose one of these files:
<data name="SettingsToolTipOpenCacheButton" xml:space="preserve"><value>Browse the cache folder</value></data>
<data name="SettingsToolTipClearCacheButton" xml:space="preserve"><value>Delete files from the cache</value></data>
<data name="SettingsDialogSummmary" xml:space="preserve"><value>{0} files, {1}, {2} free</value></data>
<data name="SettingsDialogSummmaryFreeSpaceUnknown" xml:space="preserve"><value>{0} files, {1}, free space unknown</value></data>
<data name="SettingsDialogSummaryInvalid" xml:space="preserve"><value>Invalid path: {0}</value></data>
<data name="SettingsDialogCacheDescrip" xml:space="preserve"><value>Choose a folder for storing CKAN's mod downloads:</value></data>
<data name="SettingsDialogDeleteConfirm" xml:space="preserve"><value>Do you really want to delete {0} cached files, freeing {1}?</value></data>
Expand Down

0 comments on commit 2d5d05e

Please sign in to comment.