Andrea
Andrea

Reputation: 4503

Open every file but not links to other directories when using scandir()

I want to recursively copy one directory into another (like cp -R) using POSIX scandir().

The problem is that when I copy a directory like /sys/bus/, which contains links to higher levels (for example: foo/foo1/foo2/foo/foo1/foo2/foo/... ) the system enters a loop status and copies the directories "in the middle" forever...

How can I check if the file I'm opening with dirent is a link or not?

Upvotes: 4

Views: 241

Answers (1)

Eyal
Eyal

Reputation: 5848

Look at this: How to check whether two file names point to the same physical file

You need to store a list of inodes that you have visited to make sure that you don't get any duplicates. If you have two hard links to the same file, there is no "one" canonical name. One possibility is to first store all the files and then recurse through all the filenames. You can store the path structure separately from the inodes and file contents.

Upvotes: 1

Related Questions