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 ***************************************************************************/
22 #include <kio/global.h>
23 #include <kio/slavebase.h>
24 #include <k3process.h>
25 #include <kio/authinfo.h>
28 #define FISH_EXEC_CMD 'X'
30 class fishProtocol
: public KIO::SlaveBase
33 fishProtocol(const QByteArray
&pool_socket
, const QByteArray
&app_socket
);
34 virtual ~fishProtocol();
37 Connects to a server and logs us in via SSH. Then starts FISH protocol.
38 @ref isConnected is set to true if logging on was successful.
39 It is set to false if the connection becomes closed.
42 void openConnection();
47 void shutdownConnection(bool forced
=false);
48 /** sets connection information for subsequent commands */
49 void setHost(const QString
& host
, quint16 port
, const QString
& user
, const QString
& pass
);
50 /** Forced close of the connection */
51 void closeConnection();
53 void get(const KUrl
& url
);
55 void put(const KUrl
& url
, int permissions
, KIO::JobFlags flags
);
56 /** aborts command sequence and calls error() */
57 void error(int type
, const QString
&detail
);
58 /** executes next command in sequence or calls finished() if all is done */
61 void stat(const KUrl
& url
);
62 /** find mimetype for a file */
63 void mimetype(const KUrl
& url
);
64 /** list a directory */
65 void listDir(const KUrl
& url
);
66 /** create a directory */
67 void mkdir(const KUrl
&url
, int permissions
);
69 void rename(const KUrl
& src
, const KUrl
& dest
, KIO::JobFlags flags
);
70 /** create a symlink */
71 void symlink(const QString
& target
, const KUrl
& dest
, KIO::JobFlags flags
);
72 /** change file permissions */
73 void chmod(const KUrl
& url
, int permissions
);
75 void copy(const KUrl
&src
, const KUrl
&dest
, int permissions
, KIO::JobFlags flags
);
78 /** removes a file or directory */
79 void del(const KUrl
&u
, bool isfile
);
80 /** special like background execute */
81 void special( const QByteArray
&data
);
83 private: // Private attributes
84 /** the SSH process used to communicate with the remote end */
86 /** fd for reading and writing to the process */
88 /** 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 */
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 */
187 int establishConnection(char *buffer
, KIO::fileoffset_t buflen
);
188 int received(const char *buffer
, KIO::fileoffset_t buflen
);
190 /** builds each FISH request and sets the error counter */
191 bool sendCommand(fish_command_type cmd
, ...);
192 /** checks response string for result code, converting 000 and 001 appropriately */
193 int handleResponse(const QString
&str
);
194 /** parses a ls -l time spec */
195 int makeTimeFromLs(const QString
&dayStr
, const QString
&monthStr
, const QString
&timeyearStr
);
196 /** executes a chain of commands */
198 /** creates the subprocess */
199 bool connectionStart();
200 /** writes one chunk of data to stdin of child process */
201 void writeChild(const char *buf
, KIO::fileoffset_t len
);
202 /** parses response from server and acts accordingly */
203 void manageConnection(const QString
&line
);
204 /** writes to process */
205 void writeStdin(const QString
&line
);
207 void setHostInternal(const KUrl
& u
);