Changeset View
Changeset View
Standalone View
Standalone View
src/ioslaves/file/kauth/filehelper.cpp
- This file was added.
1 | /*** | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2017 by Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | ***/ | ||||
20 | | ||||
21 | #include <utime.h> | ||||
22 | #include <fcntl.h> | ||||
23 | #include <unistd.h> | ||||
24 | #include <dirent.h> | ||||
25 | #include <sys/stat.h> | ||||
26 | | ||||
27 | #include "filehelper.h" | ||||
28 | #include "../sharefd.h" | ||||
29 | | ||||
30 | static void sendFileDescriptor(int fd) | ||||
31 | { | ||||
32 | FdSender fdSender; | ||||
33 | fdSender.connectToPath("org_kde_kio_file_helper_socket"); | ||||
34 | if (fdSender.isConnected()) { | ||||
35 | fdSender.sendFileDescriptor(fd); | ||||
36 | return; | ||||
37 | } | ||||
38 | errno = -1; | ||||
39 | } | ||||
40 | | ||||
41 | enum { | ||||
42 | CHMOD = 1, | ||||
43 | CHOWN, | ||||
44 | DEL, | ||||
45 | MKDIR, | ||||
46 | OPEN, | ||||
47 | OPENDIR, | ||||
48 | RENAME, | ||||
49 | RMDIR, | ||||
50 | SYMLINK, | ||||
51 | UTIME, | ||||
52 | }; | ||||
53 | | ||||
54 | ActionReply FileHelper::exec(const QVariantMap &args) | ||||
55 | { | ||||
56 | errno = 0; | ||||
57 | | ||||
58 | ActionReply reply; | ||||
59 | QByteArray data = args["arguments"].toByteArray(); | ||||
60 | QDataStream in(data); | ||||
61 | int action; | ||||
62 | QVariant arg1, arg2, arg3; | ||||
63 | in >> action >> arg1 >> arg2 >> arg3; | ||||
64 | | ||||
65 | // the path of an existing or a new file/dir upon which the method will operate | ||||
66 | const QByteArray path = arg1.toByteArray(); | ||||
67 | | ||||
68 | if (action == CHMOD) { | ||||
69 | int mode = arg2.toInt(); | ||||
70 | chmod(path.data(), mode); | ||||
71 | } else if (action == CHOWN) { | ||||
72 | int uid = arg2.toInt(); | ||||
73 | int gid = arg3.toInt(); | ||||
74 | chown(path.data(), uid, gid); | ||||
75 | } else if (action == DEL) { | ||||
76 | unlink(path.data()); | ||||
77 | } else if (action == MKDIR) { | ||||
78 | mkdir(path.data(), 0777); | ||||
79 | } else if (action == OPEN) { | ||||
80 | int oflags = arg2.toInt(); | ||||
81 | int mode = arg3.toInt(); | ||||
82 | int fd = open(path.data(), oflags, mode); | ||||
83 | sendFileDescriptor(fd); | ||||
84 | close(fd); | ||||
85 | } else if(action == OPENDIR) { | ||||
86 | DIR *dp = opendir(path.data()); | ||||
87 | int fd = dirfd(dp); | ||||
88 | sendFileDescriptor(fd); | ||||
89 | closedir(dp); | ||||
90 | } else if (action == RENAME) { | ||||
91 | const QByteArray newName = arg2.toByteArray(); | ||||
92 | rename(path.data(), newName.data()); | ||||
93 | } else if (action == RMDIR) { | ||||
94 | rmdir(path.data()); | ||||
95 | } else if (action == SYMLINK) { | ||||
96 | const QByteArray target = arg2.toByteArray(); | ||||
97 | symlink(target.data(), path.data()); | ||||
98 | } else if (action == UTIME) { | ||||
99 | utimbuf ut; | ||||
100 | ut.actime = arg2.toULongLong(); | ||||
101 | ut.modtime = arg3.toULongLong(); | ||||
102 | utime(path.data(), &ut); | ||||
103 | } | ||||
104 | | ||||
105 | if (errno) | ||||
106 | reply.setError(errno); | ||||
107 | | ||||
108 | return reply; | ||||
109 | } | ||||
110 | | ||||
111 | | ||||
112 | KAUTH_HELPER_MAIN("org.kde.kio.file", FileHelper) |