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     * @deprecated please use DataProducerFileSet instead
036     */
037    public final class FileSetDataProducer implements DataProducer {
038    
039        private final FileSet fileset;
040    
041        public FileSetDataProducer( final FileSet pFileset ) {
042            fileset = pFileset;
043        }
044    
045        public void produce( final DataConsumer pReceiver ) throws IOException {
046            String user = "root";
047            int uid = 0;
048            String group = "root";
049            int gid = 0;
050            int filemode = TarEntry.DEFAULT_FILE_MODE;
051            int dirmode = TarEntry.DEFAULT_DIR_MODE;
052            String prefix = "";
053    
054            if (fileset instanceof Tar.TarFileSet) {
055                Tar.TarFileSet tarfileset = (Tar.TarFileSet) fileset;
056                user = tarfileset.getUserName();
057                uid = tarfileset.getUid();
058                group = tarfileset.getGroup();
059                gid = tarfileset.getGid();
060                filemode = tarfileset.getMode();
061                // TODO check on the ant list
062                // it's deprecated but WTF is one supposed to use instead?
063                dirmode = tarfileset.getDirMode();
064                prefix = tarfileset.getPrefix();
065            }
066    
067            final DirectoryScanner scanner = fileset.getDirectoryScanner(fileset.getProject());
068            scanner.scan();
069    
070            final File basedir = scanner.getBasedir();
071    
072            final String[] directories = scanner.getIncludedDirectories();
073            for (int i = 0; i < directories.length; i++) {
074                final String name = directories[i].replace('\\', '/');
075    
076                pReceiver.onEachDir(prefix + "/" + name, null, user, uid, group, gid, dirmode, 0);
077            }
078    
079            final String[] files = scanner.getIncludedFiles();
080            for (int i = 0; i < files.length; i++) {
081                final String name = files[i].replace('\\', '/');
082                final File file = new File(basedir, name);
083    
084                final InputStream inputStream = new FileInputStream(file);
085                try {
086                    pReceiver.onEachFile(inputStream, prefix + "/" + name, null, user, uid, group, gid,filemode, file.length());
087                } finally {
088                    inputStream.close();
089                }
090            }
091        }
092    }