@@ -73,7 +73,8 @@ public abstract class AbstractArchiver
73
73
/**
74
74
* A list of the following objects:
75
75
* <ul>
76
- * <li>Instances of {@link ArchiveEntry}, which are passed back by {@link #getResources()} without modifications.</li>
76
+ * <li>Instances of {@link ArchiveEntry}, which are passed back by {@link #getResources()} without modifications
77
+ * .</li>
77
78
* <li>Instances of {@link PlexusIoResourceCollection}, which are converted into an {@link Iterator} over instances
78
79
* of {@link ArchiveEntry} by {@link #getResources()}.
79
80
* </ul>
@@ -82,13 +83,13 @@ public abstract class AbstractArchiver
82
83
83
84
private boolean includeEmptyDirs = true ;
84
85
85
- private int fileMode = -1 ;
86
+ private int forcedFileMode = -1 ; // Will always be used
86
87
87
- private int directoryMode = -1 ;
88
+ private int forcedDirectoryMode = -1 ; // Will always be used
88
89
89
- private int defaultFileMode = -1 ;
90
+ private int defaultFileMode = -1 ; // Optionally used if a value is needed
90
91
91
- private int defaultDirectoryMode = -1 ;
92
+ private int defaultDirectoryMode = -1 ; // Optionally used if a value is needed
92
93
93
94
private boolean forced = true ;
94
95
@@ -101,7 +102,8 @@ public abstract class AbstractArchiver
101
102
// On lunix-like systems, we replace windows backslashes with forward slashes
102
103
private final boolean replacePathSlashesToJavaPaths = File .separatorChar == '/' ;
103
104
104
- private final List <Closeable > closeables = new ArrayList <Closeable >( );
105
+ private final List <Closeable > closeables = new ArrayList <Closeable >();
106
+
105
107
/**
106
108
* since 2.2 is on by default
107
109
*
@@ -112,6 +114,36 @@ public abstract class AbstractArchiver
112
114
// contextualized.
113
115
private ArchiverManager archiverManager ;
114
116
117
+ private static class AddedResourceCollection
118
+ {
119
+ private final PlexusIoResourceCollection resources ;
120
+
121
+ private final int forcedFileMode ;
122
+
123
+ private final int forcedDirectoryMode ;
124
+
125
+ public AddedResourceCollection ( PlexusIoResourceCollection resources , int forcedFileMode , int forcedDirMode )
126
+ {
127
+ this .resources = resources ;
128
+ this .forcedFileMode = forcedFileMode ;
129
+ this .forcedDirectoryMode = forcedDirMode ;
130
+ }
131
+
132
+ private int maybeOverridden ( int suggestedMode , boolean isDir )
133
+ {
134
+ if ( isDir )
135
+ {
136
+ return forcedDirectoryMode >= 0 ? forcedDirectoryMode : suggestedMode ;
137
+ }
138
+ else
139
+ {
140
+ return forcedFileMode >= 0 ? forcedFileMode : suggestedMode ;
141
+
142
+ }
143
+ }
144
+
145
+ }
146
+
115
147
/**
116
148
* @since 1.1
117
149
*/
@@ -138,11 +170,11 @@ public final void setFileMode( final int mode )
138
170
{
139
171
if ( mode >= 0 )
140
172
{
141
- fileMode = ( mode & UnixStat .PERM_MASK ) | UnixStat .FILE_FLAG ;
173
+ forcedFileMode = ( mode & UnixStat .PERM_MASK ) | UnixStat .FILE_FLAG ;
142
174
}
143
175
else
144
176
{
145
- fileMode = -1 ;
177
+ forcedFileMode = -1 ;
146
178
}
147
179
}
148
180
@@ -153,12 +185,12 @@ public final void setDefaultFileMode( final int mode )
153
185
154
186
public final int getOverrideFileMode ()
155
187
{
156
- return fileMode ;
188
+ return forcedFileMode ;
157
189
}
158
190
159
191
public final int getFileMode ()
160
192
{
161
- if ( fileMode < 0 )
193
+ if ( forcedFileMode < 0 )
162
194
{
163
195
if ( defaultFileMode < 0 )
164
196
{
@@ -168,7 +200,7 @@ public final int getFileMode()
168
200
return defaultFileMode ;
169
201
}
170
202
171
- return fileMode ;
203
+ return forcedFileMode ;
172
204
}
173
205
174
206
public final int getDefaultFileMode ()
@@ -188,11 +220,11 @@ public final void setDirectoryMode( final int mode )
188
220
{
189
221
if ( mode >= 0 )
190
222
{
191
- directoryMode = ( mode & UnixStat .PERM_MASK ) | UnixStat .DIR_FLAG ;
223
+ forcedDirectoryMode = ( mode & UnixStat .PERM_MASK ) | UnixStat .DIR_FLAG ;
192
224
}
193
225
else
194
226
{
195
- directoryMode = -1 ;
227
+ forcedDirectoryMode = -1 ;
196
228
}
197
229
}
198
230
@@ -203,12 +235,12 @@ public final void setDefaultDirectoryMode( final int mode )
203
235
204
236
public final int getOverrideDirectoryMode ()
205
237
{
206
- return directoryMode ;
238
+ return forcedDirectoryMode ;
207
239
}
208
240
209
241
public final int getDirectoryMode ()
210
242
{
211
- if ( directoryMode < 0 )
243
+ if ( forcedDirectoryMode < 0 )
212
244
{
213
245
if ( defaultDirectoryMode < 0 )
214
246
{
@@ -218,7 +250,7 @@ public final int getDirectoryMode()
218
250
return defaultDirectoryMode ;
219
251
}
220
252
221
- return directoryMode ;
253
+ return forcedDirectoryMode ;
222
254
}
223
255
224
256
public final int getDefaultDirectoryMode ()
@@ -338,8 +370,8 @@ public void addSymlink( String symlinkName, String symlinkDestination )
338
370
public void addSymlink ( String symlinkName , int permissions , String symlinkDestination )
339
371
throws ArchiverException
340
372
{
341
- doAddResource ( ArchiveEntry . createSymlinkEntry ( symlinkName , permissions , symlinkDestination ,
342
- getDirectoryMode () ) );
373
+ doAddResource (
374
+ ArchiveEntry . createSymlinkEntry ( symlinkName , permissions , symlinkDestination , getDirectoryMode () ) );
343
375
}
344
376
345
377
protected ArchiveEntry asArchiveEntry ( @ Nonnull final PlexusIoResource resource , final String destFileName ,
@@ -353,35 +385,49 @@ protected ArchiveEntry asArchiveEntry( @Nonnull final PlexusIoResource resource,
353
385
354
386
if ( resource .isFile () )
355
387
{
356
- return ArchiveEntry .createFileEntry ( destFileName , resource , permissions , collection ,
357
- getDirectoryMode () );
388
+ return ArchiveEntry .createFileEntry ( destFileName , resource , permissions , collection , getDirectoryMode () );
358
389
}
359
390
else
360
391
{
361
392
return ArchiveEntry .createDirectoryEntry ( destFileName , resource , permissions , getDirectoryMode () );
362
393
}
363
394
}
364
395
365
- protected ArchiveEntry asArchiveEntry ( final PlexusIoResourceCollection collection ,
366
- final PlexusIoResource resource )
396
+ private int maybeOverridden ( int suggestedMode , boolean isDir )
397
+ {
398
+ if ( isDir )
399
+ {
400
+ return forcedDirectoryMode >= 0 ? forcedDirectoryMode : suggestedMode ;
401
+ }
402
+ else
403
+ {
404
+ return forcedFileMode >= 0 ? forcedFileMode : suggestedMode ;
405
+
406
+ }
407
+ }
408
+
409
+ private ArchiveEntry asArchiveEntry ( final AddedResourceCollection collection , final PlexusIoResource resource )
367
410
throws ArchiverException
368
411
{
369
- final String destFileName = collection .getName ( resource );
412
+ final String destFileName = collection .resources . getName ( resource );
370
413
371
- int permissions = -1 ;
372
- if ( resource instanceof ResourceAttributeSupplier )
414
+ int fromResource = -1 ;
415
+ if ( resource instanceof ResourceAttributeSupplier )
373
416
{
374
417
final PlexusIoResourceAttributes attrs = ( (ResourceAttributeSupplier ) resource ).getAttributes ();
375
418
376
419
if ( attrs != null )
377
420
{
378
- permissions = attrs .getOctalMode ();
421
+ fromResource = attrs .getOctalMode ();
379
422
}
380
423
}
381
424
382
- return asArchiveEntry ( resource , destFileName , permissions , collection );
425
+ return asArchiveEntry ( resource , destFileName ,
426
+ collection .maybeOverridden ( fromResource , resource .isDirectory () ),
427
+ collection .resources );
383
428
}
384
429
430
+
385
431
public void addResource ( final PlexusIoResource resource , final String destFileName , final int permissions )
386
432
throws ArchiverException
387
433
{
@@ -426,7 +472,7 @@ public ResourceIterator getResources()
426
472
{
427
473
private final Iterator addedResourceIter = resources .iterator ();
428
474
429
- private PlexusIoResourceCollection currentResourceCollection ;
475
+ private AddedResourceCollection currentResourceCollection ;
430
476
431
477
private Iterator ioResourceIter ;
432
478
@@ -449,13 +495,13 @@ public boolean hasNext()
449
495
{
450
496
nextEntry = (ArchiveEntry ) o ;
451
497
}
452
- else if ( o instanceof PlexusIoResourceCollection )
498
+ else if ( o instanceof AddedResourceCollection )
453
499
{
454
- currentResourceCollection = (PlexusIoResourceCollection ) o ;
500
+ currentResourceCollection = (AddedResourceCollection ) o ;
455
501
456
502
try
457
503
{
458
- ioResourceIter = currentResourceCollection .getResources ();
504
+ ioResourceIter = currentResourceCollection .resources . getResources ();
459
505
}
460
506
catch ( final IOException e )
461
507
{
@@ -492,7 +538,7 @@ else if ( o instanceof PlexusIoResourceCollection )
492
538
}
493
539
}
494
540
495
- if ( nextEntry != null && seenEntries .contains ( normalizedForDuplicateCheck (nextEntry ) ))
541
+ if ( nextEntry != null && seenEntries .contains ( normalizedForDuplicateCheck ( nextEntry ) ) )
496
542
{
497
543
final String path = nextEntry .getName ();
498
544
@@ -540,7 +586,7 @@ public ArchiveEntry next()
540
586
final ArchiveEntry next = nextEntry ;
541
587
nextEntry = null ;
542
588
543
- seenEntries .add ( normalizedForDuplicateCheck (next ) );
589
+ seenEntries .add ( normalizedForDuplicateCheck ( next ) );
544
590
545
591
return next ;
546
592
}
@@ -550,8 +596,9 @@ public void remove()
550
596
throw new UnsupportedOperationException ( "Does not support iterator" );
551
597
}
552
598
553
- private String normalizedForDuplicateCheck (ArchiveEntry entry ){
554
- return entry .getName ().replace ( '\\' , '/' );
599
+ private String normalizedForDuplicateCheck ( ArchiveEntry entry )
600
+ {
601
+ return entry .getName ().replace ( '\\' , '/' );
555
602
}
556
603
557
604
};
@@ -654,8 +701,9 @@ protected PlexusIoResourceCollection asResourceCollection( final ArchivedFileSet
654
701
"Error adding archived file-set. PlexusIoResourceCollection not found for: " + archiveFile , e );
655
702
}
656
703
657
- if (resources instanceof EncodingSupported ) {
658
- ((EncodingSupported )resources ).setEncoding ( charset );
704
+ if ( resources instanceof EncodingSupported )
705
+ {
706
+ ( (EncodingSupported ) resources ).setEncoding ( charset );
659
707
}
660
708
661
709
if ( resources instanceof PlexusIoArchivedResourceCollection )
@@ -702,11 +750,12 @@ protected PlexusIoResourceCollection asResourceCollection( final ArchivedFileSet
702
750
public void addResources ( final PlexusIoResourceCollection collection )
703
751
throws ArchiverException
704
752
{
705
- doAddResource ( collection );
753
+ doAddResource ( new AddedResourceCollection ( collection , forcedFileMode , forcedDirectoryMode ) );
706
754
}
707
755
708
- private void doAddResource (Object item ){
709
- resources .add ( item );
756
+ private void doAddResource ( Object item )
757
+ {
758
+ resources .add ( item );
710
759
}
711
760
712
761
public void addArchivedFileSet ( final ArchivedFileSet fileSet )
@@ -838,11 +887,11 @@ protected boolean isUptodate()
838
887
{
839
888
l = ( (ArchiveEntry ) o ).getResource ().getLastModified ();
840
889
}
841
- else if ( o instanceof PlexusIoResourceCollection )
890
+ else if ( o instanceof AddedResourceCollection )
842
891
{
843
892
try
844
893
{
845
- l = ( (PlexusIoResourceCollection ) o ).getLastModified ();
894
+ l = ( (AddedResourceCollection ) o ). resources .getLastModified ();
846
895
}
847
896
catch ( final IOException e )
848
897
{
@@ -967,11 +1016,15 @@ protected void validate()
967
1016
968
1017
protected abstract String getArchiveType ();
969
1018
970
- private void addCloseable (Object maybeCloseable ){
971
- if (maybeCloseable instanceof Closeable )
1019
+ private void addCloseable ( Object maybeCloseable )
1020
+ {
1021
+ if ( maybeCloseable instanceof Closeable )
1022
+ {
972
1023
closeables .add ( (Closeable ) maybeCloseable );
1024
+ }
973
1025
974
1026
}
1027
+
975
1028
private void closeIterators ()
976
1029
{
977
1030
for ( Closeable closeable : closeables )
@@ -980,6 +1033,7 @@ private void closeIterators()
980
1033
}
981
1034
982
1035
}
1036
+
983
1037
protected abstract void close ()
984
1038
throws IOException ;
985
1039
0 commit comments