001/* 002 * Copyright 2007-2018 The jdeb developers. 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 */ 016package org.vafer.jdeb.mapping; 017 018import org.apache.commons.compress.archivers.tar.TarArchiveEntry; 019import org.vafer.jdeb.utils.Utils; 020 021/** 022 * Applies a uniform set of permissions and ownership to all entries. 023 */ 024public final class PermMapper implements Mapper { 025 026 private final int strip; 027 private final String prefix; 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 this.strip = strip; 045 this.prefix = (prefix == null) ? "" : prefix; 046 this.uid = uid; 047 this.gid = gid; 048 this.user = user; 049 this.group = group; 050 this.fileMode = fileMode; 051 this.dirMode = dirMode; 052 } 053 054 public PermMapper( int uid, int gid, String user, String group, String fileMode, String dirMode, int strip, String prefix ) { 055 this(uid, gid, user, group, toMode(fileMode), toMode(dirMode), strip, prefix); 056 } 057 058 public TarArchiveEntry map( final TarArchiveEntry entry ) { 059 entry.setName(Utils.joinUnixPath( 060 prefix, 061 Utils.stripPath(strip, entry.getName() 062 ))); 063 064 // Set ownership 065 if (uid > -1) { 066 entry.setUserId(uid); 067 } 068 if (gid > -1) { 069 entry.setGroupId(gid); 070 } 071 if (user != null) { 072 entry.setUserName(user); 073 } 074 if (group != null) { 075 entry.setGroupName(group); 076 } 077 078 // Set permissions 079 if (entry.isDirectory()) { 080 if (dirMode > -1) { 081 entry.setMode(dirMode); 082 } 083 } else { 084 if (fileMode > -1) { 085 entry.setMode(fileMode); 086 } 087 } 088 089 return entry; 090 } 091}