001    /*
002     * Copyright 2008 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 FileSetDataProducer implements DataProducer {
037    
038            private final FileSet fileset;
039    
040            public FileSetDataProducer( 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                            dirmode = tarfileset.getDirMode();
061                            prefix = tarfileset.getPrefix();
062                    }
063    
064                    final DirectoryScanner scanner = fileset.getDirectoryScanner(fileset.getProject());
065                    scanner.scan();
066    
067                    final File basedir = scanner.getBasedir();
068    
069                    final String[] directories = scanner.getIncludedDirectories();
070                    for (int i = 0; i < directories.length; i++) {
071                            final String name = directories[i];
072    
073                            pReceiver.onEachDir(prefix + "/" + name, null, user, uid, group, gid, dirmode, 0);
074                    }
075    
076                    final String[] files = scanner.getIncludedFiles();
077                    for (int i = 0; i < files.length; i++) {
078                            final String name = files[i];
079                            final File file = new File(basedir, name);
080    
081                            final InputStream inputStream = new FileInputStream(file);
082                            try {
083                                    pReceiver.onEachFile(inputStream, prefix + "/" + name, null, user, uid, group, gid,filemode, file.length());
084                            } finally {
085                                    inputStream.close();
086                            }
087    
088                    }
089            }
090    }