Skip to content

Commit e1cad36

Browse files
committed
[PLXCOMP-271] Take 2
1 parent 6f6d74f commit e1cad36

File tree

6 files changed

+63
-23
lines changed

6 files changed

+63
-23
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<dependency>
5757
<groupId>org.codehaus.plexus</groupId>
5858
<artifactId>plexus-io</artifactId>
59-
<version>2.3.3</version>
59+
<version>2.3.4</version>
6060
</dependency>
6161
<dependency>
6262
<groupId>org.apache.commons</groupId>

src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ public void addSymlink( String symlinkName, String symlinkDestination )
350350
public void addSymlink( String symlinkName, int permissions, String symlinkDestination )
351351
throws ArchiverException
352352
{
353-
resources.add( ArchiveEntry.createSymlinkEntry( symlinkName, permissions, symlinkDestination ) );
353+
resources.add( ArchiveEntry.createSymlinkEntry( symlinkName, permissions, symlinkDestination,
354+
getDirectoryMode() ) );
354355
}
355356

356357
protected ArchiveEntry asArchiveEntry( @Nonnull final PlexusIoResource resource, final String destFileName,
@@ -364,11 +365,12 @@ protected ArchiveEntry asArchiveEntry( @Nonnull final PlexusIoResource resource,
364365

365366
if ( resource.isFile() )
366367
{
367-
return ArchiveEntry.createFileEntry( destFileName, resource, permissions, collection );
368+
return ArchiveEntry.createFileEntry( destFileName, resource, permissions, collection,
369+
getDirectoryMode() );
368370
}
369371
else
370372
{
371-
return ArchiveEntry.createDirectoryEntry( destFileName, resource, permissions );
373+
return ArchiveEntry.createDirectoryEntry( destFileName, resource, permissions, getDirectoryMode() );
372374
}
373375
}
374376

@@ -427,12 +429,14 @@ public void addFile( @Nonnull final File inputFile, @Nonnull String destFileName
427429

428430
if ( include( in, destFileName ) )
429431
{
430-
resources.add( ArchiveEntry.createFileEntry( destFileName, inputFile, permissions ) );
432+
resources.add( ArchiveEntry.createFileEntry( destFileName, inputFile, permissions,
433+
getDirectoryMode() ) );
431434
}
432435
}
433436
else
434437
{
435-
resources.add( ArchiveEntry.createFileEntry( destFileName, inputFile, permissions ) );
438+
resources.add( ArchiveEntry.createFileEntry( destFileName, inputFile, permissions,
439+
getDirectoryMode() ) );
436440
}
437441
}
438442
catch ( final IOException e )

src/main/java/org/codehaus/plexus/archiver/ArchiveEntry.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class ArchiveEntry
5555

5656
private final int mode;
5757

58+
private final int defaultDirMode; // Sometimes a directory needs to be created. Which mode should it be ?
59+
// this mode is at the time of the creation of the archive entry, which is an important distinction
60+
5861
private PlexusIoResourceAttributes attributes;
5962

6063
/**
@@ -64,12 +67,14 @@ public class ArchiveEntry
6467
* @param type FILE or DIRECTORY
6568
* @param mode octal unix style permissions
6669
* @param collection
70+
* @param defaultDirMode
6771
*/
6872
private ArchiveEntry( String name, @Nonnull PlexusIoResource resource, int type, int mode,
69-
PlexusIoResourceCollection collection )
73+
PlexusIoResourceCollection collection, int defaultDirMode )
7074
{
7175
this.name = name;
7276
this.collection = collection;
77+
this.defaultDirMode = defaultDirMode;
7378
try {
7479
this.resource = collection != null ? collection.resolve(resource) : resource;
7580
} catch (IOException e) {
@@ -151,18 +156,18 @@ public int getMode()
151156
}
152157

153158
public static ArchiveEntry createFileEntry( String target, PlexusIoResource resource, int permissions,
154-
PlexusIoResourceCollection collection )
159+
PlexusIoResourceCollection collection, int defaultDirectoryPermissions )
155160
throws ArchiverException
156161
{
157162
if ( resource.isDirectory() )
158163
{
159164
throw new ArchiverException( "Not a file: " + resource.getName() );
160165
}
161166
final int type = resource.isSymbolicLink() ? SYMLINK : FILE;
162-
return new ArchiveEntry( target, resource, type, permissions, collection );
167+
return new ArchiveEntry( target, resource, type, permissions, collection, defaultDirectoryPermissions );
163168
}
164169

165-
public static ArchiveEntry createFileEntry( String target, File file, int permissions )
170+
public static ArchiveEntry createFileEntry( String target, File file, int permissions, int defaultDirectoryPermissions )
166171
throws ArchiverException
167172
{
168173
if ( !file.isFile() )
@@ -181,21 +186,23 @@ public static ArchiveEntry createFileEntry( String target, File file, int permis
181186
}
182187

183188
final PlexusIoFileResource res = PlexusIoFileResource.justAFile( file, attrs );
184-
return new ArchiveEntry( target, res, FILE, permissions, null );
189+
return new ArchiveEntry( target, res, FILE, permissions, null, defaultDirectoryPermissions );
185190
}
186191

187-
public static ArchiveEntry createDirectoryEntry( String target, @Nonnull PlexusIoResource resource, int permissions )
192+
public static ArchiveEntry createDirectoryEntry( String target, @Nonnull PlexusIoResource resource, int permissions,
193+
int defaultDirectoryPermissions )
188194
throws ArchiverException
189195
{
190196
if ( !resource.isDirectory() )
191197
{
192198
throw new ArchiverException( "Not a directory: " + resource.getName() );
193199
}
194200
final int type = resource.isSymbolicLink() ? SYMLINK : DIRECTORY;
195-
return new ArchiveEntry( target, resource, type, permissions, null );
201+
return new ArchiveEntry( target, resource, type, permissions, null, defaultDirectoryPermissions );
196202
}
197203

198-
public static ArchiveEntry createDirectoryEntry( String target, final File file, int permissions )
204+
public static ArchiveEntry createDirectoryEntry( String target, final File file, int permissions,
205+
int defaultDirMode1 )
199206
throws ArchiverException
200207
{
201208
if ( !file.isDirectory() )
@@ -204,31 +211,33 @@ public static ArchiveEntry createDirectoryEntry( String target, final File file,
204211
}
205212

206213
final PlexusIoFileResource res = new PlexusIoFileResource( file, ArchiverAttributeUtils.getFileAttributes(file));
207-
return new ArchiveEntry( target, res, DIRECTORY, permissions, null );
214+
return new ArchiveEntry( target, res, DIRECTORY, permissions, null, defaultDirMode1 );
208215
}
209216

210-
public static ArchiveEntry createEntry( String target, File file, int filePerm, int dirPerm )
217+
public static ArchiveEntry createEntry( String target, File file, int filePerm, int dirPerm, int defaultDirectoryPermissions )
211218
throws ArchiverException
212219
{
213220
if ( file.isDirectory() )
214221
{
215-
return createDirectoryEntry( target, file, dirPerm );
222+
return createDirectoryEntry( target, file, dirPerm, defaultDirectoryPermissions );
216223
}
217224
else if ( file.isFile() )
218225
{
219-
return createFileEntry( target, file, filePerm );
226+
return createFileEntry( target, file, filePerm, defaultDirectoryPermissions );
220227
}
221228
else // FIXME: handle symlinks?
222229
{
223230
throw new ArchiverException( "Neither a file nor a directory: " + file );
224231
}
225232
}
226233

227-
public static ArchiveEntry createSymlinkEntry(String symlinkName, int permissions, String symlinkDestination)
234+
public static ArchiveEntry createSymlinkEntry( String symlinkName, int permissions, String symlinkDestination,
235+
int defaultDirectoryPermissions
236+
)
228237
{
229238
File symlinkFile = new File(symlinkName);
230239
final ArchiveEntry archiveEntry = new ArchiveEntry(symlinkName, new PlexusIoVirtualSymlinkResource(symlinkFile, symlinkDestination), SYMLINK, permissions,
231-
null );
240+
null, defaultDirectoryPermissions );
232241
return archiveEntry;
233242
}
234243

@@ -246,4 +255,9 @@ public void setResourceAttributes( PlexusIoResourceAttributes attributes )
246255
{
247256
return resource;
248257
}
258+
259+
public int getDefaultDirMode()
260+
{
261+
return defaultDirMode;
262+
}
249263
}

src/main/java/org/codehaus/plexus/archiver/Archiver.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,24 @@ void addResources( PlexusIoResourceCollection resources )
211211

212212
int getDefaultFileMode();
213213

214+
/**
215+
* This is the forced mode that should be used regardless if set, otherwise falls back to default.
216+
* @param mode
217+
*/
214218
void setDirectoryMode( int mode );
215219

220+
/**
221+
* Gets the forced mode for directories, falling back to default if none is forced.
222+
* @return
223+
*/
216224
int getDirectoryMode();
217225

218226
int getOverrideDirectoryMode();
219227

228+
/**
229+
* This is the "default" value we should use if no other value is specified
230+
* @param mode
231+
*/
220232
void setDefaultDirectoryMode( int mode );
221233

222234
int getDefaultDirectoryMode();

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ protected final void addResources( ResourceIterator resources, ZipArchiveOutputS
382382
name = name + "/";
383383
}
384384

385-
addParentDirs( base, name, zOut, "" );
385+
addParentDirs( entry, base, name, zOut, "" );
386386

387387
if ( entry.getResource().isFile() )
388388
{
@@ -404,7 +404,8 @@ protected final void addResources( ResourceIterator resources, ZipArchiveOutputS
404404
*
405405
*/
406406
@SuppressWarnings({"JavaDoc"})
407-
protected final void addParentDirs( File baseDir, String entry, ZipArchiveOutputStream zOut, String prefix )
407+
private void addParentDirs( ArchiveEntry archiveEntry, File baseDir, String entry, ZipArchiveOutputStream zOut,
408+
String prefix )
408409
throws IOException
409410
{
410411
if ( !doFilesonly && getIncludeEmptyDirs() )
@@ -442,7 +443,7 @@ protected final void addParentDirs( File baseDir, String entry, ZipArchiveOutput
442443
// the
443444
// At this point we could do something like read the atr
444445
final PlexusIoResource res = new AnonymousResource( f);
445-
zipDir( res, zOut, prefix + dir, getDirectoryMode() );
446+
zipDir( res, zOut, prefix + dir, archiveEntry.getDefaultDirMode() );
446447
}
447448
}
448449
}

src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public void testImplicitPermissions()
9494
archiver.setDirectoryMode( 0641 );
9595
archiver.setFileMode( 0222 );
9696
archiver.addFile( new File( "pom.xml" ), "fizz/buzz/pom.xml" );
97+
archiver.setDefaultDirectoryMode( 0530);
98+
archiver.setDirectoryMode( -1 ); // Not forced mode
99+
archiver.setFileMode( 0111 );
100+
archiver.addFile( new File( "pom.xml" ), "fazz/bazz/pam.xml" );
97101
archiver.createArchive();
98102

99103
assertTrue( zipFile.exists() );
@@ -103,6 +107,11 @@ public void testImplicitPermissions()
103107
ZipArchiveEntry pom = zf.getEntry( "fizz/buzz/pom.xml" );
104108
assertEquals( 0100222, pom.getUnixMode() );
105109

110+
ZipArchiveEntry fazz = zf.getEntry( "fazz/" );
111+
assertEquals( 040530, fazz.getUnixMode() );
112+
ZipArchiveEntry pam = zf.getEntry( "fazz/bazz/pam.xml" );
113+
assertEquals( 0100111, pam.getUnixMode() );
114+
106115

107116
}
108117
public void testCreateArchiveWithDetectedModes()

0 commit comments

Comments
 (0)