1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.packager.internal;
  27 
  28 import java.io.File;
  29 import java.util.Collection;
  30 import java.util.LinkedHashSet;
  31 import java.util.Set;
  32 
  33 /**
  34  * RelativeFileSet
  35  *
  36  * A class encapsulating a directory and a set of files within it.
  37  */
  38 public class RelativeFileSet {
  39 
  40     private String mode;
  41     private String os;
  42     private String arch;
  43 
  44     private File basedir;
  45     private Set<String> files = new LinkedHashSet<>();
  46 
  47     public RelativeFileSet(RelativeFileSet copy) {
  48         mode = copy.mode;
  49         os = copy.os;
  50         arch = copy.arch;
  51         basedir = copy.basedir;
  52         files = new LinkedHashSet<>(copy.files);
  53     }
  54 
  55     public RelativeFileSet(File base, Collection<File> files) {
  56         basedir = base;
  57         String baseAbsolute = basedir.getAbsolutePath();
  58         for (File f: files) {
  59             String absolute = f.getAbsolutePath();
  60             if (!absolute.startsWith(baseAbsolute)) {
  61                 throw new RuntimeException("File " + f.getAbsolutePath() +
  62                         " does not belong to " + baseAbsolute);
  63             }
  64             if (!absolute.equals(baseAbsolute)) {
  65                     // possible in jpackager case
  66                 this.files.add(absolute.substring(baseAbsolute.length()+1));
  67             }
  68         }
  69     }
  70 
  71     public void upshift() {
  72         String root = basedir.getName();
  73         basedir = basedir.getParentFile();
  74         Set<String> newFiles = new LinkedHashSet<>();
  75         for (String s : files) {
  76             newFiles.add(root + File.separator + s);
  77         }
  78         files = newFiles;
  79     }
  80 
  81     public RelativeFileSet(File base, Set<File> files) {
  82         this(base, (Collection<File>) files);
  83     }
  84 
  85     public boolean contains(String[] requiredFiles) {
  86         boolean result = true;
  87 
  88         for(String fname: requiredFiles) {
  89             if (!files.contains(fname)) {
  90                 Log.debug("  RelativeFileSet does not contain [" + fname + "]");
  91                 result = false;
  92             }
  93         }
  94 
  95         return result;
  96     }
  97 
  98     public boolean contains(String requiredFile) {
  99         if (files.contains(requiredFile)) {
 100             return true;
 101         } else {
 102             Log.debug("RelativeFileSet does not contain [" +requiredFile+ "]");
 103             return false;
 104         }
 105     }
 106 
 107     public File getBaseDirectory() {
 108         return basedir;
 109     }
 110 
 111     public Set<String> getIncludedFiles() {
 112         return files;
 113     }
 114 
 115     public void dump() {
 116         Log.verbose("\n=========\nBasedir: " + basedir + "\n");
 117         for (String fname : files) {
 118             Log.verbose("  " + fname);
 119         }
 120         Log.verbose("\n========");
 121     }
 122 
 123     public String getMode() {
 124         return mode;
 125     }
 126 
 127     public void setMode(String mode) {
 128         this.mode = mode;
 129     }
 130 
 131     public String getOs() {
 132         return os;
 133     }
 134 
 135     public void setOs(String os) {
 136         this.os = os;
 137     }
 138 
 139     public String getArch() {
 140         return arch;
 141     }
 142 
 143     public void setArch(String arch) {
 144         this.arch = arch;
 145     }
 146 
 147     @Override
 148     public String toString() {
 149         return "RelativeFileSet{basedir:" + basedir + ", files:" + files + "}";
 150     }
 151 
 152 }