KCoreAddons

kfilesystemtype.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2011 David Faure <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-only
7 */
8 
9 #include "kfilesystemtype.h"
10 #include "kcoreaddons_debug.h"
11 
12 #include <QFile>
13 
14 #ifndef Q_OS_WIN
15 inline KFileSystemType::Type kde_typeFromName(const char *name)
16 {
17  /* clang-format off */
18  if (qstrncmp(name, "nfs", 3) == 0
19  || qstrncmp(name, "autofs", 6) == 0
20  || qstrncmp(name, "cachefs", 7) == 0
21  || qstrncmp(name, "fuse.sshfs", 10) == 0
22  || qstrncmp(name, "[email protected]", 9) == 0) { // #178678
23 
24  return KFileSystemType::Nfs;
25  }
26  if (qstrncmp(name, "fat", 3) == 0
27  || qstrncmp(name, "vfat", 4) == 0
28  || qstrncmp(name, "msdos", 5) == 0) {
29  return KFileSystemType::Fat;
30  }
31  if (qstrncmp(name, "cifs", 4) == 0
32  || qstrncmp(name, "smbfs", 5) == 0) {
33  return KFileSystemType::Smb;
34  }
35  if (qstrncmp(name, "ramfs", 5) == 0) {
37  }
38  /* clang-format on */
39 
41 }
42 
43 #if defined(Q_OS_BSD4) && !defined(Q_OS_NETBSD)
44 #include <sys/mount.h>
45 #include <sys/param.h>
46 
47 KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
48 {
49  struct statfs buf;
50  if (statfs(path.constData(), &buf) != 0) {
51  return KFileSystemType::Unknown;
52  }
53  return kde_typeFromName(buf.f_fstypename);
54 }
55 
56 #elif defined(Q_OS_LINUX) || defined(Q_OS_HURD)
57 #include <sys/vfs.h>
58 #ifdef QT_LINUXBASE
59 // LSB 3.2 has statfs in sys/statfs.h, sys/vfs.h is just an empty dummy header
60 #include <sys/statfs.h>
61 #endif
62 #ifndef NFS_SUPER_MAGIC
63 #define NFS_SUPER_MAGIC 0x00006969
64 #endif
65 #ifndef AUTOFS_SUPER_MAGIC
66 #define AUTOFS_SUPER_MAGIC 0x00000187
67 #endif
68 #ifndef AUTOFSNG_SUPER_MAGIC
69 #define AUTOFSNG_SUPER_MAGIC 0x7d92b1a0
70 #endif
71 #ifndef MSDOS_SUPER_MAGIC
72 #define MSDOS_SUPER_MAGIC 0x00004d44
73 #endif
74 #ifndef SMB_SUPER_MAGIC
75 #define SMB_SUPER_MAGIC 0x0000517B
76 #endif
77 #ifndef SMB2_MAGIC_NUMBER
78 #define SMB2_MAGIC_NUMBER 0xFE534D42
79 #endif
80 #ifndef CIFS_MAGIC_NUMBER
81 #define CIFS_MAGIC_NUMBER 0xFF534D42
82 #endif
83 #ifndef FUSE_SUPER_MAGIC
84 #define FUSE_SUPER_MAGIC 0x65735546
85 #endif
86 #ifndef RAMFS_MAGIC
87 #define RAMFS_MAGIC 0x858458F6
88 #endif
89 
90 // Reverse-engineering without C++ code:
91 // strace stat -f /mnt 2>&1|grep statfs|grep mnt, and look for f_type
92 
93 static KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
94 {
95  struct statfs buf;
96  if (statfs(path.constData(), &buf) != 0) {
97  return KFileSystemType::Unknown;
98  }
99  switch (static_cast<unsigned long>(buf.f_type)) {
100  case NFS_SUPER_MAGIC:
101  case AUTOFS_SUPER_MAGIC:
102  case AUTOFSNG_SUPER_MAGIC:
103  case FUSE_SUPER_MAGIC: // TODO could be anything. Need to use statfs() to find out more.
104  return KFileSystemType::Nfs;
105  case SMB_SUPER_MAGIC:
106  case SMB2_MAGIC_NUMBER:
107  case CIFS_MAGIC_NUMBER:
108  return KFileSystemType::Smb;
109  case MSDOS_SUPER_MAGIC:
110  return KFileSystemType::Fat;
111  case RAMFS_MAGIC:
112  return KFileSystemType::Ramfs;
113  default:
114  return KFileSystemType::Other;
115  }
116 }
117 
118 #elif defined(Q_OS_SOLARIS) || defined(Q_OS_IRIX) || defined(Q_OS_AIX) || defined(Q_OS_HPUX) || defined(Q_OS_OSF) || defined(Q_OS_QNX) || defined(Q_OS_SCO) \
119  || defined(Q_OS_UNIXWARE) || defined(Q_OS_RELIANT) || defined(Q_OS_NETBSD)
120 #include <sys/statvfs.h>
121 
122 KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
123 {
124  struct statvfs buf;
125  if (statvfs(path.constData(), &buf) != 0) {
126  return KFileSystemType::Unknown;
127  }
128 #if defined(Q_OS_NETBSD)
129  return kde_typeFromName(buf.f_fstypename);
130 #else
131  return kde_typeFromName(buf.f_basetype);
132 #endif
133 }
134 #endif
135 #else
136 KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
137 {
138  return KFileSystemType::Unknown;
139 }
140 #endif
141 
143 {
144  return determineFileSystemTypeImpl(QFile::encodeName(path));
145 }
FAT or similar (msdos, fat, vfat)
KCOREADDONS_EXPORT Type fileSystemType(const QString &path)
Returns the file system type at a given path, as much as we are able to figure it out...
const char * constData() const const
ext, reiser, and so on. "Normal" local filesystems.
NFS or other full-featured networked filesystems (autofs, subfs, cachefs, sshfs)
SMB/CIFS mount (networked but with some FAT-like behavior)
QByteArray encodeName(const QString &fileName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 18 2021 23:02:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.