Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > open file by inode?

Reply
Thread Tools

open file by inode?

 
 
ivowel@gmail.com
Guest
Posts: n/a
 
      05-28-2006

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

sincerely,

/iaw

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      05-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      05-28-2006
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__

 
Reply With Quote
 
ivowel@gmail.com
Guest
Posts: n/a
 
      05-28-2006

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...

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      05-28-2006
John W. Krahn wrote:
> (E-Mail Removed) 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
| | | (E-Mail Removed) | würde.
__/ | http://www.hjp.at/ | -- Ralph Angenendt in dang 2006-04-15
 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      05-28-2006
(E-Mail 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).
--
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)
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      05-29-2006
(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
Re: how to open a file in some application using Tkinter i am usingTKINTER to create GUI application i want to know how to open a worddocument in open office or any other applicatio Fredrik Lundh Python 1 01-09-2008 10:40 AM
File::open and File.open Schle Daniel Ruby 4 10-24-2006 09:34 PM
Why cannot open .txt file with Encoding = UNICODE using javascript window.open()? ml Java 0 11-30-2004 07:43 AM
How do I open a database connection on an access file that currently open ? THY ASP .Net 4 08-22-2003 03:50 PM



Advertisments