From e1723ec1d970da9e4f5f2fb3ac9c1e9ffedc8f9e Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 11 Oct 2017 03:32:33 +0200 Subject: [PATCH] bsdflags support: do not open BLK/CHR/LNK files, fixes #3130 opening a device file for a non-existing device can be very slow. symlinks will make the open() call fail as it is using O_NOFOLLOW. also: lstat -> stat(..., follow_symlinks=False) like everywhere else. (cherry picked from commit a6ee4e9aedd2eb124c3a486f12131215661f77e1) --- src/borg/platform/linux.pyx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/borg/platform/linux.pyx b/src/borg/platform/linux.pyx index 7c3cf1b0b..25f71fa18 100644 --- a/src/borg/platform/linux.pyx +++ b/src/borg/platform/linux.pyx @@ -72,8 +72,11 @@ BSD_TO_LINUX_FLAGS = { def set_flags(path, bsd_flags, fd=None): - if fd is None and stat.S_ISLNK(os.lstat(path).st_mode): - return + if fd is None: + st = os.stat(path, follow_symlinks=False) + if stat.S_ISBLK(st.st_mode) or stat.S_ISCHR(st.st_mode) or stat.S_ISLNK(st.st_mode): + # see comment in get_flags() + return cdef int flags = 0 for bsd_flag, linux_flag in BSD_TO_LINUX_FLAGS.items(): if bsd_flags & bsd_flag: @@ -92,6 +95,10 @@ def set_flags(path, bsd_flags, fd=None): def get_flags(path, st): + if stat.S_ISBLK(st.st_mode) or stat.S_ISCHR(st.st_mode) or stat.S_ISLNK(st.st_mode): + # avoid opening devices files - trying to open non-present devices can be rather slow. + # avoid opening symlinks, O_NOFOLLOW would make the open() fail anyway. + return 0 cdef int linux_flags try: fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK|os.O_NOFOLLOW)