summaryrefslogtreecommitdiffstats
path: root/tdeioslave/sftp/sftpfileattr.cpp
blob: ddd94bbe814802bbcf35361eb8f73f3d7e2a0044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/***************************************************************************
                          sftpfileattr.cpp  -  description
                             -------------------
    begin                : Sat Jun 30 2001
    copyright            : (C) 2001 by Lucas Fisher
    email                : ljfisher@iastate.edu
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "sftpfileattr.h"

#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <tqstring.h>
#include <tqdatastream.h>

#include <tdeio/global.h>
#include <kremoteencoding.h>

using namespace TDEIO;

sftpFileAttr::sftpFileAttr(){
    clear();
    mDirAttrs = false;
}

sftpFileAttr::sftpFileAttr(KRemoteEncoding* encoding){
    clear();
    mDirAttrs = false;
    mEncoding = encoding;
}


/** Constructor to initialize the file attributes on declaration. */
sftpFileAttr::sftpFileAttr(TQ_ULLONG size, uid_t uid, gid_t gid,
                    mode_t permissions, time_t atime,
                    time_t mtime, TQ_UINT32 extendedCount) {
    clear();
    mDirAttrs = false;
    mSize  = size;
    mUid   = uid;
    mGid   = gid;
    mAtime = atime;
    mMtime = mtime;
    mPermissions   = permissions;
    mExtendedCount = extendedCount;
}

sftpFileAttr::~sftpFileAttr(){
}

/** Returns a UDSEntry describing the file.
The UDSEntry is generated from the sftp file attributes. */
UDSEntry sftpFileAttr::entry() {
    UDSEntry entry;
    UDSAtom atom;

    atom.m_uds = UDS_NAME;
    atom.m_str = mFilename;
    entry.append(atom);

    if( mFlags & SSH2_FILEXFER_ATTR_SIZE ) {
        atom.m_uds = UDS_SIZE;
        atom.m_long = mSize;
        entry.append(atom);
    }

    if( mFlags & SSH2_FILEXFER_ATTR_ACMODTIME ) {
        atom.m_uds = UDS_ACCESS_TIME;
        atom.m_long = mAtime;
        entry.append(atom);

        atom.m_uds = UDS_MODIFICATION_TIME;
        atom.m_long = mMtime;
        entry.append(atom);
    }

    if( mFlags & SSH2_FILEXFER_ATTR_UIDGID ) {
        if( mUserName.isEmpty() || mGroupName.isEmpty() )
            getUserGroupNames();

        atom.m_uds = UDS_USER;
        atom.m_str = mUserName;
        entry.append(atom);

        atom.m_uds = UDS_GROUP;
        atom.m_str = mGroupName;
        entry.append(atom);
    }

    if( mFlags & SSH2_FILEXFER_ATTR_PERMISSIONS ) {
        atom.m_uds = UDS_ACCESS;
        atom.m_long = mPermissions;
        entry.append(atom);

        mode_t type = fileType();

        // Set the type if we know what it is
        if( type != 0 ) {
            atom.m_uds = UDS_FILE_TYPE;
            atom.m_long = (mLinkType ? mLinkType:type);
            entry.append(atom);
        }

        if( S_ISLNK(type) ) {
            atom.m_uds = UDS_LINK_DEST;
            atom.m_str = mLinkDestination;
            entry.append(atom);
        }
    }

    return entry;
}

/** Use to output the file attributes to a sftp packet */
TQDataStream& operator<< (TQDataStream& s, const sftpFileAttr& fa) {
    s << (TQ_UINT32)fa.mFlags;

    if( fa.mFlags & SSH2_FILEXFER_ATTR_SIZE )
        { s << (TQ_ULLONG)fa.mSize; }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_UIDGID )
        { s << (TQ_UINT32)fa.mUid << (TQ_UINT32)fa.mGid; }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_PERMISSIONS )
        { s << (TQ_UINT32)fa.mPermissions; }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_ACMODTIME )
        { s << (TQ_UINT32)fa.mAtime << (TQ_UINT32)fa.mMtime; }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_EXTENDED ) {
        s << (TQ_UINT32)fa.mExtendedCount;
        // XXX: Write extensions to data stream here
        // s.writeBytes(extendedtype).writeBytes(extendeddata);
    }
    return s;
}


/** Use to read a file attribute from a sftp packet */
TQDataStream& operator>> (TQDataStream& s, sftpFileAttr& fa) {

    // XXX Add some error checking in here in case
    //     we get a bad sftp packet.

    fa.clear();

    if( fa.mDirAttrs ) {
        TQCString fn;
        s >> fn;
        fn.truncate( fn.size() );

        fa.mFilename = fa.mEncoding->decode( fn );

        s >> fa.mLongname;
        fa.mLongname.truncate( fa.mLongname.size() );
        //kdDebug() << ">>: ftpfileattr long filename (" << fa.mLongname.size() << ")= " << fa.mLongname <<  endl;
    }

    s >> fa.mFlags;  // get flags

    if( fa.mFlags & SSH2_FILEXFER_ATTR_SIZE ) {
        TQ_ULLONG fileSize;
        s >> fileSize;
        fa.setFileSize(fileSize);
    }

    TQ_UINT32 x;

    if( fa.mFlags & SSH2_FILEXFER_ATTR_UIDGID ) {
        s >> x; fa.setUid(x);
        s >> x; fa.setGid(x);
    }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_PERMISSIONS ) {
        s >> x; fa.setPermissions(x);
    }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_ACMODTIME ) {
        s >> x; fa.setAtime(x);
        s >> x; fa.setMtime(x);
    }

    if( fa.mFlags & SSH2_FILEXFER_ATTR_EXTENDED ) {
        s >> x; fa.setExtendedCount(x);
        // XXX: Read in extensions from data stream here
        // s.readBytes(extendedtype).readBytes(extendeddata);
    }

    fa.getUserGroupNames();
    return s;
}
/** Parse longname for the owner and group names. */
void sftpFileAttr::getUserGroupNames(){
    // Get the name of the owner and group of the file from longname.
    TQString user, group;
    if( mLongname.isEmpty() ) {
        // do not have the user name so use the user id instead
        user.setNum(mUid);
        group.setNum(mGid);
    }
    else {
        int field = 0;
        int i = 0;
        int l = mLongname.length();

        TQString longName = mEncoding->decode( mLongname );

        kdDebug(7120) << "Decoded:  " << longName << endl;

        // Find the beginning of the third field which contains the user name.
        while( field != 2 ) {
            if( longName[i].isSpace() ) {
                field++; i++;
                while( i < l && longName[i].isSpace() ) { i++; }
            }
            else { i++; }
        }
        // i is the index of the first character of the third field.
        while( i < l && !longName[i].isSpace() ) {
            user.append(longName[i]);
            i++;
        }

        // i is the first character of the space between fields 3 and 4
        // user contains the owner's user name
        while( i < l && longName[i].isSpace() ) {
            i++;
        }

        // i is the first character of the fourth field
        while( i < l && !longName[i].isSpace() ) {
            group.append(longName[i]);
            i++;
        }
        // group contains the name of the group.
    }

    mUserName = user;
    mGroupName = group;
}

/** No descriptions */
kdbgstream& operator<< (kdbgstream& s, sftpFileAttr& a) {
    s << "Filename: " << a.mFilename
      << ", Uid: " << a.mUid
      << ", Gid: " << a.mGid
      << ", Username: " << a.mUserName
      << ", GroupName: " << a.mGroupName
      << ", Permissions: " << a.mPermissions
      << ", size: " << a.mSize
      << ", atime: " << a.mAtime
      << ", mtime: " << a.mMtime
      << ", extended cnt: " << a.mExtendedCount;

    if (S_ISLNK(a.mLinkType)) {
      s << ", Link Type: " << a.mLinkType;
      s << ", Link Destination: " << a.mLinkDestination;
    }

    return s;
}

/** Make sure it builds with NDEBUG */
kndbgstream& operator<< (kndbgstream& s, sftpFileAttr& ) {
    return s;
}

/** Clear all attributes and flags. */
void sftpFileAttr::clear(){
    clearAtime();
    clearMtime();
    clearGid();
    clearUid();
    clearFileSize();
    clearPermissions();
    clearExtensions();
    mFilename = TQString::null;
    mGroupName =  TQString::null;
    mUserName = TQString::null;
    mLinkDestination = TQString::null;
    mFlags = 0;
    mLongname = "\0";
    mLinkType = 0;
}

/** Return the size of the sftp attribute. */
TQ_UINT32 sftpFileAttr::size() const{
    TQ_UINT32 size = 4; // for the attr flag
    if( mFlags & SSH2_FILEXFER_ATTR_SIZE )
        size += 8;

    if( mFlags & SSH2_FILEXFER_ATTR_UIDGID )
        size += 8;

    if( mFlags & SSH2_FILEXFER_ATTR_PERMISSIONS )
        size += 4;

    if( mFlags & SSH2_FILEXFER_ATTR_ACMODTIME )
        size += 8;

    if( mFlags & SSH2_FILEXFER_ATTR_EXTENDED ) {
        size += 4;
        // add size of extensions
    }
    return size;
}

/** Returns the file type as determined from the file permissions */
mode_t sftpFileAttr::fileType() const{
    mode_t type = 0;

    if( S_ISLNK(mPermissions) )
      type |= S_IFLNK;

    if( S_ISREG(mPermissions) )
      type |= S_IFREG;
    else if( S_ISDIR(mPermissions) )
      type |= S_IFDIR;
    else if( S_ISCHR(mPermissions) )
      type |= S_IFCHR;
    else if( S_ISBLK(mPermissions) )
      type |= S_IFBLK;
    else if( S_ISFIFO(mPermissions) )
      type |= S_IFIFO;
    else if( S_ISSOCK(mPermissions) )
      type |= S_IFSOCK;

    return type;
}

void sftpFileAttr::setEncoding( KRemoteEncoding* encoding )
{
    mEncoding = encoding;
}