How to check if a file is a present in a directory(directly/indirectly)
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? Regards, winged_elite
Maybe it helps: http://stackoverflow.com/questions/8347553/how-do-i-open-a-directory-at-kern... 2015-03-06 12:08 GMT-03:00 noyb noybee <afzalulh@gmail.com>:
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?
Regards, winged_elite
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Gabriel Duarte Linux User #471185 Rio de Janeiro / RJ http://genericdev.wordpress.com/
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....
participants (3)
-
Gabriel Duarte -
noyb noybee -
Valdis.Kletnieks@vt.edu