-
Notifications
You must be signed in to change notification settings - Fork 776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure filename
in xopp format is always saved as a relative path to the directory it resides in
#4262
base: master
Are you sure you want to change the base?
Conversation
15c1571
to
ccd234f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one mutex related comment. The rest LGTM.
Ideally, this should be tested on various platforms, on shared partitions (e.g. in dual boot), but also on external media (usb key, external drive), and on cloud sync'd folders (e.g. Dropbox) opened on different computers.
@rolandlo If I remember correctly, you had worked on the attached pdf mode back in the day. Would you mind giving this a glance in case there was a hidden reason for absolute paths I'm not aware of? |
I will take a look at it. There absolutely might be a hidden reason for absolute paths, but I'm not aware of it right now and I will have to test in various situations. |
I think there's one use case for absolute paths, which is moving the This whole relative/absolute path problem can be entirely avoided with the packaged file format, but I've had almost no time to work on it in the past few months (help would be appreciated). |
Cool beans, got that locking fixed! @Technius Yeah, that's the only use case I can think of for absolute paths too. I figured it's much less intuitive and is extremely fragile. For example, if a set of files is on a USB, it could break even when switching between different Linux distros. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Merging in 72 hours, leaving time for anyone who can to remember if there was a hidden reason for using absolute paths.
Does that really work on Windows when the PDF (or image) and the |
OMG!! MSVC returns an empty string? Yeah let's hold this - it looks like we have to give special treatment to msvc and cygwin or something... Thanks for bringing this up!
|
So... I'm not sure what to do with this. I guess we should treat differently the case were the .xopp file and the .pdf file are not on the same partition. I don't know if this can be easily achieved using std::filesystem. |
Yeah - I too was thinking we can have an exception for Windows - where if the drive letter is different, use the absolute path. I suppose this would mean we would have to call a helper function, and do a little dirty work in there just for Windows - does that sound okay? With some of my other PRs merged, I think this is now something I can consider implementing |
Given that the file name / path conversions have been a source of numerous bugs over the years, it would be nice if we could also refactor the path resolution logic into helper functions, and then write unit tests for them. One potential solution for dealing with the separate drive problem is to only save as a relative path when their root paths are the same. |
That should be achievable via std::filesystem @Technius it might be better to directly try to calculate a relative path. When it does not exist or is unresolvable then use the canonical path. |
5a3447c
to
9114f20
Compare
The latest commit addresses feedback; namely:
|
8b64fc7
to
edc6645
Compare
I just fixed up the PR again, renaming the method to On Windows, the path is now normalized to use UNIX-style file separators. I checked that these paths are still serializable to Additional tests were also added to ensurethe saved path is non-empty (which is a possibility on windows when While we're on the topic of background PDF annotation paths, I propose we close #2774. That issue proposes having a set of paths to search for background PDFs. One reason to include it was to mitigate the effects of saving PDF background files as absolute paths. But now that these background PDFs are saved as relative paths, there is little reason for the feature, besides its extra complexity and non-intuitive behavior. Issues are piling up as they are, and I like to say no thanks to some of them to improve the issue signal to noise ratio. Lastly, are there any blockers for this change? It addresses the latest feedback - namely more tests and that the canonical path should be used if the relative path could not be calculated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to check the root path even for non-Windows platforms (see cppreference on path syntax) - better safe than sorry.
Plus I don't think it makes sense to store a relative path if the locations are that 'far apart' (i.e. not even same root path).
xournalpp/src/core/control/xojfile/SaveHandler.cpp
Lines 279 to 284 in edc6645
background->setAttrib("domain", "absolute"); | |
auto normalizedPath = Util::normalizeAssetPath( | |
std::filesystem::current_path() / p->getBackgroundImage().getFilepath().string(), | |
target.parent_path()); | |
background->setAttrib("filename", normalizedPath); | |
p->getBackgroundImage().setCloneId(id); |
Probably better to ensure all paths are absolute?
BTW we must also ensure all the strings are converted as UTF-8 to avoid locale and issues.
test/unit_tests/util/PathTest.cpp
Outdated
p = Util::normalizeAssetPath("C:\\dir\\file.txt", "D:"); | ||
EXPECT_EQ(string("C:\\dir\\file.txt"), p.string()); | ||
|
||
p = Util::normalizeAssetPath("C:\\dir\\file.txt", "D:\\base"); | ||
EXPECT_EQ(string("C:\\dir\\file.txt"), p.string()); | ||
|
||
// do not return empty if asset_path is relative | ||
p = Util::normalizeAssetPath("../dir/file.txt", "D:"); | ||
EXPECT_TRUE(!p.empty()); | ||
|
||
p = Util::normalizeAssetPath("../dir/file.txt", "D:\\base"); | ||
EXPECT_TRUE(!p.empty()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want the result to always have forward slashes.
@hyperupcall this PR is nearly done and merge-able, actually only the tests must be corrected. So my question is, if you still want to continue working on it and if you have time to do it? If you are pretty busy, we would just finish it. |
If I don't push any commits by April 13th (tomorrow), feel free to finish it up. I wish I was able to close this earlier |
A heuristic, and the ability, to manage assets afterward would be good. Neither completely absolute nor relative paths are a good final solution. |
85c6a67
to
55a1134
Compare
55a1134
to
2e74978
Compare
Co-authored-by: danemtsov <49734973+danemtsov@users.noreply.github.com>
- fs::proximate combines a check for validity with fs::relative. - Fixed some type conversion issues
2e74978
to
7202c5e
Compare
#include <filesystem> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be reverted, or we should choose, to abandon compilers / STL implementations which do not provide std::filesystem
@xournalpp/core before we merge this, we should discuss, if we really wan't to override all old files.
|
Let's save this for after the release of 1.2.0. I don't think we should be making such a large change right before the next release. |
Any news on this? Allowing the use of relative paths, so that it always works as long as I keep the PDF and xopp file in the same directory, is something that i am waiting for with excitement ^_^ Edit: Relative paths are currently possible by attatching the pdf in the "Annotate PDF dialouge", but it is a relatively hidden option, not default behaviour, and for a document "test.pdf" produces the file "test.xopp.bg.pdf", which clutters my directories. I have not read the details of this proposal, as I believe they are beyond me, but I am really hoping that we could look for a relative path by default, and without the extra ".xopp.bg.pdf" file. |
+1 for relative paths to background PDFs! This would solve lots or problems. It would be cool to have an option for relative or absolute paths as default setting. Are there any reasons this PR is still waiting? |
There are several issues stemming from the fact that annotated PDF backgrounds break when:
filename
is stored as an absolute path.xopp
file different than the$PWD
when opening one from the command line (passing it as $1)$PWD
is equivalent to$HOME
, which is nearly always the common parent of xopp and pdf files.Specifically, this
.xopp
file, rather than$PWD
)std::filesystem::relative
with two absolute paths, which useslexically_relative
under the hood after canonicalizationOne of the issues floated around this changeset, which I unfortunately found only after I did my on work - which appears to do nearly the same thing except:
background->setAttrib("domain", "absolute")
near abackground->setAttrib("filename", ...)
in the save handler, I performed this change)Validation method: