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 <QFile>
11 #include "kcoreaddons_debug.h"
12 //#include <errno.h>
13 
14 #ifndef Q_OS_WIN
15 inline KFileSystemType::Type kde_typeFromName(const char *name)
16 {
17  if (qstrncmp(name, "nfs", 3) == 0
18  || qstrncmp(name, "autofs", 6) == 0
19  || qstrncmp(name, "cachefs", 7) == 0
20  || qstrncmp(name, "fuse.sshfs", 10) == 0
21  || qstrncmp(name, "[email protected]", 9) == 0) { // #178678
22  return KFileSystemType::Nfs;
23  }
24  if (qstrncmp(name, "fat", 3) == 0
25  || qstrncmp(name, "vfat", 4) == 0
26  || qstrncmp(name, "msdos", 5) == 0) {
27  return KFileSystemType::Fat;
28  }
29  if (qstrncmp(name, "cifs", 4) == 0
30  || qstrncmp(name, "smbfs", 5) == 0) {
31  return KFileSystemType::Smb;
32  }
33  if (qstrncmp(name, "ramfs", 5) == 0) {
35  }
36 
38 }
39 
40 #if defined(Q_OS_BSD4) && !defined(Q_OS_NETBSD)
41 # include <sys/param.h>
42 # include <sys/mount.h>
43 
44 KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
45 {
46  struct statfs buf;
47  if (statfs(path.constData(), &buf) != 0) {
48  return KFileSystemType::Unknown;
49  }
50  return kde_typeFromName(buf.f_fstypename);
51 }
52 
53 #elif defined(Q_OS_LINUX) || defined(Q_OS_HURD)
54 # include <sys/vfs.h>
55 # ifdef QT_LINUXBASE
56 // LSB 3.2 has statfs in sys/statfs.h, sys/vfs.h is just an empty dummy header
57 # include <sys/statfs.h>
58 # endif
59 # ifndef NFS_SUPER_MAGIC
60 # define NFS_SUPER_MAGIC 0x00006969
61 # endif
62 # ifndef AUTOFS_SUPER_MAGIC
63 # define AUTOFS_SUPER_MAGIC 0x00000187
64 # endif
65 # ifndef AUTOFSNG_SUPER_MAGIC
66 # define AUTOFSNG_SUPER_MAGIC 0x7d92b1a0
67 # endif
68 # ifndef MSDOS_SUPER_MAGIC
69 # define MSDOS_SUPER_MAGIC 0x00004d44
70 # endif
71 # ifndef SMB_SUPER_MAGIC
72 # define SMB_SUPER_MAGIC 0x0000517B
73 #endif
74 # ifndef FUSE_SUPER_MAGIC
75 # define FUSE_SUPER_MAGIC 0x65735546
76 # endif
77 # ifndef RAMFS_MAGIC
78 # define RAMFS_MAGIC 0x858458F6
79 # endif
80 
81 // Reverse-engineering without C++ code:
82 // strace stat -f /mnt 2>&1|grep statfs|grep mnt, and look for f_type
83 
84 static KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
85 {
86  struct statfs buf;
87  if (statfs(path.constData(), &buf) != 0) {
88  //qCDebug(KCOREADDONS_DEBUG) << path << errno << strerror(errno);
89  return KFileSystemType::Unknown;
90  }
91  switch (static_cast<unsigned long>(buf.f_type)) {
92  case NFS_SUPER_MAGIC:
93  case AUTOFS_SUPER_MAGIC:
94  case AUTOFSNG_SUPER_MAGIC:
95  case FUSE_SUPER_MAGIC: // TODO could be anything. Need to use statfs() to find out more.
96  return KFileSystemType::Nfs;
97  case SMB_SUPER_MAGIC:
98  return KFileSystemType::Smb;
99  case MSDOS_SUPER_MAGIC:
100  return KFileSystemType::Fat;
101  case RAMFS_MAGIC:
102  return KFileSystemType::Ramfs;
103  default:
104  return KFileSystemType::Other;
105  }
106 }
107 
108 #elif defined(Q_OS_SOLARIS) || defined(Q_OS_IRIX) || defined(Q_OS_AIX) || defined(Q_OS_HPUX) \
109  || defined(Q_OS_OSF) || defined(Q_OS_QNX) || defined(Q_OS_SCO) \
110  || defined(Q_OS_UNIXWARE) || defined(Q_OS_RELIANT) || defined(Q_OS_NETBSD)
111 # include <sys/statvfs.h>
112 
113 KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
114 {
115  struct statvfs buf;
116  if (statvfs(path.constData(), &buf) != 0) {
117  return KFileSystemType::Unknown;
118  }
119 #if defined(Q_OS_NETBSD)
120  return kde_typeFromName(buf.f_fstypename);
121 #else
122  return kde_typeFromName(buf.f_basetype);
123 #endif
124 }
125 #endif
126 #else
127 KFileSystemType::Type determineFileSystemTypeImpl(const QByteArray &path)
128 {
129  return KFileSystemType::Unknown;
130 }
131 #endif
132 
134 {
135  return determineFileSystemTypeImpl(QFile::encodeName(path));
136 }
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-2020 The KDE developers.
Generated on Fri Aug 7 2020 23:00:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.