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            ret = Integer.toOctalString( 0777 - mode );
052        }
053        catch ( final NumberFormatException e )
054        {
055            try
056            {
057                Integer.parseInt( modeStr );
058            }
059            catch ( final NumberFormatException e1 )
060            {
061                ret = modeStr;
062            }
063        }
064
065        if ( ret == null )
066        {
067            throw new IllegalArgumentException( "The mode is a number but is not octal" );
068        }
069
070        return ret;
071    }
072}