open file by inode?

Discussion in 'Perl Misc' started by ivowel@gmail.com, May 28, 2006.

  1. Guest

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

    sincerely,

    /iaw
     
    , May 28, 2006
    #1
    1. Advertising

  2. wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 28, 2006
    #2
    1. Advertising

  3. Guest

    John W. Krahn wrote:
    > 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__
     
    , May 28, 2006
    #3
  4. Guest

    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...
     
    , May 28, 2006
    #4
  5. John W. Krahn wrote:
    > wrote:
    >> 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

    --
    _ | Peter J. Holzer | Man könnte sich [die Diskussion] auch
    |_|_) | Sysadmin WSR/LUGA | sparen, wenn man sie sich einfach sparen
    | | | | würde.
    __/ | http://www.hjp.at/ | -- Ralph Angenendt in dang 2006-04-15
     
    Peter J. Holzer, May 28, 2006
    #5
  6. Juha Laiho Guest

    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).
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, May 28, 2006
    #6
  7. Guest

    wrote:
    > 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).

    --
    http://DavidFilmer.com
     
    , May 29, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Stap
    Replies:
    16
    Views:
    549
    E. Robert Tisdale
    Nov 12, 2003
  2. removing file by inode

    , Mar 23, 2006, in forum: Python
    Replies:
    5
    Views:
    590
    Arne Ludwig
    Mar 23, 2006
  3. kerb
    Replies:
    4
    Views:
    354
  4. asit

    inode number in windows XP

    asit, Jan 25, 2008, in forum: Python
    Replies:
    2
    Views:
    1,353
    Gabriel Genellina
    Jan 25, 2008
  5. Replies:
    4
    Views:
    273
    Nobody
    Jan 31, 2013
Loading...

Share This Page