001 /*
002 * Copyright 2010 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.vafer.jdeb.producers;
017
018 import java.io.File;
019 import java.io.FileInputStream;
020 import java.io.IOException;
021 import java.io.InputStream;
022
023 import org.apache.tools.ant.DirectoryScanner;
024 import org.apache.tools.ant.taskdefs.Tar;
025 import org.apache.tools.ant.types.FileSet;
026 import org.apache.tools.tar.TarEntry;
027 import org.vafer.jdeb.DataConsumer;
028 import org.vafer.jdeb.DataProducer;
029
030 /**
031 * DataProducer providing data from an Ant fileset. TarFileSets are also
032 * supported with their permissions.
033 *
034 * @author Emmanuel Bourg
035 */
036 public final class DataProducerFileSet implements DataProducer {
037
038 private final FileSet fileset;
039
040 public DataProducerFileSet( final FileSet pFileset ) {
041 fileset = pFileset;
042 }
043
044 public void produce( final DataConsumer pReceiver ) throws IOException {
045 String user = "root";
046 int uid = 0;
047 String group = "root";
048 int gid = 0;
049 int filemode = TarEntry.DEFAULT_FILE_MODE;
050 int dirmode = TarEntry.DEFAULT_DIR_MODE;
051 String prefix = "";
052
053 if (fileset instanceof Tar.TarFileSet) {
054 Tar.TarFileSet tarfileset = (Tar.TarFileSet) fileset;
055 user = tarfileset.getUserName();
056 uid = tarfileset.getUid();
057 group = tarfileset.getGroup();
058 gid = tarfileset.getGid();
059 filemode = tarfileset.getMode();
060 // TODO check on the ant list
061 // it's deprecated but WTF is one supposed to use instead?
062 dirmode = tarfileset.getDirMode();
063 prefix = tarfileset.getPrefix();
064 }
065
066 final DirectoryScanner scanner = fileset.getDirectoryScanner(fileset.getProject());
067 scanner.scan();
068
069 final File basedir = scanner.getBasedir();
070
071 final String[] directories = scanner.getIncludedDirectories();
072 for (int i = 0; i < directories.length; i++) {
073 final String name = directories[i].replace('\\', '/');
074
075 pReceiver.onEachDir(prefix + "/" + name, null, user, uid, group, gid, dirmode, 0);
076 }
077
078 final String[] files = scanner.getIncludedFiles();
079 for (int i = 0; i < files.length; i++) {
080 final String name = files[i].replace('\\', '/');
081 final File file = new File(basedir, name);
082
083 final InputStream inputStream = new FileInputStream(file);
084 try {
085 pReceiver.onEachFile(inputStream, prefix + "/" + name, null, user, uid, group, gid,filemode, file.length());
086 } finally {
087 inputStream.close();
088 }
089 }
090 }
091 }