1 package org.codehaus.plexus.archiver;
2
3 /*
4 * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5 * Author(s) : Franck Bonin, Neticoa SAS France
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
21 //import org.codehaus.plexus.archiver.resources.PlexusIoVirtualSymlinkResource;
22
23 //import java.io.File;
24 //import java.io.IOException;
25 //import java.io.InputStream;
26
27 //import javax.annotation.Nonnull;
28
29 import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
30 import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
31 //import org.codehaus.plexus.components.io.resources.PlexusIoFileResource;
32 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
33 //import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
34 //import org.codehaus.plexus.components.io.resources.ResourceFactory;
35
36 //import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
37
38 /**
39 * @author Franck Bonin
40 * @version $Revision$ $Date$
41 */
42 public class ArchiveContentEntry
43 {
44 public static final String ROLE = ArchiveContentEntry.class.getName();
45
46 public static final int FILE = 1;
47
48 public static final int DIRECTORY = 2;
49
50 public static final int SYMLINK = 3;
51
52 //@Nonnull private PlexusIoResource resource;
53
54 private final String name;
55
56 private final int type;
57
58 private final int mode;
59
60 //private final int defaultDirMode; // Sometimes a directory needs to be created. Which mode should it be ?
61 // this mode is at the time of the creation of the archive entry, which is an important distinction
62
63 private PlexusIoResourceAttributes attributes;
64
65 /**
66 * @param name the filename as it will appear in the archive. This is platform-specific
67 * normalized with File.separatorChar
68 * //@param resource original filename
69 * @param type FILE or DIRECTORY
70 * @param mode octal unix style permissions
71 * //@param collection
72 * //@param defaultDirMode
73 */
74 private ArchiveContentEntry( String name, /*@Nonnull*/ PlexusIoResource resource, int type, int mode/*,
75 PlexusIoResourceCollection collection, int defaultDirMode*/ )
76 {
77 this.name = name;
78 //this.defaultDirMode = defaultDirMode;
79 /*try {
80 this.resource = collection != null ? collection.resolve(resource) : resource;
81 } catch (IOException e) {
82 throw new ArchiverException("Error resolving resource " + resource.getName(), e);
83 }*/
84 this.attributes = ( resource instanceof ResourceAttributeSupplier )
85 ? ( (ResourceAttributeSupplier) resource ).getAttributes() : null;
86
87 this.type = type;
88 int permissions = mode;
89
90 if ( mode == -1 && this.attributes == null )
91 {
92 permissions = resource.isFile() ? Archiver.DEFAULT_FILE_MODE
93 : resource.isSymbolicLink() ? Archiver.DEFAULT_SYMLILNK_MODE : Archiver.DEFAULT_DIR_MODE;
94 }
95
96 this.mode = permissions == -1 ? permissions : ( permissions & UnixStat.PERM_MASK )
97 | ( type == FILE ? UnixStat.FILE_FLAG : type == SYMLINK ? UnixStat.LINK_FLAG : UnixStat.DIR_FLAG );
98 }
99
100 /**
101 * @return the filename of this entry in the archive.
102 */
103 public String getName()
104 {
105 return name;
106 }
107
108 /**
109 * @return The original file that will be stored in the archive.
110 * @deprecated_ As of 1.0-alpha-10, file entries are no longer backed
111 * by files, but by instances of {@link PlexusIoResource}.
112 * Consequently, you should use {@link #getInputStream()}-
113 */
114 /*
115 public File getFile()
116 {
117 if ( resource instanceof PlexusIoFileResource )
118 {
119 return ( (PlexusIoFileResource) resource ).getFile();
120 }
121 return null;
122 }
123 */
124 /**
125 * @return The resource contents.
126 */
127 /*
128 public InputStream getInputStream()
129 throws IOException
130 {
131 return resource.getContents();
132 }
133 */
134 /**
135 * @return FILE or DIRECTORY
136 */
137 public int getType()
138 {
139 return type;
140 }
141
142 /**
143 * @return octal user/group/other unix like permissions.
144 */
145 public int getMode()
146 {
147 if ( mode != -1 )
148 {
149 return mode;
150 }
151 /*
152 if ( attributes != null && attributes.getOctalMode() > -1 )
153 {
154 return attributes.getOctalMode();
155 }
156 */
157 return ( ( type == FILE ? Archiver.DEFAULT_FILE_MODE
158 : type == SYMLINK ? Archiver.DEFAULT_SYMLILNK_MODE : Archiver.DEFAULT_DIR_MODE ) & UnixStat.PERM_MASK )
159 | ( type == FILE ? UnixStat.FILE_FLAG : type == SYMLINK ? UnixStat.LINK_FLAG : UnixStat.DIR_FLAG );
160 }
161
162 public static ArchiveContentEntry createFileEntry( String target, PlexusIoResource resource, int permissions )
163 {
164 return new ArchiveContentEntry( target, resource, FILE, permissions );
165 }
166 /*
167 public static ArchiveEntry createFileEntry( String target, PlexusIoResource resource, int permissions,
168 PlexusIoResourceCollection collection, int defaultDirectoryPermissions )
169 throws ArchiverException
170 {
171 if ( resource.isDirectory() )
172 {
173 throw new ArchiverException( "Not a file: " + resource.getName() );
174 }
175 final int type = resource.isSymbolicLink() ? SYMLINK : FILE;
176 return new ArchiveEntry( target, resource, type, permissions, collection, defaultDirectoryPermissions );
177 }
178
179 public static ArchiveEntry createFileEntry( String target, File file, int permissions,
180 int defaultDirectoryPermissions )
181 throws ArchiverException, IOException {
182 if ( !file.isFile() )
183 {
184 throw new ArchiverException( "Not a file: " + file );
185 }
186
187 final PlexusIoResource res = ResourceFactory.createResource( file );
188
189 final int type;
190 if (res.isSymbolicLink()){
191 type = SYMLINK;
192 permissions = permissions & ~(UnixStat.FILE_FLAG); // remove file flag again .doh.
193 } else {
194 type = FILE; // File flag was there already. This is a bit of a mess !
195 }
196
197 return new ArchiveEntry( target, res, type, permissions, null, defaultDirectoryPermissions );
198 }
199 */
200 public static ArchiveContentEntry createDirectoryEntry( String target, PlexusIoResource resource, int permissions )
201 {
202 return new ArchiveContentEntry( target, resource, DIRECTORY, permissions );
203 }
204
205 /*
206 public static ArchiveEntry createDirectoryEntry( String target, @Nonnull PlexusIoResource resource, int permissions,
207 int defaultDirectoryPermissions )
208 throws ArchiverException
209 {
210 if ( !resource.isDirectory() )
211 {
212 throw new ArchiverException( "Not a directory: " + resource.getName() );
213 }
214 final int type;
215 if (resource.isSymbolicLink()){
216 type = SYMLINK;
217 permissions = permissions & ~(UnixStat.DIR_FLAG); // remove dir flag again .doh.
218 } else {
219 type = DIRECTORY; // Dir flag was there already. This is a bit of a mess !
220
221 }
222 return new ArchiveEntry( target, resource, type, permissions, null, defaultDirectoryPermissions );
223 }
224
225 public static ArchiveEntry createDirectoryEntry( String target, final File file, int permissions,
226 int defaultDirMode1 )
227 throws ArchiverException, IOException {
228 if ( !file.isDirectory() )
229 {
230 throw new ArchiverException( "Not a directory: " + file );
231 }
232
233 final PlexusIoResource res = createResource( file);
234 return new ArchiveEntry( target, res, DIRECTORY, permissions, null, defaultDirMode1 );
235 }
236 */
237 public static ArchiveContentEntry createSymlinkEntry( String target, PlexusIoResource resource, int permissions )
238 {
239 return new ArchiveContentEntry( target, resource, SYMLINK, permissions );
240 }
241 /*
242 public static ArchiveEntry createSymlinkEntry( String symlinkName, int permissions, String symlinkDestination,
243 int defaultDirectoryPermissions
244 )
245 {
246 File symlinkFile = new File(symlinkName);
247 final ArchiveEntry archiveEntry = new ArchiveEntry(symlinkName,
248 new PlexusIoVirtualSymlinkResource(symlinkFile, symlinkDestination), SYMLINK, permissions,
249 null, defaultDirectoryPermissions );
250 return archiveEntry;
251 }
252 */
253 /*
254 public PlexusIoResourceAttributes getResourceAttributes()
255 {
256 return attributes;
257 }
258
259 public void setResourceAttributes( PlexusIoResourceAttributes attributes )
260 {
261 this.attributes = attributes;
262 }
263
264 public @Nonnull PlexusIoResource getResource()
265 {
266 return resource;
267 }
268
269 public int getDefaultDirMode()
270 {
271 return defaultDirMode;
272 }
273 */
274 }