001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.jpkg;
032    
033    import java.io.File;
034    import java.io.IOException;
035    import java.util.Collection;
036    
037    import org.apache.commons.io.DirectoryWalker;
038    
039    /**
040     * Walks a given destroot appending all files and directories to a {@link PackageTarFile}.
041     * Package private.
042     * @see PackageTarFile
043     */
044    class DestrootWalker extends DirectoryWalker
045    {
046        /**
047         * Construct a {@link DestrootWalker}.
048         * @param destroot The {@link File} which is the root of the destroot.
049         * @param tar The {@link PackageTarFile} which will have the destroot contents added to it.
050         */
051        public DestrootWalker (File destroot, PackageTarFile tar)
052        {
053            _destroot = destroot.getAbsoluteFile();
054            _destrootPath = _destroot.getAbsolutePath();
055            _tar = tar;
056        }
057    
058        /**
059         * Walk the destroot, adding the contents to the tar file. This method should not be called more
060         * than once.
061         * @throws IOException If any i/o error is encountered while walking the destroot.
062         */
063        public void walk ()
064            throws IOException
065        {
066            walk(_destroot, null);
067        }
068    
069        @Override
070        @SuppressWarnings("unchecked")
071        protected void handleDirectoryStart (File directory, int depth, Collection results)
072            throws IOException
073        {
074            // no entry for the root directory
075            if (depth == 0) {
076                return;
077            }
078    
079            addFile(directory);
080        }
081    
082        @Override
083        @SuppressWarnings("unchecked")
084        protected void handleFile (File file, int depth, Collection results)
085            throws IOException
086        {
087            addFile(file);
088        }
089    
090        /**
091         * Add a discovered {@link File} to the tar file.
092         */
093        private void addFile (File file)
094            throws IOException
095        {
096            try {
097                _tar.addFile(file, _destrootPath);
098    
099            // pass any encountered exception back as an IOException to conform to the DirectoryWalker
100            // interface.
101            } catch (final DuplicatePermissionsException dpe) {
102                throw new IOException(dpe.getMessage());
103            }
104        }
105    
106        private final File _destroot;
107        private final String _destrootPath;
108        private final PackageTarFile _tar;
109    }