File indexing completed on 2024-04-14 03:49:41

0001 /*
0002     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0003     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0004     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "fsutils.h"
0010 #include "enginedebug.h"
0011 
0012 #ifdef Q_OS_LINUX
0013 #include <cerrno>
0014 #include <unistd.h>
0015 #include <sys/ioctl.h>
0016 #include <fcntl.h>
0017 #endif
0018 
0019 using namespace Baloo;
0020 
0021 void FSUtils::disableCoW(const QString &path)
0022 {
0023 #ifndef Q_OS_LINUX
0024     Q_UNUSED(path);
0025 #else
0026     // from linux/fs.h, so that Baloo does not depend on Linux header files
0027 #ifndef FS_IOC_GETFLAGS
0028 #define FS_IOC_GETFLAGS     _IOR('f', 1, long)
0029 #endif
0030 #ifndef FS_IOC_SETFLAGS
0031 #define FS_IOC_SETFLAGS     _IOW('f', 2, long)
0032 #endif
0033 
0034     // Disable COW on file
0035 #ifndef FS_NOCOW_FL
0036 #define FS_NOCOW_FL         0x00800000
0037 #endif
0038 
0039     ulong flags = 0;
0040     const int fd = open(qPrintable(path), O_RDONLY);
0041     if (fd == -1) {
0042         qCWarning(ENGINE) << "Failed to open" << path << "to modify flags (" << errno << ")";
0043         return;
0044     }
0045 
0046     if (ioctl(fd, FS_IOC_GETFLAGS, &flags) == -1) {
0047         const int errno_ioctl = errno;
0048         // ignore ENOTTY, filesystem does not support attrs (and likely neither supports COW)
0049         if (errno_ioctl != ENOTTY) {
0050             qCWarning(ENGINE) << "ioctl error: failed to get file flags (" << errno_ioctl << ")";
0051         }
0052         close(fd);
0053         return;
0054     }
0055     if (!(flags & FS_NOCOW_FL)) {
0056         flags |= FS_NOCOW_FL;
0057         if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == -1) {
0058             const int errno_ioctl = errno;
0059             // ignore EOPNOTSUPP, returned on filesystems not supporting COW
0060             if (errno_ioctl != EOPNOTSUPP) {
0061                 qCWarning(ENGINE) << "ioctl error: failed to set file flags (" << errno_ioctl << ")";
0062             }
0063             close(fd);
0064             return;
0065         }
0066     }
0067     close(fd);
0068 #endif
0069 }