View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.producers;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.tools.ant.DirectoryScanner;
24  import org.apache.tools.tar.TarEntry;
25  import org.vafer.jdeb.DataConsumer;
26  import org.vafer.jdeb.DataProducer;
27  import org.vafer.jdeb.mapping.Mapper;
28  import org.vafer.jdeb.utils.Utils;
29  
30  /**
31   * DataProducer iterating over a directory.
32   * For cross-platform permissions and ownerships you probably want to use a Mapper, too. 
33   * 
34   * @author Torsten Curdt <tcurdt@vafer.org>
35   */
36  public final class DataProducerDirectory extends AbstractDataProducer implements DataProducer {
37  
38  	private final DirectoryScanner scanner = new DirectoryScanner();
39  	
40  	public DataProducerDirectory( final File pDir, final String[] pIncludes, final String[] pExcludes, final Mapper[] pMappers ) {
41  		super(pIncludes, pExcludes, pMappers);
42  		scanner.setBasedir(pDir);
43  		scanner.setIncludes(pIncludes);
44  		scanner.setExcludes(pExcludes);
45  		scanner.setCaseSensitive(true);
46  		scanner.setFollowSymlinks(true);
47  	}
48  	
49  	public void produce( final DataConsumer receiver ) throws IOException {
50  
51  		scanner.scan();
52  
53  		final File baseDir = scanner.getBasedir();
54  
55  		final String[] dirs = scanner.getIncludedDirectories();
56  		for (int i = 0; i < dirs.length; i++) {
57  			final File file = new File(baseDir, dirs[i]);
58  			String dirname = getFilename(baseDir, file);
59  
60  			if ("".equals(dirname)) {
61  				continue;
62  			}
63  
64  			if (!isIncluded(dirname)) {
65  				continue;
66  			}
67  
68  			if ('/' != File.separatorChar) {
69  				dirname = dirname.replace(File.separatorChar, '/');
70  			}
71  
72  			TarEntry entry = new TarEntry(dirname);
73  			entry.setUserId(0);
74  			entry.setUserName("root");
75  			entry.setGroupId(0);
76  			entry.setGroupName("root");
77  			entry.setMode(TarEntry.DEFAULT_DIR_MODE);
78  
79  			entry = map(entry);
80  
81  			entry.setSize(0);
82  
83  			receiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
84  		}
85  
86  
87  		final String[] files = scanner.getIncludedFiles();
88  
89  		for (int i = 0; i < files.length; i++) {
90  			final File file = new File(baseDir, files[i]);
91  			String filename = getFilename(baseDir, file);
92  
93  			if (!isIncluded(filename)) {
94  				continue;
95  			}
96  
97  			if ('/' != File.separatorChar) {
98  				filename = filename.replace(File.separatorChar, '/');
99  			}
100 
101 			TarEntry entry = new TarEntry(filename);
102 			entry.setUserId(0);
103 			entry.setUserName("root");
104 			entry.setGroupId(0);
105 			entry.setGroupName("root");
106 			entry.setMode(TarEntry.DEFAULT_FILE_MODE);
107 
108 			entry = map(entry);
109 
110 			entry.setSize(file.length());
111 
112 			final InputStream inputStream = new FileInputStream(file);
113 			try {
114 				receiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
115 			} finally {
116 				inputStream.close();
117 			}
118 		}
119 	}
120 
121 	private String getFilename( File root, File file ) {
122 		
123 		final String relativeFilename = file.getAbsolutePath().substring(root.getAbsolutePath().length());		
124 		
125 		return Utils.stripLeadingSlash(relativeFilename);
126 	}
127 
128 }