File indexing completed on 2024-06-16 07:42:25

0001 /*
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include <config-k3b.h>
0007 
0008 #include "k3bfilesysteminfo.h"
0009 
0010 #include "k3bglobals.h"
0011 
0012 #include <QDebug>
0013 #include <QFile>
0014 #include <QFileInfo>
0015 #include <QRegularExpression>
0016 
0017 #ifdef Q_OS_FREEBSD
0018 #include <sys/param.h>
0019 #include <sys/mount.h>
0020 #endif
0021 #ifdef HAVE_SYS_STATVFS_H
0022 #  include <sys/statvfs.h>
0023 #  if defined(Q_OS_NETBSD)
0024 #    include <sys/param.h>
0025 #    if __NetBSD_Version__ > 299000000
0026 #      define statfs        statvfs
0027 #      define f_type        f_fsid
0028 #    endif
0029 #  endif
0030 #endif
0031 #ifdef HAVE_SYS_VFS_H
0032 #  include <sys/vfs.h>
0033 #endif
0034 
0035 #include <errno.h>
0036 #include <string.h>
0037 
0038 
0039 
0040 class K3b::FileSystemInfo::Private
0041 {
0042 public:
0043     Private()
0044         : type(FS_UNKNOWN),
0045           statDone(false) {
0046     }
0047 
0048     FileSystemType type;
0049     QString path;
0050 
0051     bool statDone;
0052 
0053     void stat() {
0054 #ifndef Q_OS_WIN32
0055         struct statfs fs;
0056         if( !::statfs( QFile::encodeName( QFileInfo(path).absolutePath() ), &fs ) ) {
0057             switch( fs.f_type ) {
0058             case 0x4d44: // MS-DOS
0059                 type = FS_FAT;
0060         break;
0061             default:
0062                 type = FS_UNKNOWN;
0063             }
0064 
0065             statDone = true;
0066         }
0067         else {
0068             qDebug() << "(K3b::FileSystemInfo) statfs failed: " << QString::fromLocal8Bit( ::strerror(errno) );
0069         }
0070 #else
0071         statDone = true;
0072 #endif
0073     }
0074 };
0075 
0076 
0077 K3b::FileSystemInfo::FileSystemInfo()
0078 {
0079     d = new Private;
0080 }
0081 
0082 
0083 K3b::FileSystemInfo::FileSystemInfo( const QString& path )
0084 {
0085     d = new Private;
0086     d->path = path;
0087 }
0088 
0089 
0090 K3b::FileSystemInfo::FileSystemInfo( const K3b::FileSystemInfo& other )
0091 {
0092     d = new Private;
0093     d->type = other.d->type;
0094     d->path = other.d->path;
0095     d->statDone = other.d->statDone;
0096 }
0097 
0098 
0099 K3b::FileSystemInfo::~FileSystemInfo()
0100 {
0101     delete d;
0102 }
0103 
0104 
0105 QString K3b::FileSystemInfo::path() const
0106 {
0107     return d->path;
0108 }
0109 
0110 
0111 void K3b::FileSystemInfo::setPath( const QString& path )
0112 {
0113     if( d->path != path ) {
0114         d->path = path;
0115         d->statDone = false;
0116     }
0117 }
0118 
0119 
0120 K3b::FileSystemInfo::FileSystemType K3b::FileSystemInfo::type() const
0121 {
0122     if( !d->statDone )
0123         d->stat();
0124     return d->type;
0125 }
0126 
0127 
0128 QString K3b::FileSystemInfo::fixupPath( const QString& path )
0129 {
0130     QString s = K3b::fixupPath( path );
0131     if( type() == K3b::FileSystemInfo::FS_FAT ) {
0132         static const QRegularExpression rx("[\"\\?\\*/\\\\[\\]\\|\\=\\:;]");
0133         return s.replace( rx, "_" );
0134     }
0135     else
0136         return s;
0137 }