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.mapping;
017    
018    import org.apache.tools.tar.TarEntry;
019    import org.vafer.jdeb.utils.Utils;
020    
021    /**
022     * Applies a uniform set of permissions and ownership to all entries.
023     * 
024     * @author Bryan Sant <bryan.sant@gmail.com>
025     */
026    public final class PermMapper extends PrefixMapper {
027    
028        private int uid = -1;
029        private int gid = -1;
030        private String user;
031        private String group;
032        private int fileMode = -1;
033        private int dirMode = -1;
034    
035        public static int toMode(String modeString) {
036            int mode = -1;
037            if (modeString != null && modeString.length() > 0) {
038                mode = Integer.parseInt(modeString, 8);
039            }
040            return mode;
041        }
042    
043        public PermMapper(int uid, int gid, String user, String group, int fileMode, int dirMode, int strip, String prefix) {
044            super(strip, prefix);
045            this.uid = uid;
046            this.gid = gid;
047            this.user = user;
048            this.group = group;
049            this.fileMode = fileMode;
050            this.dirMode = dirMode;
051        }
052    
053        public PermMapper(int uid, int gid, String user, String group, String fileMode, String dirMode, int strip, String prefix) {
054            this(uid, gid, user, group, toMode(fileMode), toMode(dirMode), strip, prefix);
055        }
056    
057        public PermMapper(String user, String group, String fileMode, String dirMode, int strip, String prefix) {
058            this(-1, -1, user, group, toMode(fileMode), toMode(dirMode), strip, prefix);
059        }
060    
061        public TarEntry map(final TarEntry entry) {
062            final String name = entry.getName();
063    
064            final TarEntry newEntry = new TarEntry(prefix + '/' + Utils.stripPath(strip, name));
065    
066            // Set ownership
067            if (uid > -1) {
068                newEntry.setUserId(uid);
069            } else {
070                newEntry.setUserId(entry.getUserId());
071            }
072            if (gid > -1) {
073                newEntry.setGroupId(gid);
074            } else {
075                newEntry.setGroupId(entry.getGroupId());
076            }
077            if (user != null) {
078                newEntry.setUserName(user);
079            } else {
080                newEntry.setUserName(entry.getUserName());
081            }
082            if (group != null) {
083                newEntry.setGroupName(group);
084            } else {
085                newEntry.setGroupName(entry.getGroupName());
086            }
087    
088            // Set permissions
089            if (newEntry.isDirectory()) {
090                if (dirMode > -1) {
091                    newEntry.setMode(dirMode);
092                } else {
093                    newEntry.setMode(entry.getMode());
094                }
095            } else {
096                if (fileMode > -1) {
097                    newEntry.setMode(fileMode);
098                } else {
099                    newEntry.setMode(entry.getMode());
100    
101                }
102            }
103    
104            newEntry.setSize(entry.getSize());
105    
106            return newEntry;
107        }
108    }