001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2001 Johannes Lehtinen
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022/*
023 *  $Id: PackFile.java,v 1.21 2005/05/06 20:09:09 jponge Exp $
024 *  IzPack
025 *  Copyright (C) 2001 Johannes Lehtinen
026 *
027 *  File :               Pack.java
028 *  Description :        Contains informations about a pack file.
029 *  Author's email :     johannes.lehtinen@iki.fi
030 *  Author's Website :   http://www.iki.fi/jle/
031 *
032 *  This program is free software; you can redistribute it and/or
033 *  modify it under the terms of the GNU General Public License
034 *  as published by the Free Software Foundation; either version 2
035 *  of the License, or any later version.
036 *
037 *  This program is distributed in the hope that it will be useful,
038 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
039 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
040 *  GNU General Public License for more details.
041 *
042 *  You should have received a copy of the GNU General Public License
043 *  along with this program; if not, write to the Free Software
044 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
045 */
046package com.izforge.izpack;
047
048import java.io.File;
049import java.io.FileNotFoundException;
050import java.io.Serializable;
051import java.util.List;
052import java.util.Map;
053
054/**
055 * Encloses information about a packed file. This class abstracts the way file data is stored to
056 * package.
057 * 
058 * @author Johannes Lehtinen <johannes.lehtinen@iki.fi>
059 */
060public class PackFile implements Serializable
061{
062
063    static final long serialVersionUID = -834377078706854909L;
064
065    public static final int OVERRIDE_FALSE = 0;
066
067    public static final int OVERRIDE_TRUE = 1;
068
069    public static final int OVERRIDE_ASK_FALSE = 2;
070
071    public static final int OVERRIDE_ASK_TRUE = 3;
072
073    public static final int OVERRIDE_UPDATE = 4;
074
075    public String sourcePath = null;
076
077    /** The full path name of the target file */
078    private String targetPath = null;
079
080    /** The target operating system constraints of this file */
081    private List osConstraints = null;
082
083    /** The length of the file in bytes */
084    private long length = 0;
085
086    /** The last-modification time of the file. */
087    private long mtime = -1;
088
089    /** True if file is a directory (length should be 0 or ignored) */
090    private boolean isDirectory = false;
091
092    /** Whether or not this file is going to override any existing ones */
093    private int override = OVERRIDE_FALSE;
094
095    /** Additional attributes or any else for customisation */
096    private Map additionals = null;
097
098    public int previousPackNumber = -1;
099
100    public long offsetInPreviousPack = -1;
101
102    /**
103     * Constructs and initializes from a source file.
104     * 
105     * @param src file which this PackFile describes
106     * @param target the path to install the file to
107     * @param osList OS constraints
108     * @param override what to do when the file already exists
109     * @throws FileNotFoundException if the specified file does not exist.
110     */
111    public PackFile(File src, String target, List osList, int override)
112            throws FileNotFoundException
113    {
114        if (!src.exists()) // allows cleaner client co
115            throw new FileNotFoundException("No such file: " + src);
116
117        if ('/' != File.separatorChar) target = target.replace(File.separatorChar, '/');
118        if (target.endsWith("/")) target = target.substring(0, target.length() - 1);
119
120        this.sourcePath = src.getPath();
121        this.targetPath = target;
122        this.osConstraints = osList;
123        this.override = override;
124
125        this.length = src.length();
126        this.mtime = src.lastModified();
127        this.isDirectory = src.isDirectory();
128    }
129
130    /**
131     * Constructs and initializes from a source file.
132     * 
133     * @param src file which this PackFile describes
134     * @param target the path to install the file to
135     * @param osList OS constraints
136     * @param override what to do when the file already exists
137     * @param additionals additional attributes
138     * @throws FileNotFoundException if the specified file does not exist.
139     */
140    public PackFile(File src, String target, List osList, int override, Map additionals)
141            throws FileNotFoundException
142    {
143        this(src, target, osList, override);
144        this.additionals = additionals;
145    }
146
147    public void setPreviousPackFileRef(int previousPackNumber, long offsetInPreviousPack)
148    {
149        this.previousPackNumber = previousPackNumber;
150        this.offsetInPreviousPack = offsetInPreviousPack;
151    }
152
153    /** The target operating system constraints of this file */
154    public final List osConstraints()
155    {
156        return osConstraints;
157    }
158
159    /** The length of the file in bytes */
160    public final long length()
161    {
162        return length;
163    }
164
165    /** The last-modification time of the file. */
166    public final long lastModified()
167    {
168        return mtime;
169    }
170
171    /** Whether or not this file is going to override any existing ones */
172    public final int override()
173    {
174        return override;
175    }
176
177    public final boolean isDirectory()
178    {
179        return isDirectory;
180    }
181
182    public final boolean isBackReference()
183    {
184        return (previousPackNumber >= 0);
185    }
186
187    /** The full path name of the target file, using '/' as fileseparator. */
188    public final String getTargetPath()
189    {
190        return targetPath;
191    }
192
193    /**
194     * Returns the additionals map.
195     * 
196     * @return additionals
197     */
198    public Map getAdditionals()
199    {
200        return additionals;
201    }
202
203}