/*-
 * Copyright (c) 2014 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.xmvn.tools.install.impl;

import java.nio.file.Path;

/**
 * An abstract class denoting a file entry in a package with all relevant attributes.
 * <p>
 * A file does not necessairly mean a regular file, it could be for example a directory or a device file.
 * 
 * @author Mikolaj Izdebski
 */
abstract class File
{
    /**
     * Path to target file. This path must be relative to buildroot (must not be absolute).
     */
    private final Path targetPath;

    /**
     * Unix access mode (an integer in range from 0 to 0777).
     */
    private final int accessMode;

    /**
     * Create a file object with specified path and default access mode of 0644.
     * 
     * @param targetPath
     */
    public File( Path targetPath )
    {
        this( targetPath, 0644 );
    }

    /**
     * Create a file object with specified path and access mode.
     * 
     * @param targetPath
     * @param accessMode
     */
    public File( Path targetPath, int accessMode )
    {
        this.targetPath = targetPath;
        this.accessMode = accessMode;
    }

    /**
     * Return path to target file. Returned patjh is always relative to buildroot (never absolute).
     * 
     * @return file target path (never {@code null})
     */
    public Path getTargetPath()
    {
        return targetPath;
    }

    /**
     * Get Unix access mode for this file.
     * 
     * @return Unix access mode (an integer in range from 0 to 0777)
     */
    public int getAccessMode()
    {
        return accessMode;
    }
}

@@ -647,16 +647,72 @@
 .impl;%0A%0A
+import java.io.IOException;%0Aimport java.nio.file.Files;%0A
 import j
@@ -730,16 +730,16 @@
 e.Path;%0A
-
 %0A/**%0A * 
@@ -1249,16 +1249,526 @@
 sMode;%0A%0A
+    /**%0A     * Install the file into specified location.%0A     * %3Cp%3E%0A     * Implementations of this method can assume that all parent directory of target file already exists. Access mode of%0A     * target file doesn't have to be set as it will be manipulated with other means.%0A     * %0A     * @param installRoot absolute path of root directory under which the file should be installed%0A     * @throws IOException%0A     */%0A    protected abstract void installContents( Path installRoot )%0A        throws IOException;%0A%0A
     /**%0A
@@ -2159,32 +2159,305 @@
 essMode )%0A    %7B%0A
+        if ( targetPath.isAbsolute() )%0A            throw new IllegalArgumentException( %22target path must not be absolute%22 );%0A        if ( accessMode %3C 0 %7C%7C accessMode %3E 0777 )%0A            throw new IllegalArgumentException( %22access mode must be in range from 0 to 777%22 );%0A%0A
         this.tar
@@ -2937,32 +2937,32 @@
 essMode()%0A    %7B%0A
-
         return a
@@ -2977,11 +2977,774 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Install file into specified root directory.%0A     * %0A     * @param installRoot%0A     * @throws IOException%0A     */%0A    public void install( Path installRoot )%0A        throws IOException%0A    %7B%0A        installRoot = installRoot.toAbsolutePath();%0A%0A        if ( Files.exists( installRoot ) )%0A        %7B%0A            if ( !Files.isDirectory( installRoot ) )%0A                throw new IOException( %22Installation root %22 + installRoot + %22 already exists and is not a directory.%22 );%0A        %7D%0A        else%0A        %7B%0A            Files.createDirectory( installRoot );%0A        %7D%0A%0A        Path targetAbsolutePath = installRoot.resolve( targetPath );%0A        Files.createDirectories( targetAbsolutePath.getParent() );%0A%0A        installContents( installRoot );%0A    %7D
 %0A%7D%0A

Add installation methods for File