Skip to content

Commit

Permalink
fix: remove ceiling of change timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickkirschen committed May 10, 2024
1 parent c8f5ab4 commit fe91b17
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions ddir/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
from __future__ import annotations

from dataclasses import dataclass
from datetime import date
from datetime import date, datetime
from enum import Enum
from hashlib import md5
from math import ceil
from os import sep, listdir, mkdir, stat
from os.path import exists, isfile, isdir
from random import randint as rand
Expand Down Expand Up @@ -259,21 +258,17 @@ def _create_for_file(diff: DiffFileCreator, source: str, target: str, fast: bool
print(f'file not in target: {source} {target}')
diff.add_diff(Diff(DiffType.POSITIVE, 'f', source, target))
else:
# It may occur that the source modification time has a higher/lower
# precision than the target modification time. As rounding to e.g. 2
# decimal places may lead to a difference of .01, we round to the
# ceiling to avoid false positives.
source_last_changed = ceil(stat(source).st_mtime)
target_last_changed = ceil(stat(target).st_mtime)

# Here, we check if the files are different by more than one second.
# This is because on some file systems, the modification time may be
# different by one second, even if the file has not been changed.
if source_last_changed - target_last_changed > 1:
print(f'files differ: {source} is newer than {target}')
source_last_changed = stat(source).st_mtime
target_last_changed = stat(target).st_mtime

source_last_changed_str = datetime.fromtimestamp(source_last_changed).isoformat()
target_last_changed_str = datetime.fromtimestamp(target_last_changed).isoformat()

if source_last_changed > target_last_changed:
print(f'[metadata] files differ: {source} is newer than {target} ({source_last_changed_str} > {target_last_changed_str})')
diff.add_diff(Diff(DiffType.NEWER, 'f', source, target))
elif target_last_changed - source_last_changed > 1:
print(f'files differ: {target} is newer than {source}')
elif target_last_changed > source_last_changed:
print(f'[metadata] files differ: {target} is newer than {source} ({target_last_changed_str} > {source_last_changed_str})')
diff.add_diff(Diff(DiffType.OLDER, 'f', source, target))
elif not fast:
with open(source, 'rb', encoding=ENCODING) as source_file, \
Expand All @@ -282,7 +277,7 @@ def _create_for_file(diff: DiffFileCreator, source: str, target: str, fast: bool
target_md5 = md5(target_file.read()).hexdigest()

if source_md5 != target_md5:
print(f'files differ: {source} (change dates are equal)')
print(f'[content ] files differ: {source}')
diff.add_diff(Diff(DiffType.UNKNOWN, 'f', source, target))


Expand Down

0 comments on commit fe91b17

Please sign in to comment.