Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find a loop in the file system?

Tags:

c

linux

how to find a loop in the file system in Linux? i am indexing all files for making search fast(O(1))... i am using c programming language to implement by using the library functions in dir.h.... I can scan through entire file system but it goes in a loop if there is loop in the filesystem(example loop mount)... how to find the loop in file system.. i have seen updatedb command reporting when there is loop in file system... i don't understand the logic... can anyone pls help find solution for this?

like image 456
suresh Avatar asked Mar 22 '09 19:03

suresh


People also ask

How do you find the file system of a file?

In the Computer window, right click the drive you wish to check and then click Properties from the menu. In the Disk Properties window, the information is listed next to File system.

How do you loop a file in Linux?

To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.

How do I loop through a file in bash?

The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).


2 Answers

The general way to prevent re-scanning nodes in a graph is to mark nodes as you pass them, and then ignore nodes that are marked. This isn't terribly practical if you don't want to modify the graph you are scanning over, so you need a way to mark the nodes externally. The easiest way I can think of to do this under linux would be to store a the device/inode for each directory you visit. Then when you look at a directory, first check that you haven't already seen any directories with the same device/inode. This not only handles cycles, but also trees that merge back into each other.

To get the device/inode number take a look at the stat/fstat functions and the st_dev and st_ino members of the stat struct.

For storing the data, you're probably wanting to look at a hash-table or a binary tree.

like image 122
Eclipse Avatar answered Oct 20 '22 13:10

Eclipse


Btw. You don't need to search loop in the file system.

You are indexing whole disk. So you don't need to follow symbolic links as each file must be accessible in normal way (without symlinks). You just need to check points of mount if some disk is mounted more than one time just ignore the rest points of mount.

like image 24
Ade Avatar answered Oct 20 '22 11:10

Ade