Skip to content

Commit b37db21

Browse files
committed
First stab at refactoring maven-archiver to provide
file/directory permissions in a more general way in the api. This is necessary to make MNG-632 easier. o Moved UnixStat a package higher in the tree; it was not only used by ZipArchiver but also by TarArchiver. Now it's used in AbstractArchiver. o Moved the ZipArchiver default file/dir permissions constants to AbstractArchiver. o Deprecated some methods in ZipArchiver that are now in AbstractArchiver (different name). Backwards compatible, ofcourse. o Updated TarArchiver to synchronize TarOptions' dir/file mode with the AbstractArchiver's versions. Made them deprecated. o Added JUnit tests for ZipArchiver and TarArchiver to test various permissions. o Fixed coding style on some modified parts. o Removed some unused imports/fields/local vars. o Fixed some potential bugs, most notably no exceptions thrown ( if (..) { new ..Exception() } -> add 'throw' ). and a case where the wrong key was used. Tested against m2 bootstrap.
1 parent b27fa30 commit b37db21

20 files changed

+574
-192
lines changed

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

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,69 @@
1717
* limitations under the License.
1818
*/
1919

20-
import java.io.File;
21-
import java.util.HashMap;
22-
import java.util.LinkedHashMap;
23-
import java.util.Map;
24-
2520
import org.codehaus.plexus.logging.AbstractLogEnabled;
2621
import org.codehaus.plexus.logging.Logger;
2722
import org.codehaus.plexus.logging.console.ConsoleLogger;
2823
import org.codehaus.plexus.util.DirectoryScanner;
2924

25+
import java.io.File;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
3029
/**
3130
* @version $Revision$ $Date$
3231
*/
3332
public abstract class AbstractArchiver extends AbstractLogEnabled
3433
implements Archiver
3534
{
36-
private Logger logger;
35+
/**
36+
* Default value for the dirmode attribute.
37+
*/
38+
public static final int DEFAULT_DIR_MODE =
39+
UnixStat.DIR_FLAG | UnixStat.DEFAULT_DIR_PERM;
3740

38-
private String basedir;
41+
/**
42+
* Default value for the filemode attribute.
43+
*/
44+
public static final int DEFAULT_FILE_MODE =
45+
UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM;
3946

40-
private String[] excludesPattern;
47+
private Logger logger;
4148

42-
private String[] includesPattern;
43-
4449
private File destFile;
4550

4651
private Map filesMap = new HashMap();
4752

4853
private Map dirsMap = new HashMap();
4954

50-
private String prefix;
55+
private int defaultFileMode = DEFAULT_FILE_MODE;
5156

5257
private boolean includeEmptyDirs = true;
5358

59+
private int defaultDirectoryMode = DEFAULT_DIR_MODE;
60+
61+
62+
public void setDefaultFileMode( int mode )
63+
{
64+
defaultFileMode = ( mode & UnixStat.PERM_MASK ) | UnixStat.FILE_FLAG;
65+
}
66+
67+
public int getDefaultFileMode()
68+
{
69+
return defaultFileMode;
70+
}
71+
72+
public void setDefaultDirectoryMode( int mode )
73+
{
74+
System.err.println("AbstractArchiver: setDefaultDirectoryMode!!");
75+
defaultDirectoryMode = ( mode & UnixStat.PERM_MASK ) | UnixStat.DIR_FLAG;
76+
}
77+
78+
public int getDefaultDirectoryMode()
79+
{
80+
return defaultDirectoryMode;
81+
}
82+
5483
public void addDirectory( File directory )
5584
throws ArchiverException
5685
{
@@ -125,17 +154,15 @@ public void addDirectory( File directory, String prefix, String[] includes, Stri
125154
scanner.setBasedir( basedir );
126155
scanner.scan();
127156
String[] files = scanner.getIncludedFiles();
128-
for ( int i = 0; i < files.length; i++ ) {
129-
String file = files[i];
130-
file = file.replace( '\\', '/' );
131-
if ( prefix != null )
132-
{
133-
filesMap.put( prefix + file, new File( basedir, file ) );
134-
}
135-
else
136-
{
137-
filesMap.put( file, new File( basedir, file ) );
138-
}
157+
158+
for ( int i = 0; i < files.length; i++ )
159+
{
160+
String targetFile = files[i];
161+
targetFile = targetFile.replace( '\\', '/' );
162+
163+
targetFile = ( prefix == null ? "" : prefix ) + targetFile;
164+
filesMap.put( targetFile, ArchiveEntry.createEntry( targetFile,
165+
new File( basedir, targetFile ), getDefaultFileMode(), getDefaultDirectoryMode() ) );
139166
}
140167

141168
if ( includeEmptyDirs )
@@ -144,30 +171,38 @@ public void addDirectory( File directory, String prefix, String[] includes, Stri
144171
for ( int i = 0; i < dirs.length; i++ ) {
145172
String dir = dirs[i];
146173
dir = dir.replace( '\\', '/' );
147-
if ( prefix != null )
148-
{
149-
getDirs().put( prefix + dir, new File( basedir, dir ) );
150-
}
151-
else
152-
{
153-
getDirs().put( dir, new File( basedir, dir ) );
154-
}
174+
175+
dir = ( prefix == null ? "" : prefix ) + dir;
176+
177+
getDirs().put( dir, ArchiveEntry.createEntry( dir,
178+
new File( basedir, dir ), getDefaultFileMode(), getDefaultDirectoryMode() ) );
155179
}
156180
}
157181
}
158182

159183
public void addFile( File inputFile, String destFileName )
160184
throws ArchiverException
185+
{
186+
addFile( inputFile, destFileName, getDefaultFileMode() );
187+
}
188+
189+
public void addFile( File inputFile, String destFileName, int permissions )
190+
throws ArchiverException
161191
{
162192
if ( !inputFile.isFile() || !inputFile.exists() )
163193
{
164194
throw new ArchiverException( inputFile.getAbsolutePath() + " isn't a file." );
165195
}
166196

167197
destFileName = destFileName.replace( '\\', '/' );
168-
filesMap.put( destFileName, inputFile );
198+
filesMap.put( destFileName, ArchiveEntry.createFileEntry(
199+
destFileName, inputFile, permissions ) );
169200
}
170201

202+
// TODO: convert this to Collection?
203+
// Only con is that some archivers change the filename
204+
// to contain just forward slashes; they could update
205+
// the Name of the ArchiveEntry..?
171206
protected Map getFiles()
172207
{
173208
if ( !includeEmptyDirs ) return filesMap;
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
/**
4+
*
5+
* Copyright 2004 The Apache Software Foundation
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import java.io.File;
21+
22+
/**
23+
* @version $Revision: 1502 $ $Date$
24+
*/
25+
public class ArchiveEntry
26+
{
27+
public static final String ROLE = ArchiveEntry.class.getName();
28+
29+
public static final int FILE = 1;
30+
31+
public static final int DIRECTORY = 2;
32+
33+
private String name;
34+
35+
private File file;
36+
37+
private int type;
38+
39+
private int mode;
40+
41+
/**
42+
* @param name the filename as it will appear in the archive
43+
* @param original original filename
44+
* @param type FILE or DIRECTORY
45+
* @param mode octal unix style permissions
46+
*/
47+
private ArchiveEntry( String name, File original, int type, int mode )
48+
{
49+
this.name = name;
50+
this.file = original;
51+
this.type = type;
52+
this.mode = ( mode & UnixStat.PERM_MASK ) |
53+
( type == FILE ? UnixStat.FILE_FLAG : UnixStat.DIR_FLAG );
54+
}
55+
56+
/**
57+
*
58+
* @return the filename of this entry in the archive.
59+
*/
60+
public String getName()
61+
{
62+
return name;
63+
}
64+
65+
/**
66+
*
67+
* @return The original file that will be stored in the archive.
68+
*/
69+
public File getFile()
70+
{
71+
return file;
72+
}
73+
74+
/**
75+
* TODO: support for SYMLINK?
76+
* @return FILE or DIRECTORY
77+
*/
78+
public int getType()
79+
{
80+
return type;
81+
}
82+
83+
/**
84+
* @return octal user/group/other unix like permissions.
85+
*/
86+
public int getMode()
87+
{
88+
return mode;
89+
}
90+
91+
public static ArchiveEntry createFileEntry( String target, File file, int permissions )
92+
throws ArchiverException
93+
{
94+
if ( ! file.isFile() )
95+
{
96+
throw new ArchiverException( "Not a file: " + file );
97+
}
98+
else
99+
{
100+
return new ArchiveEntry( target, file, FILE, permissions );
101+
}
102+
}
103+
104+
public static ArchiveEntry createDirectoryEntry( String target, File file, int permissions )
105+
throws ArchiverException
106+
{
107+
if ( ! file.isDirectory() )
108+
{
109+
throw new ArchiverException( "Not a directory: " + file );
110+
}
111+
else
112+
{
113+
return new ArchiveEntry( target, file, DIRECTORY, permissions );
114+
}
115+
}
116+
117+
/**
118+
* Creates the correct ArchiveEntry instance for either a FILE or a
119+
* DIRECTORY.
120+
*
121+
* @param target
122+
* @param file
123+
* @param filePerm
124+
* @param dirPerm
125+
* @return
126+
* @throws ArchiverException when file is neither a directory nor a file.
127+
*/
128+
public static ArchiveEntry createEntry( String target, File file, int filePerm, int dirPerm )
129+
throws ArchiverException
130+
{
131+
if ( file.isDirectory() )
132+
{
133+
return createDirectoryEntry( target, file, dirPerm );
134+
}
135+
else if ( file.isFile() )
136+
{
137+
return createFileEntry( target, file, filePerm );
138+
}
139+
else // FIXME: handle symlinks?
140+
{
141+
throw new ArchiverException( "Neither a file nor a directory: " + file );
142+
}
143+
}
144+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ public interface Archiver
3939

4040
void addFile( File inputFile, String destFileName ) throws ArchiverException;
4141

42+
void addFile( File inputFile, String destFileName, int permissions ) throws ArchiverException;
43+
4244
File getDestFile();
4345

4446
void setDestFile( File destFile );
47+
48+
void setDefaultFileMode( int mode );
49+
50+
int getDefaultFileMode();
51+
52+
void setDefaultDirectoryMode( int mode );
53+
54+
int getDefaultDirectoryMode();
4555
}

src/main/java/org/codehaus/plexus/archiver/zip/UnixStat.java renamed to src/main/java/org/codehaus/plexus/archiver/UnixStat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.codehaus.plexus.archiver.zip;
1+
package org.codehaus.plexus.archiver;
22

33
/*
44
* Copyright 2001,2004 The Apache Software Foundation

src/main/java/org/codehaus/plexus/archiver/bzip2/BZip2Archiver.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
* limitations under the License.
1818
*/
1919

20+
import java.io.IOException;
21+
2022
import org.codehaus.plexus.archiver.AbstractArchiver;
23+
import org.codehaus.plexus.archiver.ArchiveEntry;
2124
import org.codehaus.plexus.archiver.ArchiverException;
2225

23-
import java.io.File;
24-
import java.io.IOException;
25-
2626
/**
2727
* @version $Revision$ $Date$
2828
*/
@@ -33,10 +33,10 @@ public void createArchive() throws ArchiverException, IOException
3333
BZip2Compressor compressor = new BZip2Compressor();
3434
if ( getFiles().size() > 1 )
3535
{
36-
throw new ArchiverException( "There are more than one file in input." );
36+
throw new ArchiverException( "There is more than one file in input." );
3737
}
38-
File sourceFile = (File) getFiles().values().toArray()[0];
39-
compressor.setSourceFile( sourceFile );
38+
ArchiveEntry entry = (ArchiveEntry) getFiles().values().toArray()[0];
39+
compressor.setSourceFile( entry.getFile() );
4040
compressor.setDestFile( getDestFile() );
4141
compressor.execute();
4242
}

src/main/java/org/codehaus/plexus/archiver/ear/EarArchiver.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.io.File;
2121
import java.io.IOException;
22+
23+
import org.codehaus.plexus.archiver.ArchiveEntry;
2224
import org.codehaus.plexus.archiver.ArchiverException;
2325
import org.codehaus.plexus.archiver.jar.JarArchiver;
2426
import org.codehaus.plexus.archiver.zip.ZipOutputStream;
@@ -91,8 +93,8 @@ protected void initZipOutputStream(ZipOutputStream zOut)
9193
/**
9294
* Overridden from ZipArchiver class to deal with application.xml
9395
*/
94-
protected void zipFile(File file, ZipOutputStream zOut, String vPath,
95-
int mode)
96+
protected void zipFile( ArchiveEntry entry, ZipOutputStream zOut, String vPath,
97+
int mode )
9698
throws IOException, ArchiverException
9799
{
98100
// If the file being added is META-INF/application.xml, we
@@ -103,7 +105,7 @@ protected void zipFile(File file, ZipOutputStream zOut, String vPath,
103105
if (vPath.equalsIgnoreCase("META-INF/application.xml"))
104106
{
105107
if (deploymentDescriptor == null
106-
|| !deploymentDescriptor.getAbsolutePath().equals( file.getAbsolutePath() )
108+
|| !deploymentDescriptor.getCanonicalPath().equals( entry.getFile().getCanonicalPath() )
107109
|| descriptorAdded) {
108110
getLogger().warn("Warning: selected " + archiveType
109111
+ " files include a META-INF/application.xml which will"
@@ -112,13 +114,13 @@ protected void zipFile(File file, ZipOutputStream zOut, String vPath,
112114
}
113115
else
114116
{
115-
super.zipFile(file, zOut, vPath, mode);
117+
super.zipFile( entry, zOut, vPath );
116118
descriptorAdded = true;
117119
}
118120
}
119121
else
120122
{
121-
super.zipFile(file, zOut, vPath, mode);
123+
super.zipFile( entry, zOut, vPath );
122124
}
123125
}
124126

0 commit comments

Comments
 (0)