Skip to content

Commit

Permalink
Employ nanosecond precision for stat(), if supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlopezroche committed Oct 26, 2022
1 parent 362b535 commit 8b223d6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ AC_ARG_PROGRAM

PKG_PROG_PKG_CONFIG

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#define _XOPEN_SOURCE 700
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
]], [[
struct stat st;
st.st_ctim.tv_nsec = 0;
st.st_mtim.tv_nsec = 0;
]])],
[AC_DEFINE([HAVE_NSEC_TIMES], [1], [stat supports nanosecond precision])]
[AC_DEFINE([_XOPEN_SOURCE], [700], [enable certain X/Open and POSIX features])]
)

AC_ARG_WITH([ncurses], AS_HELP_STRING([--without-ncurses], [Do not use ncurses interface]))

AC_CHECK_HEADERS([getopt.h ncursesw/curses.h])

AS_IF([test x"$with_ncurses" != x"no"],
[PKG_CHECK_MODULES([NCURSES], [ncursesw],
[LIBS="$LIBS $NCURSES_LIBS"],
[AC_SEARCH_LIBS([wget_wch], [ncursesw ncurses curses], [], [AC_ERROR([ncurses library not found (or lacks wide character support)])])]
[AC_SEARCH_LIBS([keypad], [ncursesw tinfow ncurses tinfo curses], [], [AC_ERROR([ncurses library not found (lacks keypad support)])])]
)]
[AC_DEFINE([_XOPEN_SOURCE], [700], [enable certain functions in wchar.h and strings.h])]
[AC_DEFINE([_XOPEN_SOURCE], [700], [enable certain X/Open and POSIX features])]
[AC_DEFINE([_XOPEN_SOURCE_EXTENDED], [], [enable certain functions in curses.h])]
[AC_DEFINE([_ISOC99_SOURCE], [], [enable strtoll])]
[AC_SEARCH_LIBS([pcre2_match_32], [pcre2-32], [], [AC_ERROR([pcre2 library not found])])]
Expand Down
15 changes: 15 additions & 0 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ void getfilestats(file_t *file, struct stat *info, struct stat *linfo)
file->device = info->st_dev;
file->ctime = info->st_ctime;
file->mtime = info->st_mtime;
#ifdef HAVE_NSEC_TIMES
file->ctime_nsec = info->st_ctim.tv_nsec;
file->mtime_nsec = info->st_mtim.tv_nsec;
#else
file->ctime_nsec = 0;
file->mtime_nsec = 0;
#endif
}

int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)
Expand Down Expand Up @@ -1063,6 +1070,10 @@ int sort_pairs_by_ctime(file_t *f1, file_t *f2)
return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
else if (f1->ctime > f2->ctime)
return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
else if (f1->ctime_nsec < f2->ctime_nsec)
return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
else if (f1->ctime_nsec > f2->ctime_nsec)
return !ISFLAG(flags, F_REVERSE) ? 1 : -1;

return 0;
}
Expand All @@ -1073,6 +1084,10 @@ int sort_pairs_by_mtime(file_t *f1, file_t *f2)
return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
else if (f1->mtime > f2->mtime)
return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
else if (f1->mtime_nsec < f2->mtime_nsec)
return !ISFLAG(flags, F_REVERSE) ? -1 : 1;
else if (f1->mtime_nsec > f2->mtime_nsec)
return !ISFLAG(flags, F_REVERSE) ? 1 : -1;
else
return sort_pairs_by_ctime(f1, f2);
}
Expand Down
2 changes: 2 additions & 0 deletions fdupes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ typedef struct _file {
ino_t inode;
time_t mtime;
time_t ctime;
long mtime_nsec;
long ctime_nsec;
int hasdupes; /* true only if file is first on duplicate chain */
struct _file *duplicates;
struct _file *next;
Expand Down
5 changes: 5 additions & 0 deletions removeifnotchanged.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "config.h"
#include "removeifnotchanged.h"
#include <errno.h>
#include <string.h>
Expand All @@ -38,6 +39,10 @@ int removeifnotchanged(const file_t *file, char **errorstring)
file->inode != st.st_ino ||
file->ctime != st.st_ctime ||
file->mtime != st.st_mtime ||
#ifdef HAVE_NSEC_TIMES
file->ctime_nsec != st.st_ctim.tv_nsec ||
file->mtime_nsec != st.st_mtim.tv_nsec ||
#endif
file->size != st.st_size)
{
if (errorstring != 0)
Expand Down

0 comments on commit 8b223d6

Please sign in to comment.