File: FSNode.m

package info (click to toggle)
gworkspace 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 20,832 kB
  • sloc: objc: 69,382; sh: 488; makefile: 39
file content (1088 lines) | stat: -rw-r--r-- 26,791 bytes parent folder | download
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
/* FSNode.m
 *  
 * Copyright (C) 2004-2016 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@imago.ro>
 * Date: March 2004
 *
 * This file is part of the GNUstep FSNode framework
 *
 * 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.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
 */

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepBase/GNUstep.h>
#import "FSNode.h"
#import "FSNodeRep.h"
#import "FSNFunctions.h"


@implementation FSNode

- (void)dealloc
{
  RELEASE (path);
  RELEASE (relativePath);
  RELEASE (name);  
  RELEASE (attributes);  
  RELEASE (fileType);
  RELEASE (typeDescription);
  RELEASE (crDate);
  RELEASE (crDateDescription);
  RELEASE (modDate);
  RELEASE (modDateDescription);
  RELEASE (owner);
  RELEASE (ownerId);
  RELEASE (group);
  RELEASE (groupId);

  [super dealloc];
}

+ (FSNode *)nodeWithPath:(NSString *)apath
{
  return AUTORELEASE ([[FSNode alloc] initWithRelativePath: apath parent: nil]);
}

+ (FSNode *)nodeWithRelativePath:(NSString *)rpath
                          parent:(FSNode *)aparent
{
  return AUTORELEASE ([[FSNode alloc] initWithRelativePath: rpath 
                                                    parent: aparent]);
}

- (id)initWithRelativePath:(NSString *)rpath
                    parent:(FSNode *)aparent
{    
  self = [super init];
    
  if (self) {
    NSString *lastPathComponent;
    
    fsnodeRep = [FSNodeRep sharedInstance];
    fm = [NSFileManager defaultManager];
    ws = [NSWorkspace sharedWorkspace];
    
    parent = aparent;
    ASSIGN (relativePath, rpath);
    lastPathComponent = [[relativePath lastPathComponent] retain];
    name = nil;
    
    if (parent) {
      NSString *parentPath = [parent path];
      
      if ([parentPath isEqual: path_separator()]) {
        parentPath = @"";
      }
      ASSIGN (path, ([NSString stringWithFormat: @"%@%@%@", 
                                      parentPath, path_separator(), lastPathComponent]));
    } else {
      ASSIGN (path, relativePath);
    }
        
    flags.readable = -1;
    flags.writable = -1;
    flags.executable = -1;
    flags.deletable = -1;
    flags.plain = -1;
    flags.directory = -1;
    flags.link = -1;
    flags.socket = -1;
    flags.charspecial = -1;
    flags.blockspecial = -1;
    flags.mountpoint = -1;
    flags.application = -1;
    flags.package = -1;
    flags.unknown = -1;

    crDate = nil;
    modDate = nil;
    owner = nil;
    ownerId = nil;
    group = nil;
    groupId = nil;

    filesize = 0;
    permissions = 0;
    
    fileType = nil;    
    typeDescription = nil;
    
    application = nil;
                                      
    attributes = [fm fileAttributesAtPath: path traverseLink: NO];
    RETAIN (attributes);

    /* we localize only directories which could be special */
    if ([self isDirectory])
      ASSIGN (name, NSLocalizedStringFromTableInBundle(lastPathComponent, nil, [NSBundle bundleForClass:[self class]], @""));
    else
      ASSIGN (name, lastPathComponent);
    
    [lastPathComponent release];
  }
    
  return self;
}

- (NSUInteger)hash
{
  return [path hash];
}

- (BOOL)isEqual:(id)other
{
  if (other == self) {
    return YES;
  }
  if ([other isKindOfClass: [FSNode class]]) {
    return [self isEqualToNode: (FSNode *)other];
  }
  return NO;
}

- (BOOL)isEqualToNode:(FSNode *)anode
{
  if (anode == self) {
    return YES;
  }
  return [path isEqualToString: [anode path]];
}

- (NSArray *)subNodes 
{
  CREATE_AUTORELEASE_POOL(arp);
  NSMutableArray *nodes = [NSMutableArray array];
  NSArray *fnames = [fsnodeRep directoryContentsAtPath: path];
  NSUInteger i;
  
  for (i = 0; i < [fnames count]; i++) {
    NSString *fname = [fnames objectAtIndex: i];
    FSNode *node = [[FSNode alloc] initWithRelativePath: fname parent: self];

    [nodes addObject: node];
    RELEASE (node);
  }
  
  RETAIN (nodes);
  RELEASE (arp);
    
  return [[nodes autorelease] makeImmutableCopyOnFail: NO];
}

- (NSArray *)subNodeNames 
{
  return [fsnodeRep directoryContentsAtPath: path];
}

- (NSArray *)subNodesOfParent
{
  CREATE_AUTORELEASE_POOL(arp);
  NSMutableArray *nodes = [NSMutableArray array];
  NSArray *fnames = [fsnodeRep directoryContentsAtPath: [self parentPath]];
  FSNode *pnd = nil;
  NSUInteger i;
  
  if (parent != nil) {
    pnd = [parent parent];
  }
  
  for (i = 0; i < [fnames count]; i++) {
    NSString *fname = [fnames objectAtIndex: i];
    FSNode *node = [[FSNode alloc] initWithRelativePath: fname parent: pnd];

    [nodes addObject: node];
    RELEASE (node);
  }
  
  RETAIN (nodes);
  RELEASE (arp);
    
  return [[nodes autorelease] makeImmutableCopyOnFail: NO];
}

- (NSArray *)subNodeNamesOfParent
{
  return [fsnodeRep directoryContentsAtPath: [self parentPath]];
}

+ (NSArray *)nodeComponentsToNode:(FSNode *)anode
{
  CREATE_AUTORELEASE_POOL(arp);
  NSArray *pcomps = [self pathComponentsToNode: anode];
  NSMutableArray *components = [NSMutableArray array];
  NSUInteger i;
  
  for (i = 0; i < [pcomps count]; i++) {
    NSString *pcomp = [pcomps objectAtIndex: i];
    FSNode *pnode = (i == 0) ? nil : [components objectAtIndex: (i-1)];
    FSNode *node = [self nodeWithRelativePath: pcomp parent: pnode];
    
    [components insertObject: node atIndex: [components count]];
  }
  
  RETAIN (components);
  RELEASE (arp);
  
  return [[components autorelease] makeImmutableCopyOnFail: NO];
}

+ (NSArray *)pathComponentsToNode:(FSNode *)anode
{
  return [[anode path] pathComponents];
}

+ (NSArray *)nodeComponentsFromNode:(FSNode *)firstNode 
                             toNode:(FSNode *)secondNode
{
  if ([secondNode isSubnodeOfNode: firstNode]) {
    CREATE_AUTORELEASE_POOL(arp);
    NSString *p1 = [firstNode path];
    NSString *p2 = [secondNode path];
    NSUInteger index = ([p1 isEqual: path_separator()]) ? [p1 length] : ([p1 length] +1);
    NSArray *pcomps = [[p2 substringFromIndex: index] pathComponents];
    NSMutableArray *components = [NSMutableArray array];
    FSNode *node;
    NSUInteger i;
    
    node = [self nodeWithPath: p1];
    [components addObject: node];
    
    for (i = 0; i < [pcomps count]; i++) {
      FSNode *pnode = [components objectAtIndex: i];
      NSString *rpath = [pcomps objectAtIndex: i];
      
      node = [self nodeWithRelativePath: rpath parent: pnode];
      [components insertObject: node atIndex: [components count]];
    }
    
    RETAIN (components);
    RELEASE (arp);
    
    return [[components autorelease] makeImmutableCopyOnFail: NO];
    
  } else if ([secondNode isEqual: firstNode]) {
    return [NSArray arrayWithObject: firstNode];
  }
  
  return nil;
}

+ (NSArray *)pathComponentsFromNode:(FSNode *)firstNode 
                             toNode:(FSNode *)secondNode
{
  if ([secondNode isSubnodeOfNode: firstNode]) {
    NSString *p1 = [firstNode path];
    NSString *p2 = [secondNode path];
    int index = ([p1 isEqual: path_separator()]) ? [p1 length] : ([p1 length] +1);
    
    return [[p2 substringFromIndex: index] pathComponents];
    
  } else if ([secondNode isEqual: firstNode]) {
    return [NSArray arrayWithObject: [firstNode name]];
  }
  
  return nil;
}

+ (NSArray *)pathsOfNodes:(NSArray *)nodes
{
  CREATE_AUTORELEASE_POOL(arp);
  NSMutableArray *paths = [NSMutableArray array];
  NSUInteger i;
  
  for (i = 0; i < [nodes count]; i++) {
    [paths addObject: [[nodes objectAtIndex: i] path]];
  }
  
  RETAIN (paths);
  RELEASE (arp);
  
  return [[paths autorelease] makeImmutableCopyOnFail: NO];
}

+ (NSUInteger)indexOfNode:(FSNode *)anode 
               inComponents:(NSArray *)nodes
{
  return [nodes indexOfObject: anode];
}

+ (NSUInteger)indexOfNodeWithPath:(NSString *)apath 
                       inComponents:(NSArray *)nodes
{
  NSUInteger i;

  for (i = 0; i < [nodes count]; i++) {
    FSNode *node = [nodes objectAtIndex: i];

    if ([[node path] isEqual: apath]) {
      return i;
    }
  }
  
  return NSNotFound;
}

+ (FSNode *)subnodeWithName:(NSString *)aname 
                 inSubnodes:(NSArray *)subnodes
{
  NSUInteger i;

  for (i = 0; i < [subnodes count]; i++) {
    FSNode *node = [subnodes objectAtIndex: i];
    
    if ([node isValid] && [[node name] isEqual: aname]) {
      return node;
    }
  }
  
  return nil;
}

+ (FSNode *)subnodeWithPath:(NSString *)apath 
                 inSubnodes:(NSArray *)subnodes
{
  NSUInteger i;

  for (i = 0; i < [subnodes count]; i++) {
    FSNode *node = [subnodes objectAtIndex: i];
    
    if ([node isValid] && [[node path] isEqual: apath]) {
      return node;
    }
  }
  
  return nil;
}

+ (BOOL)pathOfNode:(FSNode *)anode
        isEqualOrDescendentOfPath:(NSString *)apath
                  containingFiles:(NSArray *)files
{
  NSString *nodepath = [anode path];
  
  if ([nodepath isEqual: apath]) {
    return YES;
  
  } else if (isSubpathOfPath(apath, nodepath)) {
    NSUInteger i;
    
    if (files == nil) {
      return YES;
      
    } else {
      for (i = 0; i < [files count]; i++) {
        NSString *fname = [files objectAtIndex: i];
        NSString *fpath = [apath stringByAppendingPathComponent: fname];				
				
        if (([fpath isEqual: nodepath]) || (isSubpathOfPath(fpath, nodepath))) {
          return YES;
        }
      }
    }
  }

  return NO;
}

- (FSNode *)parent
{
  return parent;
}

- (NSString *)parentPath
{
  return [path stringByDeletingLastPathComponent];
}

- (NSString *)parentName
{
  return [[self parentPath] lastPathComponent];
}

- (BOOL)isSubnodeOfNode:(FSNode *)anode
{
  return isSubpathOfPath([anode path], path);
}

- (BOOL)isSubnodeOfPath:(NSString *)apath
{
  return isSubpathOfPath(apath, path);
}

- (BOOL)isParentOfNode:(FSNode *)anode
{
  return isSubpathOfPath(path, [anode path]);
}

- (BOOL)isParentOfPath:(NSString *)apath
{
  return isSubpathOfPath(path, apath);
}

- (NSString *)path
{
  return path;
}

- (NSString *)relativePath
{
  return relativePath;
}

- (NSString *)name
{
  return name;
}

- (NSString *)fileType
{
  if (attributes && (fileType == nil)) {
    ASSIGN (fileType, [attributes fileType]);
  }
  return (fileType ? fileType : (NSString *)[NSString string]);
}

- (NSString *)application
{
  if ([self isApplication] == NO) {
    return application;
  }
  return nil;
}

- (void)setTypeFlags
{  
  flags.plain = 0;
  flags.directory = 0;
  flags.link = 0;
  flags.socket = 0;
  flags.charspecial = 0;
  flags.blockspecial = 0;
  flags.mountpoint = 0;
  flags.application = 0;
  flags.package = 0;
  flags.unknown = 0;

  if (fileType == nil) {
    [self fileType];
  }
  
  if (fileType) {
    if (fileType == NSFileTypeRegular) {
      flags.plain = 1;

    } else if (fileType == NSFileTypeDirectory) {
	    NSString *defApp = nil, *type = nil;

	    [ws getInfoForFile: path application: &defApp type: &type]; 
      
      if (defApp) {
        ASSIGN (application, defApp);
      }
      
      flags.directory = 1;

	    if (type == NSApplicationFileType) {
        flags.application = 1;
        flags.package = 1;
	    } else if (type == NSPlainFileType) {
        flags.package = 1;
      } else if (type == NSFilesystemFileType) {
        flags.mountpoint = 1;
      } 

    } else if (fileType == NSFileTypeSymbolicLink) {
      NSDictionary *attrs = [fm fileAttributesAtPath: path traverseLink: YES];

      if (attrs) {
        [self setFlagsForSymLink: attrs];
      }

      flags.link = 1;
    } else if (fileType == NSFileTypeSocket) {
      flags.socket = 1;
    } else if (fileType == NSFileTypeCharacterSpecial) {
      flags.charspecial = 1;
    } else if (fileType == NSFileTypeBlockSpecial) {
      flags.blockspecial = 1;
    } else {
      flags.unknown = 1;
    } 
  } else {
    flags.unknown = 1;
  }
}

- (void)setFlagsForSymLink:(NSDictionary *)attrs
{  
  NSString *ftype = [attrs fileType];

  if (ftype == NSFileTypeRegular) {
    flags.plain = 1;

  } else if (ftype == NSFileTypeDirectory) {
	  NSString *defApp = nil, *type = nil;

	  [ws getInfoForFile: path application: &defApp type: &type]; 
      
    if (defApp) {
      ASSIGN (application, defApp);
    }
    
    flags.directory = 1;

	  if (type == NSApplicationFileType) {
      flags.application = 1;
      flags.package = 1;
	  } else if (type == NSPlainFileType) {
      flags.package = 1;
    } else if (type == NSFilesystemFileType) {
      flags.mountpoint = 1;
    } 

  } else if (ftype == NSFileTypeSymbolicLink) {
    attrs = [fm fileAttributesAtPath: path traverseLink: YES];
    if (attrs) {
      [self setFlagsForSymLink: attrs];
    }
  } else if (ftype == NSFileTypeSocket) {
    flags.socket = 1;
  } else if (ftype == NSFileTypeCharacterSpecial) {
    flags.charspecial = 1;
  } else if (ftype == NSFileTypeBlockSpecial) {
    flags.blockspecial = 1;
  } else {
    flags.unknown = 1;
  } 

  ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"symbolic link", nil, [NSBundle bundleForClass:[self class]], @""));
}

- (NSString *)typeDescription
{
  if (typeDescription == nil) {
    if ([self isPlain]) {
      ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"plain file", nil, [NSBundle bundleForClass:[self class]], @""));
    } else if ([self isDirectory]) {
      if ([self isApplication]) {
        ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"application", nil, [NSBundle bundleForClass:[self class]], @""));
      } else if ([self isPackage]) {
        ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"package", nil, [NSBundle bundleForClass:[self class]], @""));
      } else if ([self isMountPoint]) {
        ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"mount point", nil, [NSBundle bundleForClass:[self class]], @""));
      } else {
        ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"directory", nil, [NSBundle bundleForClass:[self class]], @""));
      }
    } else if ([self isLink]) {
      ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"symbolic link", nil, [NSBundle bundleForClass:[self class]], @""));
    } else if ([self isSocket]) {
      ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"socket", nil, [NSBundle bundleForClass:[self class]], @""));
    } else if ([self isCharspecial]) {
      ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"character special", nil, [NSBundle bundleForClass:[self class]], @""));
    } else if ([self isBlockspecial]) {
      ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"block special", nil, [NSBundle bundleForClass:[self class]], @""));
    } else {
      ASSIGN (typeDescription, NSLocalizedStringFromTableInBundle(@"unknown", nil, [NSBundle bundleForClass:[self class]], @""));
    }
  }

  return typeDescription;
}

- (NSDate *)creationDate
{
  if (attributes && (crDate == nil)) {
    ASSIGN (crDate, [attributes fileCreationDate]);
  }
  return (crDate ? crDate : (NSDate *)[NSDate date]);
}

- (NSString *)crDateDescription
{
  NSDate *date = [self creationDate];
  
  if (date) {
    if (crDateDescription == nil) {
      NSString *descr = [date descriptionWithCalendarFormat: @"%b %d %Y" 
                            timeZone: [NSTimeZone localTimeZone] locale: nil];

      ASSIGN (crDateDescription, descr);   
    }

    return crDateDescription;
  }

  return [NSString string];
}

- (NSDate *)modificationDate
{
  if (attributes && (modDate == nil)) {
    ASSIGN (modDate, [attributes fileModificationDate]);
  }
  return (modDate ? modDate : (NSDate *)[NSDate date]);
}

- (NSString *)modDateDescription
{
  NSDate *date = [self modificationDate];

  if (date) {
    if (modDateDescription == nil) {
      NSString *descr = [date descriptionWithCalendarFormat: @"%b %d %Y" 
                            timeZone: [NSTimeZone localTimeZone] locale: nil];
      ASSIGN (modDateDescription, descr);   
    }
    return modDateDescription;
  }

  return [NSString string];
}

- (unsigned long long)fileSize
{
  if ((filesize == 0) && attributes) {
    filesize = [attributes fileSize];
  }
  return filesize;
}


- (NSString *)sizeDescription
{
  unsigned long long fsize = [self fileSize];
  NSString *sizeStr;

  sizeStr = sizeDescription(fsize);
    
  return sizeStr;
}

- (NSString *)owner
{
  if (attributes && (owner == nil)) {
    ASSIGN (owner, [attributes fileOwnerAccountName]);
  }
  return (owner ? owner : (NSString *)[NSString string]);
}

- (NSNumber *)ownerId
{
  if (attributes && (ownerId == nil)) {
    ASSIGN (ownerId, [attributes objectForKey: NSFileOwnerAccountID]);
  }
  return (ownerId ? ownerId : [NSNumber numberWithInt: 0]);
}

- (NSString *)group
{
  if (attributes && (group == nil)) {
    ASSIGN (group, [attributes fileGroupOwnerAccountName]);
  }
  return (group ? group : (NSString *)[NSString string]);
}

- (NSNumber *)groupId
{
  if (attributes && (groupId == nil)) {
    ASSIGN (groupId, [attributes objectForKey: NSFileGroupOwnerAccountID]);
  }
  return (groupId ? groupId : [NSNumber numberWithInt: 0]);
}

- (unsigned long)permissions
{
  if ((permissions == 0) && attributes) {
    permissions = [attributes filePosixPermissions];
  }
  return permissions;
}

- (BOOL)isPlain 
{
  if (flags.plain == -1) {
    [self setTypeFlags];
  }
  return (flags.plain ? YES : NO);  
}

- (BOOL)isDirectory 
{
  if (flags.directory == -1) {
    [self setTypeFlags];
  }
  return (flags.directory ? YES : NO);
}

- (BOOL)isLink 
{
  if (flags.link == -1) {
    [self setTypeFlags];
  }
  return (flags.link ? YES : NO);
}

- (BOOL)isSocket
{
  if (flags.socket == -1) {
    [self setTypeFlags];
  }
  return (flags.socket ? YES : NO);
}

- (BOOL)isCharspecial
{
  if (flags.charspecial == -1) {
    [self setTypeFlags];
  }
  return (flags.charspecial ? YES : NO);
}

- (BOOL)isBlockspecial
{
  if (flags.blockspecial == -1) {
    [self setTypeFlags];
  }
  return (flags.blockspecial ? YES : NO);
}

- (BOOL)isMountPoint
{
  if (flags.mountpoint == -1) {
    [self setTypeFlags];
  }
  return (flags.mountpoint ? YES : NO);
}

- (void)setMountPoint:(BOOL)value
{
  flags.mountpoint = value;
}

- (BOOL)isApplication 
{
  if (flags.application == -1) {
    [self setTypeFlags];
  }
  return (flags.application ? YES : NO);
}

- (BOOL)isPackage
{
  if (flags.package == -1) {
    [self setTypeFlags];
  }
  return (flags.package ? YES : NO);
}

- (BOOL)isReadable 
{
  if (flags.readable == -1) {
    flags.readable = [fm isReadableFileAtPath: path];
  }
  return (flags.readable ? YES : NO);
}

- (BOOL)isWritable 
{
  if (flags.writable == -1) {
    flags.writable = [fm isWritableFileAtPath: path];
  }
  return (flags.writable ? YES : NO);
}

- (void)checkWritable
{
  flags.writable = [fm isWritableFileAtPath: path];
}

- (BOOL)isParentWritable
{
  return [fm isWritableFileAtPath: [self parentPath]];
}

- (BOOL)isExecutable
{
  if (flags.executable == -1) {
    flags.executable = [fm isExecutableFileAtPath: path];
  }
  return (flags.executable ? YES : NO);
}

- (BOOL)isDeletable
{
  if (flags.deletable == -1) {
    flags.deletable = [fm isDeletableFileAtPath: path];
  }
  return (flags.deletable ? YES : NO);
}

- (BOOL)isLocked
{
  return [fsnodeRep isNodeLocked: self];
}

- (BOOL)isValid
{
  BOOL valid = (attributes != nil);

  if (valid) {
    valid = [fm fileExistsAtPath: path];

    if ((valid == NO) && flags.link) {
      valid = ([fm fileAttributesAtPath: path traverseLink: NO] != nil);
    }
  }
  
  return valid;
}

- (BOOL)hasValidPath
{
  return [fm fileExistsAtPath: path];
}

- (BOOL)isReserved
{
  return [fsnodeRep isReservedName: name];
}

- (BOOL)willBeValidAfterFileOperation:(NSDictionary *)opinfo
{
  NSString *operation = [opinfo objectForKey: @"operation"];
  NSString *source = [opinfo objectForKey: @"source"];
  NSString *destination = [opinfo objectForKey: @"destination"];
  NSArray *files = [opinfo objectForKey: @"files"];
  NSUInteger i;

  if ([operation isEqual: @"GWorkspaceRenameOperation"]) {      
    files = [NSArray arrayWithObject: [source lastPathComponent]]; 
    source = [source stringByDeletingLastPathComponent];            
  } 

  if ([self isSubnodeOfPath: source]) {
    if ([operation isEqual: NSWorkspaceMoveOperation]
        || [operation isEqual: NSWorkspaceDestroyOperation]
        || [operation isEqual: @"GWorkspaceRenameOperation"]
			  || [operation isEqual: NSWorkspaceRecycleOperation]
			  || [operation isEqual: @"GWorkspaceRecycleOutOperation"]
			  || [operation isEqual: @"GWorkspaceEmptyRecyclerOperation"]) { 
      for (i = 0; i < [files count]; i++) {
        NSString *fname = [files objectAtIndex: i];
        NSString *fpath = [source stringByAppendingPathComponent: fname];

        if ([path isEqual: fpath] || [self isSubnodeOfPath: fpath]) {  
          return NO;      
        }
      }
    } 
  }

  if ([self isSubnodeOfPath: destination]) {
    if ([operation isEqual: NSWorkspaceMoveOperation]
          || [operation isEqual: NSWorkspaceCopyOperation]
          || [operation isEqual: NSWorkspaceLinkOperation]
	|| [operation isEqual: NSWorkspaceRecycleOperation]
	|| [operation isEqual: @"GWorkspaceRecycleOutOperation"]) { 
      for (i = 0; i < [files count]; i++) {
        NSString *fname = [files objectAtIndex: i];
        NSString *fpath = [destination stringByAppendingPathComponent: fname];

        if ([path isEqual: fpath]) {
          NSString *srcpath = [source stringByAppendingPathComponent: fname];
          NSDictionary *attrs = [fm fileAttributesAtPath: srcpath 
                                            traverseLink: NO];
          if ((attrs == nil) 
                      || ([[attrs fileType] isEqual: [self fileType]] == NO)) {
            return NO;
          }

        } else if ([self isSubnodeOfPath: fpath]) {  
          NSString *ppart = subtractFirstPartFromPath(path, fpath);
          NSString *srcpath = [source stringByAppendingPathComponent: fname];
          
          srcpath = [srcpath stringByAppendingPathComponent: ppart];

          if ([fm fileExistsAtPath: srcpath]) {
            NSDictionary *attrs = [fm fileAttributesAtPath: srcpath  
                                              traverseLink: NO];
            if ((attrs == nil) 
                        || ([[attrs fileType] isEqual: [self fileType]] == NO)) {
              return NO;
            }
          } else {
            return NO;
          }
        }
      }
    }
  }
  
  return YES;
}

- (BOOL)involvedByFileOperation:(NSDictionary *)opinfo
{
  NSString *operation = [opinfo objectForKey: @"operation"];
  NSString *source = [opinfo objectForKey: @"source"];
  NSString *destination = [opinfo objectForKey: @"destination"];	 
  NSArray *files = [opinfo objectForKey: @"files"];    
  NSUInteger i;  	 

  if ([operation isEqual: @"GWorkspaceRenameOperation"]) { 
    files = [NSArray arrayWithObject: [source lastPathComponent]]; 
    source = [source stringByDeletingLastPathComponent];            
    destination = [destination stringByDeletingLastPathComponent];            
  } 

  if ([path isEqual: source] || [path isEqual: destination]) {
    return YES;
  }

  if (isSubpathOfPath(source, path)) {
    for (i = 0; i < [files count]; i++) {
      NSString *fname = [files objectAtIndex: i];
      NSString *fpath = [source stringByAppendingPathComponent: fname];				

      if (([fpath isEqual: path]) || (isSubpathOfPath(fpath, path))) {
        return YES;
      }
    }
  }
    
  if ([operation isEqual: @"GWorkspaceRenameOperation"]) {
    destination = [opinfo objectForKey: @"destination"];	 
    files = [NSArray arrayWithObject: [destination lastPathComponent]]; 
    destination = [destination stringByDeletingLastPathComponent];  
  } 
  
  if (isSubpathOfPath(destination, path)) {
    for (i = 0; i < [files count]; i++) {
      NSString *fname = [files objectAtIndex: i];
      NSString *fpath = [destination stringByAppendingPathComponent: fname];				

      if (([fpath isEqual: path]) || (isSubpathOfPath(fpath, path))) {
        return YES;
      }
    }
  }
    
  return NO;
}

@end


@implementation FSNode (Comparing)

- (NSComparisonResult)compareAccordingToPath:(FSNode *)aNode
{
  return [path compare: [aNode path]];
}

- (NSComparisonResult)compareAccordingToName:(FSNode *)aNode
{
  NSString *n1 = [self name];
  NSString *n2 = [aNode name];

  if ([n2 hasPrefix: @"."] || [n1 hasPrefix: @"."]) {
    if ([n2 hasPrefix: @"."] && [n1 hasPrefix: @"."]) {
      return [n1 caseInsensitiveCompare: n2];
    } else {
      return [n2 caseInsensitiveCompare: n1];
    }
  }
  
  return [n1 caseInsensitiveCompare: n2];
}

- (NSComparisonResult)compareAccordingToParent:(FSNode *)aNode
{
  CREATE_AUTORELEASE_POOL(pool);
  NSString *p1 = [self parentPath];
  NSString *p2 = [aNode parentPath];
  NSComparisonResult result = [p1 compare: p2];
  RELEASE (pool);
  return result;
}

- (NSComparisonResult)compareAccordingToKind:(FSNode *)aNode
{
  unsigned i1, i2;

  if ([self isDirectory]) { 
    i1 = 2; 
  } else if ([self isExecutable]) { 
    i1 = 1; 
  } else { 
    i1 = 0; 
  } 

  if ([aNode isDirectory]) { 
    i2 = 2; 
  } else if ([aNode isExecutable]) { 
    i2 = 1; 
  } else { 
    i2 = 0; 
  } 

  if (i1 == i2) {	
    return [self compareAccordingToExtension: aNode];
  }   

  return ((i1 > i2) ? NSOrderedAscending : NSOrderedDescending);
}

- (NSComparisonResult)compareAccordingToExtension:(FSNode *)aNode
{
  NSString *e1 = [[self path] pathExtension];
  NSString *e2 = [[aNode path] pathExtension];
  
  if ([e1 isEqual: e2]) {
    return [self compareAccordingToName: aNode];
  }
  
  return [e1 caseInsensitiveCompare: e2];
}

- (NSComparisonResult)compareAccordingToDate:(FSNode *)aNode
{
  return [[self modificationDate] compare: [aNode modificationDate]]; 
}

- (NSComparisonResult)compareAccordingToSize:(FSNode *)aNode
{
  unsigned long long fs1 = [self fileSize];  
  unsigned long long fs2 = [aNode fileSize];  
  return (fs1 > fs2) ? NSOrderedAscending : NSOrderedDescending;
}

- (NSComparisonResult)compareAccordingToOwner:(FSNode *)aNode
{
  return [[self owner] compare: [aNode owner]]; 
}

- (NSComparisonResult)compareAccordingToGroup:(FSNode *)aNode
{
  return [[self group] compare: [aNode group]]; 
}

@end