Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python filename from inode?

Tags:

python

I'm converting files from an old application on OS X. For reasons that must have been good at the time, it records associated files in its database by their inode numbers. (I think Apple calls it a FileID.)

Given a file name, I can find the inode in Python using os.stat(). But is there a Python way to find the name given an inode number?

Failing that, I can think of two other ways:

  1. Scan all the files in the folder to collect all their inode numbers, and save them in a dictionary for quick reference.

  2. Use os.system('find /folder/folder -inum 1234') and parse the output for what I am after. I suppose this does the same thing as above really, but done by the operating system.

I would prefer a Python native solution, but would be grateful for any other suggestions.

This will be Python2 on OS X 10.5 or 6.

like image 765
David M Avatar asked Sep 14 '13 21:09

David M


2 Answers

It looks like you'll have to bruteforce it. Filenames point to inodes. The inodes themselves don't contain a reference to the filename. Also you can have multiple hard links (locations in the file system, or file names) pointing to the same inode.

You can see the elements of an inode struct on this page. Filename isn't there.

like image 134
aychedee Avatar answered Oct 06 '22 11:10

aychedee


There is the volfs on OSX, but this might be actually slower than your 1. approach.

Hence, I'd use your 1. approach but with considering some important bits:

  • An inode can point to multiple files (hardlinks).
  • You'll have to make sure it's still the same device.

Your 2. approach is basically the same as the first one, except with additional forking overhead and more importantly basically running the same costly enumeration again and again, instead of populating a map once.

like image 42
nmaier Avatar answered Oct 06 '22 11:10

nmaier