1 | /* |
2 | SPDX-FileCopyrightText: 2000-2002 Stephan Kulow <coolo@kde.org> |
3 | SPDX-FileCopyrightText: 2000-2002 David Faure <faure@kde.org> |
4 | SPDX-FileCopyrightText: 2000-2002 Waldo Bastian <bastian@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef __file_h__ |
10 | #define __file_h__ |
11 | |
12 | #include <kio/global.h> |
13 | #include <kio/workerbase.h> |
14 | |
15 | #include <KUser> |
16 | #include <QFile> |
17 | #include <QHash> |
18 | #include <QObject> |
19 | |
20 | #include <config-kioworker-file.h> |
21 | #include <qplatformdefs.h> // mode_t |
22 | |
23 | #if HAVE_SYS_ACL_H |
24 | #include <sys/acl.h> |
25 | #endif |
26 | #if HAVE_ACL_LIBACL_H |
27 | #include <acl/libacl.h> |
28 | #endif |
29 | |
30 | #include "file_p.h" |
31 | |
32 | #include <QLoggingCategory> |
33 | Q_DECLARE_LOGGING_CATEGORY(KIO_FILE) |
34 | |
35 | class FileProtocol : public QObject, public KIO::WorkerBase |
36 | { |
37 | Q_OBJECT |
38 | public: |
39 | FileProtocol(const QByteArray &pool, const QByteArray &app); |
40 | ~FileProtocol() override; |
41 | |
42 | KIO::WorkerResult get(const QUrl &url) override; |
43 | virtual KIO::WorkerResult put(const QUrl &url, int _mode, KIO::JobFlags _flags) override; |
44 | virtual KIO::WorkerResult copy(const QUrl &src, const QUrl &dest, int mode, KIO::JobFlags flags) override; |
45 | virtual KIO::WorkerResult rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) override; |
46 | virtual KIO::WorkerResult symlink(const QString &target, const QUrl &dest, KIO::JobFlags flags) override; |
47 | |
48 | KIO::WorkerResult stat(const QUrl &url) override; |
49 | KIO::WorkerResult listDir(const QUrl &url) override; |
50 | KIO::WorkerResult mkdir(const QUrl &url, int permissions) override; |
51 | KIO::WorkerResult chmod(const QUrl &url, int permissions) override; |
52 | KIO::WorkerResult chown(const QUrl &url, const QString &owner, const QString &group) override; |
53 | KIO::WorkerResult setModificationTime(const QUrl &url, const QDateTime &mtime) override; |
54 | KIO::WorkerResult del(const QUrl &url, bool isfile) override; |
55 | KIO::WorkerResult open(const QUrl &url, QIODevice::OpenMode mode) override; |
56 | KIO::WorkerResult read(KIO::filesize_t size) override; |
57 | KIO::WorkerResult write(const QByteArray &data) override; |
58 | KIO::WorkerResult seek(KIO::filesize_t offset) override; |
59 | KIO::WorkerResult truncate(KIO::filesize_t length) override; |
60 | bool copyXattrs(const int src_fd, const int dest_fd); |
61 | KIO::WorkerResult close() override; |
62 | |
63 | KIO::WorkerResult fileSystemFreeSpace(const QUrl &url) override; |
64 | |
65 | /** |
66 | * Special commands supported by this worker: |
67 | * 1 - mount |
68 | * 2 - unmount |
69 | */ |
70 | KIO::WorkerResult special(const QByteArray &data) override; |
71 | KIO::WorkerResult unmount(const QString &point); |
72 | KIO::WorkerResult mount(bool _ro, const char *_fstype, const QString &dev, const QString &point); |
73 | |
74 | #if HAVE_POSIX_ACL |
75 | static bool isExtendedACL(acl_t acl); |
76 | #endif |
77 | |
78 | private: |
79 | int setACL(const char *path, mode_t perm, bool _directoryDefault); |
80 | QString getUserName(KUserId uid) const; |
81 | QString getGroupName(KGroupId gid) const; |
82 | KIO::WorkerResult deleteRecursive(const QString &path); |
83 | |
84 | bool privilegeOperationUnitTestMode(); |
85 | KIO::WorkerResult execWithElevatedPrivilege(ActionType action, const QVariantList &args, int errcode); |
86 | KIO::WorkerResult tryOpen(QFile &f, const QByteArray &path, int flags, int mode, int errcode); |
87 | |
88 | // We want to execute chmod/chown/utime with elevated privileges (in copy & put) |
89 | // only during the brief period privileges are elevated. If it's not the case show |
90 | // a warning and continue. |
91 | KIO::WorkerResult tryChangeFileAttr(ActionType action, const QVariantList &args, int errcode); |
92 | |
93 | KIO::WorkerResult redirect(const QUrl &url); |
94 | |
95 | // Close without calling finish(). Use this to close after error. |
96 | void closeWithoutFinish(); |
97 | |
98 | private: |
99 | QFile *mFile; |
100 | |
101 | bool resultWasCancelled(KIO::WorkerResult result); |
102 | |
103 | bool testMode = false; |
104 | KIO::StatDetails getStatDetails(); |
105 | }; |
106 | |
107 | #endif |
108 | |