Comparing Directories
So this program reads in 2 directories and then outputs which files are NEW and which have been MODIFIED. I can't figure out how to tell the difference between a subdirectory and a file in the modified directory. As it stands right now, if there's a subdirectory in moddir it'll treat it like a file and just call it NEW. But it's not suppose to do that. If the -R flag is on then the program should recurse through the subdirectories and display the corresponding information. Any suggestions would be great. I'm stumped.
Code:
#include <stdio.h> |
Your general approach is faulty. You cannot do it recursively using a loop alone. Also, your approach of scanning the second directory for each match in the first is not only slow, but incomplete: what about files that only exist in the second directory?
Instead, I would recommend you write a function that compares the contents of two directories. For example, something along the lines of Code:
#include <unistd.h> When recursing into directories, you'll need to construct the new directory names, since the directory entries only contain the final segment (name), not the full path. Here is a helper function that returns the full path needed, as a dynamically allocated string: Code:
#include <stdlib.h> _ _ _ _ _ _ _ _ _ _ If this is part of a library or real application code, there is one deeply technical issue I'd like to bring up. When handling directory structures, directories and files may be renamed at any point. Because of that, it is recommended that instead of relying solely on paths, applications should retain a descriptor to the directory, and use the fstatat(dirfd, name...) function (POSIX.1-2008, i.e. #define _POSIX_C_SOURCE 200809L). The descriptor will stay valid, even if the name of the underlying directory happened to change. I understand that this is something that is totally new and alien to programmers with only Windows experience. Let me assure you: directory names are surprisingly volatile (and for useful reasons) in other OSes. Do not let Windows-isms drag down the quality of your code. (In Linux and BSDs and derivatives, you can rename or delete open files, even executables. I've found that many with only Windows experience expect having the file open be some kind of lock that should forbid such actions. I've never understood the reasoning for that.) To open a directory for the purpose of scanning its contents, you can use Code:
#define _POSIX_C_SOURCE 200809L In the most general case, a opendirat() implementation requires a child process entering the desired directory, passing the descriptor back via an ancillary socket message, because otherwise all other threads and signal handlers (and library code) would see the current working directory flipping back and forth while the code is running. Because the current working directory is process-wide, you really need to use a separate process to enter the new directory, then pass back a reference to it. Fortunately, this mess can almost always be avoided. For example, you can use a mutex or an rwlock to protect any access that has to do with the current working directory. The difference between the general case and the above implementation is that the above implementation will only work if the current user has read rights to the directory (i.e. can see the directory listing). |
All times are GMT -5. The time now is 03:06 AM. |