File: globus-gass-cache-util.pl.in

package info (click to toggle)
globus-gass-cache-program 7.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,384 kB
  • sloc: sh: 11,181; perl: 882; ansic: 749; makefile: 33
file content (1292 lines) | stat: -rwxr-xr-x 29,296 bytes parent folder | download | duplicates (7)
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
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
#! /usr/bin/perl

# 
# Copyright 1999-2006 University of Chicago
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 

use warnings;
use strict;

my $prefix;
my $exec_prefix;
my $sbindir;
my $bindir;
my $libdir;
my $includedir;
my $datarootdir;
my $datadir;
my $libexecdir;
my $sysconfdir;
my $sharedstatedir;
my $localstatedir;
my $perlmoduledir;

BEGIN {
    $prefix="@prefix@";
    if ($ENV{GLOBUS_LOCATION}) {
        $prefix = $ENV{GLOBUS_LOCATION};
    }
    $exec_prefix="@exec_prefix@";
    $sbindir="@sbindir@";
    $bindir="@bindir@";
    $libdir="@libdir@";
    $includedir="@includedir@";
    $datarootdir="@datarootdir@";
    $datadir="@datadir@";
    $libexecdir="@libexecdir@";
    $sysconfdir="@sysconfdir@";
    $sharedstatedir="@sharedstatedir@";
    $localstatedir="@localstatedir@";
    $perlmoduledir = "@perlmoduledir@";

    unshift(@INC, $perlmoduledir);
}

$ENV{PATH} = $ENV{PATH}.":$bindir:$sbindir";

# Prototypes
sub main( @ );
sub GassCacheList ( );
sub GassCacheListNormal ( );
sub GassCacheListFlat ( );
sub GassCacheCleanupUrl ( );
sub GassCacheCleanupUrlNormal ( );
sub GassCacheCleanupUrlFlat ( );
sub GassCacheFlatDir ( $ );
sub DumpHelp( );


# ******************************************************
# Command line options
# ******************************************************
my $PerlOperations =<<END_OPERATIONS;
    -cleanup-url   - remove all tags for an URL in the cache
                     This operation requires that the URL be specified on
                     the command line.
    -list          - list the contents of the cache.
                     If either the [-t tag] or a URL is specified on the
                     command line, then only cache entries which match
                     those will be listed
END_OPERATIONS
my $PerlOptions =<<END_OPTIONS;
END_OPTIONS

my $Program = $0;
my $ProgramC = 'globus-gass-cache-util';
my $Verbose = 0;


# Option flags
my $OptionLong = 0;

# C Program flags & args
my @CprogFlags = (
		  '-a', '-add',
		  '-d', '-delete',
		  '-dirs',
		  '-m', '-mangle',
		  '-q', '-query',
		  '-cleanup-tag',
		  );
my $CprogFlagsRE =
    '(' . join( ')|(', @CprogFlags ) . ')';

# C Program flags & args
my @CprogArgs = (
		 '-r', '-resource',
		 '-n', '-newname',
		 '-t', '-tag' );
my $CprogArgsRE =
    '(' . join( ')|(', @CprogArgs ) . ')';

# Arguments that the "C" _doesn't_ understand, and need to be stripped
# off before we punt to the C program
my @CprogNonArgs = (
		    '-l', '-list',
		    '-cleanup-url',
		   );
my $CprogNonArgsRE =
    '(' . join( ')|(', @CprogNonArgs ) . ')';

# The manglings we know about..
my @Manglings = ( 'html', 'md5' );

# Hash of the "Root" directories & other cache info
my %CacheInfo;

# Make stdout sane
$|=1;

# Store the settings to use
my %Settings = (
		Url => '',		# URL to process
		MangleUrl => '',	#  Mangled version
		Tag => '',		# Tag to process
		MangledTag => '',	#  Mangled version
	       );

# Invoke "main"
main( @ARGV );

# "Main" logic
sub main( @ )
{
    # Empty command line; punt it
    if ( $#_ < 0 )
    {
	exec ( "$ProgramC", @_ );
    }

    # Build list of C program arguments..
    my @CprogArgv = grep{!/^$CprogNonArgsRE$/} @_;

    # Help; punt + our own processing...
    if ( grep( /^-help$/, @_ ) >= 1 )
    {
	DumpHelp();
	exit 0;
    }

    # Walk through the command line
    my @JobList;
    my $ArgNo;
    my $Skip = 0;
    for ($ArgNo = 0; $ArgNo <= $#_; $ArgNo++)
    {
	my $Arg = $_[$ArgNo];
	if ( $Skip > 0 )
	{
	    $Skip--;
	    next;
	}

	if ( ( $Arg eq '-l' ) || ( $Arg eq '-list' ) )
	{
	    push @JobList, 'list';
	}
	elsif ( ( $Arg eq '-cu' ) || ( $Arg eq '-cleanup-url' ) )
	{
	    push @JobList, 'cleanup-url';
	}
	elsif ( $Arg eq '-long' )
	{
	    $OptionLong = 1;
	}
	elsif ( ( $Arg eq '-v' ) || ( $Arg eq '-verbose' ) )
	{
	    $Verbose++;
	}
	elsif ( ( $Arg eq '-t' ) || ( $Arg eq '-tag' ) )
	{
	    if ( $ArgNo >= $#_ )
	    {
		print STDERR "-tag: no tag specified!\n";
		die;
	    }
	    $Settings{Tag} = $_[$ArgNo+1];
	    $Skip = 1;
	}
	elsif ( $Arg =~ /^($CprogFlagsRE)$/ )
	{
	    # Invoke the C program
	    exec ( "$ProgramC", @CprogArgv );
	}
	elsif ( $Arg =~ /^($CprogArgsRE)$/ )
	{
	    # Skip next arg...
	    $ArgNo++;
	}
	elsif ( $Arg =~ /^-/ )
	{
	    # Not sure what the hell it is, punt it off to the C prog...
	    exec ( "$ProgramC", @CprogArgv );
	}
	# Must be the URL
	else
	{
	    $Settings{Url} = $Arg;
	}
    }

    # Did we do anything?  If not, punt to the C program...
    if ( $#JobList < 0 )
    {
	exec ( "$ProgramC", @CprogArgv ) || die "exec $ProgramC";
    }

    # Ok, here we have real work to do..
    $#_ = -1;

    # Disable debugging output while learnging about the cache...
    {
	local $ENV{GLOBUS_GASS_CACHE_DEBUG};
	delete( $ENV{GLOBUS_GASS_CACHE_DEBUG} );

	# Get the cache type...
	my $Cmd = "$ProgramC -type";
	open( TYPE, "$Cmd|" ) || die "Can't run '$Cmd'";
	while ( <TYPE> )
	{
	    chomp;
	    if ( /^CACHE_TYPE: '(.*)'/o )
	    {
		$CacheInfo{CACHE_TYPE} = $1;
	    }
	}
	close( TYPE );
	die "Cache type unknown" if ( ! exists $CacheInfo{CACHE_TYPE} );

	# Get the main cache directory
	$Cmd = "$ProgramC -dirs";
	open( DIRS, "$Cmd|" ) || die "Can't run '$Cmd'";
	while ( <DIRS> )
	{
	    if ( /^CACHE_DIRECTORY: '(.*)'/o )
	    {
		$CacheInfo{CACHE_DIR} = $1;
	    }
	    elsif ( /^(\w+_ROOT): '(.*)'/o )
	    {
		$CacheInfo{$1} = $2;
	    }
	}
	close( DIRS );
    }

    # Mangle the URLs
    if ( $Settings{Url} ne '' )
    {
	$Settings{MangledUrl} = Mangle( $Settings{Url} )
    }
    # Mangle the Tags
    if ( $Settings{Tag} ne '' )
    {
	$Settings{MangledTag} = Mangle( $Settings{Tag} );
    }

    # Do the work...
    foreach my $Job ( @JobList )
    {
	if ( $Job eq 'list' )
	{
	    GassCacheList( );
	}
	elsif ( $Job eq 'cleanup-url' )
	{
	    GassCacheCleanupUrl( );
	}
    }

}   # End of logical "main"
# ******************************************************

# ******************************************************
# Function: GassCacheList()
#
# Description:
#	List the contents of the Cache
#
# Parameters:
#  None
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheList ( )
{
    my $Settings = shift;

    # Check the type & invoke the proper
    if ( $CacheInfo{CACHE_TYPE} eq 'normal' )
    {
	print "-- Normal cache --\n" if ( $Verbose );
	GassCacheListNormal( );
    }
    elsif ( $CacheInfo{CACHE_TYPE} eq 'flat' )
    {
	print "-- Flat cache --\n" if ( $Verbose );
	GassCacheListFlat( );
    }
    else
    {
	printf STDERR "Unknown cache type '%s'\n", $CacheInfo{CACHE_TYPE};
    }

}   # GassCacheList()
# ******************************************************

# ******************************************************
# Function: GassCacheListNormal()
#
# Description:
#	List the contents of the Cache "Normal" mode
#
# Parameters:
#  None
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheListNormal ( )
{

    # Verify the directories
    die "No GLOBAL root" if ( ! defined $CacheInfo{GLOBAL_ROOT} );
    die "No LOCAL root" if ( ! defined $CacheInfo{LOCAL_ROOT} );

    # This will be used several times...
    my $Cmd;


    # *************************************************
    # Now, let's do the real fun...
    #  Scan through the global directory for all URLs
    # *************************************************
    print "Scanning the global entries in $CacheInfo{GLOBAL_ROOT}\n" if( $Verbose );
    my %Global;
    $Cmd = "find $CacheInfo{GLOBAL_ROOT} -name \"data*\" -print";
    foreach my $FullPath (`$Cmd` )
    {
	chomp $FullPath;
	my @Stat = stat( $FullPath );
	my $Inode = $Stat[1];
	my $Size = $Stat[7];

	# And, let's get it's directory
	my @Dirs = split( /\//, $FullPath );
	$#Dirs--;
	my $Dir = join( "/", @Dirs );

	# Read the URL from the "url" file
	my $Url = "";
	if ( open( URL, "$Dir/url" ) )
	{
	    $Url = <URL>; chomp $Url;
	    close( URL );
	}

	# Pull out it's mangled dir
	my $Mangled = $Dir;
	$Mangled =~ s/$CacheInfo{GLOBAL_ROOT}\///;

	# Store it all in a (perl) hash
	my $r = ();
	$r->{Inode} = $Inode;
	$r->{Size} = $Size;
	$r->{Url} = $Url;
	$r->{Mangled} = $Mangled;
	$r->{Dir} = $Dir;
	$Global{$Inode} = $r;
	$r = ();
    }
    close( FIND );


    # ******************************************
    # Scan through the local directory, now..
    # ******************************************
    print "Scanning the local entries in $CacheInfo{LOCAL_ROOT}\n" if( $Verbose );
    my %Local;
    $Cmd = "find $CacheInfo{LOCAL_ROOT} -name \"data*\" -print";
    foreach my $FullPath (`$Cmd` )
    {
	chomp $FullPath;
	my @Stat = stat( $FullPath );
	my $Inode = $Stat[1];
	my $Size = $Stat[7];

	# Get the directory portion of the path
	my @Dirs = split( /\//, $FullPath );
	$#Dirs--;
	my $Dir = join( "/", @Dirs );

	# Read the tag from the tag file..
	my $Tag = "";
	if ( open( TAG, "$Dir/tag" ) )
	{
	    $Tag = <TAG>;
	    close( TAG );
	}

	# Strip the local dir portion off the hash
	my $Mangled = $Dir;
	$Mangled =~ s/$CacheInfo{LOCAL_ROOT}\///;

	# There *should* be a matching global... Let's look
	my $MangledOk = 0;
	if ( defined( $Global{$Inode} ) )
	{
	    my $GlobalMangled = $Global{$Inode}->{Mangled};
	    $Mangled =~ s/\/$GlobalMangled//;
	    $MangledOk = 1;
	}
	# Otherwise, we should be able to get the hash from the tag..
	elsif ( $Tag ne "" )
	{
	    my $Cmd = "$ProgramC -t $Tag -m";
	    if ( open ( MANGLE, "$Cmd|" ) )
	    {
		while( <MANGLE> )
		{
		    if ( /^TAG: '(.*)'/ )
		    {
			$Mangled = $1;
			$MangledOk = 1;
		    }
		}
		close( MANGLE );
	    }
	}

	# Otherwise, let's make some guesses..
	if ( ! $MangledOk )
	{
	    # Should currently be something like: md5/local/md5/global
	    foreach my $GlobalStr ( @Manglings )
	    {
		$GlobalStr = "/" . $GlobalStr . "/";
		my $GlobalStart = rindex( $Mangled, $GlobalStr );
		if ( $GlobalStart > 0 )
		{
		    $Mangled = substr( $Mangled, 0, $GlobalStart );
		    last;
		}
	    }
	}

	# Store it all away in an Inode based hash of arrays
	my $r = ();
	$r->{Inode} = $Inode;
	$r->{Size} = $Size;
	$r->{Tag} = $Tag;
	$r->{Mangled} = $Mangled;
	$r->{Dir} = $Dir;
	push( @{$Local{$Inode}}, $r );
	$r = ();
    }


    # ********************
    # Dump it all out..
    # ********************
    if ( $Verbose )
    {
	print "Matching URL=$Settings{Url}\n" if ( $Settings{Url} ne "" );
	print "Matching Tag=$Settings{Tag}\n" if ( $Settings{Tag} ne "" );
    }
    foreach my $Inode ( keys %Global )
    {
	# Does it match??
	next if (  ( $Settings{MangledUrl} ) &&
		   ( $Settings{MangledUrl} ne $Global{$Inode}->{Mangled} )  );
	if ( $Settings{Tag} ne "" )
	{
	    my $Match = 0;
	    foreach my $Local ( @{$Local{$Inode}} )
	    {
		if ( $Local->{Tag} eq $Settings{Tag} )
		{
		    $Match++;
		    last;
		}
	    }
	    next if ( ! $Match );
	}

	# Matching URL found
	print "URL: $Global{$Inode}->{Url}\n";
	if ( $OptionLong )
	{
	    print "\tSize: $Global{$Inode}->{Size}\n";
	    print "\tMangled: $Global{$Inode}->{Mangled}\n";
	}
	foreach my $Local ( @{$Local{$Inode}} )
	{
	    next if (  ( $Settings{Tag} ne "" ) && ( $Local->{Tag} ne $Settings{Tag} ) );

	    print "\tTag:" . $Local->{Tag} . "\n";
	}
    }

}   # GassCacheListNormal()
# ******************************************************

# ******************************************************
# Function: GassCacheListFlat()
#
# Description:
#	List the contents of the Cache "Flat" mode
#
# Parameters:
#  None
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheListFlat ( )
{

    # Requested URL logic
    my $ReqUrl = $Settings{Url};
    my $ReqUrlKey = "";
    if ( $Settings{MangledUrl} ne "" )
    {
	my @UrlKey;
	( $ReqUrlKey, @UrlKey ) = split( /_/, $Settings{MangledUrl} );
	$ReqUrlKey = join( "_", @UrlKey );
    }

    # Requested Tag logic
    my $ReqTag = $Settings{Tag};
    my $ReqTagKey = "";
    if ( $Settings{MangledTag} ne "" )
    {
	my @TagKey;
	( $ReqTagKey, @TagKey ) = split( /_/, $Settings{MangledTag} );
	$ReqTagKey = join( "_", @TagKey );
    }


    # *************************************************
    # Now, let's do the real fun...
    #  Scan through the global directory for all URLs
    # *************************************************
    my %Dir;
    GassCacheFlatDir( \%Dir );


    # ********************
    # Dump it all out..
    # ********************
    if ( $Verbose )
    {
	print "Matching URL=$ReqUrl K=$ReqUrlKey\n";
	print "Matching Tag=$ReqTag K=$ReqTagKey\n";
    }
    foreach my $Type ( keys %{$Dir{MangleTypes}} )
    {
	foreach my $UrlKey ( keys %{$Dir{Urls}{$Type}} )
	{
	    # Does it match the request????
	    next if (  ( $ReqUrlKey ne "" ) &&
		       ( $ReqUrlKey ne $Dir{Urls}{$Type}{$UrlKey}{Data}->{Mangled} )  );
	    next if (  ( $ReqTagKey ne "" ) &&
		       ( ! exists $Dir{Urls}{$Type}{$UrlKey}{TagKeys}{$ReqTagKey} )  );

	    print "URL: " . $Dir{Urls}{$Type}{$UrlKey}{Url}->{Text} . "\n";
	    if ( $OptionLong )
	    {
		print "\tSize: " .
		    $Dir{Urls}{$Type}{$UrlKey}{Data}->{Size} . "\n";
		print "\tMangled: " .
		    $Dir{Urls}{$Type}{$UrlKey}{Data}->{Mangled} . "\n";
	    }

	    my @TagKeys = keys %{$Dir{Urls}{$Type}{$UrlKey}{TagKeys}};
	    foreach my $TagType ( keys %{$Dir{MangleTypes}} )
	    {
		foreach my $TagKey ( @TagKeys )
		{
		    next if ( ! exists $Dir{Tags}{$TagType}{$TagKey} );
		    next if (  ( $ReqTagKey ne "" ) && ( $ReqTagKey ne $TagKey ) );

		    my $r = $Dir{Tags}{$TagType}{$TagKey};
		    print "\tTag: $r->{Tag}->{Text}\n";

		    # Sanity check...
		    if ( ! exists $r->{Keys}{$UrlKey} )
		    {
			print STDERR "$UrlKey does not exist for tag!!\n";
			next;
		    }
		}
	    }
	}
    }


}   # GassCacheListFlat()
# ******************************************************

# ******************************************************
# Function: GassCacheCleanupUrl
#
# Description:
#	Cleanup a URL in the GASS Cache
#
# Parameters:
#  URL to cleanup
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheCleanupUrl ( )
{

    # Sanity check...
    if ( $Settings{Url} eq "" )
    {
	print STDERR "CleanupUrl requires a URL to cleanup\n";
	DumpHelp( );
	exit 1;
    }

    # Check the type & invoke the proper sub function
    if ( $CacheInfo{CACHE_TYPE} eq "normal" )
    {
	print "-- Normal cache --\n" if ($Verbose);
	GassCacheCleanupUrlNormal(  );
    }
    elsif ( $CacheInfo{CACHE_TYPE} eq "flat" )
    {
	print "-- Flat cache --\n" if ($Verbose);
	GassCacheCleanupUrlFlat(  );
    }
    else
    {
	print STDERR "Unknown cache type '%s'\n", $CacheInfo{CACHE_TYPE};
    }


}   # GassCacheCleanupUrl()
# ******************************************************

# ******************************************************
# Function: GassCacheCleanupUrlNormal
#
# Description:
#	Cleanup a URL in a "normal" GASS Cache
#
# Parameters:
#  URL to cleanup
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheCleanupUrlNormal ( )
{
    my $Url = $Settings{Url};
    my $Mangled = $Settings{MangledUrl};

    # Sanity checks

    # Let's learn all about our data file...
    my $FullGlobalDir = "$CacheInfo{GLOBAL_ROOT}/$Mangled";
    if ( ! -d $FullGlobalDir )
    {
	print STDERR "Could not clean up file because ".
	    "the URL was not found in the GASS cache.\n";
	exit 1;
    }
    my $FullGlobalData = "$FullGlobalDir/data";
    if ( ! -f $FullGlobalData )
    {
	print STDERR "Could not clean up file because ".
	    "the URL was not found in the GASS cache.\n";
	exit 1;
    }
    my $FullGlobalDataInode = -1;
    {
	my @Stat = stat( $FullGlobalData );
	if ( $#Stat < 0 )
	{
	    print STDERR "Could not stat data file for URL '$Url'\n";
	    print STDERR "Should be '$FullGlobalData'\n";
	    exit 1;
	}
	$FullGlobalDataInode = $Stat[1];
    }

    # Tell the user...
    print "Found global data file @ $FullGlobalData\n" if( $Verbose );

    # Scan through the local directory, now..
    my %Local;
    my $LocalCount = 0;
    my $Cmd = "find $CacheInfo{LOCAL_ROOT} -inum $FullGlobalDataInode -print";
    open( FIND, "$Cmd|" ) || die "Can't run '$Cmd'";
    print "Scanning the local entries in $CacheInfo{LOCAL_ROOT}\n"
	if( $Verbose );
    while( <FIND> )
    {
	chomp;
	my $FullDataPath = $_;

	my @Dirs = split( /\//, $FullDataPath );
	my $DataFile = pop @Dirs;
	my $Dir = join( "/", @Dirs );
	die "Oops" if ( "$Dir/$DataFile" ne $FullDataPath );
	my $TagFile = "$Dir/tag";

	# Check it out..
	if ( ! -f $FullDataPath )
	{
	    print STDERR "$FullDataPath is not a file!\n";
	    next;
	}
	if ( ! ( $FullDataPath =~ /\/data/ ) )
	{
	    print STDERR "$FullDataPath is not a data file!!\n";
	    next;
	}
	if ( ! -d $Dir )
	{
	    print STDERR "$Dir is not a directory!!\n";
	    next;
	}
	my $Tag;
	if ( ! -f $TagFile )
	{
	    print STDERR "$TagFile <tag file> not found\n";
	    my $Tag = "";
	}
	else
	{
            local(*TAGFILE);
            open(TAGFILE, "<$TagFile");
            chomp($Tag = <TAGFILE>);
            close(TAGFILE);
	}

	# Store it...
	my $r = {};
	$r->{Tag} = $Tag;
	$r->{File} = $DataFile;
	push @{$Local{$Dir}}, $r;
	$r = {};
	$LocalCount++;
    }
    close( FIND );

    # Print results to the user...
    if( $Verbose )
    {
	if ( $LocalCount <= 0 )
	{
	    print "No local links found\n";
	}
	else
	{
	    print "Found $LocalCount local links:\n";
	    foreach my $Dir ( keys %Local )
	    {
		foreach my $FileRec ( @{$Local{$Dir}} )
		{
		    my $File = $FileRec->{File};
		    my $Tag = $FileRec->{Tag};
		    if ( $Tag eq "" )
		    {
			print "\t$Dir/$File\n";
		    }
		    else
		    {
			print "\t$Tag\n";
		    }
		}
	    }
	}
    }

    # Clean it all up...
    foreach my $Dir ( keys %Local )
    {
	foreach my $FileRec ( @{$Local{$Dir}} )
	{
	    print "$FileRec->{Tag}.. ($FileRec->{File}) " if ( $Verbose );
	    unlink "$Dir/$FileRec->{File}";
	    print "\n" if ( $Verbose );
	}

	# Blow away the extras..
	unlink "$Dir/url";
	unlink "$Dir/tag";

	# Temp dir for working in..
	my $TmpDir;

	# Blow away the directories..
	my @Dirs = split( /\//, $Dir );
	while( 1 )
	{
	    $TmpDir = join ('/', @Dirs );
	    last if ( $TmpDir eq $CacheInfo{LOCAL_ROOT} );
	    last if ( ! rmdir( $TmpDir ) );
	    print "\t$TmpDir\n " if ( $Verbose );
	    pop @Dirs;
	}

	# Check: does this directory have only a "tag" file?
	my $TagFile = "$TmpDir/tag";
	if (  ( $TmpDir ne $CacheInfo{LOCAL_ROOT} ) && ( -f "$TagFile" )  )
	{
	    my @Stat = stat( "$TagFile" );

	    # Unlink it if it's the last one..
	    if ( $Stat[3] == 1 )
	    {
		unlink( "$TagFile" );
	    }

	    # Try cleaning up more...
	    while( 1 )
	    {
		$TmpDir = join ('/', @Dirs );
		last if ( $TmpDir eq $CacheInfo{LOCAL_ROOT} );
		last if ( ! rmdir( $TmpDir ) );
		print "\t$TmpDir\n " if ( $Verbose );
		pop @Dirs;
	    }
	}
    }

    # And, remove the global dir
    {
	my $Dir = $FullGlobalDir;
	print "Global.. " if ( $Verbose );
	unlink "$Dir/data";
	unlink "$Dir/url";

	my @Dirs = split( /\//, $Dir );
	while( 1 )
	{
	    my $TmpDir = join ('/', @Dirs );
	    last if ( $TmpDir eq $CacheInfo{GLOBAL_ROOT} );
	    last if ( ! rmdir( $TmpDir ) );
	    print "\n\t$TmpDir " if( $Verbose );
	    pop @Dirs;
	}
	print "\n" if ( $Verbose );
    }

}   # GassCacheCleanupUrlNormal()
# ******************************************************

# ******************************************************
# Function: GassCacheCleanupUrlFlat
#
# Description:
#	Cleanup a URL in a "flat" GASS Cache
#
# Parameters:
#  URL to cleanup
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheCleanupUrlFlat ( )
{
    my $Url = $Settings{Url};
    my $Mangled = $Settings{MangledUrl};
    my ( $UrlMangle, @UrlKey ) = split( /_/, $Mangled );
    my $UrlKey = join( "_", @UrlKey );

    # Let's go hunting..  :-)
    my %Dir;
    GassCacheFlatDir( \%Dir );

    # Does it exist in the cache anywhere??
    if ( ! exists $Dir{Urls}{$UrlMangle}{$UrlKey} )
    {
	print STDERR "Could not clean up file because ".
	    "the URL was not found in the GASS cache.\n";
	return 1;
    }

    # Shortcut...
    my $UrlRec = $Dir{Urls}{$UrlMangle}{$UrlKey};

    # Report to the user...
    if ( $Verbose )
    {
	print "URL: " . $UrlRec->{Url}->{Text} . "\n";
	print "Local:\n";
	if ( ! exists $UrlRec->{LocalData} )
	{
	    print "\tNo local links found\n";
	}
	else
	{
	    print "\tFound " . $UrlRec->{LocalData} . " local links:\n";
	}
    }

    # Ok, now let's find tags that match this URL
    my @TagKeys = keys %{$UrlRec->{TagKeys}};
    foreach my $TagKey ( @TagKeys )
    {
	foreach my $TagType ( keys %{$Dir{MangleTypes}} )
	{
	    next if ( ! exists $Dir{Tags}{$TagType}{$TagKey} );

	    my $r = $Dir{Tags}{$TagType}{$TagKey};

	    # Sanity check!
	    if ( ! exists $r->{Keys}{$UrlKey} )
	    {
		print STDERR "$Url does not exist for tag!!\n";
		next;
	    }

	    # For verbose output, let's go count the data files...
	    if ( $Verbose )
	    {
		my $DataCount = 0;
		if ( exists $r->{Keys}{$UrlKey}{Data} )
		{
		    $DataCount = $#{$r->{Keys}{$UrlKey}{Data}} + 1;
		}
		print "\tTag [$DataCount]: " . $r->{Tag}->{Text} . "\n";
	    }

	    # Data files..
	    if ( exists $r->{Keys}{$UrlKey}{Data} )
	    {
		foreach my $Data ( @{$r->{Keys}{$UrlKey}{Data}} )
		{
		    print "\tL.Data = " . $Data->{Name} . "\n" if ( $Verbose );
		    unlink( $CacheInfo{CACHE_DIR} . "/" . $Data->{Name} );
		}
	    }

	    # Tag file?
	    if ( exists $r->{Keys}{$UrlKey}{Tag} )
	    {
		print "\tL.Tag File: " .
		    $r->{Keys}{$UrlKey}{Tag}->{Name} . "\n" if ( $Verbose > 1 );
		unlink( $CacheInfo{CACHE_DIR} . "/" .
			$r->{Keys}{$UrlKey}{Tag}->{Name} );
	    }

	    # Entry file?
	    if ( exists $r->{Keys}{$UrlKey}{Entry} )
	    {
		if ( $Verbose > 1 )
		{
		    print "\tL.Entry File: " .
			$r->{Keys}{$UrlKey}{Entry}->{Name} . "\n";
		}
		unlink( $CacheInfo{CACHE_DIR} . "/" .
			$r->{Keys}{$UrlKey}{Entry}->{Name} );
	    }
	}
    }

    # Ok, now the global files...
    print "Global..\n" if ( $Verbose );
    if ( exists $UrlRec->{DataLinks} )
    {
	foreach my $Data ( @{$UrlRec->{DataLinks}} )
	{
	    print "\tG.DataLink = " . $Data->{Name} . "\n" if ( $Verbose );
	    unlink( $CacheInfo{CACHE_DIR} . "/" . $Data->{Name} );
	}
    }
    if ( exists $UrlRec->{Data} )
    {
	print "\tG.Data = " . $UrlRec->{Data}->{Name} . "\n" if ( $Verbose );
	unlink( $CacheInfo{CACHE_DIR} . "/" . $UrlRec->{Data}->{Name} );
    }
    if ( exists $UrlRec->{Url} )
    {
	if ( $Verbose > 1 )
	{
	    print "\tG.Url = " . $UrlRec->{Url}->{Name} . "\n";
	    print "\tG.Url = '" . $UrlRec->{Url}->{Text} . "'\n";
	}
	unlink( $CacheInfo{CACHE_DIR} . "/" . $UrlRec->{Url}->{Name} );
    }
    if ( exists $UrlRec->{Entry} )
    {
	print "\tG.Entry = " . $UrlRec->{Entry}->{Name} . "\n"
	    if ( $Verbose > 1 );
	unlink( $CacheInfo{CACHE_DIR} . "/" . $UrlRec->{Entry}->{Name} );
    }

}   # GassCacheCleanupUrlFlat()
# ******************************************************

# ******************************************************
# Function: GassCacheFlatDir()
#
# Description:
#	Gather info on a "Flat" cache
#
# Parameters:
#  None
#
# Return Values:
#  None
#
# ******************************************************
sub GassCacheFlatDir ( $ )
{
    my $Dir = shift;


    # *************************************************
    # Now, let's do the real fun...
    #  Scan through the global directory for all URLs
    # *************************************************
    print "Scanning the global entries in $CacheInfo{CACHE_DIR}\n"
	if( $Verbose );
    opendir( CACHE, $CacheInfo{CACHE_DIR} );
    while( $_ = readdir( CACHE ) )
    {
	next if ( ! /_/ );
	study;

	my $FullPath = "$CacheInfo{CACHE_DIR}/$_";
	my @Stat = stat( $FullPath );
	my $Inode = $Stat[1];
	my $Size = $Stat[7];

	# Record to store...
	my %r = ( Inode => $Stat[1], Size => $Stat[7],
		  Links=>$Stat[3], Name => $_ );

	# global_md5_hash_data
	if ( /^global_([^_]+)_(.*)_data$/ )
	{
	    my $Type = $1;
	    my $Mangled = $2;
	    $r{Mangled} = $Mangled;
	    $r{Type} = "data";

	    $Dir->{MangleTypes}{$Type} = 1;
	    $Dir->{Urls}{$Type}{$Mangled}{Data} = \%r;
	}
	# global_md5_hash_data...
	elsif ( /^global_([^_]+)_(.*)_data/ )
	{
	    my $Type = $1;
	    my $Mangled = $2;
	    $r{Mangled} = $Mangled;
	    $r{Type} = "data";

	    $Dir->{MangleTypes}{$Type} = 1;
	    push( @{$Dir->{Urls}{$Type}{$Mangled}{DataLinks}}, \%r );
	}
	# global_md5_hash_url
	elsif ( /^global_([^_]+)_(.*)_url/ )
	{
	    my $Type = $1;
	    my $Mangled = $2;

	    local($/,*FP);	# slurp mode
	    open( FP, '<' . $FullPath );
	    $r{Text} = <FP>;
	    close(FP);
	    $r{Type} = "url";

	    $Dir->{MangleTypes}{$Type} = 1;
	    $Dir->{Urls}{$Type}{$Mangled}{Url} = \%r;
	}
	# global_md5_hash
	elsif ( /^global_([^_]+)_(.*)/ )
	{
	    my $Type = $1;
	    my $Mangled = $2;
	    $r{Type} = "entry";

	    $Dir->{MangleTypes}{$Type} = 1;
	    $Dir->{Urls}{$Type}{$Mangled}{Entry} = \%r;
	}
	# local_md5_hash_.*
	elsif ( /^local_([^_]+)_(.*)/ )
	{
	    my $Type = $1;
	    my $Details = $2;
	    my $Separator = "_" . $Type . "_";

	    $Dir->{MangleTypes}{$Type} = 1;

	    study $Details;

	    # local_md5_hash_md5_hash_data
	    if ( $Details =~ /^(.*)$Separator(.*)_data/ )
	    {
		my $LKey = $1;
		my $GKey = $2;
		$r{Type} = "data";

		# Update local data count for this URL
		if ( ! exists $Dir->{Urls}{$Type}{$GKey}{LocalData} )
		{
		    $Dir->{Urls}{$Type}{$GKey}{LocalData} = 0;
		}
		$Dir->{Urls}{$Type}{$GKey}{LocalData}++;

		# Add this to "Tag" list for this URL
		$Dir->{Urls}{$Type}{$GKey}{TagKeys}{$LKey} = 1;

		# Store the data file reference
		push( @{$Dir->{Tags}{$Type}{$LKey}{Keys}{$GKey}{Data}}, \%r );
	    }
	    # local_md5_hash_md5_hash_tag
	    elsif ( $Details =~ /^(.*)$Separator(.*)_tag/ )
	    {
		my $LKey = $1;
		my $GKey = $2;
		local($/,*FP);
		$r{Type} = "tag";
		open( FP, '<' . $FullPath );
		$r{Text} = <FP>;
		close(FP);

		# Add this to "Tag" list for this URL
		$Dir->{Urls}{$Type}{$GKey}{TagKeys}{$LKey} = 1;

		# Store the file reference
		$Dir->{Tags}{$Type}{$LKey}{Keys}{$GKey}{Tag} = \%r;
	    }
	    # local_md5_hash_tag
	    elsif ( $Details =~ /^(.*)_tag/ )
	    {
		local($/,*FP);
		my $LKey = $1;
		$r{Type} = "tag";
		open( FP, '<' . $FullPath );
		$r{Text} = <FP>;
		close(FP);
		$Dir->{Tags}{$Type}{$LKey}{Tag} = \%r;
	    }
	    # local_md5_hash_md5_hash
	    elsif ( $Details =~ /^(.*)$Separator(.*)/ )
	    {
		my $LKey = $1;
		my $GKey = $2;
		$r{Type} = "entry";

		# Add this to "Tag" list for this URL
		$Dir->{Urls}{$Type}{$GKey}{TagKeys}{$LKey} = 1;

		# Store the file reference
		$Dir->{Tags}{$Type}{$LKey}{Keys}{$GKey}{Entry} = \%r;
	    }
	    else
	    {
		print STDERR "Warning: Ignoring local file '$_'\n";
	    }
	}
	else
	{
	    print STDERR "Warning; Ignoring '$_'\n";
	}
    }
    closedir( CACHE );

}   # GassCacheFlatDir()
# ******************************************************

# ******************************************************
# Function: Mangle
#
# Description:
#	Mangle a string
#
# Parameters:
#  String to mangle
#
# Return Values:
#  None
#
# ******************************************************
sub Mangle( $ )
{
    my $String = shift;

    local $ENV{GLOBUS_GASS_CACHE_DEBUG};
    delete( $ENV{GLOBUS_GASS_CACHE_DEBUG} );
    my $Mangled = "";

    my $Cmd = "$ProgramC -mangle $String";
    open( MANGLE, "$Cmd|" ) || die "Can't run '$Cmd'\n";
    while ( <MANGLE> )
    {
	chomp;
	if ( /URL:\s+\'(.*)\'/ )
	{
	    $Mangled = $1;
	}
    }
    close( MANGLE );

    if ( $Mangled eq "" )
    {
	print STDERR "Failed to mangle URL!\n";
    }

    return $Mangled;

}   # Mangle()
# ******************************************************

# ******************************************************
# Function: DumpHelp
#
# Description:
#	Dumps out some help
#
# Parameters:
#  URL to cleanup
#
# Return Values:
#  None
#
# ******************************************************
sub DumpHelp( )
{
    # help does not need to be efficient...
    open( PROGC,  "$ProgramC -help 2>&1 |" )
	or die "Can't run '$ProgramC' for help";

    # Just print everything up 'til the "Valid optoins" line
    $_ = <PROGC>;
    while ( <PROGC> )
    {
	print;
	last if ( /^Valid oper/ );
    }

    # Now, all the C program operations..
    while ( <PROGC> )
    {
	chomp; s/\s+$//;
	last if ( $_ eq "" );
	print "$_\n";
    }

    # Print out _our_ operations
    print "$PerlOperations\n";

    # Now, all the C program options..
    while ( <PROGC> )
    {
	chomp; s/\s+$//;
	last if ( $_ eq "" );
	print "$_\n";
    }
    # Print out _our- options
    print "$PerlOptions\n";
    close( PROGC );

}   # DumpHelp()
# ******************************************************