OSDN Git Service

Merge branch 'master' of scm.dev.nokia.troll.no:creator/mainline
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / ssh / sftpoutgoingpacket.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "sftpoutgoingpacket_p.h"
31
32 #include "sshpacket_p.h"
33
34 #include <QtCore/QtEndian>
35
36 namespace Core {
37 namespace Internal {
38
39 namespace {
40     const quint32 DefaultAttributes = 0;
41     const quint32 SSH_FXF_READ = 0x00000001;
42     const quint32 SSH_FXF_WRITE = 0x00000002;
43     const quint32 SSH_FXF_APPEND = 0x00000004;
44     const quint32 SSH_FXF_CREAT = 0x00000008;
45     const quint32 SSH_FXF_TRUNC = 0x00000010;
46     const quint32 SSH_FXF_EXCL = 0x00000020;
47 }
48
49 SftpOutgoingPacket::SftpOutgoingPacket()
50 {
51 }
52
53 SftpOutgoingPacket &SftpOutgoingPacket::generateInit(quint32 version)
54 {
55     return init(SSH_FXP_INIT, 0).appendInt(version).finalize();
56 }
57
58 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenDir(const QString &path,
59     quint32 requestId)
60 {
61     return init(SSH_FXP_OPENDIR, requestId).appendString(path).finalize();
62 }
63
64 SftpOutgoingPacket &SftpOutgoingPacket::generateReadDir(const QByteArray &handle,
65     quint32 requestId)
66 {
67     return init(SSH_FXP_READDIR, requestId).appendString(handle).finalize();
68 }
69
70 SftpOutgoingPacket &SftpOutgoingPacket::generateCloseHandle(const QByteArray &handle,
71     quint32 requestId)
72 {
73     return init(SSH_FXP_CLOSE, requestId).appendString(handle).finalize();
74 }
75
76 SftpOutgoingPacket &SftpOutgoingPacket::generateMkDir(const QString &path,
77     quint32 requestId)
78 {
79     return init(SSH_FXP_MKDIR, requestId).appendString(path)
80         .appendInt(DefaultAttributes).finalize();
81 }
82
83 SftpOutgoingPacket &SftpOutgoingPacket::generateRmDir(const QString &path,
84     quint32 requestId)
85 {
86     return init(SSH_FXP_RMDIR, requestId).appendString(path).finalize();
87 }
88
89 SftpOutgoingPacket &SftpOutgoingPacket::generateRm(const QString &path,
90     quint32 requestId)
91 {
92     return init(SSH_FXP_REMOVE, requestId).appendString(path).finalize();
93 }
94
95 SftpOutgoingPacket &SftpOutgoingPacket::generateRename(const QString &oldPath,
96     const QString &newPath, quint32 requestId)
97 {
98     return init(SSH_FXP_RENAME, requestId).appendString(oldPath)
99         .appendString(newPath).finalize();
100 }
101
102 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForWriting(const QString &path,
103     SftpOverwriteMode mode, quint32 requestId)
104 {
105     return generateOpenFile(path, Write, mode, requestId);
106 }
107
108 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForReading(const QString &path,
109     quint32 requestId)
110 {
111     // Note: Overwrite mode is irrelevant and will be ignored.
112     return generateOpenFile(path, Read, SftpSkipExisting, requestId);
113 }
114
115 SftpOutgoingPacket &SftpOutgoingPacket::generateReadFile(const QByteArray &handle,
116     quint64 offset, quint32 length, quint32 requestId)
117 {
118     return init(SSH_FXP_READ, requestId).appendString(handle).appendInt64(offset)
119         .appendInt(length).finalize();
120 }
121
122 SftpOutgoingPacket &SftpOutgoingPacket::generateFstat(const QByteArray &handle,
123     quint32 requestId)
124 {
125     return init(SSH_FXP_FSTAT, requestId).appendString(handle).finalize();
126 }
127
128 SftpOutgoingPacket &SftpOutgoingPacket::generateWriteFile(const QByteArray &handle,
129     quint64 offset, const QByteArray &data, quint32 requestId)
130 {
131     return init(SSH_FXP_WRITE, requestId).appendString(handle)
132         .appendInt64(offset).appendString(data).finalize();
133 }
134
135 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFile(const QString &path,
136     OpenType openType, SftpOverwriteMode mode, quint32 requestId)
137 {
138     quint32 pFlags;
139     switch (openType) {
140     case Read:
141         pFlags = SSH_FXF_READ;
142         break;
143     case Write:
144         pFlags = SSH_FXF_WRITE | SSH_FXF_CREAT;
145         switch (mode) {
146         case SftpOverwriteExisting: pFlags |= SSH_FXF_TRUNC; break;
147         case SftpAppendToExisting: pFlags |= SSH_FXF_APPEND; break;
148         case SftpSkipExisting: pFlags |= SSH_FXF_EXCL; break;
149         }
150         break;
151     }
152     return init(SSH_FXP_OPEN, requestId).appendString(path).appendInt(pFlags)
153         .appendInt(DefaultAttributes).finalize();
154 }
155
156 SftpOutgoingPacket &SftpOutgoingPacket::init(SftpPacketType type,
157     quint32 requestId)
158 {
159     m_data.resize(TypeOffset + 1);
160     m_data[TypeOffset] = type;
161     if (type != SSH_FXP_INIT) {
162         appendInt(requestId);
163 #ifdef CREATOR_SSH_DEBUG
164         qDebug("Generating SFTP packet of type %d with request id %u", type,
165             requestId);
166 #endif
167     }
168     return *this;
169 }
170
171 SftpOutgoingPacket &SftpOutgoingPacket::appendInt(quint32 val)
172 {
173     m_data.append(AbstractSshPacket::encodeInt(val));
174     return *this;
175 }
176
177 SftpOutgoingPacket &SftpOutgoingPacket::appendInt64(quint64 value)
178 {
179     m_data.append(AbstractSshPacket::encodeInt(value));
180     return *this;
181 }
182
183 SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QString &string)
184 {
185     m_data.append(AbstractSshPacket::encodeString(string.toUtf8()));
186     return *this;
187 }
188
189 SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QByteArray &string)
190 {
191     m_data += AbstractSshPacket::encodeString(string);
192     return *this;
193 }
194
195 SftpOutgoingPacket &SftpOutgoingPacket::finalize()
196 {
197     AbstractSshPacket::setLengthField(m_data);
198     return *this;
199 }
200
201 } // namespace Internal
202 } // namespace Core