How to check if a file is a present in a directory(directly/indirectly)

Valdis.Kletnieks at vt.edu Valdis.Kletnieks at vt.edu
Fri Mar 6 12:18:52 EST 2015


On Fri, 06 Mar 2015 20:38:46 +0530, noyb noybee said:
> I am building a LKM in which I need to check whether a file is inside
> a particular directory(directly or inside sub-directories). Is there a
> function for this? Do I need to use the lookup function in
> inode_operations for this or is there a better method?

Inside subdirectories is an intractable problem, even from userspace.  You
basically need to walk *all* the inodes:

find /mounpoint -inum file_inode_number | grep /mountpoint/directory/to/check

If the filesystem is large, that can take a while.

Also, a file can be in more than one subdirectory at once:

[~] cd /tmp
[/tmp] mkdir z9
[/tmp] cd z9
[/tmp/z9] mkdir a b
[/tmp/z9] mkdir a/c
[/tmp/z9] touch a/c/d
[/tmp/z9] ln a/c/d a/first
[/tmp/z9] ln a/c/d b/second
[/tmp/z9] ln a/c/d third
[/tmp/z9] ls -lR
/tmp/z9] ls -liR
.:                              
total 0                         
62326 drwxr-xr-x. 3 valdis valdis 80 Mar  6 12:13 a
62327 drwxr-xr-x. 2 valdis valdis 60 Mar  6 12:13 b
62388 -rw-r--r--. 4 valdis valdis  0 Mar  6 12:13 third
                                
./a:                            
total 0                         
63471 drwxr-xr-x. 2 valdis valdis 60 Mar  6 12:13 c
62388 -rw-r--r--. 4 valdis valdis  0 Mar  6 12:13 first
                                
./a/c:
total 0
62388 -rw-r--r--. 4 valdis valdis 0 Mar  6 12:13 d

./b:
total 0
62388 -rw-r--r--. 4 valdis valdis 0 Mar  6 12:13 second

So which directory is it "under"?  And can this sort of stunt be
exploited with a race condition on a large filesystem? (Hint -there's
a *reason* that SELinux and Smack both use file labels instead of
pathnames...)

I think you need to stop and think about what you're *actually*
trying to accomplish here.  I suspect that what you *think* you are
checking isn't what you should *actually* be checking....
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150306/7f78fd71/attachment.bin 


More information about the Kernelnewbies mailing list