Baloo

fsutils.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
3  SPDX-FileCopyrightText: 2014 Daniel Vrátil <[email protected]>
4  SPDX-FileCopyrightText: 2015 Vishesh Handa <[email protected]>
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 
19 using namespace Baloo;
20 
21 void 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, 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);
53  return;
54  }
55  if (!(flags & FS_NOCOW_FL)) {
56  flags |= FS_NOCOW_FL;
57  if (ioctl(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);
64  return;
65  }
66  }
67  close(fd);
68 #endif
69 }
QAction * open(const QObject *recvr, const char *slot, QObject *parent)
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
QAction * close(const QObject *recvr, const char *slot, QObject *parent)
QString path(const QString &relativePath)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.