-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathArchiveEntry.java
265 lines (222 loc) · 9.19 KB
/
ArchiveEntry.java
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
package org.codehaus.plexus.archiver;
/**
*
* Copyright 2004 The Apache Software Foundation
*
* 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.
*/
import org.codehaus.plexus.archiver.resources.PlexusIoVirtualSymlinkResource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nonnull;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
import org.codehaus.plexus.components.io.resources.PlexusIoFileResource;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
import org.codehaus.plexus.components.io.resources.ResourceFactory;
import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
/**
* @version $Revision: 1502 $ $Date$
*/
public class ArchiveEntry
{
public static final String ROLE = ArchiveEntry.class.getName();
public static final int FILE = 1;
public static final int DIRECTORY = 2;
public static final int SYMLINK = 3;
@Nonnull private PlexusIoResource resource;
private final String name;
private final int type;
private final int mode;
private final int defaultDirMode; // Sometimes a directory needs to be created. Which mode should it be ?
// this mode is at the time of the creation of the archive entry, which is an important distinction
private PlexusIoResourceAttributes attributes;
private final boolean addSynchronously;
/**
* @param name the filename as it will appear in the archive. This is platform-specific
* normalized with File.separatorChar
* @param resource original filename
* @param type FILE or DIRECTORY
* @param mode octal unix style permissions
* @param collection
* @param defaultDirMode
*/
private ArchiveEntry( String name, @Nonnull PlexusIoResource resource, int type, int mode,
PlexusIoResourceCollection collection, int defaultDirMode )
{
this.name = name;
this.defaultDirMode = defaultDirMode;
try {
this.resource = collection != null ? collection.resolve(resource) : resource;
} catch (IOException e) {
throw new ArchiverException("Error resolving resource " + resource.getName(), e);
}
this.attributes = ( resource instanceof ResourceAttributeSupplier)
? ( (ResourceAttributeSupplier) resource ).getAttributes() : null;
this.type = type;
int permissions = mode;
if ( mode == PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE && this.attributes == null )
{
permissions = resource.isFile() ? Archiver.DEFAULT_FILE_MODE
: resource.isSymbolicLink() ? Archiver.DEFAULT_SYMLILNK_MODE : Archiver.DEFAULT_DIR_MODE;
}
this.mode = permissions == PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ? permissions
: ( permissions & UnixStat.PERM_MASK ) |
( type == FILE ? UnixStat.FILE_FLAG : type == SYMLINK ? UnixStat.LINK_FLAG : UnixStat.DIR_FLAG );
this.addSynchronously = ( collection != null && !collection.isConcurrentAccessSupported() );
}
/**
* @return the filename of this entry in the archive.
*/
public String getName()
{
return name;
}
/**
* @return The original file that will be stored in the archive.
* @deprecated As of 1.0-alpha-10, file entries are no longer backed
* by files, but by instances of {@link PlexusIoResource}.
* Consequently, you should use {@link #getInputStream()}-
*/
public File getFile()
{
if ( resource instanceof PlexusIoFileResource )
{
return ( (PlexusIoFileResource) resource ).getFile();
}
return null;
}
/**
* @return The resource contents.
*/
public InputStream getInputStream()
throws IOException
{
return resource.getContents();
}
/**
* @return FILE or DIRECTORY
*/
public int getType()
{
return type;
}
/**
* @return octal user/group/other unix like permissions.
*/
public int getMode()
{
if ( mode != -1 )
{
return mode;
}
if ( attributes != null && attributes.getOctalMode() > -1 )
{
return attributes.getOctalMode();
}
return ( ( type == FILE ? Archiver.DEFAULT_FILE_MODE
: type == SYMLINK ? Archiver.DEFAULT_SYMLILNK_MODE : Archiver.DEFAULT_DIR_MODE ) & UnixStat.PERM_MASK ) |
( type == FILE ? UnixStat.FILE_FLAG : type == SYMLINK ? UnixStat.LINK_FLAG : UnixStat.DIR_FLAG );
}
/**
* Indicates if this entry should be added to the archive synchronously
* before adding the next entry and/or accessing the next entry of {@link ResourceIterator}.
*
* @return {@code true} if this entry should be added synchronously
*/
public boolean shouldAddSynchronously()
{
return addSynchronously;
}
public static ArchiveEntry createFileEntry( String target, PlexusIoResource resource, int permissions,
PlexusIoResourceCollection collection, int defaultDirectoryPermissions )
throws ArchiverException
{
if ( resource.isDirectory() )
{
throw new ArchiverException( "Not a file: " + resource.getName() );
}
final int type = resource.isSymbolicLink() ? SYMLINK : FILE;
return new ArchiveEntry( target, resource, type, permissions, collection, defaultDirectoryPermissions );
}
public static ArchiveEntry createFileEntry( String target, File file, int permissions, int defaultDirectoryPermissions )
throws ArchiverException, IOException {
if ( !file.isFile() )
{
throw new ArchiverException( "Not a file: " + file );
}
final PlexusIoResource res = ResourceFactory.createResource( file );
final int type;
if (res.isSymbolicLink()){
type = SYMLINK;
permissions = permissions & ~(UnixStat.FILE_FLAG); // remove file flag again .doh.
} else {
type = FILE; // File flag was there already. This is a bit of a mess !
}
return new ArchiveEntry( target, res, type, permissions, null, defaultDirectoryPermissions );
}
public static ArchiveEntry createDirectoryEntry( String target, @Nonnull PlexusIoResource resource, int permissions,
int defaultDirectoryPermissions )
throws ArchiverException
{
if ( !resource.isDirectory() )
{
throw new ArchiverException( "Not a directory: " + resource.getName() );
}
final int type;
if (resource.isSymbolicLink()){
type = SYMLINK;
permissions = permissions & ~(UnixStat.DIR_FLAG); // remove dir flag again .doh.
} else {
type = DIRECTORY; // Dir flag was there already. This is a bit of a mess !
}
return new ArchiveEntry( target, resource, type, permissions, null, defaultDirectoryPermissions );
}
public static ArchiveEntry createDirectoryEntry( String target, final File file, int permissions,
int defaultDirMode1 )
throws ArchiverException, IOException {
if ( !file.isDirectory() )
{
throw new ArchiverException( "Not a directory: " + file );
}
final PlexusIoResource res = createResource( file);
return new ArchiveEntry( target, res, DIRECTORY, permissions, null, defaultDirMode1 );
}
public static ArchiveEntry createSymlinkEntry( String symlinkName, int permissions, String symlinkDestination,
int defaultDirectoryPermissions
)
{
File symlinkFile = new File(symlinkName);
final ArchiveEntry archiveEntry = new ArchiveEntry(symlinkName, new PlexusIoVirtualSymlinkResource(symlinkFile, symlinkDestination), SYMLINK, permissions,
null, defaultDirectoryPermissions );
return archiveEntry;
}
public PlexusIoResourceAttributes getResourceAttributes()
{
return attributes;
}
public void setResourceAttributes( PlexusIoResourceAttributes attributes )
{
this.attributes = attributes;
}
public @Nonnull PlexusIoResource getResource()
{
return resource;
}
public int getDefaultDirMode()
{
return defaultDirMode;
}
}