From bb7c1fcbfcb7a2493115c76a897d2a2dcb359782 Mon Sep 17 00:00:00 2001 From: drweb86 <4712173+drweb86@users.noreply.github.com> Date: Mon, 29 Jul 2024 21:40:04 +0300 Subject: [PATCH] #2024.07.29 - Reduced storage validation errors message size. --- help/Version History (Changelog).md | 10 ++++++ sources/BUtil.Core/Logs/FileLog.cs | 5 +-- sources/BUtil.Core/Misc/ExceptionHelper.cs | 30 ++++++++++++++++++ sources/BUtil.Core/Misc/ExecuteFailover.cs | 8 ++--- sources/BUtil.Core/Misc/ImproveIt.cs | 2 +- .../Storages/FailoverStorageWrapper.cs | 31 ++++++++++++------- sources/BUtil.Core/Storages/FolderStorage.cs | 10 +----- sources/BUtil.Core/Storages/FtpsStorage.cs | 14 ++------- sources/BUtil.Core/Storages/IStorage.cs | 2 +- sources/BUtil.Core/Storages/StorageBase.cs | 2 +- sources/BUtil.Core/Storages/StorageFactory.cs | 24 +++++++++----- sources/BUtil.Core/Storages/StorageHelper.cs | 16 ++++++++++ .../BUtil.Linux/Services/LinuxSambaStorage.cs | 4 +-- sources/BUtil.Windows/Services/MtpStorage.cs | 12 +------ .../Services/WindowsSambaStorage.cs | 12 +------ .../EditMediaTask/EditMediaTaskViewModel.cs | 3 +- sources/ubuntu-install.sh | 2 +- 17 files changed, 113 insertions(+), 74 deletions(-) create mode 100644 sources/BUtil.Core/Misc/ExceptionHelper.cs create mode 100644 sources/BUtil.Core/Storages/StorageHelper.cs diff --git a/help/Version History (Changelog).md b/help/Version History (Changelog).md index 17128227..ab428023 100644 --- a/help/Version History (Changelog).md +++ b/help/Version History (Changelog).md @@ -1,3 +1,13 @@ +# 2024.07.29 + +## Changes + +- Reduced storage validation errors message size. + +## Bug Fixes + +- FTPS validation could prevent creation of task. + # 2024.07.16 ## Changes diff --git a/sources/BUtil.Core/Logs/FileLog.cs b/sources/BUtil.Core/Logs/FileLog.cs index 35e09fe6..dba07fdb 100644 --- a/sources/BUtil.Core/Logs/FileLog.cs +++ b/sources/BUtil.Core/Logs/FileLog.cs @@ -1,5 +1,6 @@  using BUtil.Core.Localization; +using BUtil.Core.Misc; using System; using System.Globalization; using System.IO; @@ -40,7 +41,7 @@ public FileLog(string taskName) } catch (ArgumentException e) { - throw new LogException(e.Message); + throw new LogException(ExceptionHelper.ToString(e)); } _taskName = taskName; } @@ -61,7 +62,7 @@ public override void Open() } catch (Exception e) { - throw new LogException(e.Message, e); + throw new LogException(ExceptionHelper.ToString(e), e); } } diff --git a/sources/BUtil.Core/Misc/ExceptionHelper.cs b/sources/BUtil.Core/Misc/ExceptionHelper.cs new file mode 100644 index 00000000..b2987e6b --- /dev/null +++ b/sources/BUtil.Core/Misc/ExceptionHelper.cs @@ -0,0 +1,30 @@ +using System; +using System.Text; + +namespace BUtil.Core.Misc; + +public static class ExceptionHelper +{ + public static string ToString(Exception ex) + { + var builder = new StringBuilder(); + builder.Append(ex.Message); + + int depth = 5; + Exception? innerException; + + do + { + depth--; + innerException = ex.InnerException; + if (innerException == null) + break; + + builder.AppendLine(); + builder.Append(innerException.Message); + } + while (depth > 0); + + return builder.ToString(); + } +} diff --git a/sources/BUtil.Core/Misc/ExecuteFailover.cs b/sources/BUtil.Core/Misc/ExecuteFailover.cs index 48ba8909..3c636d02 100644 --- a/sources/BUtil.Core/Misc/ExecuteFailover.cs +++ b/sources/BUtil.Core/Misc/ExecuteFailover.cs @@ -17,10 +17,10 @@ public static void TryNTimes(Action logError, Action func, int times = 1 catch (Exception e) { logError(e.ToString()); + if (--times <= 0) + throw new InvalidOperationException("No more attempts left"); logError("Retrying in 30 seconds"); Thread.Sleep(30 * 1000); - if (--times <= 0) - throw; } } } @@ -36,10 +36,10 @@ public static T TryNTimes(Action logError, Func func, int times = catch (Exception e) { logError(e.ToString()); + if (--times <= 0) + throw new InvalidOperationException("No more attempts left"); logError("Retrying in 30 seconds"); Thread.Sleep(30 * 1000); - if (--times <= 0) - throw; } } throw new NotImplementedException(); diff --git a/sources/BUtil.Core/Misc/ImproveIt.cs b/sources/BUtil.Core/Misc/ImproveIt.cs index afc9786c..bd92766d 100644 --- a/sources/BUtil.Core/Misc/ImproveIt.cs +++ b/sources/BUtil.Core/Misc/ImproveIt.cs @@ -31,7 +31,7 @@ public static void ProcessUnhandledException(Exception exception) builder.AppendLine("BUtil " + CopyrightInfo.Version + " - Bug report (" + DateTime.Now.ToString("g", CultureInfo.InvariantCulture) + ")"); builder.AppendLine("Please report about it here: "); builder.AppendLine(ApplicationLinks.HomePage); - builder.AppendLine(exception.Message); + builder.AppendLine(ExceptionHelper.ToString(exception)); builder.AppendLine(exception.StackTrace); builder.AppendLine(exception.Source); diff --git a/sources/BUtil.Core/Storages/FailoverStorageWrapper.cs b/sources/BUtil.Core/Storages/FailoverStorageWrapper.cs index 7dfa05b9..0c99b4d2 100644 --- a/sources/BUtil.Core/Storages/FailoverStorageWrapper.cs +++ b/sources/BUtil.Core/Storages/FailoverStorageWrapper.cs @@ -6,10 +6,11 @@ namespace BUtil.Core.Storages; -public class FailoverStorageWrapper(ILog log, IStorage storage) : IStorage +public class FailoverStorageWrapper(ILog log, IStorage storage, int? triesCount) : IStorage { private readonly ILog _log = log; private readonly IStorage _storage = storage; + private readonly int _triesCount = triesCount ?? 10; public void Delete(string file) { @@ -29,60 +30,68 @@ public void Download(string relativeFileName, string targetFileName) { ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Download \"{relativeFileName}\" to \"{targetFileName}\": {error}"), - () => _storage.Download(relativeFileName, targetFileName)); + () => _storage.Download(relativeFileName, targetFileName), + _triesCount); } public bool Exists(string relativeFileName) { return ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Exists \"{relativeFileName}\": {error}"), - () => _storage.Exists(relativeFileName)); + () => _storage.Exists(relativeFileName), + _triesCount); } public void DeleteFolder(string relativeFolderName) { ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Delete folder \"{relativeFolderName}\": {error}"), - () => _storage.DeleteFolder(relativeFolderName)); + () => _storage.DeleteFolder(relativeFolderName), + _triesCount); } public string[] GetFolders(string relativeFolderName, string? mask = null) { return ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Get folders \"{relativeFolderName}\" by mask \"{mask}\": {error}"), - () => _storage.GetFolders(relativeFolderName, mask)); + () => _storage.GetFolders(relativeFolderName, mask), + _triesCount); } - public string? Test(bool writeMode) + public string? Test() { - return _storage.Test(writeMode); + return _storage.Test(); } public IStorageUploadResult Upload(string sourceFile, string relativeFileName) { return ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Upload \"{sourceFile}\" to \"{relativeFileName}\": {error}"), - () => _storage.Upload(sourceFile, relativeFileName)); + () => _storage.Upload(sourceFile, relativeFileName), + _triesCount); } public string[] GetFiles(string? relativeFolderName = null, SearchOption option = SearchOption.TopDirectoryOnly) { return ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Get files \"{relativeFolderName}\" with option \"{option}\": {error}"), - () => _storage.GetFiles(relativeFolderName, option)); + () => _storage.GetFiles(relativeFolderName, option), + _triesCount); } public DateTime GetModifiedTime(string relativeFileName) { return ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Get file \"{relativeFileName}\" modified date: {error}"), - () => _storage.GetModifiedTime(relativeFileName)); + () => _storage.GetModifiedTime(relativeFileName), + _triesCount); } public void Move(string fromRelativeFileName, string toRelativeFileName) { ExecuteFailover.TryNTimes( error => _log.WriteLine(LoggingEvent.Error, $"Move file \"{fromRelativeFileName}\" to \"{toRelativeFileName}\": {error}"), - () => _storage.Move(fromRelativeFileName, toRelativeFileName)); + () => _storage.Move(fromRelativeFileName, toRelativeFileName), + _triesCount); } } diff --git a/sources/BUtil.Core/Storages/FolderStorage.cs b/sources/BUtil.Core/Storages/FolderStorage.cs index e5b917a1..dc008352 100644 --- a/sources/BUtil.Core/Storages/FolderStorage.cs +++ b/sources/BUtil.Core/Storages/FolderStorage.cs @@ -63,21 +63,13 @@ private void Unmount() } } - public override string? Test(bool writeMode) + public override string? Test() { if (!Directory.Exists(Settings.DestinationFolder)) return string.Format(Localization.Resources.DirectoryStorage_Field_Directory_Validation_NotFound, Settings.DestinationFolder); if (!Path.IsPathFullyQualified(Settings.DestinationFolder)) return string.Format(Localization.Resources.DirectoryStorage_Field_Directory_Validation_NotFound, Settings.DestinationFolder); - if (writeMode) - { - var folder = Guid.NewGuid().ToString(); - var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString()); - _ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!"); - DeleteFolder(folder); - } - return null; } diff --git a/sources/BUtil.Core/Storages/FtpsStorage.cs b/sources/BUtil.Core/Storages/FtpsStorage.cs index 42438323..e458de0b 100644 --- a/sources/BUtil.Core/Storages/FtpsStorage.cs +++ b/sources/BUtil.Core/Storages/FtpsStorage.cs @@ -1,12 +1,12 @@  using BUtil.Core.ConfigurationFileModels.V2; using BUtil.Core.Logs; +using BUtil.Core.Misc; using FluentFTP; using System; using System.IO; using System.Linq; using System.Net.Security; -using System.Reflection; using System.Security; using System.Text.RegularExpressions; @@ -137,7 +137,7 @@ private void Unmount() _client.Dispose(); } - public override string? Test(bool writeMode) + public override string? Test() { if (!string.IsNullOrWhiteSpace(Settings.Folder) && !_client.DirectoryExists(Settings.Folder)) { @@ -151,15 +151,7 @@ private void Unmount() } catch (Exception e) { - return e.Message; - } - - if (writeMode) - { - var folder = Guid.NewGuid().ToString(); - var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString()); - _ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!"); - DeleteFolder(folder); + return ExceptionHelper.ToString(e); } } return null; diff --git a/sources/BUtil.Core/Storages/IStorage.cs b/sources/BUtil.Core/Storages/IStorage.cs index ed4d0dd3..d6950865 100644 --- a/sources/BUtil.Core/Storages/IStorage.cs +++ b/sources/BUtil.Core/Storages/IStorage.cs @@ -15,5 +15,5 @@ public interface IStorage : IDisposable string[] GetFolders(string relativeFolderName, string? mask = null); string[] GetFiles(string? relativeFolderName = null, SearchOption option = SearchOption.TopDirectoryOnly); DateTime GetModifiedTime(string relativeFileName); - string? Test(bool writeMode); + string? Test(); } diff --git a/sources/BUtil.Core/Storages/StorageBase.cs b/sources/BUtil.Core/Storages/StorageBase.cs index 7d9713be..fd7969ed 100644 --- a/sources/BUtil.Core/Storages/StorageBase.cs +++ b/sources/BUtil.Core/Storages/StorageBase.cs @@ -11,7 +11,7 @@ public abstract class StorageBase(ILog log, TStorageSettings s protected readonly TStorageSettings Settings = settings; public abstract IStorageUploadResult Upload(string sourceFile, string relativeFileName); - public abstract string? Test(bool writeMode); + public abstract string? Test(); public abstract void Delete(string relativeFileName); public abstract void DeleteFolder(string relativeFolderName); diff --git a/sources/BUtil.Core/Storages/StorageFactory.cs b/sources/BUtil.Core/Storages/StorageFactory.cs index ce385e3a..5bd835bf 100644 --- a/sources/BUtil.Core/Storages/StorageFactory.cs +++ b/sources/BUtil.Core/Storages/StorageFactory.cs @@ -1,23 +1,24 @@ using BUtil.Core.ConfigurationFileModels.V2; using BUtil.Core.Logs; +using BUtil.Core.Misc; using System; namespace BUtil.Core.Storages; public class StorageFactory { - public static IStorage Create(ILog log, IStorageSettingsV2 storageSettings, bool autodetectConnectionSettings) + public static IStorage Create(ILog log, IStorageSettingsV2 storageSettings, bool autodetectConnectionSettings, int? triesCount = null) { if (storageSettings is FolderStorageSettingsV2 folder) - return new FailoverStorageWrapper(log, new FolderStorage(log, folder)); + return new FailoverStorageWrapper(log, new FolderStorage(log, folder), triesCount); else if (storageSettings is SambaStorageSettingsV2 samba) - return new FailoverStorageWrapper(log, PlatformSpecificExperience.Instance.GetSmbStorage(log, samba)); + return new FailoverStorageWrapper(log, PlatformSpecificExperience.Instance.GetSmbStorage(log, samba), triesCount); else if (storageSettings is FtpsStorageSettingsV2 ftps) - return new FailoverStorageWrapper(log, new FtpsStorage(log, ftps, autodetectConnectionSettings)); + return new FailoverStorageWrapper(log, new FtpsStorage(log, ftps, autodetectConnectionSettings), triesCount); else if (storageSettings is MtpStorageSettings mtp) { var mtpStorage = PlatformSpecificExperience.Instance.GetMtpStorage(log, mtp) ?? throw new NotSupportedException("Your OS does not support MTP storage"); - return new FailoverStorageWrapper(log, mtpStorage); + return new FailoverStorageWrapper(log, mtpStorage, triesCount); } throw new ArgumentOutOfRangeException(nameof(storageSettings)); } @@ -32,12 +33,19 @@ public static IStorage Create(ILog log, IStorageSettingsV2 storageSettings, bool try { - using var storage = Create(log, storageSettings, true); - return storage.Test(writeMode); + using var storage = Create(log, storageSettings, true, 1); + var readonlyChecksError = storage.Test(); + if (!string.IsNullOrWhiteSpace(readonlyChecksError)) + return readonlyChecksError; + + if (writeMode) + StorageHelper.WriteTest(storage); + + return null; } catch (Exception ex) { - return ex.Message; + return ExceptionHelper.ToString(ex); } } } diff --git a/sources/BUtil.Core/Storages/StorageHelper.cs b/sources/BUtil.Core/Storages/StorageHelper.cs new file mode 100644 index 00000000..82d859c4 --- /dev/null +++ b/sources/BUtil.Core/Storages/StorageHelper.cs @@ -0,0 +1,16 @@ +using System; +using System.IO; +using System.Reflection; + +namespace BUtil.Core.Storages; + +public class StorageHelper +{ + public static void WriteTest(IStorage storage) + { + var folder = "BUtil check " + Guid.NewGuid().ToString(); + var file = Path.Combine(folder, Guid.NewGuid().ToString()); + storage.Upload(Assembly.GetExecutingAssembly().Location, file); + storage.DeleteFolder(folder); + } +} diff --git a/sources/BUtil.Linux/Services/LinuxSambaStorage.cs b/sources/BUtil.Linux/Services/LinuxSambaStorage.cs index 0ad8e6c0..e81b87a2 100644 --- a/sources/BUtil.Linux/Services/LinuxSambaStorage.cs +++ b/sources/BUtil.Linux/Services/LinuxSambaStorage.cs @@ -66,9 +66,9 @@ killall gvfsd }); } - public override string? Test(bool writeMode) + public override string? Test() { - return _proxy.Test(writeMode); + return _proxy.Test(); } public override IStorageUploadResult Upload(string sourceFile, string relativeFileName) diff --git a/sources/BUtil.Windows/Services/MtpStorage.cs b/sources/BUtil.Windows/Services/MtpStorage.cs index 67fcbc27..97f16536 100644 --- a/sources/BUtil.Windows/Services/MtpStorage.cs +++ b/sources/BUtil.Windows/Services/MtpStorage.cs @@ -3,8 +3,6 @@ using BUtil.Core.Logs; using BUtil.Core.Storages; using MediaDevices; -using System.Diagnostics; -using System.Reflection; using System.Security; namespace BUtil.Windows.Services; @@ -78,21 +76,13 @@ private void Unmount() _mediaDevice?.Dispose(); } - public override string? Test(bool writeMode) + public override string? Test() { if (!_mediaDevice.DirectoryExists(Settings.Folder)) { return BUtil.Core.Localization.Resources.Field_Folder_Validation_NotExist; } - if (writeMode) - { - var folder = Guid.NewGuid().ToString(); - var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString()); - _ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!"); - DeleteFolder(folder); - } - return null; } diff --git a/sources/BUtil.Windows/Services/WindowsSambaStorage.cs b/sources/BUtil.Windows/Services/WindowsSambaStorage.cs index ed8e8dae..1df436c2 100644 --- a/sources/BUtil.Windows/Services/WindowsSambaStorage.cs +++ b/sources/BUtil.Windows/Services/WindowsSambaStorage.cs @@ -3,8 +3,6 @@ using BUtil.Core.Logs; using BUtil.Core.Storages; using BUtil.Windows.Utils; -using System.IO; -using System.Reflection; using System.Security; namespace BUtil.Windows.Services; @@ -92,19 +90,11 @@ private void Unmount() throw new InvalidOperationException($"Cannot unmount"); } - public override string? Test(bool writeMode) + public override string? Test() { if (!Directory.Exists(Settings.Url)) return string.Format(Resources.DirectoryStorage_Field_Directory_Validation_NotFound, Settings.Url); ; - if (writeMode) - { - var folder = Guid.NewGuid().ToString(); - var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString()); - _ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!"); - DeleteFolder(folder); - } - return null; } diff --git a/sources/butil-ui/Controls/EditMediaTask/EditMediaTaskViewModel.cs b/sources/butil-ui/Controls/EditMediaTask/EditMediaTaskViewModel.cs index 1616559a..3ba87c97 100644 --- a/sources/butil-ui/Controls/EditMediaTask/EditMediaTaskViewModel.cs +++ b/sources/butil-ui/Controls/EditMediaTask/EditMediaTaskViewModel.cs @@ -1,5 +1,6 @@ using BUtil.Core.ConfigurationFileModels.V2; using BUtil.Core.Localization; +using BUtil.Core.Misc; using BUtil.Core.Options; using butil_ui.ViewModels; using System; @@ -74,7 +75,7 @@ public async Task ButtonOkCommand() } catch (Exception e) { - await Messages.ShowErrorBox(e.Message); + await Messages.ShowErrorBox(ExceptionHelper.ToString(e)); return; } diff --git a/sources/ubuntu-install.sh b/sources/ubuntu-install.sh index bdbc45ef..4592daf5 100644 --- a/sources/ubuntu-install.sh +++ b/sources/ubuntu-install.sh @@ -3,7 +3,7 @@ # Fail on first error. set -e -version=2024.07.16 +version=2024.07.29 sourceCodeInstallationDirectory=/usr/local/src/butil binariesInstallationDirectory=/usr/local/butil