/* dqrs.c++ PUBLIC DOMAIN 2016 */ /* part of dqrs */ /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" #include "report.h" #include "output_buffer.h" #define DELETED 4 // RED #define CREATED 2 // GREEN #define UNCOMPARABLE 3 // CYAN #define IDENTICAL 7 // WHITE #define DIFFERENT 5 // MAGENTA #define PARTIAL 6 // YELLOW #define SUBDIR 1 // BLUE static void dqrs_recurse (DIR *dir_ptr_a, DIR *dir_ptr_b); static int dqrs_file (int dir_fd_a, int dir_fd_b, const char *basename) {{{ off_t size_a; off_t size_b; int fd_a = open_file_only (dir_fd_a, basename, &size_a); int errval_a = errno; int fd_b = open_file_only (dir_fd_b, basename, &size_b); int errval_b = errno; if ((fd_a < 0) && (fd_b < 0)) { return -1; } if (fd_a < 0) { (void)close (fd_b); report (basename, ((errval_a == ENOENT) ? CREATED : UNCOMPARABLE)); return 0; } if (fd_b < 0) { (void)close (fd_a); report (basename, ((errval_b == ENOENT) ? DELETED : UNCOMPARABLE)); return 0; } if (size_a != size_b) { (void)close (fd_a); (void)close (fd_b); report (basename, DIFFERENT); return 0; } if (size_a == 0) { (void)close (fd_a); (void)close (fd_b); report (basename, IDENTICAL); return 0; } const void *ptr_a = mmap (NULL, size_a, PROT_READ, MAP_PRIVATE, fd_a, 0); if (ptr_a == MAP_FAILED) { close (fd_a); close (fd_b); report (basename, UNCOMPARABLE); return 0; } const void *ptr_b = mmap (NULL, size_a, PROT_READ, MAP_PRIVATE, fd_b, 0); if (ptr_b == MAP_FAILED) { munmap ((void*)ptr_a, size_a); close (fd_a); close (fd_b); report (basename, UNCOMPARABLE); return 0; } if (memcmp (ptr_a, ptr_b, size_a)) { munmap ((void*)ptr_a, size_a); munmap ((void*)ptr_b, size_a); close (fd_a); close (fd_b); report (basename, DIFFERENT); return 0; } munmap ((void*)ptr_a, size_a); munmap ((void*)ptr_b, size_a); close (fd_a); close (fd_b); report (basename, IDENTICAL); return 0; }}} static int dqrs_link (int dir_fd_a, int dir_fd_b, const char *basename) {{{ char *linktext_a = readlinkata (dir_fd_a, basename); int errval_a = errno; char *linktext_b = readlinkata (dir_fd_b, basename); int errval_b = errno; if ((!linktext_a) && (!linktext_b)) { return -1; } if (!linktext_a) { free (linktext_b); report (basename, ((errval_a == ENOENT) ? CREATED : UNCOMPARABLE)); return 0; } if (!linktext_b) { free (linktext_a); report (basename, ((errval_b == ENOENT) ? DELETED : UNCOMPARABLE)); return 0; } if (strcmp (linktext_a, linktext_b)) { free (linktext_a); free (linktext_b); report (basename, DIFFERENT); return 0; } else { free (linktext_a); free (linktext_b); report (basename, IDENTICAL); return 0; } }}} static int dqrs_dir (int dir_fd_a, int dir_fd_b, const char *basename) {{{ DIR *dir_ptr_a = opendirat (dir_fd_a, basename); int errval_a = errno; DIR *dir_ptr_b = opendirat (dir_fd_b, basename); int errval_b = errno; if ((!dir_ptr_a) && (!dir_ptr_b)) { return -1; } if (!dir_ptr_a) { (void)closedir (dir_ptr_b); report (basename, ((errval_a == ENOENT) ? CREATED : UNCOMPARABLE)); return 0; } if (!dir_ptr_b) { (void)closedir (dir_ptr_a); report (basename, ((errval_b == ENOENT) ? DELETED : UNCOMPARABLE)); return 0; } report (basename, SUBDIR); size_t path_length = push_path(basename); dqrs_recurse (dir_ptr_a, dir_ptr_b); pop_path (path_length); (void)closedir (dir_ptr_a); (void)closedir (dir_ptr_b); return 0; }}} static void dqrs (int dir_fd_a, int dir_fd_b, const char *basename) {{{ if (!dqrs_file (dir_fd_a, dir_fd_b, basename)) { return; } if (!dqrs_link (dir_fd_a, dir_fd_b, basename)) { return; } if (!dqrs_dir (dir_fd_a, dir_fd_b, basename)) { return; } report (basename, UNCOMPARABLE); }}} static void add_all (DIR *dir_ptr, std::set &entries) {{{ for (;;) { struct dirent *dent = readdir (dir_ptr); if (!dent) { break; } if (((dent->d_name[0] == '.') && (dent->d_name[1] == '.') && (dent->d_name[2] == 0)) || ((dent->d_name[0] == '.') && (dent->d_name[1] == 0))) { continue; } entries.insert (dent->d_name); } }}} static void dqrs_recurse (DIR *dir_ptr_a, DIR *dir_ptr_b) {{{ std::set entries; add_all (dir_ptr_a, entries); add_all (dir_ptr_b, entries); int dir_fd_a = dirfd (dir_ptr_a); int dir_fd_b = dirfd (dir_ptr_b); for (auto it = entries.begin(); it != entries.end(); ++it) { dqrs (dir_fd_a, dir_fd_b, it->c_str()); } }}} int main (int argc, char **argv) {{{ if (argc < 3) { error (EXIT_FAILURE, 0, "missing argument"); } if (argc > 3) { error (EXIT_FAILURE, 0, "too many arguments"); } DIR *dir_ptr_a = opendir (argv[1]); if (!dir_ptr_a) { error (EXIT_FAILURE, errno, "%s", argv[1]); } DIR *dir_ptr_b = opendir (argv[2]); if (!dir_ptr_b) { error (EXIT_FAILURE, errno, "%s", argv[2]); } dqrs_recurse (dir_ptr_a, dir_ptr_b); (void)closedir (dir_ptr_a); (void)closedir (dir_ptr_b); output_flush (); return 0; }}}