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 "fdsender.h" | ||||
29 | #include "../file_p.h" | ||||
30 | | ||||
31 | static bool sendFileDescriptor(int fd, const char *socketPath) | ||||
32 | { | ||||
33 | FdSender fdSender(socketPath); | ||||
34 | if (fdSender.isConnected() && fdSender.sendFileDescriptor(fd)) { | ||||
35 | return true; | ||||
36 | } | ||||
37 | return false; | ||||
38 | } | ||||
39 | | ||||
40 | ActionReply FileHelper::exec(const QVariantMap &args) | ||||
41 | { | ||||
42 | ActionReply reply; | ||||
43 | QByteArray data = args["arguments"].toByteArray(); | ||||
44 | QDataStream in(data); | ||||
45 | int action; | ||||
46 | QVariant arg1, arg2, arg3, arg4; | ||||
47 | in >> action >> arg1 >> arg2 >> arg3 >> arg4; | ||||
48 | | ||||
49 | // the path of an existing or a new file/dir upon which the method will operate | ||||
50 | const QByteArray path = arg1.toByteArray(); | ||||
51 | | ||||
52 | switch(action) { | ||||
53 | case CHMOD: { | ||||
54 | int mode = arg2.toInt(); | ||||
55 | if (chmod(path.data(), mode) == 0) { | ||||
56 | return reply; | ||||
57 | } | ||||
58 | break; | ||||
59 | } | ||||
60 | case CHOWN: { | ||||
61 | int uid = arg2.toInt(); | ||||
62 | int gid = arg3.toInt(); | ||||
63 | if (chown(path.data(), uid, gid) == 0) { | ||||
64 | return reply; | ||||
65 | } | ||||
66 | break; | ||||
67 | } | ||||
68 | case DEL: { | ||||
69 | if (unlink(path.data()) == 0) { | ||||
70 | return reply; | ||||
71 | } | ||||
72 | break; | ||||
73 | } | ||||
74 | case MKDIR: { | ||||
75 | if (mkdir(path.data(), 0777) == 0) { | ||||
76 | return reply; | ||||
77 | } | ||||
78 | break; | ||||
79 | } | ||||
80 | case OPEN: { | ||||
81 | int oflags = arg2.toInt(); | ||||
82 | int mode = arg3.toInt(); | ||||
83 | int fd = open(path.data(), oflags, mode); | ||||
84 | bool success = (fd != -1) && sendFileDescriptor(fd, arg4.toByteArray().constData()); | ||||
85 | close(fd); | ||||
86 | if (success) { | ||||
87 | return reply; | ||||
88 | } | ||||
89 | break; | ||||
90 | } | ||||
91 | case OPENDIR: { | ||||
92 | DIR *dp = opendir(path.data()); | ||||
93 | bool success = false; | ||||
94 | if (dp) { | ||||
95 | int fd = dirfd(dp); | ||||
96 | success = (fd != -1) && sendFileDescriptor(fd, arg4.toByteArray().constData()); | ||||
97 | closedir(dp); | ||||
98 | if (success) { | ||||
99 | return reply; | ||||
100 | } | ||||
101 | } | ||||
102 | break; | ||||
103 | } | ||||
104 | case RENAME: { | ||||
105 | const QByteArray newName = arg2.toByteArray(); | ||||
106 | if (rename(path.data(), newName.data()) == 0) { | ||||
107 | return reply; | ||||
108 | } | ||||
109 | break; | ||||
110 | } | ||||
111 | case RMDIR: { | ||||
112 | if (rmdir(path.data()) == 0) { | ||||
113 | return reply; | ||||
114 | } | ||||
115 | break; | ||||
116 | } | ||||
117 | case SYMLINK: { | ||||
118 | const QByteArray target = arg2.toByteArray(); | ||||
119 | if (symlink(target.data(), path.data()) == 0) { | ||||
120 | return reply; | ||||
121 | } | ||||
122 | break; | ||||
123 | } | ||||
124 | case UTIME: { | ||||
125 | utimbuf ut; | ||||
126 | ut.actime = arg2.toULongLong(); | ||||
127 | ut.modtime = arg3.toULongLong(); | ||||
128 | if (utime(path.data(), &ut) == 0) { | ||||
129 | return reply; | ||||
130 | } | ||||
131 | break; | ||||
132 | } | ||||
133 | }; | ||||
134 | | ||||
135 | reply.setError(errno ? errno : -1); | ||||
136 | return reply; | ||||
137 | } | ||||
138 | | ||||
139 | KAUTH_HELPER_MAIN("org.kde.kio.file", FileHelper) |