OSDN Git Service

21b29b67c487b67e4bc721e1eae1fc741508bd69
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / ssh / sftpoutgoingpacket.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "sftpoutgoingpacket_p.h"
35
36 #include "sshpacket_p.h"
37
38 #include <QtCore/QtEndian>
39
40 namespace Utils {
41 namespace Internal {
42
43 namespace {
44     const quint32 DefaultAttributes = 0;
45     const quint32 SSH_FXF_READ = 0x00000001;
46     const quint32 SSH_FXF_WRITE = 0x00000002;
47     const quint32 SSH_FXF_APPEND = 0x00000004;
48     const quint32 SSH_FXF_CREAT = 0x00000008;
49     const quint32 SSH_FXF_TRUNC = 0x00000010;
50     const quint32 SSH_FXF_EXCL = 0x00000020;
51 }
52
53 SftpOutgoingPacket::SftpOutgoingPacket()
54 {
55 }
56
57 SftpOutgoingPacket &SftpOutgoingPacket::generateInit(quint32 version)
58 {
59     return init(SSH_FXP_INIT, 0).appendInt(version).finalize();
60 }
61
62 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenDir(const QString &path,
63     quint32 requestId)
64 {
65     return init(SSH_FXP_OPENDIR, requestId).appendString(path).finalize();
66 }
67
68 SftpOutgoingPacket &SftpOutgoingPacket::generateReadDir(const QByteArray &handle,
69     quint32 requestId)
70 {
71     return init(SSH_FXP_READDIR, requestId).appendString(handle).finalize();
72 }
73
74 SftpOutgoingPacket &SftpOutgoingPacket::generateCloseHandle(const QByteArray &handle,
75     quint32 requestId)
76 {
77     return init(SSH_FXP_CLOSE, requestId).appendString(handle).finalize();
78 }
79
80 SftpOutgoingPacket &SftpOutgoingPacket::generateMkDir(const QString &path,
81     quint32 requestId)
82 {
83     return init(SSH_FXP_MKDIR, requestId).appendString(path)
84         .appendInt(DefaultAttributes).finalize();
85 }
86
87 SftpOutgoingPacket &SftpOutgoingPacket::generateRmDir(const QString &path,
88     quint32 requestId)
89 {
90     return init(SSH_FXP_RMDIR, requestId).appendString(path).finalize();
91 }
92
93 SftpOutgoingPacket &SftpOutgoingPacket::generateRm(const QString &path,
94     quint32 requestId)
95 {
96     return init(SSH_FXP_REMOVE, requestId).appendString(path).finalize();
97 }
98
99 SftpOutgoingPacket &SftpOutgoingPacket::generateRename(const QString &oldPath,
100     const QString &newPath, quint32 requestId)
101 {
102     return init(SSH_FXP_RENAME, requestId).appendString(oldPath)
103         .appendString(newPath).finalize();
104 }
105
106 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForWriting(const QString &path,
107     SftpOverwriteMode mode, quint32 requestId)
108 {
109     return generateOpenFile(path, Write, mode, requestId);
110 }
111
112 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForReading(const QString &path,
113     quint32 requestId)
114 {
115     // Note: Overwrite mode is irrelevant and will be ignored.
116     return generateOpenFile(path, Read, SftpSkipExisting, requestId);
117 }
118
119 SftpOutgoingPacket &SftpOutgoingPacket::generateReadFile(const QByteArray &handle,
120     quint64 offset, quint32 length, quint32 requestId)
121 {
122     return init(SSH_FXP_READ, requestId).appendString(handle).appendInt64(offset)
123         .appendInt(length).finalize();
124 }
125
126 SftpOutgoingPacket &SftpOutgoingPacket::generateFstat(const QByteArray &handle,
127     quint32 requestId)
128 {
129     return init(SSH_FXP_FSTAT, requestId).appendString(handle).finalize();
130 }
131
132 SftpOutgoingPacket &SftpOutgoingPacket::generateWriteFile(const QByteArray &handle,
133     quint64 offset, const QByteArray &data, quint32 requestId)
134 {
135     return init(SSH_FXP_WRITE, requestId).appendString(handle)
136         .appendInt64(offset).appendString(data).finalize();
137 }
138
139 SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFile(const QString &path,
140     OpenType openType, SftpOverwriteMode mode, quint32 requestId)
141 {
142     quint32 pFlags;
143     switch (openType) {
144     case Read:
145         pFlags = SSH_FXF_READ;
146         break;
147     case Write:
148         pFlags = SSH_FXF_WRITE | SSH_FXF_CREAT;
149         switch (mode) {
150         case SftpOverwriteExisting: pFlags |= SSH_FXF_TRUNC; break;
151         case SftpAppendToExisting: pFlags |= SSH_FXF_APPEND; break;
152         case SftpSkipExisting: pFlags |= SSH_FXF_EXCL; break;
153         }
154         break;
155     }
156     return init(SSH_FXP_OPEN, requestId).appendString(path).appendInt(pFlags)
157         .appendInt(DefaultAttributes).finalize();
158 }
159
160 SftpOutgoingPacket &SftpOutgoingPacket::init(SftpPacketType type,
161     quint32 requestId)
162 {
163     m_data.resize(TypeOffset + 1);
164     m_data[TypeOffset] = type;
165     if (type != SSH_FXP_INIT) {
166         appendInt(requestId);
167 #ifdef CREATOR_SSH_DEBUG
168         qDebug("Generating SFTP packet of type %d with request id %u", type,
169             requestId);
170 #endif
171     }
172     return *this;
173 }
174
175 SftpOutgoingPacket &SftpOutgoingPacket::appendInt(quint32 val)
176 {
177     m_data.append(AbstractSshPacket::encodeInt(val));
178     return *this;
179 }
180
181 SftpOutgoingPacket &SftpOutgoingPacket::appendInt64(quint64 value)
182 {
183     m_data.append(AbstractSshPacket::encodeInt(value));
184     return *this;
185 }
186
187 SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QString &string)
188 {
189     m_data.append(AbstractSshPacket::encodeString(string.toUtf8()));
190     return *this;
191 }
192
193 SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QByteArray &string)
194 {
195     m_data += AbstractSshPacket::encodeString(string);
196     return *this;
197 }
198
199 SftpOutgoingPacket &SftpOutgoingPacket::finalize()
200 {
201     AbstractSshPacket::setLengthField(m_data);
202     return *this;
203 }
204
205 } // namespace Internal
206 } // namespace Utils