Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/PORTABLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Conversion work folders, temporary files and engine caches keep their existing b
A proxy set in Settings → General → Network is kept in `data/settings.json` as well, so the copy downloads engines
and updates through it on any computer without touching that computer's proxy settings.
An explicit `FILEE_DATA_DIR` still takes precedence.
Installer packages keep using the normal user profile.
Installer packages keep using the normal user profile. A distribution package made from the Linux archive must
leave out `filee.portable` and any `data` folder (`installer/linux/INSTALL.txt` says so for packagers), or every user
of it gets portable mode in a folder they can't write to. When the data folder can't be written, Filee shows the
error in the UI language's fonts and writes it to stderr too, with the cause, since the log would be in that folder.
Update a marked copy by replacing its application files while keeping `filee.portable` and `data`.

### Bringing existing data along
Expand Down
5 changes: 5 additions & 0 deletions installer/linux/INSTALL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ in data beside the executable. On first start the app offers to copy existing
profile data and downloaded engines into unused portable data, leaving originals intact.
Installing with install.sh uses the normal user profile instead.

Packagers: delete filee.portable (and any data folder) from an installed copy,
for example after cp -a into /opt/filee, as install.sh does. With the marker,
Filee keeps its data in data beside the executable; where users can't write
there, it shows an error and does not start.

Run ./install.sh to install for the current user. It requires Python 3, creates
an application launcher and ~/.local/bin/filee, and does not require root.
Run ./install.sh --upgrade to safely upgrade an existing installation in place
Expand Down
65 changes: 45 additions & 20 deletions src/Filee.App/PortableStartupFailure.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
// A portable copy with an unwritable data folder shows an error without loading settings or logging elsewhere.
// A portable copy with an unwritable data folder shows an error without loading settings or logging elsewhere. The
// message also goes to stderr, with the cause in English, since the log lives in that same folder (#1590).

using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Layout;
using Avalonia.Media;
using Filee.App.Services;
using Filee.Core.Settings;

namespace Filee.App;

internal static class PortableStartupFailure
{
public static int Show(string message)
public static int Show(string message, PortableStorageException error)
{
Console.Error.WriteLine(message);
Console.Error.WriteLine(Cause(error));
if (OperatingSystem.IsWindows())
Filee.Platform.Windows.Elevation.ShowWarning(message);
else
{
try
{
// Filee's own fonts, as in the app: without them Avalonia drew Chinese and other scripts the system's
// default font lacks as empty boxes (#1590).
AppBuilder.Configure(() => new FailureApplication(message)).UsePlatformDetect()
.ConfigureFonts(fonts =>
{
fonts.AddFontCollection(new FileeFontCollection());
fonts.AddFontCollection(new SystemUiFontCollection());
})
.StartWithClassicDesktopLifetime([], ShutdownMode.OnMainWindowClose);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
Expand All @@ -31,31 +42,45 @@ public static int Show(string message)
return 1;
}

/// <summary>
/// The line for stderr after the message: the folder, why it failed, and the marker that asked for it, in English
/// for a bug report or a packager's terminal.
/// </summary>
internal static string Cause(PortableStorageException error) =>
$"Filee: {error.Message} ({error.InnerException?.Message ?? "unknown error"}). The {PortableMode.MarkerName} file " +
"beside Filee turns on portable mode; delete it to use the normal user profile. Installed packages must not include it.";

/// <summary>The message window, in the UI font chain of <paramref name="language"/> and its reading direction.</summary>
internal static Window CreateWindow(string message, string language)
{
var close = new Button { Content = LocalizationService.EarlyText("common.close"), HorizontalAlignment = HorizontalAlignment.Right };
var window = new Window
{
Title = "Filee",
Width = 520,
SizeToContent = SizeToContent.Height,
CanResize = false,
FontFamily = SoftPalette.FontFamilyFor(language),
FlowDirection = SoftPalette.IsRightToLeft(language) ? FlowDirection.RightToLeft : FlowDirection.LeftToRight,
Content = new StackPanel
{
Margin = new Thickness(24),
Spacing = 18,
Children = { new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }, close },
},
};
close.Click += (_, _) => window.Close();
return window;
}

private sealed class FailureApplication(string message) : Application
{
public override void Initialize() => Styles.Add(new Avalonia.Themes.Fluent.FluentTheme());

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var close = new Button { Content = Services.LocalizationService.EarlyText("common.close"), HorizontalAlignment = HorizontalAlignment.Right };
var window = new Window
{
Title = "Filee",
Width = 520,
SizeToContent = SizeToContent.Height,
CanResize = false,
Content = new StackPanel
{
Margin = new Thickness(24),
Spacing = 18,
Children = { new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }, close },
},
};
close.Click += (_, _) => window.Close();
desktop.MainWindow = window;
}
desktop.MainWindow = CreateWindow(message, LocalizationService.Resolve("auto"));
base.OnFrameworkInitializationCompleted();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filee.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static int Main(string[] args)
{
var message = string.Format(System.Globalization.CultureInfo.CurrentCulture,
LocalizationService.EarlyText("startup.portable_unwritable"), ex.DataDirectory);
return PortableStartupFailure.Show(message);
return PortableStartupFailure.Show(message, ex);
}
using var portableScope = portable;
// ImageMagick reads its policy once, at its first use; the app also calls it directly (crop, previews).
Expand Down
38 changes: 38 additions & 0 deletions tests/Filee.App.Tests/PortableStartupFailureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// The window and stderr line of a portable copy whose data folder can't be written (#1590): drawn in the UI language's
// fonts, and the cause written where a packager or bug report sees it.

using Avalonia.Controls;
using Avalonia.Headless.XUnit;
using Avalonia.Media;
using Filee.App.Services;
using Filee.Core.Settings;

namespace Filee.App.Tests;

public class PortableStartupFailureTests
{
[AvaloniaTheory]
[InlineData("zh-CN", FlowDirection.LeftToRight)]
[InlineData("ar", FlowDirection.RightToLeft)]
[InlineData("en", FlowDirection.LeftToRight)]
public void The_window_uses_the_ui_fonts_and_direction_of_the_language(string language, FlowDirection direction)
{
var window = PortableStartupFailure.CreateWindow("Filee 无法写入便携数据文件夹", language);

Assert.Equal(SoftPalette.FontFamilyFor(language), window.FontFamily);
Assert.Equal(direction, window.FlowDirection);
Assert.Contains(((StackPanel)window.Content!).Children.OfType<TextBlock>(), t => t.Text == "Filee 无法写入便携数据文件夹");
}

[Fact]
public void Stderr_names_the_folder_the_reason_and_the_marker()
{
var error = new PortableStorageException("/opt/filee/data", new UnauthorizedAccessException("Access to the path '/opt/filee/data' is denied."));

var line = PortableStartupFailure.Cause(error);

Assert.Contains("/opt/filee/data", line);
Assert.Contains("is denied", line);
Assert.Contains(PortableMode.MarkerName, line);
}
}
Loading