1/*
2 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3 SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
4 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "fsutils.h"
10#include "enginedebug.h"
11
12#ifdef Q_OS_LINUX
13#include <cerrno>
14#include <unistd.h>
15#include <sys/ioctl.h>
16#include <fcntl.h>
17#endif
18
19using namespace Baloo;
20
21void FSUtils::disableCoW(const QString &path)
22{
23#ifndef Q_OS_LINUX
24 Q_UNUSED(path);
25#else
26 // from linux/fs.h, so that Baloo does not depend on Linux header files
27#ifndef FS_IOC_GETFLAGS
28#define FS_IOC_GETFLAGS _IOR('f', 1, long)
29#endif
30#ifndef FS_IOC_SETFLAGS
31#define FS_IOC_SETFLAGS _IOW('f', 2, long)
32#endif
33
34 // Disable COW on file
35#ifndef FS_NOCOW_FL
36#define FS_NOCOW_FL 0x00800000
37#endif
38
39 ulong flags = 0;
40 const int fd = open(qPrintable(path), O_RDONLY);
41 if (fd == -1) {
42 qCWarning(ENGINE) << "Failed to open" << path << "to modify flags (" << errno << ")";
43 return;
44 }
45
46 if (ioctl(fd: fd, FS_IOC_GETFLAGS, &flags) == -1) {
47 const int errno_ioctl = errno;
48 // ignore ENOTTY, filesystem does not support attrs (and likely neither supports COW)
49 if (errno_ioctl != ENOTTY) {
50 qCWarning(ENGINE) << "ioctl error: failed to get file flags (" << errno_ioctl << ")";
51 }
52 close(fd: fd);
53 return;
54 }
55 if (!(flags & FS_NOCOW_FL)) {
56 flags |= FS_NOCOW_FL;
57 if (ioctl(fd: fd, FS_IOC_SETFLAGS, &flags) == -1) {
58 const int errno_ioctl = errno;
59 // ignore EOPNOTSUPP, returned on filesystems not supporting COW
60 if (errno_ioctl != EOPNOTSUPP) {
61 qCWarning(ENGINE) << "ioctl error: failed to set file flags (" << errno_ioctl << ")";
62 }
63 close(fd: fd);
64 return;
65 }
66 }
67 close(fd: fd);
68#endif
69}
70

source code of baloo/src/engine/fsutils.cpp