File: fileElement.m

package info (click to toggle)
ftp.app 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 664 kB
  • sloc: objc: 2,955; makefile: 11
file content (544 lines) | stat: -rw-r--r-- 18,631 bytes parent folder | download | duplicates (2)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 Project: FTP

 Copyright (C) 2005-2017 Riccardo Mottola

 Author: Riccardo Mottola

 Created: 2005-04-18

 Single element of a file listing

 This application 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.

 This application is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU General Public
 License along with this library; if not, write to the Free
 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#import "fileElement.h"


@implementation FileElement

- (void)dealloc
{
  [fileName release];
  [filePath release];
  [linkTargetName release];
  [modifDate release];
  [super dealloc];
}

/* -- coder methods for copying -- */
- (Class) classForCoder
{
  return [FileElement class];
}

- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
{
  return self;
}

- (void) encodeWithCoder: (NSCoder*)aCoder
{
  [aCoder encodeObject: fileName];
  [aCoder encodeObject: filePath];
  [aCoder encodeObject: linkTargetName];
  [aCoder encodeValueOfObjCType: @encode(BOOL) at: &isDir];
  [aCoder encodeValueOfObjCType: @encode(BOOL) at: &isLink];
  [aCoder encodeValueOfObjCType: @encode(unsigned long long) at: &size];
  [aCoder encodeObject: modifDate];
}

- (id) initWithCoder: (NSCoder*)aCoder
{
  self = [super init];
  fileName = [[aCoder decodeObject] retain];
  filePath = [[aCoder decodeObject] retain];
  linkTargetName = [[aCoder decodeObject] retain];
  [aCoder decodeValueOfObjCType: @encode(BOOL) at: &isDir];
  [aCoder decodeValueOfObjCType: @encode(BOOL) at: &isLink];
  [aCoder decodeValueOfObjCType: @encode(unsigned long long) at: &size];
  modifDate = [[aCoder decodeObject] retain];
  return self;
}

/*
 initialize a file element using attrbutes of NSFileManager
 */
- (id)initWithPath:(NSString *)path andAttributes:(NSDictionary *)attribs
{
  self = [super init];
  if (self)
    {
      size = [attribs fileSize];
      modifDate = [[attribs objectForKey:NSFileModificationDate] retain];
      if ([attribs fileType] == NSFileTypeDirectory)
        isDir = YES;
      else
        isDir = NO;
      isLink = NO;
      filePath = [path retain];
      fileName = [[filePath lastPathComponent] retain];
    }
  return self;
}

- (NSDictionary *)attributes
{
  NSMutableDictionary *attr;
  
  attr = [[NSMutableDictionary alloc] init];
  [attr setObject:[NSNumber numberWithLongLong:size] forKey:NSFileSize];
  if (modifDate)
    [attr setObject:modifDate forKey:NSFileModificationDate];
  if (isDir)
    [attr setObject:NSFileTypeDirectory forKey:NSFileType];
  else
    [attr setObject:NSFileTypeRegular forKey:NSFileType];
  
  [attr autorelease];
  return [NSDictionary dictionaryWithDictionary:attr];
}

/* as a parser aid, check if a string is a month */
- (int)checkMonth: (NSString *)token
{
  if ([token compare:@"Jan" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 1;
    
  if ([token compare:@"Feb" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 2;
    
  if ([token compare:@"Mar" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 3;
    
  if ([token compare:@"Apr" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 4;
    
  if ([token compare:@"May" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 5;
    
  if ([token compare:@"Jun" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 6;
    
  if ([token compare:@"Jul" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 7;
    
  if ([token compare:@"Aug" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 8;
    
  if ([token compare:@"Sep" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 9;
    
  if ([token compare:@"Oct" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 10;
    
  if ([token compare:@"Nov" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 11;
    
  if ([token compare:@"Dec" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
    return 12;
    
  return 0;
}

/*
 tries to parse a single line of LIST
 currently unix-style results will work, like

 drwx------  22 multix  staff   704 Apr  2 14:46 Documents

 or these where one of user/group are omitted
 drwxr-xr-x   8 users     4096 Apr 15 19:58 Documents
 
 inital blocks can be omitted, like:
 drwx------  multix  staff   704 Apr  2 14:46 Documents
 
 user/group could be numerical
 drwx------  22 4567  120   704 Apr  2 14:46 Documents
 
 the hour time could be the year
 drwx------  22 multix  staff   704 Apr  2 2005 Documents
 
 the filename could contain one or more spaces
 -rw-r--r--   1 multix  staff     0 May 25 10:08 test file
 
 the filesize can be zero..
 
 it will skip a line that is not considered meaningful, like:
 total 74184 (typical from Unix ls -l)
 and return nil.
 
 some attempt at scanning OS400 / i5 ftp listings is attempted too,
 those are in the style of:
 SVIFELMA        32768 02/05/07 15:32:42 *FILE      INSOLUTO
 SVIFELMA                                *MEM       INSOLUTO.INSOLUTO

 the feof line is ignored
 
 */
- (id)initWithLsLine :(char *)line
{
  NSString       *fullLine;
  NSMutableArray *splitLine;
  unichar        ch;
  unsigned       elementsFound;
  NSCharacterSet *whiteSet;
  unsigned       lineLen;
  NSRange        searchRange;
  NSRange        tokenEnd;
  NSRange        tokenRange;
  BOOL           foundStandardMonth;
  BOOL           foundOneBeforeMonth;
  long long      tempLL;
  
  self = [super init];
  if (self)
    {
      // typical Unix end of listing
      if (strstr(line, "total") == line)
        return nil;
    
    // typical IBM OS400 end of listing
    if (strstr(line, "feof") == line)
        return nil;

    fileName = nil;
    filePath = nil;
    linkTargetName = nil;
    isLink = NO;
    isDir = NO;
    size = 0;
    modifDate = nil;
    
    whiteSet = [NSCharacterSet whitespaceCharacterSet];
    splitLine = [NSMutableArray arrayWithCapacity:5];
    fullLine = [NSString stringWithCString:line];
    lineLen = [fullLine length];
    ch = [fullLine characterAtIndex:0];
    if (ch == '-' || ch == 'd' || ch == 'l')
    {
        // this is a unix-like listing
        unsigned cursor;

        // file permissions block
        cursor = 0;
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically links - (user) - group - size
        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"second token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
            
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"third token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"fourth token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"fifth token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        foundOneBeforeMonth = [self checkMonth:[splitLine objectAtIndex:4]];
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically month
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"sixth token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        foundStandardMonth = [self checkMonth:[splitLine objectAtIndex:5]];
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically day of the month
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"seventh token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically year or hour
        // but it could be fileName already
        if (foundStandardMonth)
        {
            searchRange = NSMakeRange(cursor, lineLen-cursor);
            tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
            tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
	    //            NSLog(@"eighth token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
            [splitLine addObject:[fullLine substringWithRange:tokenRange]];
            cursor = NSMaxRange(tokenEnd);
        
            while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
                cursor++;
        }
        
        // typically the filename
        if (cursor < lineLen)
        {
            tokenRange = NSMakeRange(cursor, lineLen-cursor);
	    //            NSLog(@"last token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
            [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        }
        
        elementsFound = [splitLine count];
        // If the listing had only 8 elements out of 9
        // Missing user is supposed and a blank is inserted
        if (elementsFound == 8)
          {
            [splitLine insertObject: @"" atIndex:2];
            foundStandardMonth = foundOneBeforeMonth;
            elementsFound = [splitLine count];
          }
        
        // Parse split data
        if (foundStandardMonth && elementsFound == 9)
          {
            NSRange linkArrowRange;
            NSString *tmpFileName;
            NSString *tmpSizeString;

            // everything is fine and ok, we have a full-blown listing and parsed it well;            
            isDir = NO;
            isLink = NO;
            if ([[splitLine objectAtIndex:0] characterAtIndex:0] == 'd')
                isDir = YES;
            else if ([[splitLine objectAtIndex:0] characterAtIndex:0] == 'l')
                isLink = YES;
            
            size = 0;
            tmpSizeString = [splitLine objectAtIndex:4];
            [[NSScanner scannerWithString: tmpSizeString] scanLongLong:&tempLL];
	    if (tempLL > 0)
	      size = (unsigned long long)tempLL;

            tmpFileName = [splitLine objectAtIndex:elementsFound -1];
	    linkArrowRange = [tmpFileName rangeOfString: @" -> "];
            if (isLink)
              {
                if (linkArrowRange.location == NSNotFound)
                  {
                    NSLog(@"we have a link, but no target to parse: %@", tmpFileName);
                    fileName = [tmpFileName retain];
                  }
                else
                  {
                    fileName = [[tmpFileName substringToIndex: linkArrowRange.location] retain];
                    linkTargetName = [[tmpFileName substringFromIndex: linkArrowRange.location+linkArrowRange.length] retain];
                    NSLog(@"we have a link, target: %@", linkTargetName);
                  }
                // we suppose that files have extensions, dirs not
                // links have no further information
                if ([[fileName pathExtension] isEqualToString:@""])
                  {
                    NSLog(@"we suppose %@ is a dir", fileName);
                    isDir = YES;
                  }
              }
            else
              {
                fileName = [tmpFileName retain];

              }
          }
        else
          return nil;
        
    } else if ((fullLine != NULL) && ([fullLine rangeOfString: @"*FILE"].location != NSNotFound))
    {
        // maybe it is an IBM AS400 / i5 style line
	// this is an IBM listing, having the *FILE element
        unsigned cursor;

        // username block
        cursor = 0;
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// file size        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// date     
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// time       
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// record type       
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// file name    
        // typically the filename
        if (cursor < lineLen)
        {
            tokenRange = NSMakeRange(cursor, lineLen-cursor);
            [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        }

    	// everything is fine and ok, we have a full-blown listing and parsed it well;            
        isDir = NO; /* OS400 is not hierarchical */
        [[NSScanner scannerWithString: [splitLine objectAtIndex:1]] scanLongLong:&tempLL];
	if (tempLL > 0)
	  size = (unsigned long long)tempLL;
	else
	  size = 0;
        fileName = [[splitLine objectAtIndex:5] retain];

    } else
    {
	/* we don't know better */
	return nil;
    }
    
    /* let's ignore the current and the parent directory some servers send over... */
    if([fileName isEqualToString:@"."])
        return nil;
    else if([fileName isEqualToString:@".."])
        return nil;
    }
  return self;
}

/* accessors */
- (NSString *)name
{
  return fileName;
}

/* sets the file name and updates the full path */
- (void)setName: (NSString *)n
{
  NSString *basePath;

  [fileName release];
  fileName = [n retain];
  basePath = [filePath stringByDeletingLastPathComponent];
  filePath = [[basePath stringByAppendingPathComponent: fileName] retain];
}

- (NSString *)path
{
  return filePath;
}

/* sets the full path and updates the name */
- (void)setPath: (NSString *)p
{
  [filePath release];
  [fileName release];
  filePath = [p retain];
  fileName = [[filePath lastPathComponent] retain];
}

- (NSString *)linkTargetName
{
  return linkTargetName;
}

- (BOOL)isDir
{
  return isDir;
}

- (BOOL)isLink
{
  return isLink;
}

- (void)setIsLink:(BOOL)flag
{
  isLink = flag;
}

- (unsigned long long)size
{
  return size;
}


@end