From 05ce2d18a6f568001518429b4a0cff7ccf384d2a Mon Sep 17 00:00:00 2001 From: JxxIT <110342008+JxxIT@users.noreply.github.com> Date: Sat, 5 Apr 2025 09:02:09 +0200 Subject: [PATCH] Fix utime sometimes failing --- osxphotos/fileutil.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/osxphotos/fileutil.py b/osxphotos/fileutil.py index 6e59c7495..7f04e7438 100644 --- a/osxphotos/fileutil.py +++ b/osxphotos/fileutil.py @@ -8,6 +8,7 @@ import typing as t from abc import ABC, abstractmethod from tempfile import TemporaryDirectory +import fcntl from .imageconverter import ImageConverter from .platform import is_macos @@ -163,7 +164,26 @@ def rmdir(cls, dirpath): def utime(cls, path, times): """Set the access and modified time of path.""" path = normalize_fs_path(path) - os.utime(path, times=times) + + fd = None + try: + # Open file and set F_NOCACHE to prevent filesystem cache interference + fd = os.open(path, os.O_RDONLY) + fcntl.fcntl(fd, fcntl.F_NOCACHE, 1) + + os.utime(path, times) + return True + finally: + if fd is not None: + try: + # Clear F_NOCACHE flag before closing + fcntl.fcntl(fd, fcntl.F_NOCACHE, 0) + os.close(fd) + except: + try: + os.close(fd) + except: + pass @classmethod def cmp(cls, f1, f2, mtime1=None):