Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top