open file by inode?

I

ivowel

is it possible for perl to open a file in unix not knowing its
filename, but knowing its inode?

sincerely,

/iaw
 
J

John W. Krahn

is it possible for perl to open a file in unix not knowing its
filename, but knowing its inode?

Sure:

my $inode = 12345;

# Could be more then one file with the same inode
chomp( my @files = `find / -inum $inode 2>/dev/null` );

@files or die "Cannot find a file with inode $inode.\n";

open my $fh, '<', $files[ 0 ] or die "Cannot open $files[0]: $!";



John
 
U

usenet

John said:
chomp( my @files = `find / -inum $inode 2>/dev/null` );

Or, with the IO::All module (the Swiss Army Knife of IO), with basic
error handling kindly provided by the module itself:

#!/usr/bin/perl
use strict; use warnings;
use IO::All;

my $inode = 12345;

foreach my $file ( io(".")
-> filter(sub {$_->inode == $inode})
-> all_files() ) {
print io($file) -> slurp();
}

__END__
 
I

ivowel

yikes. I was storing inodes because I wanted to speed up file access.
this does not seem to be the case if I have to look through my entire
filesystem to locate the inode...
 
P

Peter J. Holzer

John said:
is it possible for perl to open a file in unix not knowing its
filename, but knowing its inode?
Sure:

No.

my $inode = 12345;

# Could be more then one file with the same inode
chomp( my @files = `find / -inum $inode 2>/dev/null` );

@files or die "Cannot find a file with inode $inode.\n";

open my $fh, '<', $files[ 0 ] or die "Cannot open $files[0]: $!";

You still need to know the file name for open. If for some reason you
cannot determine the file name from the inode number (e.g., because you
cannot read its directory or because it has been unlinked), you cannot
open it.

hp
 
J

Juha Laiho

(e-mail address removed) said:
is it possible for perl to open a file in unix not knowing its
filename, but knowing its inode?

Unix does not provide inode-to-path mapping, and Perl does not change this
fact.

As others have shown, you can do brute-force lookup to find the inode,
but this is inefficient, and subject to directory permissions.

Actually, being able to open a file by inode number would be a seurity
risk (think of chroot and temporary files which are unlinked from directory
tree after they're created).
 
U

usenet

yikes. I was storing inodes because I wanted to speed up file access.

Oops. You won't get your intended result (this is an OS limitation, not
a Perl limitation). The fastest way to access a file is by its
absolute directory/filename (ie, no symbolic links anywhere in the
path).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top