问题
That's a theory question. I've searched over the internet with no satisfying luck, I just want to understand what's this jargon. I've seen examples of Java, JSON, etc but I couldn't find in Google nor here in StackOverflow a simple explanation, no code needed =P
So, an absolute path it's a way to get to a certain file or location describing the full route to it, the full path, and it's OS dependent (the absolute paths for Windows and Linux for example, are different) A relative path it's a route to a file or location which it's described from the current location .. (two dots) indicating a superior level in the directories tree. That has been clear to me for several years now.
When searching I've even seen there are canonicalized files too! All I know it's CANONICAL means something like "according to the rules" or something.
Can somebody please enlighten me in therms of theory about canonical stuff please? =) THANKS!!
PD (So Its wasn't just a Ubuntu random name after all) XD
PD2 Sadly I can't give the answer to everyone, I gave it to the one that helped me most understand the concept, but thanks a lot to everyone =)
回答1:
The whole point of making anything "canonical" is so that you can compare two things. For example, both ../../here/bar/x
and ./test/../../bar/x
may refer to the same location, but you can't do a textual comparison on the two paths. However, if you turn them into their canonical representation, they both become ../bar/x
, and we see that they actually refer to the same thing.
In short, it is often the case that you have many ways of referring to one thing, and in that case you may be able to define a canonical representation which is unique and which allows you to get a handle on collections of such things.
(If you're looking for more examples, all of mathematics is full of "canonical" constructions for all sorts of objects, and very much with the same purpose in mind. Maybe this Wikipedia article can provide some additional directions.)
回答2:
A good way to define a canonical path will be: the shortest absolute path
(short, in the meaning of string-length).
This is an example of the difference between an absolute path and a canonical path:
absolute path: C:\abc\..\abc\file.txt
canonical path: C:\abc\file.txt
回答3:
What a canonical path is (or its difference from an absolute path) is system dependent.
Typically if a (full) path contains aliases, shortcuts or symbolic links the canonical path resolves all these into the actual directories they refer.
Example: if /bin/a
is a sym link, you can find it anywhere you request for an absolute path e.g. from java.io.File#getAbsolutePath while the real file (i.e. the actual target of the link) i.e. usr/local/bin/a
would be return as a canonical path e.g. from java.io.File#getCanonicalPath
回答4:
The most issues with canonical paths occur when you are passing the name of a dir and not file. For file, if we are providing absolute path that is also the canonical path. But for dir it means omitting the last "/". For example, "/var/tmp/foo" is a canonical path while "/var/tmp/foo/" is not.
来源:https://stackoverflow.com/questions/12100299/whats-a-canonical-path