Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file by inode

Is it possible to open a file knowing its inode?

ls -i /tmp/test/test.txt
529965 /tmp/test/test.txt

I can provide path, inode (above 529965) and I am looking to get in return a file descriptor.

like image 850
dev Avatar asked Mar 18 '16 19:03

dev


People also ask

How do I open an inode file in Linux?

Using ls command The simplist method of viewing the assigned inode of files on a Linux filesystem is to use the ls command. When used with the -i flag the results for each file contains the file's inode number. In the example above two directories are returned by the ls command.

How do I view inode files?

Using the ls command You can also use the ls command, together with the -i option, to get a file's inode number. This command lists files and directories within the filesystem.

What is inode of a file?

What is an inode? By definition, an inode is an index node. It serves as a unique identifier for a specific piece of metadata on a given filesystem. Each piece of metadata describes what we think of as a file. That's right, inodes operate on each filesystem, independent of the others.

What is inode used for?

Inodes keep track of all the files on a Linux system. Except for the file name and the actual content of the file, inodes save everything else. It's like a file-based data structure that holds metadata about all of the files in the system.


2 Answers

This is not possible because it would open a loophole in the access control rules. Whether you can open a file depends not only on its own access permission bits, but on the permission bits of every containing directory. (For instance, in your example, if test.txt were mode 644 but the containing directory test were mode 700, then only root and the owner of test could open test.txt.) Inode numbers only identify the file, not the containing directories (it's possible for a file to be in more than one directory; read up on "hard links") so the kernel cannot perform a complete set of access control checks with only an inode number.

(Some Unix implementations have offered nonstandard root-only APIs to open a file by inode number, bypassing some of the access-control rules, but if current Linux has such an API, I don't know about it.)

like image 111
zwol Avatar answered Nov 03 '22 07:11

zwol


Not exactly what you are asking, but (as hinted by zwol) both Linux and NetBSD/FreeBSD provide the ability to open files using previously created “handles”: These are inode-like persistent names that identify a file on a file system.

On *BSD (getfh and fhopen) using this is as simple as:

#include <sys/param.h>
#include <sys/mount.h>


fhandle_t file_handle;
getfh("<file_path>", &file_handle);  // Or `getfhat` for the *at-style API

// … possibly save handle as bytes somewhere and recreate it some time later …

int fd = fhopen(&file_handle, O_RDWR);

The last call requiring the caller to be root however.

The Linux name_to_handle_at and open_by_handle_at system calls are similar, but a lot more explicit and require the caller to keep track of the relevant file system mount IDs/UUIDs themselves, so I'll humbly link to the detailed example in the manpage instead. Beware, that the example is not complete if you are looking to persist the handles across reboots; one has to convert the received mount ID to a persistent filesystem identifier, such as a filesystem UUID, and convert that back to a mount ID later on. In essence they do the same however. And just like on *BSD using the later system call requires elevated privileges (CAP_DAC_READ_SEARCH to be exact).

like image 7
ntninja Avatar answered Nov 03 '22 07:11

ntninja