From d2399a7ce0af49098a065c48409d1512f37c7c21 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 6 Feb 2024 08:17:24 +0000 Subject: Fix selecting a custom training file in flatpak This is done by copying any training to the temporary tesseract directory, and always using that as the TESSDIR. This works as it's writeable (unlike the /app/share directory that flatpak would otherwise work) --- cmd/rescribe/main.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'cmd/rescribe/main.go') diff --git a/cmd/rescribe/main.go b/cmd/rescribe/main.go index b33a66c..f532678 100644 --- a/cmd/rescribe/main.go +++ b/cmd/rescribe/main.go @@ -241,11 +241,33 @@ These training files are included in rescribe, and are always available: } } - // if trainingPath doesn't exist, set it to the embedded training instead - _, err = os.Stat(trainingPath) - if err != nil && !os.IsExist(err) { - trainingPath = filepath.Base(trainingPath) - trainingPath = filepath.Join(tessdatadir, trainingPath) + // copy training path to the tessdir directory, so that we can keep that a + // writeable space, which is needed opening other trainings in sandboxes + // like flatpak + in, err := os.Open(trainingPath) + trainingPath = filepath.Join(tessdatadir, filepath.Base(trainingPath)) + if err != nil { + in, err = os.Open(trainingPath) + if err != nil { + log.Fatalf("Error opening training file %s: %v", trainingPath, err) + } + } + defer in.Close() + newPath := trainingPath + ".new" + out, err := os.Create(newPath) + if err != nil { + log.Fatalf("Error creating training file %s: %v", newPath, err) + } + defer out.Close() + _, err = io.Copy(out, in) + if err != nil { + log.Fatalf("Error copying training file to %s: %v", newPath, err) + } + in.Close() + out.Close() + err = os.Rename(newPath, trainingPath) + if err != nil { + log.Fatalf("Error moving new training file to %s: %v", trainingPath, err) } abstraining, err := filepath.Abs(trainingPath) -- cgit v1.2.1-24-ge1ad