001package org.apache.maven.wagon;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022/**
023 * Utility class for common operations for file/directory permissions.
024 *
025 * @author <a href="mailto:juam at users.sourceforge.net">Juan F. Codagnone</a>
026 * @see PermissionModeUtils
027 * @since Sep 3, 2005
028 */
029public final class PermissionModeUtils
030{
031    private PermissionModeUtils()
032    {
033    }
034    
035    /**
036     * See the System Interfaces volume of IEEE Std 1003.1-2001, umask(1)
037     *
038     * @param modeStr permission mode (numeric or symbolic)
039     * @return the mode that can be used with umask to accomplish modeStr.
040     */
041    public static String getUserMaskFor( String modeStr )
042    {
043        String ret = null;
044
045        try
046        {
047            int mode = Integer.valueOf( modeStr, 8 ).intValue();
048
049            mode = mode % 8 + ( ( mode / 8 ) % 8 ) * 8 + ( ( mode / 64 ) % 8 ) * 64;
050
051            // CHECKSTYLE_OFF: MagicNumber
052            ret = Integer.toOctalString( 0777 - mode );
053            // CHECKSTYLE_ON: MagicNumber
054        }
055        catch ( final NumberFormatException e )
056        {
057            try
058            {
059                Integer.parseInt( modeStr );
060            }
061            catch ( final NumberFormatException e1 )
062            {
063                ret = modeStr;
064            }
065        }
066
067        if ( ret == null )
068        {
069            throw new IllegalArgumentException( "The mode is a number but is not octal" );
070        }
071
072        return ret;
073    }
074}