1 /***************************************************************************
2 fish.h - a FISH kioslave
4 begin : Thu Oct 4 17:09:14 CEST 2001
5 copyright : (C) 2001 by Jörg Walter
6 email : trouble@garni.ch
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation, version 2 of the License *
15 ***************************************************************************/
20 #include <kio/global.h>
21 #include <kio/slavebase.h>
23 #include <kio/authinfo.h>
26 #define FISH_EXEC_CMD 'X'
28 class fishProtocol
: public KIO::SlaveBase
31 fishProtocol(const QByteArray
&pool_socket
, const QByteArray
&app_socket
);
32 virtual ~fishProtocol();
35 Connects to a server and logs us in via SSH. Then starts FISH protocol.
36 @ref isConnected is set to true if logging on was successful.
37 It is set to false if the connection becomes closed.
40 void openConnection();
45 void shutdownConnection(bool forced
=false);
46 /** sets connection information for subsequent commands */
47 void setHost(const QString
& host
, quint16 port
, const QString
& user
, const QString
& pass
);
48 /** Forced close of the connection */
49 void closeConnection();
51 void get(const KUrl
& url
);
53 void put(const KUrl
& url
, int permissions
, KIO::JobFlags flags
);
54 /** aborts command sequence and calls error() */
55 void error(int type
, const QString
&detail
);
56 /** executes next command in sequence or calls finished() if all is done */
59 void stat(const KUrl
& url
);
60 /** find mimetype for a file */
61 void mimetype(const KUrl
& url
);
62 /** list a directory */
63 void listDir(const KUrl
& url
);
64 /** create a directory */
65 void mkdir(const KUrl
&url
, int permissions
);
67 void rename(const KUrl
& src
, const KUrl
& dest
, KIO::JobFlags flags
);
68 /** create a symlink */
69 void symlink(const QString
& target
, const KUrl
& dest
, KIO::JobFlags flags
);
70 /** change file permissions */
71 void chmod(const KUrl
& url
, int permissions
);
73 void copy(const KUrl
&src
, const KUrl
&dest
, int permissions
, KIO::JobFlags flags
);
76 /** removes a file or directory */
77 void del(const KUrl
&u
, bool isfile
);
78 /** special like background execute */
79 void special( const QByteArray
&data
);
81 private: // Private attributes
82 /** fd for reading and writing to the process */
84 /** buffer for data to be written */
90 /** current write position in buffer */
91 KIO::fileoffset_t outBufPos
;
92 /** length of buffer */
93 KIO::fileoffset_t outBufLen
;
94 /** use su if true else use ssh */
96 /** // FIXME: just a workaround for konq deficiencies */
98 /** // FIXME: just a workaround for konq deficiencies */
99 QString redirectUser
, redirectPass
;
101 protected: // Protected attributes
103 KIO::UDSEntry udsEntry
;
105 KIO::UDSEntry udsStatEntry
;
115 /** url of current request */
117 /** true if connection is logged in successfully */
119 /** host name of current connection */
120 QString connectionHost
;
121 /** user name of current connection */
122 QString connectionUser
;
123 /** port of current connection */
125 /** password of current connection */
126 QString connectionPassword
;
127 /** AuthInfo object used for logging in */
128 KIO::AuthInfo connectionAuth
;
129 /** number of lines received, == 0 -> everything went ok */
131 /** queue for lines to be sent */
132 QList
<QByteArray
> qlist
;
133 /** queue for commands to be sent */
134 QStringList commandList
;
135 /** queue for commands to be sent */
136 QList
<int> commandCodes
;
137 /** bytes still to be read in raw mode */
138 KIO::fileoffset_t rawRead
;
139 /** bytes still to be written in raw mode */
140 KIO::fileoffset_t rawWrite
;
141 /** data bytes to read in next read command */
142 KIO::fileoffset_t recvLen
;
143 /** data bytes to write in next write command */
144 KIO::fileoffset_t sendLen
;
145 /** true if the last write operation was finished */
147 /** true if a command stack is currently executing */
149 /** reason of LIST command */
150 enum { CHECK
, LIST
} listReason
;
151 /** true if FISH server understands APPEND command */
153 /** permission of created file */
155 /** true if file may be overwritten */
157 /** current position of write */
158 KIO::fileoffset_t putPos
;
159 /** true if file already existed */
161 /** true if this is the first login attempt (== use cached password) */
165 /** buffer for storing bytes used for MimeMagic */
166 QByteArray mimeBuffer
;
167 /** whther the mimetype has been sent already */
169 /** number of bytes read so far */
170 KIO::fileoffset_t dataRead
;
171 /** details about each fishCommand */
172 static const struct fish_info
{
178 /** last FISH command sent to server */
179 enum fish_command_type
{ FISH_FISH
, FISH_VER
, FISH_PWD
, FISH_LIST
, FISH_STAT
,
180 FISH_RETR
, FISH_STOR
,
181 FISH_CWD
, FISH_CHMOD
, FISH_DELE
, FISH_MKD
, FISH_RMD
,
182 FISH_RENAME
, FISH_LINK
, FISH_SYMLINK
, FISH_CHOWN
,
183 FISH_CHGRP
, FISH_READ
, FISH_WRITE
, FISH_COPY
, FISH_APPEND
, FISH_EXEC
} fishCommand
;
185 protected: // Protected methods
186 /** manages initial communication setup including password queries */
188 int establishConnection(char *buffer
, KIO::fileoffset_t buflen
);
190 int establishConnection(const QByteArray
&buffer
);
192 int received(const char *buffer
, KIO::fileoffset_t buflen
);
194 /** builds each FISH request and sets the error counter */
195 bool sendCommand(fish_command_type cmd
, ...);
196 /** checks response string for result code, converting 000 and 001 appropriately */
197 int handleResponse(const QString
&str
);
198 /** parses a ls -l time spec */
199 int makeTimeFromLs(const QString
&dayStr
, const QString
&monthStr
, const QString
&timeyearStr
);
200 /** executes a chain of commands */
202 /** creates the subprocess */
203 bool connectionStart();
204 /** writes one chunk of data to stdin of child process */
206 void writeChild(const char *buf
, KIO::fileoffset_t len
);
208 void writeChild(const QByteArray
&buf
, KIO::fileoffset_t len
);
210 /** parses response from server and acts accordingly */
211 void manageConnection(const QString
&line
);
212 /** writes to process */
213 void writeStdin(const QString
&line
);
215 void setHostInternal(const KUrl
& u
);