MacOS – Why does macOS not assign unique identifiers to files

filesystemmacos

I want to preface this question by disclosing I know next to nothing about computer architecture, storage and operating systems.

For a personal project, I'm trying to make a roadmap for how to make a small helper program that will enable linking to annotations in PDFs.

I have hit a small roadblock, though. Say I have a PDF called Doc. I'm reading and I make an annotation called Annot. Because the Annot is contained in Doc, I would first have to link to Doc's path:

Path > Doc > Annot

This could work by linking to the doc's path. There is one problem with this, though. I often reorganize my PDFs as my collection grows, so a link like that would soon break. Which leads to my question:

Why do macOS by default not assign each file on your harddrive a UUID for easy recall? In such a way, I would be able to link like:

  1. Link to Doc path with UUID as argument
  2. If path doesn't contain a document with Doc's UUID, search for a document with that UUID and go to path
  3. Return Doc

I think I might be able to make a similar solution by assigning a UUID to a documents metadata.

So to sum up: Is there any reason this wouldn't be included by default? Is there any reason this would be a bad idea?

Best Answer

Bookmark remains persistent across reboots. "File reference URL" (file:///.file/id=367.27/) is not reboot safe. If user relocates a file, both "String-based path" (/Users/me/a.txt) and "Path-based URL" (file://localhost/Users/me/a.txt) break too.

Locating Files Using Bookmarks

If you want to save the location of a file persistently, use the bookmark capabilities of NSURL. A bookmark is an opaque data structure, enclosed in an NSData object, that describes the location of a file. Whereas path- and file reference URLs are potentially fragile between launches of your app, a bookmark can usually be used to re-create a URL to a file even in cases where the file was moved or renamed.

To create a bookmark for an existing URL, use the bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error: method of NSURL. Specifying the NSURLBookmarkCreationSuitableForBookmarkFile option creates an NSData object suitable for saving to disk. Listing 2-3 shows a simple example implementation that uses this method to create a bookmark data object.

source