nuttyartist / notes

Fast and beautiful note-taking app written in C++. Write down your thoughts.
https://www.get-notes.com
Mozilla Public License 2.0
3.6k stars 316 forks source link

'Import database' feature doesn't work on Flatpak #643

Closed guihkx closed 7 months ago

guihkx commented 10 months ago

Steps to reproduce:

  1. Run Notes using the terminal: flatpak run io.github.nuttyartist.notes//stable
  2. Open the settings menu (little gear button)
  3. Open the Import/Export Notes menu
  4. Click Import
  5. Select any .nbk file
  6. Hit OK

What happens:

Importing fails silently (UI-wise), but this error pops up in the terminal:

onImportNotesRequested
onImportNotesRequested Error: connection with database fail

I tried debugging this for a while, but I ultimately got stuck and I'm not sure what to do next.

Running the app with strace showed me something interesting (warning: if you run this, expect a large amount of text):

$ strace -e openat -ff flatpak run io.github.nuttyartist.notes//stable
[pid 16568] openat(AT_FDCWD, "/run/user/1000/doc/<random string>/db.nbk", O_RDONLY|O_CLOEXEC) = 37
[pid 16574] openat(AT_FDCWD, "/run/user/1000/doc/<random string>/db.nbk", O_RDONLY|O_CLOEXEC) = 37
onImportNotesRequested
[pid 16574] openat(AT_FDCWD, "/run/flatpak/doc/<random string>/db.nbk", O_RDWR|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0644) = -1 ELOOP (Too many levels of symbolic links)
[pid 16574] openat(AT_FDCWD, "/run/flatpak/doc/<random string>/db.nbk", O_RDONLY|O_NOFOLLOW|O_CLOEXEC) = -1 ELOOP (Too many levels of symbolic links)
onImportNotesRequested Error: connection with database fail

Just to briefly explain that weird path (/run/user/...) where the .nbk file is located:

Our Flatpak app is sandboxed and doesn't have direct access to user files, so even though the user can choose any file from his computer using Qt's file chooser, that file will then be copied to a temporary location decided by xdg-desktop-portal, just so our app can access only that file.

Anyway, now analyzing the strace output as whole: For some reason, only the two last openat() attempts specified the O_NOFOLLOW flag, which then fail with the ELOOP (Too many levels of symbolic links) error.

I tried figuring out where exactly the failure occurs, and at least this part I think I got right:

https://github.com/nuttyartist/notes/blob/7466cfb726182493295838df6163e8f2fc545c2a/src/mainwindow.cpp#L2828

https://github.com/nuttyartist/notes/blob/7466cfb726182493295838df6163e8f2fc545c2a/src/dbmanager.cpp#L1747

https://github.com/nuttyartist/notes/blob/7466cfb726182493295838df6163e8f2fc545c2a/src/dbmanager.cpp#L1757

I added some logging to figure out what was the exact error given by QSqlDatabase::open(), and it's this one:

QSqlError("14", "Error opening database", "unable to open database file")

Which appears to come from here on the Qt side:

https://code.qt.io/cgit/qt/qtbase.git/tree/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp?h=6.4.2#n762

So I thought this would be easily reproducible outside the Flatpak world if I just tried importing an .nbk file that was a symbolic link to another .nbk file, but that didn't fail the import process at all. :smiling_face_with_tear:

Additionally, I manually inspected the Flatpak sandbox of Notes to see if /run/flatpak/doc/<random string>/db.nbk is actually a symbolic link, but it's really not:

$ flatpak run --command=file io.github.nuttyartist.notes//stable /run/flatpak/doc/<random string>/db.nbk
/run/flatpak/doc/<random string>/db.nbk: SQLite 3.x database, last written using SQLite version 3043000, file counter 23, database pages 19, cookie 0x4, schema 4, UTF-8, version-valid-for 23
$ flatpak run --command=bash io.github.nuttyartist.notes//stable -c 'test -h /run/flatpak/doc/<random string>/db.nbk && echo "symbolic link confirmed!" || echo "not a symbolic link!"'
not a symbolic link!

Is all of this a red herring? I don't know. :shrug:

guihkx commented 10 months ago

Here's a quick-'n-dirty workaround for manually importing the .nbk file on Flatpak:

  1. Run Notes at least once
  2. Quit the app
  3. Rename your *.nbk backup file to literally this: notes.db
  4. Move/Copy notes.db to this directory: ~/.var/app/io.github.nuttyartist.notes/config/Awesomeness/

You can now run Notes normally and your notes should be back.

nuttyartist commented 10 months ago

Thanks for the thorough investigation! Very strange behavior.

guihkx commented 7 months ago

This was actually a bug in xdg-desktop-portal:

Which has been fixed in version 1.18.2.

Luckily, Arch Linux already has this version, so I revisited this and importing the nbk file now works as it should!

nuttyartist commented 7 months ago

Awesome, thanks for investigating!